]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
scsi: lpfc: Correct oops on vport port resets
[mirror_ubuntu-eoan-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
dea3101e
JB
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
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
da0436e9 37#include "lpfc_hw4.h"
dea3101e
JB
38#include "lpfc_hw.h"
39#include "lpfc_sli.h"
da0436e9 40#include "lpfc_sli4.h"
ea2151b4 41#include "lpfc_nl.h"
dea3101e
JB
42#include "lpfc_disc.h"
43#include "lpfc_scsi.h"
44#include "lpfc.h"
45#include "lpfc_crtn.h"
46#include "lpfc_logmsg.h"
47#include "lpfc_compat.h"
858c9f6c 48#include "lpfc_debugfs.h"
04c68496 49#include "lpfc_vport.h"
61bda8f7 50#include "lpfc_version.h"
dea3101e
JB
51
52/* There are only four IOCB completion types. */
53typedef enum _lpfc_iocb_type {
54 LPFC_UNKNOWN_IOCB,
55 LPFC_UNSOL_IOCB,
56 LPFC_SOL_IOCB,
57 LPFC_ABORT_IOCB
58} lpfc_iocb_type;
59
4f774513
JS
60
61/* Provide function prototypes local to this module. */
62static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
63 uint32_t);
64static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
45ed1190
JS
65 uint8_t *, uint32_t *);
66static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
67 struct lpfc_iocbq *);
6669f9bb
JS
68static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
69 struct hbq_dmabuf *);
0558056c
JS
70static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
71 struct lpfc_cqe *);
8a9d2e80
JS
72static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
73 int);
ba20c853
JS
74static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
75 uint32_t);
e8d3c3b1
JS
76static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
77static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
0558056c 78
4f774513
JS
79static IOCB_t *
80lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
81{
82 return &iocbq->iocb;
83}
84
85/**
86 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
87 * @q: The Work Queue to operate on.
88 * @wqe: The work Queue Entry to put on the Work queue.
89 *
90 * This routine will copy the contents of @wqe to the next available entry on
91 * the @q. This function will then ring the Work Queue Doorbell to signal the
92 * HBA to start processing the Work Queue Entry. This function returns 0 if
93 * successful. If no entries are available on @q then this function will return
94 * -ENOMEM.
95 * The caller is expected to hold the hbalock when calling this routine.
96 **/
97static uint32_t
98lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
99{
2e90f4b5 100 union lpfc_wqe *temp_wqe;
4f774513
JS
101 struct lpfc_register doorbell;
102 uint32_t host_index;
027140ea 103 uint32_t idx;
4f774513 104
2e90f4b5
JS
105 /* sanity check on queue memory */
106 if (unlikely(!q))
107 return -ENOMEM;
108 temp_wqe = q->qe[q->host_index].wqe;
109
4f774513 110 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
111 idx = ((q->host_index + 1) % q->entry_count);
112 if (idx == q->hba_index) {
b84daac9 113 q->WQ_overflow++;
4f774513 114 return -ENOMEM;
b84daac9
JS
115 }
116 q->WQ_posted++;
4f774513 117 /* set consumption flag every once in a while */
ff78d8f9 118 if (!((q->host_index + 1) % q->entry_repost))
f0d9bccc 119 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
fedd3b7b
JS
120 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
121 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
4f774513
JS
122 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
123
124 /* Update the host index before invoking device */
125 host_index = q->host_index;
027140ea
JS
126
127 q->host_index = idx;
4f774513
JS
128
129 /* Ring Doorbell */
130 doorbell.word0 = 0;
962bc51b
JS
131 if (q->db_format == LPFC_DB_LIST_FORMAT) {
132 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
133 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
134 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
135 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
136 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
137 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
138 } else {
139 return -EINVAL;
140 }
141 writel(doorbell.word0, q->db_regaddr);
4f774513
JS
142
143 return 0;
144}
145
146/**
147 * lpfc_sli4_wq_release - Updates internal hba index for WQ
148 * @q: The Work Queue to operate on.
149 * @index: The index to advance the hba index to.
150 *
151 * This routine will update the HBA index of a queue to reflect consumption of
152 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
153 * an entry the host calls this function to update the queue's internal
154 * pointers. This routine returns the number of entries that were consumed by
155 * the HBA.
156 **/
157static uint32_t
158lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
159{
160 uint32_t released = 0;
161
2e90f4b5
JS
162 /* sanity check on queue memory */
163 if (unlikely(!q))
164 return 0;
165
4f774513
JS
166 if (q->hba_index == index)
167 return 0;
168 do {
169 q->hba_index = ((q->hba_index + 1) % q->entry_count);
170 released++;
171 } while (q->hba_index != index);
172 return released;
173}
174
175/**
176 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
177 * @q: The Mailbox Queue to operate on.
178 * @wqe: The Mailbox Queue Entry to put on the Work queue.
179 *
180 * This routine will copy the contents of @mqe to the next available entry on
181 * the @q. This function will then ring the Work Queue Doorbell to signal the
182 * HBA to start processing the Work Queue Entry. This function returns 0 if
183 * successful. If no entries are available on @q then this function will return
184 * -ENOMEM.
185 * The caller is expected to hold the hbalock when calling this routine.
186 **/
187static uint32_t
188lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
189{
2e90f4b5 190 struct lpfc_mqe *temp_mqe;
4f774513 191 struct lpfc_register doorbell;
4f774513 192
2e90f4b5
JS
193 /* sanity check on queue memory */
194 if (unlikely(!q))
195 return -ENOMEM;
196 temp_mqe = q->qe[q->host_index].mqe;
197
4f774513
JS
198 /* If the host has not yet processed the next entry then we are done */
199 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
200 return -ENOMEM;
201 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
202 /* Save off the mailbox pointer for completion */
203 q->phba->mbox = (MAILBOX_t *)temp_mqe;
204
205 /* Update the host index before invoking device */
4f774513
JS
206 q->host_index = ((q->host_index + 1) % q->entry_count);
207
208 /* Ring Doorbell */
209 doorbell.word0 = 0;
210 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
211 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
212 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
4f774513
JS
213 return 0;
214}
215
216/**
217 * lpfc_sli4_mq_release - Updates internal hba index for MQ
218 * @q: The Mailbox Queue to operate on.
219 *
220 * This routine will update the HBA index of a queue to reflect consumption of
221 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
222 * an entry the host calls this function to update the queue's internal
223 * pointers. This routine returns the number of entries that were consumed by
224 * the HBA.
225 **/
226static uint32_t
227lpfc_sli4_mq_release(struct lpfc_queue *q)
228{
2e90f4b5
JS
229 /* sanity check on queue memory */
230 if (unlikely(!q))
231 return 0;
232
4f774513
JS
233 /* Clear the mailbox pointer for completion */
234 q->phba->mbox = NULL;
235 q->hba_index = ((q->hba_index + 1) % q->entry_count);
236 return 1;
237}
238
239/**
240 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
241 * @q: The Event Queue to get the first valid EQE from
242 *
243 * This routine will get the first valid Event Queue Entry from @q, update
244 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
245 * the Queue (no more work to do), or the Queue is full of EQEs that have been
246 * processed, but not popped back to the HBA then this routine will return NULL.
247 **/
248static struct lpfc_eqe *
249lpfc_sli4_eq_get(struct lpfc_queue *q)
250{
2e90f4b5 251 struct lpfc_eqe *eqe;
027140ea 252 uint32_t idx;
2e90f4b5
JS
253
254 /* sanity check on queue memory */
255 if (unlikely(!q))
256 return NULL;
257 eqe = q->qe[q->hba_index].eqe;
4f774513
JS
258
259 /* If the next EQE is not valid then we are done */
cb5172ea 260 if (!bf_get_le32(lpfc_eqe_valid, eqe))
4f774513
JS
261 return NULL;
262 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
263 idx = ((q->hba_index + 1) % q->entry_count);
264 if (idx == q->host_index)
4f774513
JS
265 return NULL;
266
027140ea 267 q->hba_index = idx;
27f344eb
JS
268
269 /*
270 * insert barrier for instruction interlock : data from the hardware
271 * must have the valid bit checked before it can be copied and acted
272 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
273 * instructions allowing action on content before valid bit checked,
274 * add barrier here as well. May not be needed as "content" is a
275 * single 32-bit entity here (vs multi word structure for cq's).
276 */
277 mb();
4f774513
JS
278 return eqe;
279}
280
ba20c853
JS
281/**
282 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
283 * @q: The Event Queue to disable interrupts
284 *
285 **/
286static inline void
287lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
288{
289 struct lpfc_register doorbell;
290
291 doorbell.word0 = 0;
292 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
293 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
294 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
295 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
296 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
297 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
298}
299
4f774513
JS
300/**
301 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
302 * @q: The Event Queue that the host has completed processing for.
303 * @arm: Indicates whether the host wants to arms this CQ.
304 *
305 * This routine will mark all Event Queue Entries on @q, from the last
306 * known completed entry to the last entry that was processed, as completed
307 * by clearing the valid bit for each completion queue entry. Then it will
308 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
309 * The internal host index in the @q will be updated by this routine to indicate
310 * that the host has finished processing the entries. The @arm parameter
311 * indicates that the queue should be rearmed when ringing the doorbell.
312 *
313 * This function will return the number of EQEs that were popped.
314 **/
315uint32_t
316lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
317{
318 uint32_t released = 0;
319 struct lpfc_eqe *temp_eqe;
320 struct lpfc_register doorbell;
321
2e90f4b5
JS
322 /* sanity check on queue memory */
323 if (unlikely(!q))
324 return 0;
325
4f774513
JS
326 /* while there are valid entries */
327 while (q->hba_index != q->host_index) {
328 temp_eqe = q->qe[q->host_index].eqe;
cb5172ea 329 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
4f774513
JS
330 released++;
331 q->host_index = ((q->host_index + 1) % q->entry_count);
332 }
333 if (unlikely(released == 0 && !arm))
334 return 0;
335
336 /* ring doorbell for number popped */
337 doorbell.word0 = 0;
338 if (arm) {
339 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
340 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
341 }
342 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
343 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
6b5151fd
JS
344 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
345 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
346 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
4f774513 347 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
a747c9ce
JS
348 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
349 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
350 readl(q->phba->sli4_hba.EQCQDBregaddr);
4f774513
JS
351 return released;
352}
353
354/**
355 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
356 * @q: The Completion Queue to get the first valid CQE from
357 *
358 * This routine will get the first valid Completion Queue Entry from @q, update
359 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
360 * the Queue (no more work to do), or the Queue is full of CQEs that have been
361 * processed, but not popped back to the HBA then this routine will return NULL.
362 **/
363static struct lpfc_cqe *
364lpfc_sli4_cq_get(struct lpfc_queue *q)
365{
366 struct lpfc_cqe *cqe;
027140ea 367 uint32_t idx;
4f774513 368
2e90f4b5
JS
369 /* sanity check on queue memory */
370 if (unlikely(!q))
371 return NULL;
372
4f774513 373 /* If the next CQE is not valid then we are done */
cb5172ea 374 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
4f774513
JS
375 return NULL;
376 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
377 idx = ((q->hba_index + 1) % q->entry_count);
378 if (idx == q->host_index)
4f774513
JS
379 return NULL;
380
381 cqe = q->qe[q->hba_index].cqe;
027140ea 382 q->hba_index = idx;
27f344eb
JS
383
384 /*
385 * insert barrier for instruction interlock : data from the hardware
386 * must have the valid bit checked before it can be copied and acted
387 * upon. Speculative instructions were allowing a bcopy at the start
388 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
389 * after our return, to copy data before the valid bit check above
390 * was done. As such, some of the copied data was stale. The barrier
391 * ensures the check is before any data is copied.
392 */
393 mb();
4f774513
JS
394 return cqe;
395}
396
397/**
398 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
399 * @q: The Completion Queue that the host has completed processing for.
400 * @arm: Indicates whether the host wants to arms this CQ.
401 *
402 * This routine will mark all Completion queue entries on @q, from the last
403 * known completed entry to the last entry that was processed, as completed
404 * by clearing the valid bit for each completion queue entry. Then it will
405 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
406 * The internal host index in the @q will be updated by this routine to indicate
407 * that the host has finished processing the entries. The @arm parameter
408 * indicates that the queue should be rearmed when ringing the doorbell.
409 *
410 * This function will return the number of CQEs that were released.
411 **/
412uint32_t
413lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
414{
415 uint32_t released = 0;
416 struct lpfc_cqe *temp_qe;
417 struct lpfc_register doorbell;
418
2e90f4b5
JS
419 /* sanity check on queue memory */
420 if (unlikely(!q))
421 return 0;
4f774513
JS
422 /* while there are valid entries */
423 while (q->hba_index != q->host_index) {
424 temp_qe = q->qe[q->host_index].cqe;
cb5172ea 425 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
4f774513
JS
426 released++;
427 q->host_index = ((q->host_index + 1) % q->entry_count);
428 }
429 if (unlikely(released == 0 && !arm))
430 return 0;
431
432 /* ring doorbell for number popped */
433 doorbell.word0 = 0;
434 if (arm)
435 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
436 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
437 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
6b5151fd
JS
438 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
439 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
440 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
4f774513
JS
441 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
442 return released;
443}
444
445/**
446 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
447 * @q: The Header Receive Queue to operate on.
448 * @wqe: The Receive Queue Entry to put on the Receive queue.
449 *
450 * This routine will copy the contents of @wqe to the next available entry on
451 * the @q. This function will then ring the Receive Queue Doorbell to signal the
452 * HBA to start processing the Receive Queue Entry. This function returns the
453 * index that the rqe was copied to if successful. If no entries are available
454 * on @q then this function will return -ENOMEM.
455 * The caller is expected to hold the hbalock when calling this routine.
456 **/
457static int
458lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
459 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
460{
2e90f4b5
JS
461 struct lpfc_rqe *temp_hrqe;
462 struct lpfc_rqe *temp_drqe;
4f774513 463 struct lpfc_register doorbell;
5a25bf36 464 int put_index;
4f774513 465
2e90f4b5
JS
466 /* sanity check on queue memory */
467 if (unlikely(!hq) || unlikely(!dq))
468 return -ENOMEM;
5a25bf36 469 put_index = hq->host_index;
2e90f4b5
JS
470 temp_hrqe = hq->qe[hq->host_index].rqe;
471 temp_drqe = dq->qe[dq->host_index].rqe;
472
4f774513
JS
473 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
474 return -EINVAL;
475 if (hq->host_index != dq->host_index)
476 return -EINVAL;
477 /* If the host has not yet processed the next entry then we are done */
478 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
479 return -EBUSY;
480 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
481 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
482
483 /* Update the host index to point to the next slot */
484 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
485 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
486
487 /* Ring The Header Receive Queue Doorbell */
73d91e50 488 if (!(hq->host_index % hq->entry_repost)) {
4f774513 489 doorbell.word0 = 0;
962bc51b
JS
490 if (hq->db_format == LPFC_DB_RING_FORMAT) {
491 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
492 hq->entry_repost);
493 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
494 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
495 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
496 hq->entry_repost);
497 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
498 hq->host_index);
499 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
500 } else {
501 return -EINVAL;
502 }
503 writel(doorbell.word0, hq->db_regaddr);
4f774513
JS
504 }
505 return put_index;
506}
507
508/**
509 * lpfc_sli4_rq_release - Updates internal hba index for RQ
510 * @q: The Header Receive Queue to operate on.
511 *
512 * This routine will update the HBA index of a queue to reflect consumption of
513 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
514 * consumed an entry the host calls this function to update the queue's
515 * internal pointers. This routine returns the number of entries that were
516 * consumed by the HBA.
517 **/
518static uint32_t
519lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
520{
2e90f4b5
JS
521 /* sanity check on queue memory */
522 if (unlikely(!hq) || unlikely(!dq))
523 return 0;
524
4f774513
JS
525 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
526 return 0;
527 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
528 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
529 return 1;
530}
531
e59058c4 532/**
3621a710 533 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
534 * @phba: Pointer to HBA context object.
535 * @pring: Pointer to driver SLI ring object.
536 *
537 * This function returns pointer to next command iocb entry
538 * in the command ring. The caller must hold hbalock to prevent
539 * other threads consume the next command iocb.
540 * SLI-2/SLI-3 provide different sized iocbs.
541 **/
ed957684
JS
542static inline IOCB_t *
543lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
544{
7e56aa25
JS
545 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
546 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
ed957684
JS
547}
548
e59058c4 549/**
3621a710 550 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
551 * @phba: Pointer to HBA context object.
552 * @pring: Pointer to driver SLI ring object.
553 *
554 * This function returns pointer to next response iocb entry
555 * in the response ring. The caller must hold hbalock to make sure
556 * that no other thread consume the next response iocb.
557 * SLI-2/SLI-3 provide different sized iocbs.
558 **/
ed957684
JS
559static inline IOCB_t *
560lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
561{
7e56aa25
JS
562 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
563 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
ed957684
JS
564}
565
e59058c4 566/**
3621a710 567 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
568 * @phba: Pointer to HBA context object.
569 *
570 * This function is called with hbalock held. This function
571 * allocates a new driver iocb object from the iocb pool. If the
572 * allocation is successful, it returns pointer to the newly
573 * allocated iocb object else it returns NULL.
574 **/
4f2e66c6 575struct lpfc_iocbq *
2e0fef85 576__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
577{
578 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
579 struct lpfc_iocbq * iocbq = NULL;
580
1c2ba475
JT
581 lockdep_assert_held(&phba->hbalock);
582
0bd4ca25 583 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
584 if (iocbq)
585 phba->iocb_cnt++;
586 if (phba->iocb_cnt > phba->iocb_max)
587 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
588 return iocbq;
589}
590
da0436e9
JS
591/**
592 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
593 * @phba: Pointer to HBA context object.
594 * @xritag: XRI value.
595 *
596 * This function clears the sglq pointer from the array of acive
597 * sglq's. The xritag that is passed in is used to index into the
598 * array. Before the xritag can be used it needs to be adjusted
599 * by subtracting the xribase.
600 *
601 * Returns sglq ponter = success, NULL = Failure.
602 **/
603static struct lpfc_sglq *
604__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
605{
da0436e9 606 struct lpfc_sglq *sglq;
6d368e53
JS
607
608 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
609 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
da0436e9
JS
610 return sglq;
611}
612
613/**
614 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
615 * @phba: Pointer to HBA context object.
616 * @xritag: XRI value.
617 *
618 * This function returns the sglq pointer from the array of acive
619 * sglq's. The xritag that is passed in is used to index into the
620 * array. Before the xritag can be used it needs to be adjusted
621 * by subtracting the xribase.
622 *
623 * Returns sglq ponter = success, NULL = Failure.
624 **/
0f65ff68 625struct lpfc_sglq *
da0436e9
JS
626__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
627{
da0436e9 628 struct lpfc_sglq *sglq;
6d368e53
JS
629
630 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
da0436e9
JS
631 return sglq;
632}
633
19ca7609 634/**
1151e3ec 635 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
19ca7609
JS
636 * @phba: Pointer to HBA context object.
637 * @xritag: xri used in this exchange.
638 * @rrq: The RRQ to be cleared.
639 *
19ca7609 640 **/
1151e3ec
JS
641void
642lpfc_clr_rrq_active(struct lpfc_hba *phba,
643 uint16_t xritag,
644 struct lpfc_node_rrq *rrq)
19ca7609 645{
1151e3ec 646 struct lpfc_nodelist *ndlp = NULL;
19ca7609 647
1151e3ec
JS
648 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
649 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
19ca7609
JS
650
651 /* The target DID could have been swapped (cable swap)
652 * we should use the ndlp from the findnode if it is
653 * available.
654 */
1151e3ec 655 if ((!ndlp) && rrq->ndlp)
19ca7609
JS
656 ndlp = rrq->ndlp;
657
1151e3ec
JS
658 if (!ndlp)
659 goto out;
660
cff261f6 661 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
662 rrq->send_rrq = 0;
663 rrq->xritag = 0;
664 rrq->rrq_stop_time = 0;
665 }
1151e3ec 666out:
19ca7609
JS
667 mempool_free(rrq, phba->rrq_pool);
668}
669
670/**
671 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
672 * @phba: Pointer to HBA context object.
673 *
674 * This function is called with hbalock held. This function
675 * Checks if stop_time (ratov from setting rrq active) has
676 * been reached, if it has and the send_rrq flag is set then
677 * it will call lpfc_send_rrq. If the send_rrq flag is not set
678 * then it will just call the routine to clear the rrq and
679 * free the rrq resource.
680 * The timer is set to the next rrq that is going to expire before
681 * leaving the routine.
682 *
683 **/
684void
685lpfc_handle_rrq_active(struct lpfc_hba *phba)
686{
687 struct lpfc_node_rrq *rrq;
688 struct lpfc_node_rrq *nextrrq;
689 unsigned long next_time;
690 unsigned long iflags;
1151e3ec 691 LIST_HEAD(send_rrq);
19ca7609
JS
692
693 spin_lock_irqsave(&phba->hbalock, iflags);
694 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
256ec0d0 695 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
19ca7609 696 list_for_each_entry_safe(rrq, nextrrq,
1151e3ec
JS
697 &phba->active_rrq_list, list) {
698 if (time_after(jiffies, rrq->rrq_stop_time))
699 list_move(&rrq->list, &send_rrq);
700 else if (time_before(rrq->rrq_stop_time, next_time))
19ca7609
JS
701 next_time = rrq->rrq_stop_time;
702 }
703 spin_unlock_irqrestore(&phba->hbalock, iflags);
06918ac5
JS
704 if ((!list_empty(&phba->active_rrq_list)) &&
705 (!(phba->pport->load_flag & FC_UNLOADING)))
19ca7609 706 mod_timer(&phba->rrq_tmr, next_time);
1151e3ec
JS
707 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
708 list_del(&rrq->list);
709 if (!rrq->send_rrq)
710 /* this call will free the rrq */
711 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
712 else if (lpfc_send_rrq(phba, rrq)) {
713 /* if we send the rrq then the completion handler
714 * will clear the bit in the xribitmap.
715 */
716 lpfc_clr_rrq_active(phba, rrq->xritag,
717 rrq);
718 }
719 }
19ca7609
JS
720}
721
722/**
723 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
724 * @vport: Pointer to vport context object.
725 * @xri: The xri used in the exchange.
726 * @did: The targets DID for this exchange.
727 *
728 * returns NULL = rrq not found in the phba->active_rrq_list.
729 * rrq = rrq for this xri and target.
730 **/
731struct lpfc_node_rrq *
732lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
733{
734 struct lpfc_hba *phba = vport->phba;
735 struct lpfc_node_rrq *rrq;
736 struct lpfc_node_rrq *nextrrq;
737 unsigned long iflags;
738
739 if (phba->sli_rev != LPFC_SLI_REV4)
740 return NULL;
741 spin_lock_irqsave(&phba->hbalock, iflags);
742 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
743 if (rrq->vport == vport && rrq->xritag == xri &&
744 rrq->nlp_DID == did){
745 list_del(&rrq->list);
746 spin_unlock_irqrestore(&phba->hbalock, iflags);
747 return rrq;
748 }
749 }
750 spin_unlock_irqrestore(&phba->hbalock, iflags);
751 return NULL;
752}
753
754/**
755 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
756 * @vport: Pointer to vport context object.
1151e3ec
JS
757 * @ndlp: Pointer to the lpfc_node_list structure.
758 * If ndlp is NULL Remove all active RRQs for this vport from the
759 * phba->active_rrq_list and clear the rrq.
760 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
19ca7609
JS
761 **/
762void
1151e3ec 763lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
19ca7609
JS
764
765{
766 struct lpfc_hba *phba = vport->phba;
767 struct lpfc_node_rrq *rrq;
768 struct lpfc_node_rrq *nextrrq;
769 unsigned long iflags;
1151e3ec 770 LIST_HEAD(rrq_list);
19ca7609
JS
771
772 if (phba->sli_rev != LPFC_SLI_REV4)
773 return;
1151e3ec
JS
774 if (!ndlp) {
775 lpfc_sli4_vport_delete_els_xri_aborted(vport);
776 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
19ca7609 777 }
1151e3ec
JS
778 spin_lock_irqsave(&phba->hbalock, iflags);
779 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
780 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
781 list_move(&rrq->list, &rrq_list);
19ca7609 782 spin_unlock_irqrestore(&phba->hbalock, iflags);
1151e3ec
JS
783
784 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
785 list_del(&rrq->list);
786 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
787 }
19ca7609
JS
788}
789
19ca7609 790/**
1151e3ec 791 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
19ca7609
JS
792 * @phba: Pointer to HBA context object.
793 * @ndlp: Targets nodelist pointer for this exchange.
794 * @xritag the xri in the bitmap to test.
795 *
796 * This function is called with hbalock held. This function
797 * returns 0 = rrq not active for this xri
798 * 1 = rrq is valid for this xri.
799 **/
1151e3ec
JS
800int
801lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19ca7609
JS
802 uint16_t xritag)
803{
1c2ba475 804 lockdep_assert_held(&phba->hbalock);
19ca7609
JS
805 if (!ndlp)
806 return 0;
cff261f6
JS
807 if (!ndlp->active_rrqs_xri_bitmap)
808 return 0;
809 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
19ca7609
JS
810 return 1;
811 else
812 return 0;
813}
814
815/**
816 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
817 * @phba: Pointer to HBA context object.
818 * @ndlp: nodelist pointer for this target.
819 * @xritag: xri used in this exchange.
820 * @rxid: Remote Exchange ID.
821 * @send_rrq: Flag used to determine if we should send rrq els cmd.
822 *
823 * This function takes the hbalock.
824 * The active bit is always set in the active rrq xri_bitmap even
825 * if there is no slot avaiable for the other rrq information.
826 *
827 * returns 0 rrq actived for this xri
828 * < 0 No memory or invalid ndlp.
829 **/
830int
831lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
b42c07c8 832 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
19ca7609 833{
19ca7609 834 unsigned long iflags;
b42c07c8
JS
835 struct lpfc_node_rrq *rrq;
836 int empty;
837
838 if (!ndlp)
839 return -EINVAL;
840
841 if (!phba->cfg_enable_rrq)
842 return -EINVAL;
19ca7609
JS
843
844 spin_lock_irqsave(&phba->hbalock, iflags);
b42c07c8
JS
845 if (phba->pport->load_flag & FC_UNLOADING) {
846 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
847 goto out;
848 }
849
850 /*
851 * set the active bit even if there is no mem available.
852 */
853 if (NLP_CHK_FREE_REQ(ndlp))
854 goto out;
855
856 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
857 goto out;
858
cff261f6
JS
859 if (!ndlp->active_rrqs_xri_bitmap)
860 goto out;
861
862 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
b42c07c8
JS
863 goto out;
864
19ca7609 865 spin_unlock_irqrestore(&phba->hbalock, iflags);
b42c07c8
JS
866 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
867 if (!rrq) {
868 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
869 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
870 " DID:0x%x Send:%d\n",
871 xritag, rxid, ndlp->nlp_DID, send_rrq);
872 return -EINVAL;
873 }
e5771b4d
JS
874 if (phba->cfg_enable_rrq == 1)
875 rrq->send_rrq = send_rrq;
876 else
877 rrq->send_rrq = 0;
b42c07c8 878 rrq->xritag = xritag;
256ec0d0
JS
879 rrq->rrq_stop_time = jiffies +
880 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
b42c07c8
JS
881 rrq->ndlp = ndlp;
882 rrq->nlp_DID = ndlp->nlp_DID;
883 rrq->vport = ndlp->vport;
884 rrq->rxid = rxid;
b42c07c8
JS
885 spin_lock_irqsave(&phba->hbalock, iflags);
886 empty = list_empty(&phba->active_rrq_list);
887 list_add_tail(&rrq->list, &phba->active_rrq_list);
888 phba->hba_flag |= HBA_RRQ_ACTIVE;
889 if (empty)
890 lpfc_worker_wake_up(phba);
891 spin_unlock_irqrestore(&phba->hbalock, iflags);
892 return 0;
893out:
894 spin_unlock_irqrestore(&phba->hbalock, iflags);
895 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
896 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
897 " DID:0x%x Send:%d\n",
898 xritag, rxid, ndlp->nlp_DID, send_rrq);
899 return -EINVAL;
19ca7609
JS
900}
901
da0436e9
JS
902/**
903 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
904 * @phba: Pointer to HBA context object.
19ca7609 905 * @piocb: Pointer to the iocbq.
da0436e9 906 *
dafe8cea 907 * This function is called with the ring lock held. This function
6d368e53 908 * gets a new driver sglq object from the sglq list. If the
da0436e9
JS
909 * list is not empty then it is successful, it returns pointer to the newly
910 * allocated sglq object else it returns NULL.
911 **/
912static struct lpfc_sglq *
19ca7609 913__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
da0436e9
JS
914{
915 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
916 struct lpfc_sglq *sglq = NULL;
19ca7609 917 struct lpfc_sglq *start_sglq = NULL;
19ca7609
JS
918 struct lpfc_scsi_buf *lpfc_cmd;
919 struct lpfc_nodelist *ndlp;
920 int found = 0;
921
1c2ba475
JT
922 lockdep_assert_held(&phba->hbalock);
923
19ca7609
JS
924 if (piocbq->iocb_flag & LPFC_IO_FCP) {
925 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
926 ndlp = lpfc_cmd->rdata->pnode;
be858b65 927 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
6c7cf486 928 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
19ca7609 929 ndlp = piocbq->context_un.ndlp;
6c7cf486
JS
930 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
931 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
932 ndlp = NULL;
933 else
934 ndlp = piocbq->context_un.ndlp;
935 } else {
19ca7609 936 ndlp = piocbq->context1;
6c7cf486 937 }
19ca7609 938
da0436e9 939 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
19ca7609
JS
940 start_sglq = sglq;
941 while (!found) {
942 if (!sglq)
943 return NULL;
ee0f4fe1 944 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
19ca7609
JS
945 /* This xri has an rrq outstanding for this DID.
946 * put it back in the list and get another xri.
947 */
948 list_add_tail(&sglq->list, lpfc_sgl_list);
949 sglq = NULL;
950 list_remove_head(lpfc_sgl_list, sglq,
951 struct lpfc_sglq, list);
952 if (sglq == start_sglq) {
953 sglq = NULL;
954 break;
955 } else
956 continue;
957 }
958 sglq->ndlp = ndlp;
959 found = 1;
6d368e53 960 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
19ca7609
JS
961 sglq->state = SGL_ALLOCATED;
962 }
da0436e9
JS
963 return sglq;
964}
965
e59058c4 966/**
3621a710 967 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
968 * @phba: Pointer to HBA context object.
969 *
970 * This function is called with no lock held. This function
971 * allocates a new driver iocb object from the iocb pool. If the
972 * allocation is successful, it returns pointer to the newly
973 * allocated iocb object else it returns NULL.
974 **/
2e0fef85
JS
975struct lpfc_iocbq *
976lpfc_sli_get_iocbq(struct lpfc_hba *phba)
977{
978 struct lpfc_iocbq * iocbq = NULL;
979 unsigned long iflags;
980
981 spin_lock_irqsave(&phba->hbalock, iflags);
982 iocbq = __lpfc_sli_get_iocbq(phba);
983 spin_unlock_irqrestore(&phba->hbalock, iflags);
984 return iocbq;
985}
986
4f774513
JS
987/**
988 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
989 * @phba: Pointer to HBA context object.
990 * @iocbq: Pointer to driver iocb object.
991 *
992 * This function is called with hbalock held to release driver
993 * iocb object to the iocb pool. The iotag in the iocb object
994 * does not change for each use of the iocb object. This function
995 * clears all other fields of the iocb object when it is freed.
996 * The sqlq structure that holds the xritag and phys and virtual
997 * mappings for the scatter gather list is retrieved from the
998 * active array of sglq. The get of the sglq pointer also clears
999 * the entry in the array. If the status of the IO indiactes that
1000 * this IO was aborted then the sglq entry it put on the
1001 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1002 * IO has good status or fails for any other reason then the sglq
1003 * entry is added to the free list (lpfc_sgl_list).
1004 **/
1005static void
1006__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1007{
1008 struct lpfc_sglq *sglq;
1009 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0
JS
1010 unsigned long iflag = 0;
1011 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4f774513 1012
1c2ba475
JT
1013 lockdep_assert_held(&phba->hbalock);
1014
4f774513
JS
1015 if (iocbq->sli4_xritag == NO_XRI)
1016 sglq = NULL;
1017 else
6d368e53
JS
1018 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1019
0e9bb8d7 1020
4f774513 1021 if (sglq) {
0f65ff68
JS
1022 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1023 (sglq->state != SGL_XRI_ABORTED)) {
4f774513
JS
1024 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1025 iflag);
1026 list_add(&sglq->list,
1027 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1028 spin_unlock_irqrestore(
1029 &phba->sli4_hba.abts_sgl_list_lock, iflag);
0f65ff68 1030 } else {
dafe8cea 1031 spin_lock_irqsave(&pring->ring_lock, iflag);
0f65ff68 1032 sglq->state = SGL_FREED;
19ca7609 1033 sglq->ndlp = NULL;
fedd3b7b
JS
1034 list_add_tail(&sglq->list,
1035 &phba->sli4_hba.lpfc_sgl_list);
dafe8cea 1036 spin_unlock_irqrestore(&pring->ring_lock, iflag);
2a9bf3d0
JS
1037
1038 /* Check if TXQ queue needs to be serviced */
0e9bb8d7 1039 if (!list_empty(&pring->txq))
2a9bf3d0 1040 lpfc_worker_wake_up(phba);
0f65ff68 1041 }
4f774513
JS
1042 }
1043
1044
1045 /*
1046 * Clean all volatile data fields, preserve iotag and node struct.
1047 */
1048 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
6d368e53 1049 iocbq->sli4_lxritag = NO_XRI;
4f774513
JS
1050 iocbq->sli4_xritag = NO_XRI;
1051 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1052}
1053
2a9bf3d0 1054
e59058c4 1055/**
3772a991 1056 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
1057 * @phba: Pointer to HBA context object.
1058 * @iocbq: Pointer to driver iocb object.
1059 *
1060 * This function is called with hbalock held to release driver
1061 * iocb object to the iocb pool. The iotag in the iocb object
1062 * does not change for each use of the iocb object. This function
1063 * clears all other fields of the iocb object when it is freed.
1064 **/
a6ababd2 1065static void
3772a991 1066__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 1067{
2e0fef85 1068 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30 1069
1c2ba475 1070 lockdep_assert_held(&phba->hbalock);
0e9bb8d7 1071
604a3e30
JB
1072 /*
1073 * Clean all volatile data fields, preserve iotag and node struct.
1074 */
1075 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 1076 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
1077 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1078}
1079
3772a991
JS
1080/**
1081 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1082 * @phba: Pointer to HBA context object.
1083 * @iocbq: Pointer to driver iocb object.
1084 *
1085 * This function is called with hbalock held to release driver
1086 * iocb object to the iocb pool. The iotag in the iocb object
1087 * does not change for each use of the iocb object. This function
1088 * clears all other fields of the iocb object when it is freed.
1089 **/
1090static void
1091__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1092{
1c2ba475
JT
1093 lockdep_assert_held(&phba->hbalock);
1094
3772a991 1095 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 1096 phba->iocb_cnt--;
3772a991
JS
1097}
1098
e59058c4 1099/**
3621a710 1100 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
1101 * @phba: Pointer to HBA context object.
1102 * @iocbq: Pointer to driver iocb object.
1103 *
1104 * This function is called with no lock held to release the iocb to
1105 * iocb pool.
1106 **/
2e0fef85
JS
1107void
1108lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1109{
1110 unsigned long iflags;
1111
1112 /*
1113 * Clean all volatile data fields, preserve iotag and node struct.
1114 */
1115 spin_lock_irqsave(&phba->hbalock, iflags);
1116 __lpfc_sli_release_iocbq(phba, iocbq);
1117 spin_unlock_irqrestore(&phba->hbalock, iflags);
1118}
1119
a257bf90
JS
1120/**
1121 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1122 * @phba: Pointer to HBA context object.
1123 * @iocblist: List of IOCBs.
1124 * @ulpstatus: ULP status in IOCB command field.
1125 * @ulpWord4: ULP word-4 in IOCB command field.
1126 *
1127 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1128 * on the list by invoking the complete callback function associated with the
1129 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1130 * fields.
1131 **/
1132void
1133lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1134 uint32_t ulpstatus, uint32_t ulpWord4)
1135{
1136 struct lpfc_iocbq *piocb;
1137
1138 while (!list_empty(iocblist)) {
1139 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
a257bf90
JS
1140 if (!piocb->iocb_cmpl)
1141 lpfc_sli_release_iocbq(phba, piocb);
1142 else {
1143 piocb->iocb.ulpStatus = ulpstatus;
1144 piocb->iocb.un.ulpWord[4] = ulpWord4;
1145 (piocb->iocb_cmpl) (phba, piocb, piocb);
1146 }
1147 }
1148 return;
1149}
1150
e59058c4 1151/**
3621a710
JS
1152 * lpfc_sli_iocb_cmd_type - Get the iocb type
1153 * @iocb_cmnd: iocb command code.
e59058c4
JS
1154 *
1155 * This function is called by ring event handler function to get the iocb type.
1156 * This function translates the iocb command to an iocb command type used to
1157 * decide the final disposition of each completed IOCB.
1158 * The function returns
1159 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1160 * LPFC_SOL_IOCB if it is a solicited iocb completion
1161 * LPFC_ABORT_IOCB if it is an abort iocb
1162 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1163 *
1164 * The caller is not required to hold any lock.
1165 **/
dea3101e
JB
1166static lpfc_iocb_type
1167lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1168{
1169 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1170
1171 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1172 return 0;
1173
1174 switch (iocb_cmnd) {
1175 case CMD_XMIT_SEQUENCE_CR:
1176 case CMD_XMIT_SEQUENCE_CX:
1177 case CMD_XMIT_BCAST_CN:
1178 case CMD_XMIT_BCAST_CX:
1179 case CMD_ELS_REQUEST_CR:
1180 case CMD_ELS_REQUEST_CX:
1181 case CMD_CREATE_XRI_CR:
1182 case CMD_CREATE_XRI_CX:
1183 case CMD_GET_RPI_CN:
1184 case CMD_XMIT_ELS_RSP_CX:
1185 case CMD_GET_RPI_CR:
1186 case CMD_FCP_IWRITE_CR:
1187 case CMD_FCP_IWRITE_CX:
1188 case CMD_FCP_IREAD_CR:
1189 case CMD_FCP_IREAD_CX:
1190 case CMD_FCP_ICMND_CR:
1191 case CMD_FCP_ICMND_CX:
f5603511
JS
1192 case CMD_FCP_TSEND_CX:
1193 case CMD_FCP_TRSP_CX:
1194 case CMD_FCP_TRECEIVE_CX:
1195 case CMD_FCP_AUTO_TRSP_CX:
dea3101e
JB
1196 case CMD_ADAPTER_MSG:
1197 case CMD_ADAPTER_DUMP:
1198 case CMD_XMIT_SEQUENCE64_CR:
1199 case CMD_XMIT_SEQUENCE64_CX:
1200 case CMD_XMIT_BCAST64_CN:
1201 case CMD_XMIT_BCAST64_CX:
1202 case CMD_ELS_REQUEST64_CR:
1203 case CMD_ELS_REQUEST64_CX:
1204 case CMD_FCP_IWRITE64_CR:
1205 case CMD_FCP_IWRITE64_CX:
1206 case CMD_FCP_IREAD64_CR:
1207 case CMD_FCP_IREAD64_CX:
1208 case CMD_FCP_ICMND64_CR:
1209 case CMD_FCP_ICMND64_CX:
f5603511
JS
1210 case CMD_FCP_TSEND64_CX:
1211 case CMD_FCP_TRSP64_CX:
1212 case CMD_FCP_TRECEIVE64_CX:
dea3101e
JB
1213 case CMD_GEN_REQUEST64_CR:
1214 case CMD_GEN_REQUEST64_CX:
1215 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
1216 case DSSCMD_IWRITE64_CR:
1217 case DSSCMD_IWRITE64_CX:
1218 case DSSCMD_IREAD64_CR:
1219 case DSSCMD_IREAD64_CX:
dea3101e
JB
1220 type = LPFC_SOL_IOCB;
1221 break;
1222 case CMD_ABORT_XRI_CN:
1223 case CMD_ABORT_XRI_CX:
1224 case CMD_CLOSE_XRI_CN:
1225 case CMD_CLOSE_XRI_CX:
1226 case CMD_XRI_ABORTED_CX:
1227 case CMD_ABORT_MXRI64_CN:
6669f9bb 1228 case CMD_XMIT_BLS_RSP64_CX:
dea3101e
JB
1229 type = LPFC_ABORT_IOCB;
1230 break;
1231 case CMD_RCV_SEQUENCE_CX:
1232 case CMD_RCV_ELS_REQ_CX:
1233 case CMD_RCV_SEQUENCE64_CX:
1234 case CMD_RCV_ELS_REQ64_CX:
57127f15 1235 case CMD_ASYNC_STATUS:
ed957684
JS
1236 case CMD_IOCB_RCV_SEQ64_CX:
1237 case CMD_IOCB_RCV_ELS64_CX:
1238 case CMD_IOCB_RCV_CONT64_CX:
3163f725 1239 case CMD_IOCB_RET_XRI64_CX:
dea3101e
JB
1240 type = LPFC_UNSOL_IOCB;
1241 break;
3163f725
JS
1242 case CMD_IOCB_XMIT_MSEQ64_CR:
1243 case CMD_IOCB_XMIT_MSEQ64_CX:
1244 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1245 case CMD_IOCB_RCV_ELS_LIST64_CX:
1246 case CMD_IOCB_CLOSE_EXTENDED_CN:
1247 case CMD_IOCB_ABORT_EXTENDED_CN:
1248 case CMD_IOCB_RET_HBQE64_CN:
1249 case CMD_IOCB_FCP_IBIDIR64_CR:
1250 case CMD_IOCB_FCP_IBIDIR64_CX:
1251 case CMD_IOCB_FCP_ITASKMGT64_CX:
1252 case CMD_IOCB_LOGENTRY_CN:
1253 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1254 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 1255 __func__, iocb_cmnd);
3163f725
JS
1256 type = LPFC_UNKNOWN_IOCB;
1257 break;
dea3101e
JB
1258 default:
1259 type = LPFC_UNKNOWN_IOCB;
1260 break;
1261 }
1262
1263 return type;
1264}
1265
e59058c4 1266/**
3621a710 1267 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
1268 * @phba: Pointer to HBA context object.
1269 *
1270 * This function is called from SLI initialization code
1271 * to configure every ring of the HBA's SLI interface. The
1272 * caller is not required to hold any lock. This function issues
1273 * a config_ring mailbox command for each ring.
1274 * This function returns zero if successful else returns a negative
1275 * error code.
1276 **/
dea3101e 1277static int
ed957684 1278lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e
JB
1279{
1280 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
1281 LPFC_MBOXQ_t *pmb;
1282 MAILBOX_t *pmbox;
1283 int i, rc, ret = 0;
dea3101e 1284
ed957684
JS
1285 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1286 if (!pmb)
1287 return -ENOMEM;
04c68496 1288 pmbox = &pmb->u.mb;
ed957684 1289 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 1290 for (i = 0; i < psli->num_rings; i++) {
dea3101e
JB
1291 lpfc_config_ring(phba, i, pmb);
1292 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1293 if (rc != MBX_SUCCESS) {
92d7f7b0 1294 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1295 "0446 Adapter failed to init (%d), "
dea3101e
JB
1296 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1297 "ring %d\n",
e8b62011
JS
1298 rc, pmbox->mbxCommand,
1299 pmbox->mbxStatus, i);
2e0fef85 1300 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
1301 ret = -ENXIO;
1302 break;
dea3101e
JB
1303 }
1304 }
ed957684
JS
1305 mempool_free(pmb, phba->mbox_mem_pool);
1306 return ret;
dea3101e
JB
1307}
1308
e59058c4 1309/**
3621a710 1310 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
1311 * @phba: Pointer to HBA context object.
1312 * @pring: Pointer to driver SLI ring object.
1313 * @piocb: Pointer to the driver iocb object.
1314 *
1315 * This function is called with hbalock held. The function adds the
1316 * new iocb to txcmplq of the given ring. This function always returns
1317 * 0. If this function is called for ELS ring, this function checks if
1318 * there is a vport associated with the ELS command. This function also
1319 * starts els_tmofunc timer if this is an ELS command.
1320 **/
dea3101e 1321static int
2e0fef85
JS
1322lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1323 struct lpfc_iocbq *piocb)
dea3101e 1324{
1c2ba475
JT
1325 lockdep_assert_held(&phba->hbalock);
1326
2319f847 1327 BUG_ON(!piocb);
22466da5 1328
dea3101e 1329 list_add_tail(&piocb->list, &pring->txcmplq);
4f2e66c6 1330 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
2a9bf3d0 1331
92d7f7b0
JS
1332 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1333 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
2319f847
MFO
1334 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1335 BUG_ON(!piocb->vport);
1336 if (!(piocb->vport->load_flag & FC_UNLOADING))
1337 mod_timer(&piocb->vport->els_tmofunc,
1338 jiffies +
1339 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1340 }
dea3101e 1341
2e0fef85 1342 return 0;
dea3101e
JB
1343}
1344
e59058c4 1345/**
3621a710 1346 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
1347 * @phba: Pointer to HBA context object.
1348 * @pring: Pointer to driver SLI ring object.
1349 *
1350 * This function is called with hbalock held to get next
1351 * iocb in txq of the given ring. If there is any iocb in
1352 * the txq, the function returns first iocb in the list after
1353 * removing the iocb from the list, else it returns NULL.
1354 **/
2a9bf3d0 1355struct lpfc_iocbq *
2e0fef85 1356lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1357{
dea3101e
JB
1358 struct lpfc_iocbq *cmd_iocb;
1359
1c2ba475
JT
1360 lockdep_assert_held(&phba->hbalock);
1361
858c9f6c 1362 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
2e0fef85 1363 return cmd_iocb;
dea3101e
JB
1364}
1365
e59058c4 1366/**
3621a710 1367 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
1368 * @phba: Pointer to HBA context object.
1369 * @pring: Pointer to driver SLI ring object.
1370 *
1371 * This function is called with hbalock held and the caller must post the
1372 * iocb without releasing the lock. If the caller releases the lock,
1373 * iocb slot returned by the function is not guaranteed to be available.
1374 * The function returns pointer to the next available iocb slot if there
1375 * is available slot in the ring, else it returns NULL.
1376 * If the get index of the ring is ahead of the put index, the function
1377 * will post an error attention event to the worker thread to take the
1378 * HBA to offline state.
1379 **/
dea3101e
JB
1380static IOCB_t *
1381lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1382{
34b02dcd 1383 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
7e56aa25 1384 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1c2ba475
JT
1385
1386 lockdep_assert_held(&phba->hbalock);
1387
7e56aa25
JS
1388 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1389 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1390 pring->sli.sli3.next_cmdidx = 0;
dea3101e 1391
7e56aa25
JS
1392 if (unlikely(pring->sli.sli3.local_getidx ==
1393 pring->sli.sli3.next_cmdidx)) {
dea3101e 1394
7e56aa25 1395 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e 1396
7e56aa25 1397 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea3101e 1398 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 1399 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 1400 "is bigger than cmd ring %d\n",
e8b62011 1401 pring->ringno,
7e56aa25
JS
1402 pring->sli.sli3.local_getidx,
1403 max_cmd_idx);
dea3101e 1404
2e0fef85 1405 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
1406 /*
1407 * All error attention handlers are posted to
1408 * worker thread
1409 */
1410 phba->work_ha |= HA_ERATT;
1411 phba->work_hs = HS_FFER3;
92d7f7b0 1412
5e9d9b82 1413 lpfc_worker_wake_up(phba);
dea3101e
JB
1414
1415 return NULL;
1416 }
1417
7e56aa25 1418 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea3101e
JB
1419 return NULL;
1420 }
1421
ed957684 1422 return lpfc_cmd_iocb(phba, pring);
dea3101e
JB
1423}
1424
e59058c4 1425/**
3621a710 1426 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
1427 * @phba: Pointer to HBA context object.
1428 * @iocbq: Pointer to driver iocb object.
1429 *
1430 * This function gets an iotag for the iocb. If there is no unused iotag and
1431 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1432 * array and assigns a new iotag.
1433 * The function returns the allocated iotag if successful, else returns zero.
1434 * Zero is not a valid iotag.
1435 * The caller is not required to hold any lock.
1436 **/
604a3e30 1437uint16_t
2e0fef85 1438lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 1439{
2e0fef85
JS
1440 struct lpfc_iocbq **new_arr;
1441 struct lpfc_iocbq **old_arr;
604a3e30
JB
1442 size_t new_len;
1443 struct lpfc_sli *psli = &phba->sli;
1444 uint16_t iotag;
dea3101e 1445
2e0fef85 1446 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1447 iotag = psli->last_iotag;
1448 if(++iotag < psli->iocbq_lookup_len) {
1449 psli->last_iotag = iotag;
1450 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1451 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1452 iocbq->iotag = iotag;
1453 return iotag;
2e0fef85 1454 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1455 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1456 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
1457 spin_unlock_irq(&phba->hbalock);
1458 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
1459 GFP_KERNEL);
1460 if (new_arr) {
2e0fef85 1461 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1462 old_arr = psli->iocbq_lookup;
1463 if (new_len <= psli->iocbq_lookup_len) {
1464 /* highly unprobable case */
1465 kfree(new_arr);
1466 iotag = psli->last_iotag;
1467 if(++iotag < psli->iocbq_lookup_len) {
1468 psli->last_iotag = iotag;
1469 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1470 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1471 iocbq->iotag = iotag;
1472 return iotag;
1473 }
2e0fef85 1474 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1475 return 0;
1476 }
1477 if (psli->iocbq_lookup)
1478 memcpy(new_arr, old_arr,
1479 ((psli->last_iotag + 1) *
311464ec 1480 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1481 psli->iocbq_lookup = new_arr;
1482 psli->iocbq_lookup_len = new_len;
1483 psli->last_iotag = iotag;
1484 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1485 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1486 iocbq->iotag = iotag;
1487 kfree(old_arr);
1488 return iotag;
1489 }
8f6d98d2 1490 } else
2e0fef85 1491 spin_unlock_irq(&phba->hbalock);
dea3101e 1492
bc73905a 1493 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1494 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1495 psli->last_iotag);
dea3101e 1496
604a3e30 1497 return 0;
dea3101e
JB
1498}
1499
e59058c4 1500/**
3621a710 1501 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1502 * @phba: Pointer to HBA context object.
1503 * @pring: Pointer to driver SLI ring object.
1504 * @iocb: Pointer to iocb slot in the ring.
1505 * @nextiocb: Pointer to driver iocb object which need to be
1506 * posted to firmware.
1507 *
1508 * This function is called with hbalock held to post a new iocb to
1509 * the firmware. This function copies the new iocb to ring iocb slot and
1510 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1511 * a completion call back for this iocb else the function will free the
1512 * iocb object.
1513 **/
dea3101e
JB
1514static void
1515lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1516 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1517{
1c2ba475 1518 lockdep_assert_held(&phba->hbalock);
dea3101e 1519 /*
604a3e30 1520 * Set up an iotag
dea3101e 1521 */
604a3e30 1522 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1523
e2a0a9d6 1524
a58cbd52
JS
1525 if (pring->ringno == LPFC_ELS_RING) {
1526 lpfc_debugfs_slow_ring_trc(phba,
1527 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1528 *(((uint32_t *) &nextiocb->iocb) + 4),
1529 *(((uint32_t *) &nextiocb->iocb) + 6),
1530 *(((uint32_t *) &nextiocb->iocb) + 7));
1531 }
1532
dea3101e
JB
1533 /*
1534 * Issue iocb command to adapter
1535 */
92d7f7b0 1536 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e
JB
1537 wmb();
1538 pring->stats.iocb_cmd++;
1539
1540 /*
1541 * If there is no completion routine to call, we can release the
1542 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1543 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1544 */
1545 if (nextiocb->iocb_cmpl)
1546 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1547 else
2e0fef85 1548 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e
JB
1549
1550 /*
1551 * Let the HBA know what IOCB slot will be the next one the
1552 * driver will put a command into.
1553 */
7e56aa25
JS
1554 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1555 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e
JB
1556}
1557
e59058c4 1558/**
3621a710 1559 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1560 * @phba: Pointer to HBA context object.
1561 * @pring: Pointer to driver SLI ring object.
1562 *
1563 * The caller is not required to hold any lock for calling this function.
1564 * This function updates the chip attention bits for the ring to inform firmware
1565 * that there are pending work to be done for this ring and requests an
1566 * interrupt when there is space available in the ring. This function is
1567 * called when the driver is unable to post more iocbs to the ring due
1568 * to unavailability of space in the ring.
1569 **/
dea3101e 1570static void
2e0fef85 1571lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1572{
1573 int ringno = pring->ringno;
1574
1575 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1576
1577 wmb();
1578
1579 /*
1580 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1581 * The HBA will tell us when an IOCB entry is available.
1582 */
1583 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1584 readl(phba->CAregaddr); /* flush */
1585
1586 pring->stats.iocb_cmd_full++;
1587}
1588
e59058c4 1589/**
3621a710 1590 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1591 * @phba: Pointer to HBA context object.
1592 * @pring: Pointer to driver SLI ring object.
1593 *
1594 * This function updates the chip attention register bit for the
1595 * given ring to inform HBA that there is more work to be done
1596 * in this ring. The caller is not required to hold any lock.
1597 **/
dea3101e 1598static void
2e0fef85 1599lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1600{
1601 int ringno = pring->ringno;
1602
1603 /*
1604 * Tell the HBA that there is work to do in this ring.
1605 */
34b02dcd
JS
1606 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1607 wmb();
1608 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1609 readl(phba->CAregaddr); /* flush */
1610 }
dea3101e
JB
1611}
1612
e59058c4 1613/**
3621a710 1614 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1615 * @phba: Pointer to HBA context object.
1616 * @pring: Pointer to driver SLI ring object.
1617 *
1618 * This function is called with hbalock held to post pending iocbs
1619 * in the txq to the firmware. This function is called when driver
1620 * detects space available in the ring.
1621 **/
dea3101e 1622static void
2e0fef85 1623lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1624{
1625 IOCB_t *iocb;
1626 struct lpfc_iocbq *nextiocb;
1627
1c2ba475
JT
1628 lockdep_assert_held(&phba->hbalock);
1629
dea3101e
JB
1630 /*
1631 * Check to see if:
1632 * (a) there is anything on the txq to send
1633 * (b) link is up
1634 * (c) link attention events can be processed (fcp ring only)
1635 * (d) IOCB processing is not blocked by the outstanding mbox command.
1636 */
0e9bb8d7
JS
1637
1638 if (lpfc_is_link_up(phba) &&
1639 (!list_empty(&pring->txq)) &&
dea3101e 1640 (pring->ringno != phba->sli.fcp_ring ||
0b727fea 1641 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e
JB
1642
1643 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1644 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1645 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1646
1647 if (iocb)
1648 lpfc_sli_update_ring(phba, pring);
1649 else
1650 lpfc_sli_update_full_ring(phba, pring);
1651 }
1652
1653 return;
1654}
1655
e59058c4 1656/**
3621a710 1657 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1658 * @phba: Pointer to HBA context object.
1659 * @hbqno: HBQ number.
1660 *
1661 * This function is called with hbalock held to get the next
1662 * available slot for the given HBQ. If there is free slot
1663 * available for the HBQ it will return pointer to the next available
1664 * HBQ entry else it will return NULL.
1665 **/
a6ababd2 1666static struct lpfc_hbq_entry *
ed957684
JS
1667lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1668{
1669 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1670
1c2ba475
JT
1671 lockdep_assert_held(&phba->hbalock);
1672
ed957684
JS
1673 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1674 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1675 hbqp->next_hbqPutIdx = 0;
1676
1677 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1678 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1679 uint32_t getidx = le32_to_cpu(raw_index);
1680
1681 hbqp->local_hbqGetIdx = getidx;
1682
1683 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1684 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1685 LOG_SLI | LOG_VPORT,
e8b62011 1686 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1687 "%u is > than hbqp->entry_count %u\n",
e8b62011 1688 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1689 hbqp->entry_count);
1690
1691 phba->link_state = LPFC_HBA_ERROR;
1692 return NULL;
1693 }
1694
1695 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1696 return NULL;
1697 }
1698
51ef4c26
JS
1699 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1700 hbqp->hbqPutIdx;
ed957684
JS
1701}
1702
e59058c4 1703/**
3621a710 1704 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1705 * @phba: Pointer to HBA context object.
1706 *
1707 * This function is called with no lock held to free all the
1708 * hbq buffers while uninitializing the SLI interface. It also
1709 * frees the HBQ buffers returned by the firmware but not yet
1710 * processed by the upper layers.
1711 **/
ed957684
JS
1712void
1713lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1714{
92d7f7b0
JS
1715 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1716 struct hbq_dmabuf *hbq_buf;
3163f725 1717 unsigned long flags;
51ef4c26 1718 int i, hbq_count;
3163f725 1719 uint32_t hbqno;
ed957684 1720
51ef4c26 1721 hbq_count = lpfc_sli_hbq_count();
ed957684 1722 /* Return all memory used by all HBQs */
3163f725 1723 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1724 for (i = 0; i < hbq_count; ++i) {
1725 list_for_each_entry_safe(dmabuf, next_dmabuf,
1726 &phba->hbqs[i].hbq_buffer_list, list) {
1727 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1728 list_del(&hbq_buf->dbuf.list);
1729 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1730 }
a8adb832 1731 phba->hbqs[i].buffer_count = 0;
ed957684 1732 }
3163f725 1733 /* Return all HBQ buffer that are in-fly */
3772a991
JS
1734 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1735 list) {
3163f725
JS
1736 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1737 list_del(&hbq_buf->dbuf.list);
1738 if (hbq_buf->tag == -1) {
1739 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1740 (phba, hbq_buf);
1741 } else {
1742 hbqno = hbq_buf->tag >> 16;
1743 if (hbqno >= LPFC_MAX_HBQS)
1744 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1745 (phba, hbq_buf);
1746 else
1747 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1748 hbq_buf);
1749 }
1750 }
1751
1752 /* Mark the HBQs not in use */
1753 phba->hbq_in_use = 0;
1754 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1755}
1756
e59058c4 1757/**
3621a710 1758 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1759 * @phba: Pointer to HBA context object.
1760 * @hbqno: HBQ number.
1761 * @hbq_buf: Pointer to HBQ buffer.
1762 *
1763 * This function is called with the hbalock held to post a
1764 * hbq buffer to the firmware. If the function finds an empty
1765 * slot in the HBQ, it will post the buffer. The function will return
1766 * pointer to the hbq entry if it successfully post the buffer
1767 * else it will return NULL.
1768 **/
3772a991 1769static int
ed957684 1770lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1771 struct hbq_dmabuf *hbq_buf)
3772a991 1772{
1c2ba475 1773 lockdep_assert_held(&phba->hbalock);
3772a991
JS
1774 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1775}
1776
1777/**
1778 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1779 * @phba: Pointer to HBA context object.
1780 * @hbqno: HBQ number.
1781 * @hbq_buf: Pointer to HBQ buffer.
1782 *
1783 * This function is called with the hbalock held to post a hbq buffer to the
1784 * firmware. If the function finds an empty slot in the HBQ, it will post the
1785 * buffer and place it on the hbq_buffer_list. The function will return zero if
1786 * it successfully post the buffer else it will return an error.
1787 **/
1788static int
1789lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1790 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1791{
1792 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1793 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684 1794
1c2ba475 1795 lockdep_assert_held(&phba->hbalock);
ed957684
JS
1796 /* Get next HBQ entry slot to use */
1797 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1798 if (hbqe) {
1799 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1800
92d7f7b0
JS
1801 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1802 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
51ef4c26 1803 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
ed957684 1804 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1805 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1806 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1807 /* Sync SLIM */
ed957684
JS
1808 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1809 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1810 /* flush */
ed957684 1811 readl(phba->hbq_put + hbqno);
51ef4c26 1812 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1813 return 0;
1814 } else
1815 return -ENOMEM;
ed957684
JS
1816}
1817
4f774513
JS
1818/**
1819 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1820 * @phba: Pointer to HBA context object.
1821 * @hbqno: HBQ number.
1822 * @hbq_buf: Pointer to HBQ buffer.
1823 *
1824 * This function is called with the hbalock held to post an RQE to the SLI4
1825 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1826 * the hbq_buffer_list and return zero, otherwise it will return an error.
1827 **/
1828static int
1829lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1830 struct hbq_dmabuf *hbq_buf)
1831{
1832 int rc;
1833 struct lpfc_rqe hrqe;
1834 struct lpfc_rqe drqe;
1835
1c2ba475 1836 lockdep_assert_held(&phba->hbalock);
4f774513
JS
1837 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1838 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1839 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1840 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1841 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1842 &hrqe, &drqe);
1843 if (rc < 0)
1844 return rc;
1845 hbq_buf->tag = rc;
1846 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1847 return 0;
1848}
1849
e59058c4 1850/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1851static struct lpfc_hbq_init lpfc_els_hbq = {
1852 .rn = 1,
def9c7a9 1853 .entry_count = 256,
92d7f7b0
JS
1854 .mask_count = 0,
1855 .profile = 0,
51ef4c26 1856 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1857 .buffer_count = 0,
a257bf90
JS
1858 .init_count = 40,
1859 .add_count = 40,
92d7f7b0 1860};
ed957684 1861
e59058c4 1862/* HBQ for the extra ring if needed */
51ef4c26
JS
1863static struct lpfc_hbq_init lpfc_extra_hbq = {
1864 .rn = 1,
1865 .entry_count = 200,
1866 .mask_count = 0,
1867 .profile = 0,
1868 .ring_mask = (1 << LPFC_EXTRA_RING),
1869 .buffer_count = 0,
1870 .init_count = 0,
1871 .add_count = 5,
1872};
1873
e59058c4 1874/* Array of HBQs */
78b2d852 1875struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0 1876 &lpfc_els_hbq,
51ef4c26 1877 &lpfc_extra_hbq,
92d7f7b0 1878};
ed957684 1879
e59058c4 1880/**
3621a710 1881 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1882 * @phba: Pointer to HBA context object.
1883 * @hbqno: HBQ number.
1884 * @count: Number of HBQ buffers to be posted.
1885 *
d7c255b2
JS
1886 * This function is called with no lock held to post more hbq buffers to the
1887 * given HBQ. The function returns the number of HBQ buffers successfully
1888 * posted.
e59058c4 1889 **/
311464ec 1890static int
92d7f7b0 1891lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1892{
d7c255b2 1893 uint32_t i, posted = 0;
3163f725 1894 unsigned long flags;
92d7f7b0 1895 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1896 LIST_HEAD(hbq_buf_list);
eafe1df9 1897 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1898 return 0;
51ef4c26 1899
d7c255b2
JS
1900 if ((phba->hbqs[hbqno].buffer_count + count) >
1901 lpfc_hbq_defs[hbqno]->entry_count)
1902 count = lpfc_hbq_defs[hbqno]->entry_count -
1903 phba->hbqs[hbqno].buffer_count;
1904 if (!count)
1905 return 0;
1906 /* Allocate HBQ entries */
1907 for (i = 0; i < count; i++) {
1908 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1909 if (!hbq_buffer)
1910 break;
1911 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1912 }
3163f725
JS
1913 /* Check whether HBQ is still in use */
1914 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1915 if (!phba->hbq_in_use)
d7c255b2
JS
1916 goto err;
1917 while (!list_empty(&hbq_buf_list)) {
1918 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1919 dbuf.list);
1920 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1921 (hbqno << 16));
3772a991 1922 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1923 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1924 posted++;
1925 } else
51ef4c26 1926 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1927 }
3163f725 1928 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1929 return posted;
1930err:
eafe1df9 1931 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1932 while (!list_empty(&hbq_buf_list)) {
1933 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1934 dbuf.list);
1935 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1936 }
1937 return 0;
ed957684
JS
1938}
1939
e59058c4 1940/**
3621a710 1941 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1942 * @phba: Pointer to HBA context object.
1943 * @qno: HBQ number.
1944 *
1945 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1946 * is called with no lock held. The function returns the number of HBQ entries
1947 * successfully allocated.
e59058c4 1948 **/
92d7f7b0
JS
1949int
1950lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1951{
def9c7a9
JS
1952 if (phba->sli_rev == LPFC_SLI_REV4)
1953 return 0;
1954 else
1955 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1956 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 1957}
ed957684 1958
e59058c4 1959/**
3621a710 1960 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
1961 * @phba: Pointer to HBA context object.
1962 * @qno: HBQ queue number.
1963 *
1964 * This function is called from SLI initialization code path with
1965 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 1966 * function returns the number of HBQ entries successfully allocated.
e59058c4 1967 **/
a6ababd2 1968static int
92d7f7b0
JS
1969lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1970{
def9c7a9
JS
1971 if (phba->sli_rev == LPFC_SLI_REV4)
1972 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
73d91e50 1973 lpfc_hbq_defs[qno]->entry_count);
def9c7a9
JS
1974 else
1975 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1976 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
1977}
1978
3772a991
JS
1979/**
1980 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1981 * @phba: Pointer to HBA context object.
1982 * @hbqno: HBQ number.
1983 *
1984 * This function removes the first hbq buffer on an hbq list and returns a
1985 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1986 **/
1987static struct hbq_dmabuf *
1988lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1989{
1990 struct lpfc_dmabuf *d_buf;
1991
1992 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1993 if (!d_buf)
1994 return NULL;
1995 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1996}
1997
e59058c4 1998/**
3621a710 1999 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
2000 * @phba: Pointer to HBA context object.
2001 * @tag: Tag of the hbq buffer.
2002 *
71892418
SH
2003 * This function searches for the hbq buffer associated with the given tag in
2004 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2005 * otherwise it returns NULL.
e59058c4 2006 **/
a6ababd2 2007static struct hbq_dmabuf *
92d7f7b0 2008lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 2009{
92d7f7b0
JS
2010 struct lpfc_dmabuf *d_buf;
2011 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
2012 uint32_t hbqno;
2013
2014 hbqno = tag >> 16;
a0a74e45 2015 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 2016 return NULL;
ed957684 2017
3772a991 2018 spin_lock_irq(&phba->hbalock);
51ef4c26 2019 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 2020 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 2021 if (hbq_buf->tag == tag) {
3772a991 2022 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2023 return hbq_buf;
ed957684
JS
2024 }
2025 }
3772a991 2026 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2027 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 2028 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 2029 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 2030 return NULL;
ed957684
JS
2031}
2032
e59058c4 2033/**
3621a710 2034 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
2035 * @phba: Pointer to HBA context object.
2036 * @hbq_buffer: Pointer to HBQ buffer.
2037 *
2038 * This function is called with hbalock. This function gives back
2039 * the hbq buffer to firmware. If the HBQ does not have space to
2040 * post the buffer, it will free the buffer.
2041 **/
ed957684 2042void
51ef4c26 2043lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
2044{
2045 uint32_t hbqno;
2046
51ef4c26
JS
2047 if (hbq_buffer) {
2048 hbqno = hbq_buffer->tag >> 16;
3772a991 2049 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 2050 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
2051 }
2052}
2053
e59058c4 2054/**
3621a710 2055 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
2056 * @mbxCommand: mailbox command code.
2057 *
2058 * This function is called by the mailbox event handler function to verify
2059 * that the completed mailbox command is a legitimate mailbox command. If the
2060 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2061 * and the mailbox event handler will take the HBA offline.
2062 **/
dea3101e
JB
2063static int
2064lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2065{
2066 uint8_t ret;
2067
2068 switch (mbxCommand) {
2069 case MBX_LOAD_SM:
2070 case MBX_READ_NV:
2071 case MBX_WRITE_NV:
a8adb832 2072 case MBX_WRITE_VPARMS:
dea3101e
JB
2073 case MBX_RUN_BIU_DIAG:
2074 case MBX_INIT_LINK:
2075 case MBX_DOWN_LINK:
2076 case MBX_CONFIG_LINK:
2077 case MBX_CONFIG_RING:
2078 case MBX_RESET_RING:
2079 case MBX_READ_CONFIG:
2080 case MBX_READ_RCONFIG:
2081 case MBX_READ_SPARM:
2082 case MBX_READ_STATUS:
2083 case MBX_READ_RPI:
2084 case MBX_READ_XRI:
2085 case MBX_READ_REV:
2086 case MBX_READ_LNK_STAT:
2087 case MBX_REG_LOGIN:
2088 case MBX_UNREG_LOGIN:
dea3101e
JB
2089 case MBX_CLEAR_LA:
2090 case MBX_DUMP_MEMORY:
2091 case MBX_DUMP_CONTEXT:
2092 case MBX_RUN_DIAGS:
2093 case MBX_RESTART:
2094 case MBX_UPDATE_CFG:
2095 case MBX_DOWN_LOAD:
2096 case MBX_DEL_LD_ENTRY:
2097 case MBX_RUN_PROGRAM:
2098 case MBX_SET_MASK:
09372820 2099 case MBX_SET_VARIABLE:
dea3101e 2100 case MBX_UNREG_D_ID:
41415862 2101 case MBX_KILL_BOARD:
dea3101e 2102 case MBX_CONFIG_FARP:
41415862 2103 case MBX_BEACON:
dea3101e
JB
2104 case MBX_LOAD_AREA:
2105 case MBX_RUN_BIU_DIAG64:
2106 case MBX_CONFIG_PORT:
2107 case MBX_READ_SPARM64:
2108 case MBX_READ_RPI64:
2109 case MBX_REG_LOGIN64:
76a95d75 2110 case MBX_READ_TOPOLOGY:
09372820 2111 case MBX_WRITE_WWN:
dea3101e
JB
2112 case MBX_SET_DEBUG:
2113 case MBX_LOAD_EXP_ROM:
57127f15 2114 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
2115 case MBX_REG_VPI:
2116 case MBX_UNREG_VPI:
858c9f6c 2117 case MBX_HEARTBEAT:
84774a4d
JS
2118 case MBX_PORT_CAPABILITIES:
2119 case MBX_PORT_IOV_CONTROL:
04c68496
JS
2120 case MBX_SLI4_CONFIG:
2121 case MBX_SLI4_REQ_FTRS:
2122 case MBX_REG_FCFI:
2123 case MBX_UNREG_FCFI:
2124 case MBX_REG_VFI:
2125 case MBX_UNREG_VFI:
2126 case MBX_INIT_VPI:
2127 case MBX_INIT_VFI:
2128 case MBX_RESUME_RPI:
c7495937
JS
2129 case MBX_READ_EVENT_LOG_STATUS:
2130 case MBX_READ_EVENT_LOG:
dcf2a4e0
JS
2131 case MBX_SECURITY_MGMT:
2132 case MBX_AUTH_PORT:
940eb687 2133 case MBX_ACCESS_VDATA:
dea3101e
JB
2134 ret = mbxCommand;
2135 break;
2136 default:
2137 ret = MBX_SHUTDOWN;
2138 break;
2139 }
2e0fef85 2140 return ret;
dea3101e 2141}
e59058c4
JS
2142
2143/**
3621a710 2144 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
2145 * @phba: Pointer to HBA context object.
2146 * @pmboxq: Pointer to mailbox command.
2147 *
2148 * This is completion handler function for mailbox commands issued from
2149 * lpfc_sli_issue_mbox_wait function. This function is called by the
2150 * mailbox event handler function with no lock held. This function
2151 * will wake up thread waiting on the wait queue pointed by context1
2152 * of the mailbox.
2153 **/
04c68496 2154void
2e0fef85 2155lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e
JB
2156{
2157 wait_queue_head_t *pdone_q;
858c9f6c 2158 unsigned long drvr_flag;
dea3101e
JB
2159
2160 /*
2161 * If pdone_q is empty, the driver thread gave up waiting and
2162 * continued running.
2163 */
7054a606 2164 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2165 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e
JB
2166 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2167 if (pdone_q)
2168 wake_up_interruptible(pdone_q);
858c9f6c 2169 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
2170 return;
2171}
2172
e59058c4
JS
2173
2174/**
3621a710 2175 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
2176 * @phba: Pointer to HBA context object.
2177 * @pmb: Pointer to mailbox object.
2178 *
2179 * This function is the default mailbox completion handler. It
2180 * frees the memory resources associated with the completed mailbox
2181 * command. If the completed command is a REG_LOGIN mailbox command,
2182 * this function will issue a UREG_LOGIN to re-claim the RPI.
2183 **/
dea3101e 2184void
2e0fef85 2185lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2186{
d439d286 2187 struct lpfc_vport *vport = pmb->vport;
dea3101e 2188 struct lpfc_dmabuf *mp;
d439d286 2189 struct lpfc_nodelist *ndlp;
5af5eee7 2190 struct Scsi_Host *shost;
04c68496 2191 uint16_t rpi, vpi;
7054a606
JS
2192 int rc;
2193
dea3101e 2194 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 2195
dea3101e
JB
2196 if (mp) {
2197 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2198 kfree(mp);
2199 }
7054a606
JS
2200
2201 /*
2202 * If a REG_LOGIN succeeded after node is destroyed or node
2203 * is in re-discovery driver need to cleanup the RPI.
2204 */
2e0fef85 2205 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
2206 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2207 !pmb->u.mb.mbxStatus) {
2208 rpi = pmb->u.mb.un.varWords[0];
6d368e53 2209 vpi = pmb->u.mb.un.varRegLogin.vpi;
04c68496 2210 lpfc_unreg_login(phba, vpi, rpi, pmb);
de96e9c5 2211 pmb->vport = vport;
92d7f7b0 2212 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
2213 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2214 if (rc != MBX_NOT_FINISHED)
2215 return;
2216 }
2217
695a814e
JS
2218 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2219 !(phba->pport->load_flag & FC_UNLOADING) &&
2220 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
2221 shost = lpfc_shost_from_vport(vport);
2222 spin_lock_irq(shost->host_lock);
2223 vport->vpi_state |= LPFC_VPI_REGISTERED;
2224 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2225 spin_unlock_irq(shost->host_lock);
695a814e
JS
2226 }
2227
d439d286
JS
2228 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2229 ndlp = (struct lpfc_nodelist *)pmb->context2;
2230 lpfc_nlp_put(ndlp);
2231 pmb->context2 = NULL;
2232 }
2233
dcf2a4e0
JS
2234 /* Check security permission status on INIT_LINK mailbox command */
2235 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2236 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2237 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2238 "2860 SLI authentication is required "
2239 "for INIT_LINK but has not done yet\n");
2240
04c68496
JS
2241 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2242 lpfc_sli4_mbox_cmd_free(phba, pmb);
2243 else
2244 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2245}
be6bb941
JS
2246 /**
2247 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2248 * @phba: Pointer to HBA context object.
2249 * @pmb: Pointer to mailbox object.
2250 *
2251 * This function is the unreg rpi mailbox completion handler. It
2252 * frees the memory resources associated with the completed mailbox
2253 * command. An additional refrenece is put on the ndlp to prevent
2254 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2255 * the unreg mailbox command completes, this routine puts the
2256 * reference back.
2257 *
2258 **/
2259void
2260lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2261{
2262 struct lpfc_vport *vport = pmb->vport;
2263 struct lpfc_nodelist *ndlp;
2264
2265 ndlp = pmb->context1;
2266 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2267 if (phba->sli_rev == LPFC_SLI_REV4 &&
2268 (bf_get(lpfc_sli_intf_if_type,
2269 &phba->sli4_hba.sli_intf) ==
2270 LPFC_SLI_INTF_IF_TYPE_2)) {
2271 if (ndlp) {
2272 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2273 "0010 UNREG_LOGIN vpi:%x "
2274 "rpi:%x DID:%x map:%x %p\n",
2275 vport->vpi, ndlp->nlp_rpi,
2276 ndlp->nlp_DID,
2277 ndlp->nlp_usg_map, ndlp);
7c5e518c 2278 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
be6bb941
JS
2279 lpfc_nlp_put(ndlp);
2280 }
2281 }
2282 }
2283
2284 mempool_free(pmb, phba->mbox_mem_pool);
2285}
dea3101e 2286
e59058c4 2287/**
3621a710 2288 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
2289 * @phba: Pointer to HBA context object.
2290 *
2291 * This function is called with no lock held. This function processes all
2292 * the completed mailbox commands and gives it to upper layers. The interrupt
2293 * service routine processes mailbox completion interrupt and adds completed
2294 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2295 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2296 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2297 * function returns the mailbox commands to the upper layer by calling the
2298 * completion handler function of each mailbox.
2299 **/
dea3101e 2300int
2e0fef85 2301lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 2302{
92d7f7b0 2303 MAILBOX_t *pmbox;
dea3101e 2304 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
2305 int rc;
2306 LIST_HEAD(cmplq);
dea3101e
JB
2307
2308 phba->sli.slistat.mbox_event++;
2309
92d7f7b0
JS
2310 /* Get all completed mailboxe buffers into the cmplq */
2311 spin_lock_irq(&phba->hbalock);
2312 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2313 spin_unlock_irq(&phba->hbalock);
dea3101e 2314
92d7f7b0
JS
2315 /* Get a Mailbox buffer to setup mailbox commands for callback */
2316 do {
2317 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2318 if (pmb == NULL)
2319 break;
2e0fef85 2320
04c68496 2321 pmbox = &pmb->u.mb;
dea3101e 2322
858c9f6c
JS
2323 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2324 if (pmb->vport) {
2325 lpfc_debugfs_disc_trc(pmb->vport,
2326 LPFC_DISC_TRC_MBOX_VPORT,
2327 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2328 (uint32_t)pmbox->mbxCommand,
2329 pmbox->un.varWords[0],
2330 pmbox->un.varWords[1]);
2331 }
2332 else {
2333 lpfc_debugfs_disc_trc(phba->pport,
2334 LPFC_DISC_TRC_MBOX,
2335 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2336 (uint32_t)pmbox->mbxCommand,
2337 pmbox->un.varWords[0],
2338 pmbox->un.varWords[1]);
2339 }
2340 }
2341
dea3101e
JB
2342 /*
2343 * It is a fatal error if unknown mbox command completion.
2344 */
2345 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2346 MBX_SHUTDOWN) {
af901ca1 2347 /* Unknown mailbox command compl */
92d7f7b0 2348 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 2349 "(%d):0323 Unknown Mailbox command "
a183a15f 2350 "x%x (x%x/x%x) Cmpl\n",
92d7f7b0 2351 pmb->vport ? pmb->vport->vpi : 0,
04c68496 2352 pmbox->mbxCommand,
a183a15f
JS
2353 lpfc_sli_config_mbox_subsys_get(phba,
2354 pmb),
2355 lpfc_sli_config_mbox_opcode_get(phba,
2356 pmb));
2e0fef85 2357 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
2358 phba->work_hs = HS_FFER3;
2359 lpfc_handle_eratt(phba);
92d7f7b0 2360 continue;
dea3101e
JB
2361 }
2362
dea3101e
JB
2363 if (pmbox->mbxStatus) {
2364 phba->sli.slistat.mbox_stat_err++;
2365 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2366 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0 2367 lpfc_printf_log(phba, KERN_INFO,
a183a15f
JS
2368 LOG_MBOX | LOG_SLI,
2369 "(%d):0305 Mbox cmd cmpl "
2370 "error - RETRYing Data: x%x "
2371 "(x%x/x%x) x%x x%x x%x\n",
2372 pmb->vport ? pmb->vport->vpi : 0,
2373 pmbox->mbxCommand,
2374 lpfc_sli_config_mbox_subsys_get(phba,
2375 pmb),
2376 lpfc_sli_config_mbox_opcode_get(phba,
2377 pmb),
2378 pmbox->mbxStatus,
2379 pmbox->un.varWords[0],
2380 pmb->vport->port_state);
dea3101e
JB
2381 pmbox->mbxStatus = 0;
2382 pmbox->mbxOwner = OWN_HOST;
dea3101e 2383 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 2384 if (rc != MBX_NOT_FINISHED)
92d7f7b0 2385 continue;
dea3101e
JB
2386 }
2387 }
2388
2389 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 2390 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 2391 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
e74c03c8
JS
2392 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2393 "x%x x%x x%x\n",
92d7f7b0 2394 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 2395 pmbox->mbxCommand,
a183a15f
JS
2396 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2397 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea3101e
JB
2398 pmb->mbox_cmpl,
2399 *((uint32_t *) pmbox),
2400 pmbox->un.varWords[0],
2401 pmbox->un.varWords[1],
2402 pmbox->un.varWords[2],
2403 pmbox->un.varWords[3],
2404 pmbox->un.varWords[4],
2405 pmbox->un.varWords[5],
2406 pmbox->un.varWords[6],
e74c03c8
JS
2407 pmbox->un.varWords[7],
2408 pmbox->un.varWords[8],
2409 pmbox->un.varWords[9],
2410 pmbox->un.varWords[10]);
dea3101e 2411
92d7f7b0 2412 if (pmb->mbox_cmpl)
dea3101e 2413 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
2414 } while (1);
2415 return 0;
2416}
dea3101e 2417
e59058c4 2418/**
3621a710 2419 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
2420 * @phba: Pointer to HBA context object.
2421 * @pring: Pointer to driver SLI ring object.
2422 * @tag: buffer tag.
2423 *
2424 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2425 * is set in the tag the buffer is posted for a particular exchange,
2426 * the function will return the buffer without replacing the buffer.
2427 * If the buffer is for unsolicited ELS or CT traffic, this function
2428 * returns the buffer and also posts another buffer to the firmware.
2429 **/
76bb24ef
JS
2430static struct lpfc_dmabuf *
2431lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
2432 struct lpfc_sli_ring *pring,
2433 uint32_t tag)
76bb24ef 2434{
9f1e1b50
JS
2435 struct hbq_dmabuf *hbq_entry;
2436
76bb24ef
JS
2437 if (tag & QUE_BUFTAG_BIT)
2438 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
2439 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2440 if (!hbq_entry)
2441 return NULL;
2442 return &hbq_entry->dbuf;
76bb24ef 2443}
57127f15 2444
3772a991
JS
2445/**
2446 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2447 * @phba: Pointer to HBA context object.
2448 * @pring: Pointer to driver SLI ring object.
2449 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2450 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2451 * @fch_type: the type for the first frame of the sequence.
2452 *
2453 * This function is called with no lock held. This function uses the r_ctl and
2454 * type of the received sequence to find the correct callback function to call
2455 * to process the sequence.
2456 **/
2457static int
2458lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2459 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2460 uint32_t fch_type)
2461{
2462 int i;
2463
2464 /* unSolicited Responses */
2465 if (pring->prt[0].profile) {
2466 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2467 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2468 saveq);
2469 return 1;
2470 }
2471 /* We must search, based on rctl / type
2472 for the right routine */
2473 for (i = 0; i < pring->num_mask; i++) {
2474 if ((pring->prt[i].rctl == fch_r_ctl) &&
2475 (pring->prt[i].type == fch_type)) {
2476 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2477 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2478 (phba, pring, saveq);
2479 return 1;
2480 }
2481 }
2482 return 0;
2483}
e59058c4
JS
2484
2485/**
3621a710 2486 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
2487 * @phba: Pointer to HBA context object.
2488 * @pring: Pointer to driver SLI ring object.
2489 * @saveq: Pointer to the unsolicited iocb.
2490 *
2491 * This function is called with no lock held by the ring event handler
2492 * when there is an unsolicited iocb posted to the response ring by the
2493 * firmware. This function gets the buffer associated with the iocbs
2494 * and calls the event handler for the ring. This function handles both
2495 * qring buffers and hbq buffers.
2496 * When the function returns 1 the caller can free the iocb object otherwise
2497 * upper layer functions will free the iocb objects.
2498 **/
dea3101e
JB
2499static int
2500lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2501 struct lpfc_iocbq *saveq)
2502{
2503 IOCB_t * irsp;
2504 WORD5 * w5p;
2505 uint32_t Rctl, Type;
76bb24ef 2506 struct lpfc_iocbq *iocbq;
3163f725 2507 struct lpfc_dmabuf *dmzbuf;
dea3101e 2508
dea3101e 2509 irsp = &(saveq->iocb);
57127f15
JS
2510
2511 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2512 if (pring->lpfc_sli_rcv_async_status)
2513 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2514 else
2515 lpfc_printf_log(phba,
2516 KERN_WARNING,
2517 LOG_SLI,
2518 "0316 Ring %d handler: unexpected "
2519 "ASYNC_STATUS iocb received evt_code "
2520 "0x%x\n",
2521 pring->ringno,
2522 irsp->un.asyncstat.evt_code);
2523 return 1;
2524 }
2525
3163f725
JS
2526 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2527 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2528 if (irsp->ulpBdeCount > 0) {
2529 dmzbuf = lpfc_sli_get_buff(phba, pring,
2530 irsp->un.ulpWord[3]);
2531 lpfc_in_buf_free(phba, dmzbuf);
2532 }
2533
2534 if (irsp->ulpBdeCount > 1) {
2535 dmzbuf = lpfc_sli_get_buff(phba, pring,
2536 irsp->unsli3.sli3Words[3]);
2537 lpfc_in_buf_free(phba, dmzbuf);
2538 }
2539
2540 if (irsp->ulpBdeCount > 2) {
2541 dmzbuf = lpfc_sli_get_buff(phba, pring,
2542 irsp->unsli3.sli3Words[7]);
2543 lpfc_in_buf_free(phba, dmzbuf);
2544 }
2545
2546 return 1;
2547 }
2548
92d7f7b0 2549 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2550 if (irsp->ulpBdeCount != 0) {
2551 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2552 irsp->un.ulpWord[3]);
2553 if (!saveq->context2)
2554 lpfc_printf_log(phba,
2555 KERN_ERR,
2556 LOG_SLI,
2557 "0341 Ring %d Cannot find buffer for "
2558 "an unsolicited iocb. tag 0x%x\n",
2559 pring->ringno,
2560 irsp->un.ulpWord[3]);
76bb24ef
JS
2561 }
2562 if (irsp->ulpBdeCount == 2) {
2563 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2564 irsp->unsli3.sli3Words[7]);
2565 if (!saveq->context3)
2566 lpfc_printf_log(phba,
2567 KERN_ERR,
2568 LOG_SLI,
2569 "0342 Ring %d Cannot find buffer for an"
2570 " unsolicited iocb. tag 0x%x\n",
2571 pring->ringno,
2572 irsp->unsli3.sli3Words[7]);
2573 }
2574 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2575 irsp = &(iocbq->iocb);
76bb24ef
JS
2576 if (irsp->ulpBdeCount != 0) {
2577 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2578 irsp->un.ulpWord[3]);
9c2face6 2579 if (!iocbq->context2)
76bb24ef
JS
2580 lpfc_printf_log(phba,
2581 KERN_ERR,
2582 LOG_SLI,
2583 "0343 Ring %d Cannot find "
2584 "buffer for an unsolicited iocb"
2585 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2586 irsp->un.ulpWord[3]);
76bb24ef
JS
2587 }
2588 if (irsp->ulpBdeCount == 2) {
2589 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2590 irsp->unsli3.sli3Words[7]);
9c2face6 2591 if (!iocbq->context3)
76bb24ef
JS
2592 lpfc_printf_log(phba,
2593 KERN_ERR,
2594 LOG_SLI,
2595 "0344 Ring %d Cannot find "
2596 "buffer for an unsolicited "
2597 "iocb. tag 0x%x\n",
2598 pring->ringno,
2599 irsp->unsli3.sli3Words[7]);
2600 }
2601 }
92d7f7b0 2602 }
9c2face6
JS
2603 if (irsp->ulpBdeCount != 0 &&
2604 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2605 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2606 int found = 0;
2607
2608 /* search continue save q for same XRI */
2609 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
7851fe2c
JS
2610 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2611 saveq->iocb.unsli3.rcvsli3.ox_id) {
9c2face6
JS
2612 list_add_tail(&saveq->list, &iocbq->list);
2613 found = 1;
2614 break;
2615 }
2616 }
2617 if (!found)
2618 list_add_tail(&saveq->clist,
2619 &pring->iocb_continue_saveq);
2620 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2621 list_del_init(&iocbq->clist);
2622 saveq = iocbq;
2623 irsp = &(saveq->iocb);
2624 } else
2625 return 0;
2626 }
2627 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2628 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2629 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2630 Rctl = FC_RCTL_ELS_REQ;
2631 Type = FC_TYPE_ELS;
9c2face6
JS
2632 } else {
2633 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2634 Rctl = w5p->hcsw.Rctl;
2635 Type = w5p->hcsw.Type;
2636
2637 /* Firmware Workaround */
2638 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2639 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2640 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2641 Rctl = FC_RCTL_ELS_REQ;
2642 Type = FC_TYPE_ELS;
9c2face6
JS
2643 w5p->hcsw.Rctl = Rctl;
2644 w5p->hcsw.Type = Type;
2645 }
2646 }
92d7f7b0 2647
3772a991 2648 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2649 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2650 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2651 "Type x%x received\n",
e8b62011 2652 pring->ringno, Rctl, Type);
3772a991 2653
92d7f7b0 2654 return 1;
dea3101e
JB
2655}
2656
e59058c4 2657/**
3621a710 2658 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2659 * @phba: Pointer to HBA context object.
2660 * @pring: Pointer to driver SLI ring object.
2661 * @prspiocb: Pointer to response iocb object.
2662 *
2663 * This function looks up the iocb_lookup table to get the command iocb
2664 * corresponding to the given response iocb using the iotag of the
2665 * response iocb. This function is called with the hbalock held.
2666 * This function returns the command iocb object if it finds the command
2667 * iocb else returns NULL.
2668 **/
dea3101e 2669static struct lpfc_iocbq *
2e0fef85
JS
2670lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2671 struct lpfc_sli_ring *pring,
2672 struct lpfc_iocbq *prspiocb)
dea3101e 2673{
dea3101e
JB
2674 struct lpfc_iocbq *cmd_iocb = NULL;
2675 uint16_t iotag;
1c2ba475 2676 lockdep_assert_held(&phba->hbalock);
dea3101e 2677
604a3e30
JB
2678 iotag = prspiocb->iocb.ulpIoTag;
2679
2680 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2681 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6 2682 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
89533e9b
JS
2683 /* remove from txcmpl queue list */
2684 list_del_init(&cmd_iocb->list);
4f2e66c6 2685 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
89533e9b 2686 return cmd_iocb;
2a9bf3d0 2687 }
dea3101e
JB
2688 }
2689
dea3101e 2690 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
89533e9b 2691 "0317 iotag x%x is out of "
604a3e30 2692 "range: max iotag x%x wd0 x%x\n",
e8b62011 2693 iotag, phba->sli.last_iotag,
604a3e30 2694 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e
JB
2695 return NULL;
2696}
2697
3772a991
JS
2698/**
2699 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2700 * @phba: Pointer to HBA context object.
2701 * @pring: Pointer to driver SLI ring object.
2702 * @iotag: IOCB tag.
2703 *
2704 * This function looks up the iocb_lookup table to get the command iocb
2705 * corresponding to the given iotag. This function is called with the
2706 * hbalock held.
2707 * This function returns the command iocb object if it finds the command
2708 * iocb else returns NULL.
2709 **/
2710static struct lpfc_iocbq *
2711lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2712 struct lpfc_sli_ring *pring, uint16_t iotag)
2713{
2714 struct lpfc_iocbq *cmd_iocb;
2715
1c2ba475 2716 lockdep_assert_held(&phba->hbalock);
3772a991
JS
2717 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2718 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6
JS
2719 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2720 /* remove from txcmpl queue list */
2721 list_del_init(&cmd_iocb->list);
2722 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4f2e66c6 2723 return cmd_iocb;
2a9bf3d0 2724 }
3772a991 2725 }
89533e9b 2726
3772a991 2727 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
89533e9b 2728 "0372 iotag x%x is out of range: max iotag (x%x)\n",
3772a991
JS
2729 iotag, phba->sli.last_iotag);
2730 return NULL;
2731}
2732
e59058c4 2733/**
3621a710 2734 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2735 * @phba: Pointer to HBA context object.
2736 * @pring: Pointer to driver SLI ring object.
2737 * @saveq: Pointer to the response iocb to be processed.
2738 *
2739 * This function is called by the ring event handler for non-fcp
2740 * rings when there is a new response iocb in the response ring.
2741 * The caller is not required to hold any locks. This function
2742 * gets the command iocb associated with the response iocb and
2743 * calls the completion handler for the command iocb. If there
2744 * is no completion handler, the function will free the resources
2745 * associated with command iocb. If the response iocb is for
2746 * an already aborted command iocb, the status of the completion
2747 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2748 * This function always returns 1.
2749 **/
dea3101e 2750static int
2e0fef85 2751lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e
JB
2752 struct lpfc_iocbq *saveq)
2753{
2e0fef85 2754 struct lpfc_iocbq *cmdiocbp;
dea3101e
JB
2755 int rc = 1;
2756 unsigned long iflag;
2757
2758 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2e0fef85 2759 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2760 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2e0fef85
JS
2761 spin_unlock_irqrestore(&phba->hbalock, iflag);
2762
dea3101e
JB
2763 if (cmdiocbp) {
2764 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2765 /*
2766 * If an ELS command failed send an event to mgmt
2767 * application.
2768 */
2769 if (saveq->iocb.ulpStatus &&
2770 (pring->ringno == LPFC_ELS_RING) &&
2771 (cmdiocbp->iocb.ulpCommand ==
2772 CMD_ELS_REQUEST64_CR))
2773 lpfc_send_els_failure_event(phba,
2774 cmdiocbp, saveq);
2775
dea3101e
JB
2776 /*
2777 * Post all ELS completions to the worker thread.
2778 * All other are passed to the completion callback.
2779 */
2780 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2781 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2782 (cmdiocbp->iocb_flag &
2783 LPFC_DRIVER_ABORTED)) {
2784 spin_lock_irqsave(&phba->hbalock,
2785 iflag);
07951076
JS
2786 cmdiocbp->iocb_flag &=
2787 ~LPFC_DRIVER_ABORTED;
341af102
JS
2788 spin_unlock_irqrestore(&phba->hbalock,
2789 iflag);
07951076
JS
2790 saveq->iocb.ulpStatus =
2791 IOSTAT_LOCAL_REJECT;
2792 saveq->iocb.un.ulpWord[4] =
2793 IOERR_SLI_ABORTED;
0ff10d46
JS
2794
2795 /* Firmware could still be in progress
2796 * of DMAing payload, so don't free data
2797 * buffer till after a hbeat.
2798 */
341af102
JS
2799 spin_lock_irqsave(&phba->hbalock,
2800 iflag);
0ff10d46 2801 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2802 spin_unlock_irqrestore(&phba->hbalock,
2803 iflag);
2804 }
0f65ff68
JS
2805 if (phba->sli_rev == LPFC_SLI_REV4) {
2806 if (saveq->iocb_flag &
2807 LPFC_EXCHANGE_BUSY) {
2808 /* Set cmdiocb flag for the
2809 * exchange busy so sgl (xri)
2810 * will not be released until
2811 * the abort xri is received
2812 * from hba.
2813 */
2814 spin_lock_irqsave(
2815 &phba->hbalock, iflag);
2816 cmdiocbp->iocb_flag |=
2817 LPFC_EXCHANGE_BUSY;
2818 spin_unlock_irqrestore(
2819 &phba->hbalock, iflag);
2820 }
2821 if (cmdiocbp->iocb_flag &
2822 LPFC_DRIVER_ABORTED) {
2823 /*
2824 * Clear LPFC_DRIVER_ABORTED
2825 * bit in case it was driver
2826 * initiated abort.
2827 */
2828 spin_lock_irqsave(
2829 &phba->hbalock, iflag);
2830 cmdiocbp->iocb_flag &=
2831 ~LPFC_DRIVER_ABORTED;
2832 spin_unlock_irqrestore(
2833 &phba->hbalock, iflag);
2834 cmdiocbp->iocb.ulpStatus =
2835 IOSTAT_LOCAL_REJECT;
2836 cmdiocbp->iocb.un.ulpWord[4] =
2837 IOERR_ABORT_REQUESTED;
2838 /*
2839 * For SLI4, irsiocb contains
2840 * NO_XRI in sli_xritag, it
2841 * shall not affect releasing
2842 * sgl (xri) process.
2843 */
2844 saveq->iocb.ulpStatus =
2845 IOSTAT_LOCAL_REJECT;
2846 saveq->iocb.un.ulpWord[4] =
2847 IOERR_SLI_ABORTED;
2848 spin_lock_irqsave(
2849 &phba->hbalock, iflag);
2850 saveq->iocb_flag |=
2851 LPFC_DELAY_MEM_FREE;
2852 spin_unlock_irqrestore(
2853 &phba->hbalock, iflag);
2854 }
07951076 2855 }
dea3101e 2856 }
2e0fef85 2857 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2858 } else
2859 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e
JB
2860 } else {
2861 /*
2862 * Unknown initiating command based on the response iotag.
2863 * This could be the case on the ELS ring because of
2864 * lpfc_els_abort().
2865 */
2866 if (pring->ringno != LPFC_ELS_RING) {
2867 /*
2868 * Ring <ringno> handler: unexpected completion IoTag
2869 * <IoTag>
2870 */
a257bf90 2871 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2872 "0322 Ring %d handler: "
2873 "unexpected completion IoTag x%x "
2874 "Data: x%x x%x x%x x%x\n",
2875 pring->ringno,
2876 saveq->iocb.ulpIoTag,
2877 saveq->iocb.ulpStatus,
2878 saveq->iocb.un.ulpWord[4],
2879 saveq->iocb.ulpCommand,
2880 saveq->iocb.ulpContext);
dea3101e
JB
2881 }
2882 }
68876920 2883
dea3101e
JB
2884 return rc;
2885}
2886
e59058c4 2887/**
3621a710 2888 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2889 * @phba: Pointer to HBA context object.
2890 * @pring: Pointer to driver SLI ring object.
2891 *
2892 * This function is called from the iocb ring event handlers when
2893 * put pointer is ahead of the get pointer for a ring. This function signal
2894 * an error attention condition to the worker thread and the worker
2895 * thread will transition the HBA to offline state.
2896 **/
2e0fef85
JS
2897static void
2898lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2899{
34b02dcd 2900 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2901 /*
025dfdaf 2902 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2903 * rsp ring <portRspMax>
2904 */
2905 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2906 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2907 "is bigger than rsp ring %d\n",
e8b62011 2908 pring->ringno, le32_to_cpu(pgp->rspPutInx),
7e56aa25 2909 pring->sli.sli3.numRiocb);
875fbdfe 2910
2e0fef85 2911 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2912
2913 /*
2914 * All error attention handlers are posted to
2915 * worker thread
2916 */
2917 phba->work_ha |= HA_ERATT;
2918 phba->work_hs = HS_FFER3;
92d7f7b0 2919
5e9d9b82 2920 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
2921
2922 return;
2923}
2924
9399627f 2925/**
3621a710 2926 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
2927 * @ptr: Pointer to address of HBA context object.
2928 *
2929 * This function is invoked by the Error Attention polling timer when the
2930 * timer times out. It will check the SLI Error Attention register for
2931 * possible attention events. If so, it will post an Error Attention event
2932 * and wake up worker thread to process it. Otherwise, it will set up the
2933 * Error Attention polling timer for the next poll.
2934 **/
2935void lpfc_poll_eratt(unsigned long ptr)
2936{
2937 struct lpfc_hba *phba;
eb016566 2938 uint32_t eratt = 0;
aa6fbb75 2939 uint64_t sli_intr, cnt;
9399627f
JS
2940
2941 phba = (struct lpfc_hba *)ptr;
2942
aa6fbb75
JS
2943 /* Here we will also keep track of interrupts per sec of the hba */
2944 sli_intr = phba->sli.slistat.sli_intr;
2945
2946 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2947 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2948 sli_intr);
2949 else
2950 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2951
65791f1f
JS
2952 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
2953 do_div(cnt, phba->eratt_poll_interval);
aa6fbb75
JS
2954 phba->sli.slistat.sli_ips = cnt;
2955
2956 phba->sli.slistat.sli_prev_intr = sli_intr;
2957
9399627f
JS
2958 /* Check chip HA register for error event */
2959 eratt = lpfc_sli_check_eratt(phba);
2960
2961 if (eratt)
2962 /* Tell the worker thread there is work to do */
2963 lpfc_worker_wake_up(phba);
2964 else
2965 /* Restart the timer for next eratt poll */
256ec0d0
JS
2966 mod_timer(&phba->eratt_poll,
2967 jiffies +
65791f1f 2968 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
9399627f
JS
2969 return;
2970}
2971
875fbdfe 2972
e59058c4 2973/**
3621a710 2974 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
2975 * @phba: Pointer to HBA context object.
2976 * @pring: Pointer to driver SLI ring object.
2977 * @mask: Host attention register mask for this ring.
2978 *
2979 * This function is called from the interrupt context when there is a ring
2980 * event for the fcp ring. The caller does not hold any lock.
2981 * The function processes each response iocb in the response ring until it
25985edc 2982 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
e59058c4
JS
2983 * LE bit set. The function will call the completion handler of the command iocb
2984 * if the response iocb indicates a completion for a command iocb or it is
2985 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2986 * function if this is an unsolicited iocb.
dea3101e 2987 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
2988 * to check it explicitly.
2989 */
2990int
2e0fef85
JS
2991lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2992 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2993{
34b02dcd 2994 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 2995 IOCB_t *irsp = NULL;
87f6eaff 2996 IOCB_t *entry = NULL;
dea3101e
JB
2997 struct lpfc_iocbq *cmdiocbq = NULL;
2998 struct lpfc_iocbq rspiocbq;
dea3101e
JB
2999 uint32_t status;
3000 uint32_t portRspPut, portRspMax;
3001 int rc = 1;
3002 lpfc_iocb_type type;
3003 unsigned long iflag;
3004 uint32_t rsp_cmpl = 0;
dea3101e 3005
2e0fef85 3006 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3007 pring->stats.iocb_event++;
3008
dea3101e
JB
3009 /*
3010 * The next available response entry should never exceed the maximum
3011 * entries. If it does, treat it as an adapter hardware error.
3012 */
7e56aa25 3013 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3014 portRspPut = le32_to_cpu(pgp->rspPutInx);
3015 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 3016 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 3017 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3018 return 1;
3019 }
45ed1190
JS
3020 if (phba->fcp_ring_in_use) {
3021 spin_unlock_irqrestore(&phba->hbalock, iflag);
3022 return 1;
3023 } else
3024 phba->fcp_ring_in_use = 1;
dea3101e
JB
3025
3026 rmb();
7e56aa25 3027 while (pring->sli.sli3.rspidx != portRspPut) {
87f6eaff
JSEC
3028 /*
3029 * Fetch an entry off the ring and copy it into a local data
3030 * structure. The copy involves a byte-swap since the
3031 * network byte order and pci byte orders are different.
3032 */
ed957684 3033 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 3034 phba->last_completion_time = jiffies;
875fbdfe 3035
7e56aa25
JS
3036 if (++pring->sli.sli3.rspidx >= portRspMax)
3037 pring->sli.sli3.rspidx = 0;
875fbdfe 3038
87f6eaff
JSEC
3039 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3040 (uint32_t *) &rspiocbq.iocb,
ed957684 3041 phba->iocb_rsp_size);
a4bc3379 3042 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
3043 irsp = &rspiocbq.iocb;
3044
dea3101e
JB
3045 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3046 pring->stats.iocb_rsp++;
3047 rsp_cmpl++;
3048
3049 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
3050 /*
3051 * If resource errors reported from HBA, reduce
3052 * queuedepths of the SCSI device.
3053 */
3054 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3055 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3056 IOERR_NO_RESOURCES)) {
92d7f7b0 3057 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3058 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
3059 spin_lock_irqsave(&phba->hbalock, iflag);
3060 }
3061
dea3101e
JB
3062 /* Rsp ring <ringno> error: IOCB */
3063 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 3064 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 3065 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 3066 pring->ringno,
92d7f7b0
JS
3067 irsp->un.ulpWord[0],
3068 irsp->un.ulpWord[1],
3069 irsp->un.ulpWord[2],
3070 irsp->un.ulpWord[3],
3071 irsp->un.ulpWord[4],
3072 irsp->un.ulpWord[5],
d7c255b2
JS
3073 *(uint32_t *)&irsp->un1,
3074 *((uint32_t *)&irsp->un1 + 1));
dea3101e
JB
3075 }
3076
3077 switch (type) {
3078 case LPFC_ABORT_IOCB:
3079 case LPFC_SOL_IOCB:
3080 /*
3081 * Idle exchange closed via ABTS from port. No iocb
3082 * resources need to be recovered.
3083 */
3084 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 3085 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3086 "0333 IOCB cmd 0x%x"
dca9479b 3087 " processed. Skipping"
92d7f7b0 3088 " completion\n",
dca9479b 3089 irsp->ulpCommand);
dea3101e
JB
3090 break;
3091 }
3092
604a3e30
JB
3093 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3094 &rspiocbq);
0f65ff68
JS
3095 if (unlikely(!cmdiocbq))
3096 break;
3097 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3098 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3099 if (cmdiocbq->iocb_cmpl) {
3100 spin_unlock_irqrestore(&phba->hbalock, iflag);
3101 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3102 &rspiocbq);
3103 spin_lock_irqsave(&phba->hbalock, iflag);
3104 }
dea3101e 3105 break;
a4bc3379 3106 case LPFC_UNSOL_IOCB:
2e0fef85 3107 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 3108 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 3109 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 3110 break;
dea3101e
JB
3111 default:
3112 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3113 char adaptermsg[LPFC_MAX_ADPTMSG];
3114 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3115 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3116 MAX_MSG_DATA);
898eb71c
JP
3117 dev_warn(&((phba->pcidev)->dev),
3118 "lpfc%d: %s\n",
dea3101e
JB
3119 phba->brd_no, adaptermsg);
3120 } else {
3121 /* Unknown IOCB command */
3122 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3123 "0334 Unknown IOCB command "
92d7f7b0 3124 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 3125 type, irsp->ulpCommand,
92d7f7b0
JS
3126 irsp->ulpStatus,
3127 irsp->ulpIoTag,
3128 irsp->ulpContext);
dea3101e
JB
3129 }
3130 break;
3131 }
3132
3133 /*
3134 * The response IOCB has been processed. Update the ring
3135 * pointer in SLIM. If the port response put pointer has not
3136 * been updated, sync the pgp->rspPutInx and fetch the new port
3137 * response put pointer.
3138 */
7e56aa25
JS
3139 writel(pring->sli.sli3.rspidx,
3140 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3141
7e56aa25 3142 if (pring->sli.sli3.rspidx == portRspPut)
dea3101e
JB
3143 portRspPut = le32_to_cpu(pgp->rspPutInx);
3144 }
3145
3146 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3147 pring->stats.iocb_rsp_full++;
3148 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3149 writel(status, phba->CAregaddr);
3150 readl(phba->CAregaddr);
3151 }
3152 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3153 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3154 pring->stats.iocb_cmd_empty++;
3155
3156 /* Force update of the local copy of cmdGetInx */
7e56aa25 3157 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3158 lpfc_sli_resume_iocb(phba, pring);
3159
3160 if ((pring->lpfc_sli_cmd_available))
3161 (pring->lpfc_sli_cmd_available) (phba, pring);
3162
3163 }
3164
45ed1190 3165 phba->fcp_ring_in_use = 0;
2e0fef85 3166 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3167 return rc;
3168}
3169
e59058c4 3170/**
3772a991
JS
3171 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3172 * @phba: Pointer to HBA context object.
3173 * @pring: Pointer to driver SLI ring object.
3174 * @rspiocbp: Pointer to driver response IOCB object.
3175 *
3176 * This function is called from the worker thread when there is a slow-path
3177 * response IOCB to process. This function chains all the response iocbs until
3178 * seeing the iocb with the LE bit set. The function will call
3179 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3180 * completion of a command iocb. The function will call the
3181 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3182 * The function frees the resources or calls the completion handler if this
3183 * iocb is an abort completion. The function returns NULL when the response
3184 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3185 * this function shall chain the iocb on to the iocb_continueq and return the
3186 * response iocb passed in.
3187 **/
3188static struct lpfc_iocbq *
3189lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3190 struct lpfc_iocbq *rspiocbp)
3191{
3192 struct lpfc_iocbq *saveq;
3193 struct lpfc_iocbq *cmdiocbp;
3194 struct lpfc_iocbq *next_iocb;
3195 IOCB_t *irsp = NULL;
3196 uint32_t free_saveq;
3197 uint8_t iocb_cmd_type;
3198 lpfc_iocb_type type;
3199 unsigned long iflag;
3200 int rc;
3201
3202 spin_lock_irqsave(&phba->hbalock, iflag);
3203 /* First add the response iocb to the countinueq list */
3204 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3205 pring->iocb_continueq_cnt++;
3206
70f23fd6 3207 /* Now, determine whether the list is completed for processing */
3772a991
JS
3208 irsp = &rspiocbp->iocb;
3209 if (irsp->ulpLe) {
3210 /*
3211 * By default, the driver expects to free all resources
3212 * associated with this iocb completion.
3213 */
3214 free_saveq = 1;
3215 saveq = list_get_first(&pring->iocb_continueq,
3216 struct lpfc_iocbq, list);
3217 irsp = &(saveq->iocb);
3218 list_del_init(&pring->iocb_continueq);
3219 pring->iocb_continueq_cnt = 0;
3220
3221 pring->stats.iocb_rsp++;
3222
3223 /*
3224 * If resource errors reported from HBA, reduce
3225 * queuedepths of the SCSI device.
3226 */
3227 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3228 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3229 IOERR_NO_RESOURCES)) {
3772a991
JS
3230 spin_unlock_irqrestore(&phba->hbalock, iflag);
3231 phba->lpfc_rampdown_queue_depth(phba);
3232 spin_lock_irqsave(&phba->hbalock, iflag);
3233 }
3234
3235 if (irsp->ulpStatus) {
3236 /* Rsp ring <ringno> error: IOCB */
3237 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3238 "0328 Rsp Ring %d error: "
3239 "IOCB Data: "
3240 "x%x x%x x%x x%x "
3241 "x%x x%x x%x x%x "
3242 "x%x x%x x%x x%x "
3243 "x%x x%x x%x x%x\n",
3244 pring->ringno,
3245 irsp->un.ulpWord[0],
3246 irsp->un.ulpWord[1],
3247 irsp->un.ulpWord[2],
3248 irsp->un.ulpWord[3],
3249 irsp->un.ulpWord[4],
3250 irsp->un.ulpWord[5],
3251 *(((uint32_t *) irsp) + 6),
3252 *(((uint32_t *) irsp) + 7),
3253 *(((uint32_t *) irsp) + 8),
3254 *(((uint32_t *) irsp) + 9),
3255 *(((uint32_t *) irsp) + 10),
3256 *(((uint32_t *) irsp) + 11),
3257 *(((uint32_t *) irsp) + 12),
3258 *(((uint32_t *) irsp) + 13),
3259 *(((uint32_t *) irsp) + 14),
3260 *(((uint32_t *) irsp) + 15));
3261 }
3262
3263 /*
3264 * Fetch the IOCB command type and call the correct completion
3265 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3266 * get freed back to the lpfc_iocb_list by the discovery
3267 * kernel thread.
3268 */
3269 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3270 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3271 switch (type) {
3272 case LPFC_SOL_IOCB:
3273 spin_unlock_irqrestore(&phba->hbalock, iflag);
3274 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3275 spin_lock_irqsave(&phba->hbalock, iflag);
3276 break;
3277
3278 case LPFC_UNSOL_IOCB:
3279 spin_unlock_irqrestore(&phba->hbalock, iflag);
3280 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3281 spin_lock_irqsave(&phba->hbalock, iflag);
3282 if (!rc)
3283 free_saveq = 0;
3284 break;
3285
3286 case LPFC_ABORT_IOCB:
3287 cmdiocbp = NULL;
3288 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3289 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3290 saveq);
3291 if (cmdiocbp) {
3292 /* Call the specified completion routine */
3293 if (cmdiocbp->iocb_cmpl) {
3294 spin_unlock_irqrestore(&phba->hbalock,
3295 iflag);
3296 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3297 saveq);
3298 spin_lock_irqsave(&phba->hbalock,
3299 iflag);
3300 } else
3301 __lpfc_sli_release_iocbq(phba,
3302 cmdiocbp);
3303 }
3304 break;
3305
3306 case LPFC_UNKNOWN_IOCB:
3307 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3308 char adaptermsg[LPFC_MAX_ADPTMSG];
3309 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3310 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3311 MAX_MSG_DATA);
3312 dev_warn(&((phba->pcidev)->dev),
3313 "lpfc%d: %s\n",
3314 phba->brd_no, adaptermsg);
3315 } else {
3316 /* Unknown IOCB command */
3317 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3318 "0335 Unknown IOCB "
3319 "command Data: x%x "
3320 "x%x x%x x%x\n",
3321 irsp->ulpCommand,
3322 irsp->ulpStatus,
3323 irsp->ulpIoTag,
3324 irsp->ulpContext);
3325 }
3326 break;
3327 }
3328
3329 if (free_saveq) {
3330 list_for_each_entry_safe(rspiocbp, next_iocb,
3331 &saveq->list, list) {
61f35bff 3332 list_del_init(&rspiocbp->list);
3772a991
JS
3333 __lpfc_sli_release_iocbq(phba, rspiocbp);
3334 }
3335 __lpfc_sli_release_iocbq(phba, saveq);
3336 }
3337 rspiocbp = NULL;
3338 }
3339 spin_unlock_irqrestore(&phba->hbalock, iflag);
3340 return rspiocbp;
3341}
3342
3343/**
3344 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
3345 * @phba: Pointer to HBA context object.
3346 * @pring: Pointer to driver SLI ring object.
3347 * @mask: Host attention register mask for this ring.
3348 *
3772a991
JS
3349 * This routine wraps the actual slow_ring event process routine from the
3350 * API jump table function pointer from the lpfc_hba struct.
e59058c4 3351 **/
3772a991 3352void
2e0fef85
JS
3353lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3354 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
3355{
3356 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3357}
3358
3359/**
3360 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3361 * @phba: Pointer to HBA context object.
3362 * @pring: Pointer to driver SLI ring object.
3363 * @mask: Host attention register mask for this ring.
3364 *
3365 * This function is called from the worker thread when there is a ring event
3366 * for non-fcp rings. The caller does not hold any lock. The function will
3367 * remove each response iocb in the response ring and calls the handle
3368 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3369 **/
3370static void
3371lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3372 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3373{
34b02dcd 3374 struct lpfc_pgp *pgp;
dea3101e
JB
3375 IOCB_t *entry;
3376 IOCB_t *irsp = NULL;
3377 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 3378 uint32_t portRspPut, portRspMax;
dea3101e 3379 unsigned long iflag;
3772a991 3380 uint32_t status;
dea3101e 3381
34b02dcd 3382 pgp = &phba->port_gp[pring->ringno];
2e0fef85 3383 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3384 pring->stats.iocb_event++;
3385
dea3101e
JB
3386 /*
3387 * The next available response entry should never exceed the maximum
3388 * entries. If it does, treat it as an adapter hardware error.
3389 */
7e56aa25 3390 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3391 portRspPut = le32_to_cpu(pgp->rspPutInx);
3392 if (portRspPut >= portRspMax) {
3393 /*
025dfdaf 3394 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e
JB
3395 * rsp ring <portRspMax>
3396 */
ed957684 3397 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3398 "0303 Ring %d handler: portRspPut %d "
025dfdaf 3399 "is bigger than rsp ring %d\n",
e8b62011 3400 pring->ringno, portRspPut, portRspMax);
dea3101e 3401
2e0fef85
JS
3402 phba->link_state = LPFC_HBA_ERROR;
3403 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3404
3405 phba->work_hs = HS_FFER3;
3406 lpfc_handle_eratt(phba);
3407
3772a991 3408 return;
dea3101e
JB
3409 }
3410
3411 rmb();
7e56aa25 3412 while (pring->sli.sli3.rspidx != portRspPut) {
dea3101e
JB
3413 /*
3414 * Build a completion list and call the appropriate handler.
3415 * The process is to get the next available response iocb, get
3416 * a free iocb from the list, copy the response data into the
3417 * free iocb, insert to the continuation list, and update the
3418 * next response index to slim. This process makes response
3419 * iocb's in the ring available to DMA as fast as possible but
3420 * pays a penalty for a copy operation. Since the iocb is
3421 * only 32 bytes, this penalty is considered small relative to
3422 * the PCI reads for register values and a slim write. When
3423 * the ulpLe field is set, the entire Command has been
3424 * received.
3425 */
ed957684
JS
3426 entry = lpfc_resp_iocb(phba, pring);
3427
858c9f6c 3428 phba->last_completion_time = jiffies;
2e0fef85 3429 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
3430 if (rspiocbp == NULL) {
3431 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 3432 "completion.\n", __func__);
dea3101e
JB
3433 break;
3434 }
3435
ed957684
JS
3436 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3437 phba->iocb_rsp_size);
dea3101e
JB
3438 irsp = &rspiocbp->iocb;
3439
7e56aa25
JS
3440 if (++pring->sli.sli3.rspidx >= portRspMax)
3441 pring->sli.sli3.rspidx = 0;
dea3101e 3442
a58cbd52
JS
3443 if (pring->ringno == LPFC_ELS_RING) {
3444 lpfc_debugfs_slow_ring_trc(phba,
3445 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3446 *(((uint32_t *) irsp) + 4),
3447 *(((uint32_t *) irsp) + 6),
3448 *(((uint32_t *) irsp) + 7));
3449 }
3450
7e56aa25
JS
3451 writel(pring->sli.sli3.rspidx,
3452 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3453
3772a991
JS
3454 spin_unlock_irqrestore(&phba->hbalock, iflag);
3455 /* Handle the response IOCB */
3456 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3457 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3458
3459 /*
3460 * If the port response put pointer has not been updated, sync
3461 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3462 * response put pointer.
3463 */
7e56aa25 3464 if (pring->sli.sli3.rspidx == portRspPut) {
dea3101e
JB
3465 portRspPut = le32_to_cpu(pgp->rspPutInx);
3466 }
7e56aa25 3467 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea3101e 3468
92d7f7b0 3469 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e
JB
3470 /* At least one response entry has been freed */
3471 pring->stats.iocb_rsp_full++;
3472 /* SET RxRE_RSP in Chip Att register */
3473 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3474 writel(status, phba->CAregaddr);
3475 readl(phba->CAregaddr); /* flush */
3476 }
3477 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3478 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3479 pring->stats.iocb_cmd_empty++;
3480
3481 /* Force update of the local copy of cmdGetInx */
7e56aa25 3482 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3483 lpfc_sli_resume_iocb(phba, pring);
3484
3485 if ((pring->lpfc_sli_cmd_available))
3486 (pring->lpfc_sli_cmd_available) (phba, pring);
3487
3488 }
3489
2e0fef85 3490 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3491 return;
dea3101e
JB
3492}
3493
4f774513
JS
3494/**
3495 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3496 * @phba: Pointer to HBA context object.
3497 * @pring: Pointer to driver SLI ring object.
3498 * @mask: Host attention register mask for this ring.
3499 *
3500 * This function is called from the worker thread when there is a pending
3501 * ELS response iocb on the driver internal slow-path response iocb worker
3502 * queue. The caller does not hold any lock. The function will remove each
3503 * response iocb from the response worker queue and calls the handle
3504 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3505 **/
3506static void
3507lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3508 struct lpfc_sli_ring *pring, uint32_t mask)
3509{
3510 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3511 struct hbq_dmabuf *dmabuf;
3512 struct lpfc_cq_event *cq_event;
4f774513
JS
3513 unsigned long iflag;
3514
45ed1190
JS
3515 spin_lock_irqsave(&phba->hbalock, iflag);
3516 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3517 spin_unlock_irqrestore(&phba->hbalock, iflag);
3518 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
3519 /* Get the response iocb from the head of work queue */
3520 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3521 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3522 cq_event, struct lpfc_cq_event, list);
4f774513 3523 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3524
3525 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3526 case CQE_CODE_COMPL_WQE:
3527 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3528 cq_event);
45ed1190
JS
3529 /* Translate ELS WCQE to response IOCBQ */
3530 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3531 irspiocbq);
3532 if (irspiocbq)
3533 lpfc_sli_sp_handle_rspiocb(phba, pring,
3534 irspiocbq);
4d9ab994
JS
3535 break;
3536 case CQE_CODE_RECEIVE:
7851fe2c 3537 case CQE_CODE_RECEIVE_V1:
4d9ab994
JS
3538 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3539 cq_event);
3540 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3541 break;
3542 default:
3543 break;
3544 }
4f774513
JS
3545 }
3546}
3547
e59058c4 3548/**
3621a710 3549 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3550 * @phba: Pointer to HBA context object.
3551 * @pring: Pointer to driver SLI ring object.
3552 *
3553 * This function aborts all iocbs in the given ring and frees all the iocb
3554 * objects in txq. This function issues an abort iocb for all the iocb commands
3555 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3556 * the return of this function. The caller is not required to hold any locks.
3557 **/
2e0fef85 3558void
dea3101e
JB
3559lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3560{
2534ba75 3561 LIST_HEAD(completions);
dea3101e 3562 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3563
92d7f7b0
JS
3564 if (pring->ringno == LPFC_ELS_RING) {
3565 lpfc_fabric_abort_hba(phba);
3566 }
3567
dea3101e
JB
3568 /* Error everything on txq and txcmplq
3569 * First do the txq.
3570 */
db55fba8
JS
3571 if (phba->sli_rev >= LPFC_SLI_REV4) {
3572 spin_lock_irq(&pring->ring_lock);
3573 list_splice_init(&pring->txq, &completions);
3574 pring->txq_cnt = 0;
3575 spin_unlock_irq(&pring->ring_lock);
dea3101e 3576
db55fba8
JS
3577 spin_lock_irq(&phba->hbalock);
3578 /* Next issue ABTS for everything on the txcmplq */
3579 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3580 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3581 spin_unlock_irq(&phba->hbalock);
3582 } else {
3583 spin_lock_irq(&phba->hbalock);
3584 list_splice_init(&pring->txq, &completions);
3585 pring->txq_cnt = 0;
dea3101e 3586
db55fba8
JS
3587 /* Next issue ABTS for everything on the txcmplq */
3588 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3589 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3590 spin_unlock_irq(&phba->hbalock);
3591 }
dea3101e 3592
a257bf90
JS
3593 /* Cancel all the IOCBs from the completions list */
3594 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3595 IOERR_SLI_ABORTED);
dea3101e
JB
3596}
3597
db55fba8
JS
3598/**
3599 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3600 * @phba: Pointer to HBA context object.
3601 * @pring: Pointer to driver SLI ring object.
3602 *
3603 * This function aborts all iocbs in FCP rings and frees all the iocb
3604 * objects in txq. This function issues an abort iocb for all the iocb commands
3605 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3606 * the return of this function. The caller is not required to hold any locks.
3607 **/
3608void
3609lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3610{
3611 struct lpfc_sli *psli = &phba->sli;
3612 struct lpfc_sli_ring *pring;
3613 uint32_t i;
3614
3615 /* Look on all the FCP Rings for the iotag */
3616 if (phba->sli_rev >= LPFC_SLI_REV4) {
3617 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3618 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3619 lpfc_sli_abort_iocb_ring(phba, pring);
3620 }
3621 } else {
3622 pring = &psli->ring[psli->fcp_ring];
3623 lpfc_sli_abort_iocb_ring(phba, pring);
3624 }
3625}
3626
3627
a8e497d5 3628/**
3621a710 3629 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3630 * @phba: Pointer to HBA context object.
3631 *
3632 * This function flushes all iocbs in the fcp ring and frees all the iocb
3633 * objects in txq and txcmplq. This function will not issue abort iocbs
3634 * for all the iocb commands in txcmplq, they will just be returned with
3635 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3636 * slot has been permanently disabled.
3637 **/
3638void
3639lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3640{
3641 LIST_HEAD(txq);
3642 LIST_HEAD(txcmplq);
a8e497d5
JS
3643 struct lpfc_sli *psli = &phba->sli;
3644 struct lpfc_sli_ring *pring;
db55fba8 3645 uint32_t i;
a8e497d5
JS
3646
3647 spin_lock_irq(&phba->hbalock);
4f2e66c6
JS
3648 /* Indicate the I/O queues are flushed */
3649 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
a8e497d5
JS
3650 spin_unlock_irq(&phba->hbalock);
3651
db55fba8
JS
3652 /* Look on all the FCP Rings for the iotag */
3653 if (phba->sli_rev >= LPFC_SLI_REV4) {
3654 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3655 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3656
3657 spin_lock_irq(&pring->ring_lock);
3658 /* Retrieve everything on txq */
3659 list_splice_init(&pring->txq, &txq);
3660 /* Retrieve everything on the txcmplq */
3661 list_splice_init(&pring->txcmplq, &txcmplq);
3662 pring->txq_cnt = 0;
3663 pring->txcmplq_cnt = 0;
3664 spin_unlock_irq(&pring->ring_lock);
3665
3666 /* Flush the txq */
3667 lpfc_sli_cancel_iocbs(phba, &txq,
3668 IOSTAT_LOCAL_REJECT,
3669 IOERR_SLI_DOWN);
3670 /* Flush the txcmpq */
3671 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3672 IOSTAT_LOCAL_REJECT,
3673 IOERR_SLI_DOWN);
3674 }
3675 } else {
3676 pring = &psli->ring[psli->fcp_ring];
a8e497d5 3677
db55fba8
JS
3678 spin_lock_irq(&phba->hbalock);
3679 /* Retrieve everything on txq */
3680 list_splice_init(&pring->txq, &txq);
3681 /* Retrieve everything on the txcmplq */
3682 list_splice_init(&pring->txcmplq, &txcmplq);
3683 pring->txq_cnt = 0;
3684 pring->txcmplq_cnt = 0;
3685 spin_unlock_irq(&phba->hbalock);
3686
3687 /* Flush the txq */
3688 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3689 IOERR_SLI_DOWN);
3690 /* Flush the txcmpq */
3691 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3692 IOERR_SLI_DOWN);
3693 }
a8e497d5
JS
3694}
3695
e59058c4 3696/**
3772a991 3697 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3698 * @phba: Pointer to HBA context object.
3699 * @mask: Bit mask to be checked.
3700 *
3701 * This function reads the host status register and compares
3702 * with the provided bit mask to check if HBA completed
3703 * the restart. This function will wait in a loop for the
3704 * HBA to complete restart. If the HBA does not restart within
3705 * 15 iterations, the function will reset the HBA again. The
3706 * function returns 1 when HBA fail to restart otherwise returns
3707 * zero.
3708 **/
3772a991
JS
3709static int
3710lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3711{
41415862
JW
3712 uint32_t status;
3713 int i = 0;
3714 int retval = 0;
dea3101e 3715
41415862 3716 /* Read the HBA Host Status Register */
9940b97b
JS
3717 if (lpfc_readl(phba->HSregaddr, &status))
3718 return 1;
dea3101e 3719
41415862
JW
3720 /*
3721 * Check status register every 100ms for 5 retries, then every
3722 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3723 * every 2.5 sec for 4.
3724 * Break our of the loop if errors occurred during init.
3725 */
3726 while (((status & mask) != mask) &&
3727 !(status & HS_FFERM) &&
3728 i++ < 20) {
dea3101e 3729
41415862
JW
3730 if (i <= 5)
3731 msleep(10);
3732 else if (i <= 10)
3733 msleep(500);
3734 else
3735 msleep(2500);
dea3101e 3736
41415862 3737 if (i == 15) {
2e0fef85 3738 /* Do post */
92d7f7b0 3739 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3740 lpfc_sli_brdrestart(phba);
3741 }
3742 /* Read the HBA Host Status Register */
9940b97b
JS
3743 if (lpfc_readl(phba->HSregaddr, &status)) {
3744 retval = 1;
3745 break;
3746 }
41415862 3747 }
dea3101e 3748
41415862
JW
3749 /* Check to see if any errors occurred during init */
3750 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
3751 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3752 "2751 Adapter failed to restart, "
3753 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3754 status,
3755 readl(phba->MBslimaddr + 0xa8),
3756 readl(phba->MBslimaddr + 0xac));
2e0fef85 3757 phba->link_state = LPFC_HBA_ERROR;
41415862 3758 retval = 1;
dea3101e 3759 }
dea3101e 3760
41415862
JW
3761 return retval;
3762}
dea3101e 3763
da0436e9
JS
3764/**
3765 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3766 * @phba: Pointer to HBA context object.
3767 * @mask: Bit mask to be checked.
3768 *
3769 * This function checks the host status register to check if HBA is
3770 * ready. This function will wait in a loop for the HBA to be ready
3771 * If the HBA is not ready , the function will will reset the HBA PCI
3772 * function again. The function returns 1 when HBA fail to be ready
3773 * otherwise returns zero.
3774 **/
3775static int
3776lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3777{
3778 uint32_t status;
3779 int retval = 0;
3780
3781 /* Read the HBA Host Status Register */
3782 status = lpfc_sli4_post_status_check(phba);
3783
3784 if (status) {
3785 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3786 lpfc_sli_brdrestart(phba);
3787 status = lpfc_sli4_post_status_check(phba);
3788 }
3789
3790 /* Check to see if any errors occurred during init */
3791 if (status) {
3792 phba->link_state = LPFC_HBA_ERROR;
3793 retval = 1;
3794 } else
3795 phba->sli4_hba.intr_enable = 0;
3796
3797 return retval;
3798}
3799
3800/**
3801 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3802 * @phba: Pointer to HBA context object.
3803 * @mask: Bit mask to be checked.
3804 *
3805 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3806 * from the API jump table function pointer from the lpfc_hba struct.
3807 **/
3808int
3809lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3810{
3811 return phba->lpfc_sli_brdready(phba, mask);
3812}
3813
9290831f
JS
3814#define BARRIER_TEST_PATTERN (0xdeadbeef)
3815
e59058c4 3816/**
3621a710 3817 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3818 * @phba: Pointer to HBA context object.
3819 *
1b51197d
JS
3820 * This function is called before resetting an HBA. This function is called
3821 * with hbalock held and requests HBA to quiesce DMAs before a reset.
e59058c4 3822 **/
2e0fef85 3823void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 3824{
65a29c16
JS
3825 uint32_t __iomem *resp_buf;
3826 uint32_t __iomem *mbox_buf;
9290831f 3827 volatile uint32_t mbox;
9940b97b 3828 uint32_t hc_copy, ha_copy, resp_data;
9290831f
JS
3829 int i;
3830 uint8_t hdrtype;
3831
1c2ba475
JT
3832 lockdep_assert_held(&phba->hbalock);
3833
9290831f
JS
3834 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3835 if (hdrtype != 0x80 ||
3836 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3837 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3838 return;
3839
3840 /*
3841 * Tell the other part of the chip to suspend temporarily all
3842 * its DMA activity.
3843 */
65a29c16 3844 resp_buf = phba->MBslimaddr;
9290831f
JS
3845
3846 /* Disable the error attention */
9940b97b
JS
3847 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3848 return;
9290831f
JS
3849 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3850 readl(phba->HCregaddr); /* flush */
2e0fef85 3851 phba->link_flag |= LS_IGNORE_ERATT;
9290831f 3852
9940b97b
JS
3853 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3854 return;
3855 if (ha_copy & HA_ERATT) {
9290831f
JS
3856 /* Clear Chip error bit */
3857 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3858 phba->pport->stopped = 1;
9290831f
JS
3859 }
3860
3861 mbox = 0;
3862 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3863 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3864
3865 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 3866 mbox_buf = phba->MBslimaddr;
9290831f
JS
3867 writel(mbox, mbox_buf);
3868
9940b97b
JS
3869 for (i = 0; i < 50; i++) {
3870 if (lpfc_readl((resp_buf + 1), &resp_data))
3871 return;
3872 if (resp_data != ~(BARRIER_TEST_PATTERN))
3873 mdelay(1);
3874 else
3875 break;
3876 }
3877 resp_data = 0;
3878 if (lpfc_readl((resp_buf + 1), &resp_data))
3879 return;
3880 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 3881 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 3882 phba->pport->stopped)
9290831f
JS
3883 goto restore_hc;
3884 else
3885 goto clear_errat;
3886 }
3887
3888 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
9940b97b
JS
3889 resp_data = 0;
3890 for (i = 0; i < 500; i++) {
3891 if (lpfc_readl(resp_buf, &resp_data))
3892 return;
3893 if (resp_data != mbox)
3894 mdelay(1);
3895 else
3896 break;
3897 }
9290831f
JS
3898
3899clear_errat:
3900
9940b97b
JS
3901 while (++i < 500) {
3902 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3903 return;
3904 if (!(ha_copy & HA_ERATT))
3905 mdelay(1);
3906 else
3907 break;
3908 }
9290831f
JS
3909
3910 if (readl(phba->HAregaddr) & HA_ERATT) {
3911 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3912 phba->pport->stopped = 1;
9290831f
JS
3913 }
3914
3915restore_hc:
2e0fef85 3916 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
3917 writel(hc_copy, phba->HCregaddr);
3918 readl(phba->HCregaddr); /* flush */
3919}
3920
e59058c4 3921/**
3621a710 3922 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
3923 * @phba: Pointer to HBA context object.
3924 *
3925 * This function issues a kill_board mailbox command and waits for
3926 * the error attention interrupt. This function is called for stopping
3927 * the firmware processing. The caller is not required to hold any
3928 * locks. This function calls lpfc_hba_down_post function to free
3929 * any pending commands after the kill. The function will return 1 when it
3930 * fails to kill the board else will return 0.
3931 **/
41415862 3932int
2e0fef85 3933lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
3934{
3935 struct lpfc_sli *psli;
3936 LPFC_MBOXQ_t *pmb;
3937 uint32_t status;
3938 uint32_t ha_copy;
3939 int retval;
3940 int i = 0;
dea3101e 3941
41415862 3942 psli = &phba->sli;
dea3101e 3943
41415862 3944 /* Kill HBA */
ed957684 3945 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
3946 "0329 Kill HBA Data: x%x x%x\n",
3947 phba->pport->port_state, psli->sli_flag);
41415862 3948
98c9ea5c
JS
3949 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3950 if (!pmb)
41415862 3951 return 1;
41415862
JW
3952
3953 /* Disable the error attention */
2e0fef85 3954 spin_lock_irq(&phba->hbalock);
9940b97b
JS
3955 if (lpfc_readl(phba->HCregaddr, &status)) {
3956 spin_unlock_irq(&phba->hbalock);
3957 mempool_free(pmb, phba->mbox_mem_pool);
3958 return 1;
3959 }
41415862
JW
3960 status &= ~HC_ERINT_ENA;
3961 writel(status, phba->HCregaddr);
3962 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
3963 phba->link_flag |= LS_IGNORE_ERATT;
3964 spin_unlock_irq(&phba->hbalock);
41415862
JW
3965
3966 lpfc_kill_board(phba, pmb);
3967 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3968 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3969
3970 if (retval != MBX_SUCCESS) {
3971 if (retval != MBX_BUSY)
3972 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
3973 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3974 "2752 KILL_BOARD command failed retval %d\n",
3975 retval);
2e0fef85
JS
3976 spin_lock_irq(&phba->hbalock);
3977 phba->link_flag &= ~LS_IGNORE_ERATT;
3978 spin_unlock_irq(&phba->hbalock);
41415862
JW
3979 return 1;
3980 }
3981
f4b4c68f
JS
3982 spin_lock_irq(&phba->hbalock);
3983 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3984 spin_unlock_irq(&phba->hbalock);
9290831f 3985
41415862
JW
3986 mempool_free(pmb, phba->mbox_mem_pool);
3987
3988 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3989 * attention every 100ms for 3 seconds. If we don't get ERATT after
3990 * 3 seconds we still set HBA_ERROR state because the status of the
3991 * board is now undefined.
3992 */
9940b97b
JS
3993 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3994 return 1;
41415862
JW
3995 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3996 mdelay(100);
9940b97b
JS
3997 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3998 return 1;
41415862
JW
3999 }
4000
4001 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
4002 if (ha_copy & HA_ERATT) {
4003 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4004 phba->pport->stopped = 1;
9290831f 4005 }
2e0fef85 4006 spin_lock_irq(&phba->hbalock);
41415862 4007 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 4008 psli->mbox_active = NULL;
2e0fef85
JS
4009 phba->link_flag &= ~LS_IGNORE_ERATT;
4010 spin_unlock_irq(&phba->hbalock);
41415862 4011
41415862 4012 lpfc_hba_down_post(phba);
2e0fef85 4013 phba->link_state = LPFC_HBA_ERROR;
41415862 4014
2e0fef85 4015 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e
JB
4016}
4017
e59058c4 4018/**
3772a991 4019 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
4020 * @phba: Pointer to HBA context object.
4021 *
4022 * This function resets the HBA by writing HC_INITFF to the control
4023 * register. After the HBA resets, this function resets all the iocb ring
4024 * indices. This function disables PCI layer parity checking during
4025 * the reset.
4026 * This function returns 0 always.
4027 * The caller is not required to hold any locks.
4028 **/
41415862 4029int
2e0fef85 4030lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 4031{
41415862 4032 struct lpfc_sli *psli;
dea3101e 4033 struct lpfc_sli_ring *pring;
41415862 4034 uint16_t cfg_value;
dea3101e 4035 int i;
dea3101e 4036
41415862 4037 psli = &phba->sli;
dea3101e 4038
41415862
JW
4039 /* Reset HBA */
4040 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4041 "0325 Reset HBA Data: x%x x%x\n",
2e0fef85 4042 phba->pport->port_state, psli->sli_flag);
dea3101e
JB
4043
4044 /* perform board reset */
4045 phba->fc_eventTag = 0;
4d9ab994 4046 phba->link_events = 0;
2e0fef85
JS
4047 phba->pport->fc_myDID = 0;
4048 phba->pport->fc_prevDID = 0;
dea3101e 4049
41415862
JW
4050 /* Turn off parity checking and serr during the physical reset */
4051 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4052 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4053 (cfg_value &
4054 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4055
3772a991
JS
4056 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4057
41415862
JW
4058 /* Now toggle INITFF bit in the Host Control Register */
4059 writel(HC_INITFF, phba->HCregaddr);
4060 mdelay(1);
4061 readl(phba->HCregaddr); /* flush */
4062 writel(0, phba->HCregaddr);
4063 readl(phba->HCregaddr); /* flush */
4064
4065 /* Restore PCI cmd register */
4066 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e
JB
4067
4068 /* Initialize relevant SLI info */
41415862
JW
4069 for (i = 0; i < psli->num_rings; i++) {
4070 pring = &psli->ring[i];
dea3101e 4071 pring->flag = 0;
7e56aa25
JS
4072 pring->sli.sli3.rspidx = 0;
4073 pring->sli.sli3.next_cmdidx = 0;
4074 pring->sli.sli3.local_getidx = 0;
4075 pring->sli.sli3.cmdidx = 0;
dea3101e
JB
4076 pring->missbufcnt = 0;
4077 }
dea3101e 4078
2e0fef85 4079 phba->link_state = LPFC_WARM_START;
41415862
JW
4080 return 0;
4081}
4082
e59058c4 4083/**
da0436e9
JS
4084 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4085 * @phba: Pointer to HBA context object.
4086 *
4087 * This function resets a SLI4 HBA. This function disables PCI layer parity
4088 * checking during resets the device. The caller is not required to hold
4089 * any locks.
4090 *
4091 * This function returns 0 always.
4092 **/
4093int
4094lpfc_sli4_brdreset(struct lpfc_hba *phba)
4095{
4096 struct lpfc_sli *psli = &phba->sli;
4097 uint16_t cfg_value;
0293635e 4098 int rc = 0;
da0436e9
JS
4099
4100 /* Reset HBA */
4101 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
0293635e
JS
4102 "0295 Reset HBA Data: x%x x%x x%x\n",
4103 phba->pport->port_state, psli->sli_flag,
4104 phba->hba_flag);
da0436e9
JS
4105
4106 /* perform board reset */
4107 phba->fc_eventTag = 0;
4d9ab994 4108 phba->link_events = 0;
da0436e9
JS
4109 phba->pport->fc_myDID = 0;
4110 phba->pport->fc_prevDID = 0;
4111
da0436e9
JS
4112 spin_lock_irq(&phba->hbalock);
4113 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4114 phba->fcf.fcf_flag = 0;
da0436e9
JS
4115 spin_unlock_irq(&phba->hbalock);
4116
0293635e
JS
4117 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4118 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4119 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4120 return rc;
4121 }
4122
da0436e9
JS
4123 /* Now physically reset the device */
4124 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4125 "0389 Performing PCI function reset!\n");
be858b65
JS
4126
4127 /* Turn off parity checking and serr during the physical reset */
4128 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4129 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4130 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4131
88318816 4132 /* Perform FCoE PCI function reset before freeing queue memory */
27b01b82 4133 rc = lpfc_pci_function_reset(phba);
88318816 4134 lpfc_sli4_queue_destroy(phba);
da0436e9 4135
be858b65
JS
4136 /* Restore PCI cmd register */
4137 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4138
27b01b82 4139 return rc;
da0436e9
JS
4140}
4141
4142/**
4143 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
4144 * @phba: Pointer to HBA context object.
4145 *
4146 * This function is called in the SLI initialization code path to
4147 * restart the HBA. The caller is not required to hold any lock.
4148 * This function writes MBX_RESTART mailbox command to the SLIM and
4149 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4150 * function to free any pending commands. The function enables
4151 * POST only during the first initialization. The function returns zero.
4152 * The function does not guarantee completion of MBX_RESTART mailbox
4153 * command before the return of this function.
4154 **/
da0436e9
JS
4155static int
4156lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
4157{
4158 MAILBOX_t *mb;
4159 struct lpfc_sli *psli;
41415862
JW
4160 volatile uint32_t word0;
4161 void __iomem *to_slim;
0d878419 4162 uint32_t hba_aer_enabled;
41415862 4163
2e0fef85 4164 spin_lock_irq(&phba->hbalock);
41415862 4165
0d878419
JS
4166 /* Take PCIe device Advanced Error Reporting (AER) state */
4167 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4168
41415862
JW
4169 psli = &phba->sli;
4170
4171 /* Restart HBA */
4172 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4173 "0337 Restart HBA Data: x%x x%x\n",
2e0fef85 4174 phba->pport->port_state, psli->sli_flag);
41415862
JW
4175
4176 word0 = 0;
4177 mb = (MAILBOX_t *) &word0;
4178 mb->mbxCommand = MBX_RESTART;
4179 mb->mbxHc = 1;
4180
9290831f
JS
4181 lpfc_reset_barrier(phba);
4182
41415862
JW
4183 to_slim = phba->MBslimaddr;
4184 writel(*(uint32_t *) mb, to_slim);
4185 readl(to_slim); /* flush */
4186
4187 /* Only skip post after fc_ffinit is completed */
eaf15d5b 4188 if (phba->pport->port_state)
41415862 4189 word0 = 1; /* This is really setting up word1 */
eaf15d5b 4190 else
41415862 4191 word0 = 0; /* This is really setting up word1 */
65a29c16 4192 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
4193 writel(*(uint32_t *) mb, to_slim);
4194 readl(to_slim); /* flush */
dea3101e 4195
41415862 4196 lpfc_sli_brdreset(phba);
2e0fef85
JS
4197 phba->pport->stopped = 0;
4198 phba->link_state = LPFC_INIT_START;
da0436e9 4199 phba->hba_flag = 0;
2e0fef85 4200 spin_unlock_irq(&phba->hbalock);
41415862 4201
64ba8818
JS
4202 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4203 psli->stats_start = get_seconds();
4204
eaf15d5b
JS
4205 /* Give the INITFF and Post time to settle. */
4206 mdelay(100);
41415862 4207
0d878419
JS
4208 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4209 if (hba_aer_enabled)
4210 pci_disable_pcie_error_reporting(phba->pcidev);
4211
41415862 4212 lpfc_hba_down_post(phba);
dea3101e
JB
4213
4214 return 0;
4215}
4216
da0436e9
JS
4217/**
4218 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4219 * @phba: Pointer to HBA context object.
4220 *
4221 * This function is called in the SLI initialization code path to restart
4222 * a SLI4 HBA. The caller is not required to hold any lock.
4223 * At the end of the function, it calls lpfc_hba_down_post function to
4224 * free any pending commands.
4225 **/
4226static int
4227lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4228{
4229 struct lpfc_sli *psli = &phba->sli;
75baf696 4230 uint32_t hba_aer_enabled;
27b01b82 4231 int rc;
da0436e9
JS
4232
4233 /* Restart HBA */
4234 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4235 "0296 Restart HBA Data: x%x x%x\n",
4236 phba->pport->port_state, psli->sli_flag);
4237
75baf696
JS
4238 /* Take PCIe device Advanced Error Reporting (AER) state */
4239 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4240
27b01b82 4241 rc = lpfc_sli4_brdreset(phba);
da0436e9
JS
4242
4243 spin_lock_irq(&phba->hbalock);
4244 phba->pport->stopped = 0;
4245 phba->link_state = LPFC_INIT_START;
4246 phba->hba_flag = 0;
4247 spin_unlock_irq(&phba->hbalock);
4248
4249 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4250 psli->stats_start = get_seconds();
4251
75baf696
JS
4252 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4253 if (hba_aer_enabled)
4254 pci_disable_pcie_error_reporting(phba->pcidev);
4255
da0436e9
JS
4256 lpfc_hba_down_post(phba);
4257
27b01b82 4258 return rc;
da0436e9
JS
4259}
4260
4261/**
4262 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4263 * @phba: Pointer to HBA context object.
4264 *
4265 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4266 * API jump table function pointer from the lpfc_hba struct.
4267**/
4268int
4269lpfc_sli_brdrestart(struct lpfc_hba *phba)
4270{
4271 return phba->lpfc_sli_brdrestart(phba);
4272}
4273
e59058c4 4274/**
3621a710 4275 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
4276 * @phba: Pointer to HBA context object.
4277 *
4278 * This function is called after a HBA restart to wait for successful
4279 * restart of the HBA. Successful restart of the HBA is indicated by
4280 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4281 * iteration, the function will restart the HBA again. The function returns
4282 * zero if HBA successfully restarted else returns negative error code.
4283 **/
dea3101e
JB
4284static int
4285lpfc_sli_chipset_init(struct lpfc_hba *phba)
4286{
4287 uint32_t status, i = 0;
4288
4289 /* Read the HBA Host Status Register */
9940b97b
JS
4290 if (lpfc_readl(phba->HSregaddr, &status))
4291 return -EIO;
dea3101e
JB
4292
4293 /* Check status register to see what current state is */
4294 i = 0;
4295 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4296
dcf2a4e0
JS
4297 /* Check every 10ms for 10 retries, then every 100ms for 90
4298 * retries, then every 1 sec for 50 retires for a total of
4299 * ~60 seconds before reset the board again and check every
4300 * 1 sec for 50 retries. The up to 60 seconds before the
4301 * board ready is required by the Falcon FIPS zeroization
4302 * complete, and any reset the board in between shall cause
4303 * restart of zeroization, further delay the board ready.
dea3101e 4304 */
dcf2a4e0 4305 if (i++ >= 200) {
dea3101e
JB
4306 /* Adapter failed to init, timeout, status reg
4307 <status> */
ed957684 4308 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4309 "0436 Adapter failed to init, "
09372820
JS
4310 "timeout, status reg x%x, "
4311 "FW Data: A8 x%x AC x%x\n", status,
4312 readl(phba->MBslimaddr + 0xa8),
4313 readl(phba->MBslimaddr + 0xac));
2e0fef85 4314 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4315 return -ETIMEDOUT;
4316 }
4317
4318 /* Check to see if any errors occurred during init */
4319 if (status & HS_FFERM) {
4320 /* ERROR: During chipset initialization */
4321 /* Adapter failed to init, chipset, status reg
4322 <status> */
ed957684 4323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4324 "0437 Adapter failed to init, "
09372820
JS
4325 "chipset, status reg x%x, "
4326 "FW Data: A8 x%x AC x%x\n", status,
4327 readl(phba->MBslimaddr + 0xa8),
4328 readl(phba->MBslimaddr + 0xac));
2e0fef85 4329 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4330 return -EIO;
4331 }
4332
dcf2a4e0 4333 if (i <= 10)
dea3101e 4334 msleep(10);
dcf2a4e0
JS
4335 else if (i <= 100)
4336 msleep(100);
4337 else
4338 msleep(1000);
dea3101e 4339
dcf2a4e0
JS
4340 if (i == 150) {
4341 /* Do post */
92d7f7b0 4342 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4343 lpfc_sli_brdrestart(phba);
dea3101e
JB
4344 }
4345 /* Read the HBA Host Status Register */
9940b97b
JS
4346 if (lpfc_readl(phba->HSregaddr, &status))
4347 return -EIO;
dea3101e
JB
4348 }
4349
4350 /* Check to see if any errors occurred during init */
4351 if (status & HS_FFERM) {
4352 /* ERROR: During chipset initialization */
4353 /* Adapter failed to init, chipset, status reg <status> */
ed957684 4354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4355 "0438 Adapter failed to init, chipset, "
09372820
JS
4356 "status reg x%x, "
4357 "FW Data: A8 x%x AC x%x\n", status,
4358 readl(phba->MBslimaddr + 0xa8),
4359 readl(phba->MBslimaddr + 0xac));
2e0fef85 4360 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4361 return -EIO;
4362 }
4363
4364 /* Clear all interrupt enable conditions */
4365 writel(0, phba->HCregaddr);
4366 readl(phba->HCregaddr); /* flush */
4367
4368 /* setup host attn register */
4369 writel(0xffffffff, phba->HAregaddr);
4370 readl(phba->HAregaddr); /* flush */
4371 return 0;
4372}
4373
e59058c4 4374/**
3621a710 4375 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
4376 *
4377 * This function calculates and returns the number of HBQs required to be
4378 * configured.
4379 **/
78b2d852 4380int
ed957684
JS
4381lpfc_sli_hbq_count(void)
4382{
92d7f7b0 4383 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
4384}
4385
e59058c4 4386/**
3621a710 4387 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
4388 *
4389 * This function adds the number of hbq entries in every HBQ to get
4390 * the total number of hbq entries required for the HBA and returns
4391 * the total count.
4392 **/
ed957684
JS
4393static int
4394lpfc_sli_hbq_entry_count(void)
4395{
4396 int hbq_count = lpfc_sli_hbq_count();
4397 int count = 0;
4398 int i;
4399
4400 for (i = 0; i < hbq_count; ++i)
92d7f7b0 4401 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
4402 return count;
4403}
4404
e59058c4 4405/**
3621a710 4406 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
4407 *
4408 * This function calculates amount of memory required for all hbq entries
4409 * to be configured and returns the total memory required.
4410 **/
dea3101e 4411int
ed957684
JS
4412lpfc_sli_hbq_size(void)
4413{
4414 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4415}
4416
e59058c4 4417/**
3621a710 4418 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
4419 * @phba: Pointer to HBA context object.
4420 *
4421 * This function is called during the SLI initialization to configure
4422 * all the HBQs and post buffers to the HBQ. The caller is not
4423 * required to hold any locks. This function will return zero if successful
4424 * else it will return negative error code.
4425 **/
ed957684
JS
4426static int
4427lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4428{
4429 int hbq_count = lpfc_sli_hbq_count();
4430 LPFC_MBOXQ_t *pmb;
4431 MAILBOX_t *pmbox;
4432 uint32_t hbqno;
4433 uint32_t hbq_entry_index;
ed957684 4434
92d7f7b0
JS
4435 /* Get a Mailbox buffer to setup mailbox
4436 * commands for HBA initialization
4437 */
ed957684
JS
4438 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4439
4440 if (!pmb)
4441 return -ENOMEM;
4442
04c68496 4443 pmbox = &pmb->u.mb;
ed957684
JS
4444
4445 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4446 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 4447 phba->hbq_in_use = 1;
ed957684
JS
4448
4449 hbq_entry_index = 0;
4450 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4451 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4452 phba->hbqs[hbqno].hbqPutIdx = 0;
4453 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4454 phba->hbqs[hbqno].entry_count =
92d7f7b0 4455 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
4456 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4457 hbq_entry_index, pmb);
ed957684
JS
4458 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4459
4460 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4461 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4462 mbxStatus <status>, ring <num> */
4463
4464 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4465 LOG_SLI | LOG_VPORT,
e8b62011 4466 "1805 Adapter failed to init. "
ed957684 4467 "Data: x%x x%x x%x\n",
e8b62011 4468 pmbox->mbxCommand,
ed957684
JS
4469 pmbox->mbxStatus, hbqno);
4470
4471 phba->link_state = LPFC_HBA_ERROR;
4472 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 4473 return -ENXIO;
ed957684
JS
4474 }
4475 }
4476 phba->hbq_count = hbq_count;
4477
ed957684
JS
4478 mempool_free(pmb, phba->mbox_mem_pool);
4479
92d7f7b0 4480 /* Initially populate or replenish the HBQs */
d7c255b2
JS
4481 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4482 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
4483 return 0;
4484}
4485
4f774513
JS
4486/**
4487 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4488 * @phba: Pointer to HBA context object.
4489 *
4490 * This function is called during the SLI initialization to configure
4491 * all the HBQs and post buffers to the HBQ. The caller is not
4492 * required to hold any locks. This function will return zero if successful
4493 * else it will return negative error code.
4494 **/
4495static int
4496lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4497{
4498 phba->hbq_in_use = 1;
4499 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4500 phba->hbq_count = 1;
4501 /* Initially populate or replenish the HBQs */
4502 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4503 return 0;
4504}
4505
e59058c4 4506/**
3621a710 4507 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
4508 * @phba: Pointer to HBA context object.
4509 * @sli_mode: sli mode - 2/3
4510 *
4511 * This function is called by the sli intialization code path
4512 * to issue config_port mailbox command. This function restarts the
4513 * HBA firmware and issues a config_port mailbox command to configure
4514 * the SLI interface in the sli mode specified by sli_mode
4515 * variable. The caller is not required to hold any locks.
4516 * The function returns 0 if successful, else returns negative error
4517 * code.
4518 **/
9399627f
JS
4519int
4520lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e
JB
4521{
4522 LPFC_MBOXQ_t *pmb;
4523 uint32_t resetcount = 0, rc = 0, done = 0;
4524
4525 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4526 if (!pmb) {
2e0fef85 4527 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4528 return -ENOMEM;
4529 }
4530
ed957684 4531 phba->sli_rev = sli_mode;
dea3101e 4532 while (resetcount < 2 && !done) {
2e0fef85 4533 spin_lock_irq(&phba->hbalock);
1c067a42 4534 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4535 spin_unlock_irq(&phba->hbalock);
92d7f7b0 4536 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4537 lpfc_sli_brdrestart(phba);
dea3101e
JB
4538 rc = lpfc_sli_chipset_init(phba);
4539 if (rc)
4540 break;
4541
2e0fef85 4542 spin_lock_irq(&phba->hbalock);
1c067a42 4543 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4544 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4545 resetcount++;
4546
ed957684
JS
4547 /* Call pre CONFIG_PORT mailbox command initialization. A
4548 * value of 0 means the call was successful. Any other
4549 * nonzero value is a failure, but if ERESTART is returned,
4550 * the driver may reset the HBA and try again.
4551 */
dea3101e
JB
4552 rc = lpfc_config_port_prep(phba);
4553 if (rc == -ERESTART) {
ed957684 4554 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 4555 continue;
34b02dcd 4556 } else if (rc)
dea3101e 4557 break;
6d368e53 4558
2e0fef85 4559 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
4560 lpfc_config_port(phba, pmb);
4561 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
4562 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4563 LPFC_SLI3_HBQ_ENABLED |
4564 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
4565 LPFC_SLI3_BG_ENABLED |
4566 LPFC_SLI3_DSS_ENABLED);
ed957684 4567 if (rc != MBX_SUCCESS) {
dea3101e 4568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4569 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 4570 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 4571 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 4572 spin_lock_irq(&phba->hbalock);
04c68496 4573 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
4574 spin_unlock_irq(&phba->hbalock);
4575 rc = -ENXIO;
04c68496
JS
4576 } else {
4577 /* Allow asynchronous mailbox command to go through */
4578 spin_lock_irq(&phba->hbalock);
4579 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4580 spin_unlock_irq(&phba->hbalock);
ed957684 4581 done = 1;
cb69f7de
JS
4582
4583 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4584 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4585 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4586 "3110 Port did not grant ASABT\n");
04c68496 4587 }
dea3101e 4588 }
ed957684
JS
4589 if (!done) {
4590 rc = -EINVAL;
4591 goto do_prep_failed;
4592 }
04c68496
JS
4593 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4594 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
4595 rc = -ENXIO;
4596 goto do_prep_failed;
4597 }
04c68496 4598 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 4599 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
4600 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4601 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4602 phba->max_vpi : phba->max_vports;
4603
34b02dcd
JS
4604 } else
4605 phba->max_vpi = 0;
bc73905a
JS
4606 phba->fips_level = 0;
4607 phba->fips_spec_rev = 0;
4608 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 4609 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
4610 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4611 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4612 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4613 "2850 Security Crypto Active. FIPS x%d "
4614 "(Spec Rev: x%d)",
4615 phba->fips_level, phba->fips_spec_rev);
4616 }
4617 if (pmb->u.mb.un.varCfgPort.sec_err) {
4618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4619 "2856 Config Port Security Crypto "
4620 "Error: x%x ",
4621 pmb->u.mb.un.varCfgPort.sec_err);
4622 }
04c68496 4623 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 4624 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 4625 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 4626 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
4627
4628 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4629 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6
JS
4630
4631 if (phba->cfg_enable_bg) {
04c68496 4632 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
4633 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4634 else
4635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4636 "0443 Adapter did not grant "
4637 "BlockGuard\n");
4638 }
34b02dcd 4639 } else {
8f34f4ce 4640 phba->hbq_get = NULL;
34b02dcd 4641 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 4642 phba->max_vpi = 0;
ed957684 4643 }
92d7f7b0 4644do_prep_failed:
ed957684
JS
4645 mempool_free(pmb, phba->mbox_mem_pool);
4646 return rc;
4647}
4648
e59058c4
JS
4649
4650/**
3621a710 4651 * lpfc_sli_hba_setup - SLI intialization function
e59058c4
JS
4652 * @phba: Pointer to HBA context object.
4653 *
4654 * This function is the main SLI intialization function. This function
4655 * is called by the HBA intialization code, HBA reset code and HBA
4656 * error attention handler code. Caller is not required to hold any
4657 * locks. This function issues config_port mailbox command to configure
4658 * the SLI, setup iocb rings and HBQ rings. In the end the function
4659 * calls the config_port_post function to issue init_link mailbox
4660 * command and to start the discovery. The function will return zero
4661 * if successful, else it will return negative error code.
4662 **/
ed957684
JS
4663int
4664lpfc_sli_hba_setup(struct lpfc_hba *phba)
4665{
4666 uint32_t rc;
6d368e53
JS
4667 int mode = 3, i;
4668 int longs;
ed957684 4669
12247e81 4670 switch (phba->cfg_sli_mode) {
ed957684 4671 case 2:
78b2d852 4672 if (phba->cfg_enable_npiv) {
92d7f7b0 4673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81 4674 "1824 NPIV enabled: Override sli_mode "
92d7f7b0 4675 "parameter (%d) to auto (0).\n",
12247e81 4676 phba->cfg_sli_mode);
92d7f7b0
JS
4677 break;
4678 }
ed957684
JS
4679 mode = 2;
4680 break;
4681 case 0:
4682 case 3:
4683 break;
4684 default:
92d7f7b0 4685 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81
JS
4686 "1819 Unrecognized sli_mode parameter: %d.\n",
4687 phba->cfg_sli_mode);
ed957684
JS
4688
4689 break;
4690 }
b5c53958 4691 phba->fcp_embed_io = 0; /* SLI4 FC support only */
ed957684 4692
9399627f
JS
4693 rc = lpfc_sli_config_port(phba, mode);
4694
12247e81 4695 if (rc && phba->cfg_sli_mode == 3)
92d7f7b0 4696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4697 "1820 Unable to select SLI-3. "
4698 "Not supported by adapter.\n");
ed957684 4699 if (rc && mode != 2)
9399627f 4700 rc = lpfc_sli_config_port(phba, 2);
4597663f
JS
4701 else if (rc && mode == 2)
4702 rc = lpfc_sli_config_port(phba, 3);
ed957684 4703 if (rc)
dea3101e
JB
4704 goto lpfc_sli_hba_setup_error;
4705
0d878419
JS
4706 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4707 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4708 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4709 if (!rc) {
4710 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4711 "2709 This device supports "
4712 "Advanced Error Reporting (AER)\n");
4713 spin_lock_irq(&phba->hbalock);
4714 phba->hba_flag |= HBA_AER_ENABLED;
4715 spin_unlock_irq(&phba->hbalock);
4716 } else {
4717 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4718 "2708 This device does not support "
b069d7eb
JS
4719 "Advanced Error Reporting (AER): %d\n",
4720 rc);
0d878419
JS
4721 phba->cfg_aer_support = 0;
4722 }
4723 }
4724
ed957684
JS
4725 if (phba->sli_rev == 3) {
4726 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4727 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4728 } else {
4729 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4730 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4731 phba->sli3_options = 0;
ed957684
JS
4732 }
4733
4734 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4735 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4736 phba->sli_rev, phba->max_vpi);
ed957684 4737 rc = lpfc_sli_ring_map(phba);
dea3101e
JB
4738
4739 if (rc)
4740 goto lpfc_sli_hba_setup_error;
4741
6d368e53
JS
4742 /* Initialize VPIs. */
4743 if (phba->sli_rev == LPFC_SLI_REV3) {
4744 /*
4745 * The VPI bitmask and physical ID array are allocated
4746 * and initialized once only - at driver load. A port
4747 * reset doesn't need to reinitialize this memory.
4748 */
4749 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4750 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4751 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4752 GFP_KERNEL);
4753 if (!phba->vpi_bmask) {
4754 rc = -ENOMEM;
4755 goto lpfc_sli_hba_setup_error;
4756 }
4757
4758 phba->vpi_ids = kzalloc(
4759 (phba->max_vpi+1) * sizeof(uint16_t),
4760 GFP_KERNEL);
4761 if (!phba->vpi_ids) {
4762 kfree(phba->vpi_bmask);
4763 rc = -ENOMEM;
4764 goto lpfc_sli_hba_setup_error;
4765 }
4766 for (i = 0; i < phba->max_vpi; i++)
4767 phba->vpi_ids[i] = i;
4768 }
4769 }
4770
9399627f 4771 /* Init HBQs */
ed957684
JS
4772 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4773 rc = lpfc_sli_hbq_setup(phba);
4774 if (rc)
4775 goto lpfc_sli_hba_setup_error;
4776 }
04c68496 4777 spin_lock_irq(&phba->hbalock);
dea3101e 4778 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4779 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4780
4781 rc = lpfc_config_port_post(phba);
4782 if (rc)
4783 goto lpfc_sli_hba_setup_error;
4784
ed957684
JS
4785 return rc;
4786
92d7f7b0 4787lpfc_sli_hba_setup_error:
2e0fef85 4788 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 4789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4790 "0445 Firmware initialization failed\n");
dea3101e
JB
4791 return rc;
4792}
4793
e59058c4 4794/**
da0436e9
JS
4795 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4796 * @phba: Pointer to HBA context object.
4797 * @mboxq: mailbox pointer.
4798 * This function issue a dump mailbox command to read config region
4799 * 23 and parse the records in the region and populate driver
4800 * data structure.
e59058c4 4801 **/
da0436e9 4802static int
ff78d8f9 4803lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
dea3101e 4804{
ff78d8f9 4805 LPFC_MBOXQ_t *mboxq;
da0436e9
JS
4806 struct lpfc_dmabuf *mp;
4807 struct lpfc_mqe *mqe;
4808 uint32_t data_length;
4809 int rc;
dea3101e 4810
da0436e9
JS
4811 /* Program the default value of vlan_id and fc_map */
4812 phba->valid_vlan = 0;
4813 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4814 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4815 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4816
ff78d8f9
JS
4817 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4818 if (!mboxq)
da0436e9
JS
4819 return -ENOMEM;
4820
ff78d8f9
JS
4821 mqe = &mboxq->u.mqe;
4822 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4823 rc = -ENOMEM;
4824 goto out_free_mboxq;
4825 }
4826
da0436e9
JS
4827 mp = (struct lpfc_dmabuf *) mboxq->context1;
4828 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4829
4830 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4831 "(%d):2571 Mailbox cmd x%x Status x%x "
4832 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4833 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4834 "CQ: x%x x%x x%x x%x\n",
4835 mboxq->vport ? mboxq->vport->vpi : 0,
4836 bf_get(lpfc_mqe_command, mqe),
4837 bf_get(lpfc_mqe_status, mqe),
4838 mqe->un.mb_words[0], mqe->un.mb_words[1],
4839 mqe->un.mb_words[2], mqe->un.mb_words[3],
4840 mqe->un.mb_words[4], mqe->un.mb_words[5],
4841 mqe->un.mb_words[6], mqe->un.mb_words[7],
4842 mqe->un.mb_words[8], mqe->un.mb_words[9],
4843 mqe->un.mb_words[10], mqe->un.mb_words[11],
4844 mqe->un.mb_words[12], mqe->un.mb_words[13],
4845 mqe->un.mb_words[14], mqe->un.mb_words[15],
4846 mqe->un.mb_words[16], mqe->un.mb_words[50],
4847 mboxq->mcqe.word0,
4848 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4849 mboxq->mcqe.trailer);
4850
4851 if (rc) {
4852 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4853 kfree(mp);
ff78d8f9
JS
4854 rc = -EIO;
4855 goto out_free_mboxq;
da0436e9
JS
4856 }
4857 data_length = mqe->un.mb_words[5];
a0c87cbd 4858 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
4859 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4860 kfree(mp);
ff78d8f9
JS
4861 rc = -EIO;
4862 goto out_free_mboxq;
d11e31dd 4863 }
dea3101e 4864
da0436e9
JS
4865 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4866 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4867 kfree(mp);
ff78d8f9
JS
4868 rc = 0;
4869
4870out_free_mboxq:
4871 mempool_free(mboxq, phba->mbox_mem_pool);
4872 return rc;
da0436e9 4873}
e59058c4
JS
4874
4875/**
da0436e9
JS
4876 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4877 * @phba: pointer to lpfc hba data structure.
4878 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4879 * @vpd: pointer to the memory to hold resulting port vpd data.
4880 * @vpd_size: On input, the number of bytes allocated to @vpd.
4881 * On output, the number of data bytes in @vpd.
e59058c4 4882 *
da0436e9
JS
4883 * This routine executes a READ_REV SLI4 mailbox command. In
4884 * addition, this routine gets the port vpd data.
4885 *
4886 * Return codes
af901ca1 4887 * 0 - successful
d439d286 4888 * -ENOMEM - could not allocated memory.
e59058c4 4889 **/
da0436e9
JS
4890static int
4891lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4892 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 4893{
da0436e9
JS
4894 int rc = 0;
4895 uint32_t dma_size;
4896 struct lpfc_dmabuf *dmabuf;
4897 struct lpfc_mqe *mqe;
dea3101e 4898
da0436e9
JS
4899 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4900 if (!dmabuf)
4901 return -ENOMEM;
4902
4903 /*
4904 * Get a DMA buffer for the vpd data resulting from the READ_REV
4905 * mailbox command.
a257bf90 4906 */
da0436e9 4907 dma_size = *vpd_size;
1aee383d
JP
4908 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
4909 &dmabuf->phys, GFP_KERNEL);
da0436e9
JS
4910 if (!dmabuf->virt) {
4911 kfree(dmabuf);
4912 return -ENOMEM;
a257bf90
JS
4913 }
4914
da0436e9
JS
4915 /*
4916 * The SLI4 implementation of READ_REV conflicts at word1,
4917 * bits 31:16 and SLI4 adds vpd functionality not present
4918 * in SLI3. This code corrects the conflicts.
1dcb58e5 4919 */
da0436e9
JS
4920 lpfc_read_rev(phba, mboxq);
4921 mqe = &mboxq->u.mqe;
4922 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4923 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4924 mqe->un.read_rev.word1 &= 0x0000FFFF;
4925 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4926 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4927
4928 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4929 if (rc) {
4930 dma_free_coherent(&phba->pcidev->dev, dma_size,
4931 dmabuf->virt, dmabuf->phys);
def9c7a9 4932 kfree(dmabuf);
da0436e9
JS
4933 return -EIO;
4934 }
1dcb58e5 4935
da0436e9
JS
4936 /*
4937 * The available vpd length cannot be bigger than the
4938 * DMA buffer passed to the port. Catch the less than
4939 * case and update the caller's size.
4940 */
4941 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4942 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 4943
d7c47992
JS
4944 memcpy(vpd, dmabuf->virt, *vpd_size);
4945
da0436e9
JS
4946 dma_free_coherent(&phba->pcidev->dev, dma_size,
4947 dmabuf->virt, dmabuf->phys);
4948 kfree(dmabuf);
4949 return 0;
dea3101e
JB
4950}
4951
cd1c8301
JS
4952/**
4953 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4954 * @phba: pointer to lpfc hba data structure.
4955 *
4956 * This routine retrieves SLI4 device physical port name this PCI function
4957 * is attached to.
4958 *
4959 * Return codes
4907cb7b 4960 * 0 - successful
cd1c8301
JS
4961 * otherwise - failed to retrieve physical port name
4962 **/
4963static int
4964lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4965{
4966 LPFC_MBOXQ_t *mboxq;
cd1c8301
JS
4967 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4968 struct lpfc_controller_attribute *cntl_attr;
4969 struct lpfc_mbx_get_port_name *get_port_name;
4970 void *virtaddr = NULL;
4971 uint32_t alloclen, reqlen;
4972 uint32_t shdr_status, shdr_add_status;
4973 union lpfc_sli4_cfg_shdr *shdr;
4974 char cport_name = 0;
4975 int rc;
4976
4977 /* We assume nothing at this point */
4978 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4979 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4980
4981 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4982 if (!mboxq)
4983 return -ENOMEM;
cd1c8301 4984 /* obtain link type and link number via READ_CONFIG */
ff78d8f9
JS
4985 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4986 lpfc_sli4_read_config(phba);
4987 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4988 goto retrieve_ppname;
cd1c8301
JS
4989
4990 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4991 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4992 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4993 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4994 LPFC_SLI4_MBX_NEMBED);
4995 if (alloclen < reqlen) {
4996 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4997 "3084 Allocated DMA memory size (%d) is "
4998 "less than the requested DMA memory size "
4999 "(%d)\n", alloclen, reqlen);
5000 rc = -ENOMEM;
5001 goto out_free_mboxq;
5002 }
5003 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5004 virtaddr = mboxq->sge_array->addr[0];
5005 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5006 shdr = &mbx_cntl_attr->cfg_shdr;
5007 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5008 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5009 if (shdr_status || shdr_add_status || rc) {
5010 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5011 "3085 Mailbox x%x (x%x/x%x) failed, "
5012 "rc:x%x, status:x%x, add_status:x%x\n",
5013 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5014 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5015 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5016 rc, shdr_status, shdr_add_status);
5017 rc = -ENXIO;
5018 goto out_free_mboxq;
5019 }
5020 cntl_attr = &mbx_cntl_attr->cntl_attr;
5021 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5022 phba->sli4_hba.lnk_info.lnk_tp =
5023 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5024 phba->sli4_hba.lnk_info.lnk_no =
5025 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5026 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5027 "3086 lnk_type:%d, lnk_numb:%d\n",
5028 phba->sli4_hba.lnk_info.lnk_tp,
5029 phba->sli4_hba.lnk_info.lnk_no);
5030
5031retrieve_ppname:
5032 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5033 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5034 sizeof(struct lpfc_mbx_get_port_name) -
5035 sizeof(struct lpfc_sli4_cfg_mhdr),
5036 LPFC_SLI4_MBX_EMBED);
5037 get_port_name = &mboxq->u.mqe.un.get_port_name;
5038 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5039 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5040 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5041 phba->sli4_hba.lnk_info.lnk_tp);
5042 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5043 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5044 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5045 if (shdr_status || shdr_add_status || rc) {
5046 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5047 "3087 Mailbox x%x (x%x/x%x) failed: "
5048 "rc:x%x, status:x%x, add_status:x%x\n",
5049 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5050 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5051 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5052 rc, shdr_status, shdr_add_status);
5053 rc = -ENXIO;
5054 goto out_free_mboxq;
5055 }
5056 switch (phba->sli4_hba.lnk_info.lnk_no) {
5057 case LPFC_LINK_NUMBER_0:
5058 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5059 &get_port_name->u.response);
5060 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5061 break;
5062 case LPFC_LINK_NUMBER_1:
5063 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5064 &get_port_name->u.response);
5065 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5066 break;
5067 case LPFC_LINK_NUMBER_2:
5068 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5069 &get_port_name->u.response);
5070 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5071 break;
5072 case LPFC_LINK_NUMBER_3:
5073 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5074 &get_port_name->u.response);
5075 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5076 break;
5077 default:
5078 break;
5079 }
5080
5081 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5082 phba->Port[0] = cport_name;
5083 phba->Port[1] = '\0';
5084 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5085 "3091 SLI get port name: %s\n", phba->Port);
5086 }
5087
5088out_free_mboxq:
5089 if (rc != MBX_TIMEOUT) {
5090 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5091 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5092 else
5093 mempool_free(mboxq, phba->mbox_mem_pool);
5094 }
5095 return rc;
5096}
5097
e59058c4 5098/**
da0436e9
JS
5099 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5100 * @phba: pointer to lpfc hba data structure.
e59058c4 5101 *
da0436e9
JS
5102 * This routine is called to explicitly arm the SLI4 device's completion and
5103 * event queues
5104 **/
5105static void
5106lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5107{
962bc51b 5108 int fcp_eqidx;
da0436e9
JS
5109
5110 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5111 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
0558056c 5112 fcp_eqidx = 0;
2e90f4b5 5113 if (phba->sli4_hba.fcp_cq) {
67d12733 5114 do {
2e90f4b5
JS
5115 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
5116 LPFC_QUEUE_REARM);
67d12733 5117 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
2e90f4b5 5118 }
1ba981fd 5119
f38fa0bb 5120 if (phba->cfg_fof)
1ba981fd
JS
5121 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5122
67d12733
JS
5123 if (phba->sli4_hba.hba_eq) {
5124 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
2e90f4b5 5125 fcp_eqidx++)
67d12733 5126 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
2e90f4b5
JS
5127 LPFC_QUEUE_REARM);
5128 }
1ba981fd
JS
5129
5130 if (phba->cfg_fof)
5131 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
da0436e9
JS
5132}
5133
6d368e53
JS
5134/**
5135 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5136 * @phba: Pointer to HBA context object.
5137 * @type: The resource extent type.
b76f2dc9
JS
5138 * @extnt_count: buffer to hold port available extent count.
5139 * @extnt_size: buffer to hold element count per extent.
6d368e53 5140 *
b76f2dc9
JS
5141 * This function calls the port and retrievs the number of available
5142 * extents and their size for a particular extent type.
5143 *
5144 * Returns: 0 if successful. Nonzero otherwise.
6d368e53 5145 **/
b76f2dc9 5146int
6d368e53
JS
5147lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5148 uint16_t *extnt_count, uint16_t *extnt_size)
5149{
5150 int rc = 0;
5151 uint32_t length;
5152 uint32_t mbox_tmo;
5153 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5154 LPFC_MBOXQ_t *mbox;
5155
5156 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5157 if (!mbox)
5158 return -ENOMEM;
5159
5160 /* Find out how many extents are available for this resource type */
5161 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5162 sizeof(struct lpfc_sli4_cfg_mhdr));
5163 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5164 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5165 length, LPFC_SLI4_MBX_EMBED);
5166
5167 /* Send an extents count of 0 - the GET doesn't use it. */
5168 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5169 LPFC_SLI4_MBX_EMBED);
5170 if (unlikely(rc)) {
5171 rc = -EIO;
5172 goto err_exit;
5173 }
5174
5175 if (!phba->sli4_hba.intr_enable)
5176 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5177 else {
a183a15f 5178 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5179 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5180 }
5181 if (unlikely(rc)) {
5182 rc = -EIO;
5183 goto err_exit;
5184 }
5185
5186 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5187 if (bf_get(lpfc_mbox_hdr_status,
5188 &rsrc_info->header.cfg_shdr.response)) {
5189 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5190 "2930 Failed to get resource extents "
5191 "Status 0x%x Add'l Status 0x%x\n",
5192 bf_get(lpfc_mbox_hdr_status,
5193 &rsrc_info->header.cfg_shdr.response),
5194 bf_get(lpfc_mbox_hdr_add_status,
5195 &rsrc_info->header.cfg_shdr.response));
5196 rc = -EIO;
5197 goto err_exit;
5198 }
5199
5200 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5201 &rsrc_info->u.rsp);
5202 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5203 &rsrc_info->u.rsp);
8a9d2e80
JS
5204
5205 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5206 "3162 Retrieved extents type-%d from port: count:%d, "
5207 "size:%d\n", type, *extnt_count, *extnt_size);
5208
5209err_exit:
6d368e53
JS
5210 mempool_free(mbox, phba->mbox_mem_pool);
5211 return rc;
5212}
5213
5214/**
5215 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5216 * @phba: Pointer to HBA context object.
5217 * @type: The extent type to check.
5218 *
5219 * This function reads the current available extents from the port and checks
5220 * if the extent count or extent size has changed since the last access.
5221 * Callers use this routine post port reset to understand if there is a
5222 * extent reprovisioning requirement.
5223 *
5224 * Returns:
5225 * -Error: error indicates problem.
5226 * 1: Extent count or size has changed.
5227 * 0: No changes.
5228 **/
5229static int
5230lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5231{
5232 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5233 uint16_t size_diff, rsrc_ext_size;
5234 int rc = 0;
5235 struct lpfc_rsrc_blks *rsrc_entry;
5236 struct list_head *rsrc_blk_list = NULL;
5237
5238 size_diff = 0;
5239 curr_ext_cnt = 0;
5240 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5241 &rsrc_ext_cnt,
5242 &rsrc_ext_size);
5243 if (unlikely(rc))
5244 return -EIO;
5245
5246 switch (type) {
5247 case LPFC_RSC_TYPE_FCOE_RPI:
5248 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5249 break;
5250 case LPFC_RSC_TYPE_FCOE_VPI:
5251 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5252 break;
5253 case LPFC_RSC_TYPE_FCOE_XRI:
5254 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5255 break;
5256 case LPFC_RSC_TYPE_FCOE_VFI:
5257 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5258 break;
5259 default:
5260 break;
5261 }
5262
5263 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5264 curr_ext_cnt++;
5265 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5266 size_diff++;
5267 }
5268
5269 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5270 rc = 1;
5271
5272 return rc;
5273}
5274
5275/**
5276 * lpfc_sli4_cfg_post_extnts -
5277 * @phba: Pointer to HBA context object.
5278 * @extnt_cnt - number of available extents.
5279 * @type - the extent type (rpi, xri, vfi, vpi).
5280 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5281 * @mbox - pointer to the caller's allocated mailbox structure.
5282 *
5283 * This function executes the extents allocation request. It also
5284 * takes care of the amount of memory needed to allocate or get the
5285 * allocated extents. It is the caller's responsibility to evaluate
5286 * the response.
5287 *
5288 * Returns:
5289 * -Error: Error value describes the condition found.
5290 * 0: if successful
5291 **/
5292static int
8a9d2e80 5293lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6d368e53
JS
5294 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5295{
5296 int rc = 0;
5297 uint32_t req_len;
5298 uint32_t emb_len;
5299 uint32_t alloc_len, mbox_tmo;
5300
5301 /* Calculate the total requested length of the dma memory */
8a9d2e80 5302 req_len = extnt_cnt * sizeof(uint16_t);
6d368e53
JS
5303
5304 /*
5305 * Calculate the size of an embedded mailbox. The uint32_t
5306 * accounts for extents-specific word.
5307 */
5308 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5309 sizeof(uint32_t);
5310
5311 /*
5312 * Presume the allocation and response will fit into an embedded
5313 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5314 */
5315 *emb = LPFC_SLI4_MBX_EMBED;
5316 if (req_len > emb_len) {
8a9d2e80 5317 req_len = extnt_cnt * sizeof(uint16_t) +
6d368e53
JS
5318 sizeof(union lpfc_sli4_cfg_shdr) +
5319 sizeof(uint32_t);
5320 *emb = LPFC_SLI4_MBX_NEMBED;
5321 }
5322
5323 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5324 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5325 req_len, *emb);
5326 if (alloc_len < req_len) {
5327 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
b76f2dc9 5328 "2982 Allocated DMA memory size (x%x) is "
6d368e53
JS
5329 "less than the requested DMA memory "
5330 "size (x%x)\n", alloc_len, req_len);
5331 return -ENOMEM;
5332 }
8a9d2e80 5333 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6d368e53
JS
5334 if (unlikely(rc))
5335 return -EIO;
5336
5337 if (!phba->sli4_hba.intr_enable)
5338 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5339 else {
a183a15f 5340 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5341 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5342 }
5343
5344 if (unlikely(rc))
5345 rc = -EIO;
5346 return rc;
5347}
5348
5349/**
5350 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5351 * @phba: Pointer to HBA context object.
5352 * @type: The resource extent type to allocate.
5353 *
5354 * This function allocates the number of elements for the specified
5355 * resource type.
5356 **/
5357static int
5358lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5359{
5360 bool emb = false;
5361 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5362 uint16_t rsrc_id, rsrc_start, j, k;
5363 uint16_t *ids;
5364 int i, rc;
5365 unsigned long longs;
5366 unsigned long *bmask;
5367 struct lpfc_rsrc_blks *rsrc_blks;
5368 LPFC_MBOXQ_t *mbox;
5369 uint32_t length;
5370 struct lpfc_id_range *id_array = NULL;
5371 void *virtaddr = NULL;
5372 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5373 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5374 struct list_head *ext_blk_list;
5375
5376 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5377 &rsrc_cnt,
5378 &rsrc_size);
5379 if (unlikely(rc))
5380 return -EIO;
5381
5382 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5383 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5384 "3009 No available Resource Extents "
5385 "for resource type 0x%x: Count: 0x%x, "
5386 "Size 0x%x\n", type, rsrc_cnt,
5387 rsrc_size);
5388 return -ENOMEM;
5389 }
5390
8a9d2e80
JS
5391 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5392 "2903 Post resource extents type-0x%x: "
5393 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6d368e53
JS
5394
5395 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5396 if (!mbox)
5397 return -ENOMEM;
5398
8a9d2e80 5399 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6d368e53
JS
5400 if (unlikely(rc)) {
5401 rc = -EIO;
5402 goto err_exit;
5403 }
5404
5405 /*
5406 * Figure out where the response is located. Then get local pointers
5407 * to the response data. The port does not guarantee to respond to
5408 * all extents counts request so update the local variable with the
5409 * allocated count from the port.
5410 */
5411 if (emb == LPFC_SLI4_MBX_EMBED) {
5412 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5413 id_array = &rsrc_ext->u.rsp.id[0];
5414 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5415 } else {
5416 virtaddr = mbox->sge_array->addr[0];
5417 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5418 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5419 id_array = &n_rsrc->id;
5420 }
5421
5422 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5423 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5424
5425 /*
5426 * Based on the resource size and count, correct the base and max
5427 * resource values.
5428 */
5429 length = sizeof(struct lpfc_rsrc_blks);
5430 switch (type) {
5431 case LPFC_RSC_TYPE_FCOE_RPI:
5432 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5433 sizeof(unsigned long),
5434 GFP_KERNEL);
5435 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5436 rc = -ENOMEM;
5437 goto err_exit;
5438 }
5439 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5440 sizeof(uint16_t),
5441 GFP_KERNEL);
5442 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5443 kfree(phba->sli4_hba.rpi_bmask);
5444 rc = -ENOMEM;
5445 goto err_exit;
5446 }
5447
5448 /*
5449 * The next_rpi was initialized with the maximum available
5450 * count but the port may allocate a smaller number. Catch
5451 * that case and update the next_rpi.
5452 */
5453 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5454
5455 /* Initialize local ptrs for common extent processing later. */
5456 bmask = phba->sli4_hba.rpi_bmask;
5457 ids = phba->sli4_hba.rpi_ids;
5458 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5459 break;
5460 case LPFC_RSC_TYPE_FCOE_VPI:
5461 phba->vpi_bmask = kzalloc(longs *
5462 sizeof(unsigned long),
5463 GFP_KERNEL);
5464 if (unlikely(!phba->vpi_bmask)) {
5465 rc = -ENOMEM;
5466 goto err_exit;
5467 }
5468 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5469 sizeof(uint16_t),
5470 GFP_KERNEL);
5471 if (unlikely(!phba->vpi_ids)) {
5472 kfree(phba->vpi_bmask);
5473 rc = -ENOMEM;
5474 goto err_exit;
5475 }
5476
5477 /* Initialize local ptrs for common extent processing later. */
5478 bmask = phba->vpi_bmask;
5479 ids = phba->vpi_ids;
5480 ext_blk_list = &phba->lpfc_vpi_blk_list;
5481 break;
5482 case LPFC_RSC_TYPE_FCOE_XRI:
5483 phba->sli4_hba.xri_bmask = kzalloc(longs *
5484 sizeof(unsigned long),
5485 GFP_KERNEL);
5486 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5487 rc = -ENOMEM;
5488 goto err_exit;
5489 }
8a9d2e80 5490 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
5491 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5492 sizeof(uint16_t),
5493 GFP_KERNEL);
5494 if (unlikely(!phba->sli4_hba.xri_ids)) {
5495 kfree(phba->sli4_hba.xri_bmask);
5496 rc = -ENOMEM;
5497 goto err_exit;
5498 }
5499
5500 /* Initialize local ptrs for common extent processing later. */
5501 bmask = phba->sli4_hba.xri_bmask;
5502 ids = phba->sli4_hba.xri_ids;
5503 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5504 break;
5505 case LPFC_RSC_TYPE_FCOE_VFI:
5506 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5507 sizeof(unsigned long),
5508 GFP_KERNEL);
5509 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5510 rc = -ENOMEM;
5511 goto err_exit;
5512 }
5513 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5514 sizeof(uint16_t),
5515 GFP_KERNEL);
5516 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5517 kfree(phba->sli4_hba.vfi_bmask);
5518 rc = -ENOMEM;
5519 goto err_exit;
5520 }
5521
5522 /* Initialize local ptrs for common extent processing later. */
5523 bmask = phba->sli4_hba.vfi_bmask;
5524 ids = phba->sli4_hba.vfi_ids;
5525 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5526 break;
5527 default:
5528 /* Unsupported Opcode. Fail call. */
5529 id_array = NULL;
5530 bmask = NULL;
5531 ids = NULL;
5532 ext_blk_list = NULL;
5533 goto err_exit;
5534 }
5535
5536 /*
5537 * Complete initializing the extent configuration with the
5538 * allocated ids assigned to this function. The bitmask serves
5539 * as an index into the array and manages the available ids. The
5540 * array just stores the ids communicated to the port via the wqes.
5541 */
5542 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5543 if ((i % 2) == 0)
5544 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5545 &id_array[k]);
5546 else
5547 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5548 &id_array[k]);
5549
5550 rsrc_blks = kzalloc(length, GFP_KERNEL);
5551 if (unlikely(!rsrc_blks)) {
5552 rc = -ENOMEM;
5553 kfree(bmask);
5554 kfree(ids);
5555 goto err_exit;
5556 }
5557 rsrc_blks->rsrc_start = rsrc_id;
5558 rsrc_blks->rsrc_size = rsrc_size;
5559 list_add_tail(&rsrc_blks->list, ext_blk_list);
5560 rsrc_start = rsrc_id;
5561 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5562 phba->sli4_hba.scsi_xri_start = rsrc_start +
5563 lpfc_sli4_get_els_iocb_cnt(phba);
5564
5565 while (rsrc_id < (rsrc_start + rsrc_size)) {
5566 ids[j] = rsrc_id;
5567 rsrc_id++;
5568 j++;
5569 }
5570 /* Entire word processed. Get next word.*/
5571 if ((i % 2) == 1)
5572 k++;
5573 }
5574 err_exit:
5575 lpfc_sli4_mbox_cmd_free(phba, mbox);
5576 return rc;
5577}
5578
5579/**
5580 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5581 * @phba: Pointer to HBA context object.
5582 * @type: the extent's type.
5583 *
5584 * This function deallocates all extents of a particular resource type.
5585 * SLI4 does not allow for deallocating a particular extent range. It
5586 * is the caller's responsibility to release all kernel memory resources.
5587 **/
5588static int
5589lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5590{
5591 int rc;
5592 uint32_t length, mbox_tmo = 0;
5593 LPFC_MBOXQ_t *mbox;
5594 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5595 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5596
5597 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5598 if (!mbox)
5599 return -ENOMEM;
5600
5601 /*
5602 * This function sends an embedded mailbox because it only sends the
5603 * the resource type. All extents of this type are released by the
5604 * port.
5605 */
5606 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5607 sizeof(struct lpfc_sli4_cfg_mhdr));
5608 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5609 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5610 length, LPFC_SLI4_MBX_EMBED);
5611
5612 /* Send an extents count of 0 - the dealloc doesn't use it. */
5613 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5614 LPFC_SLI4_MBX_EMBED);
5615 if (unlikely(rc)) {
5616 rc = -EIO;
5617 goto out_free_mbox;
5618 }
5619 if (!phba->sli4_hba.intr_enable)
5620 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5621 else {
a183a15f 5622 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5623 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5624 }
5625 if (unlikely(rc)) {
5626 rc = -EIO;
5627 goto out_free_mbox;
5628 }
5629
5630 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5631 if (bf_get(lpfc_mbox_hdr_status,
5632 &dealloc_rsrc->header.cfg_shdr.response)) {
5633 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5634 "2919 Failed to release resource extents "
5635 "for type %d - Status 0x%x Add'l Status 0x%x. "
5636 "Resource memory not released.\n",
5637 type,
5638 bf_get(lpfc_mbox_hdr_status,
5639 &dealloc_rsrc->header.cfg_shdr.response),
5640 bf_get(lpfc_mbox_hdr_add_status,
5641 &dealloc_rsrc->header.cfg_shdr.response));
5642 rc = -EIO;
5643 goto out_free_mbox;
5644 }
5645
5646 /* Release kernel memory resources for the specific type. */
5647 switch (type) {
5648 case LPFC_RSC_TYPE_FCOE_VPI:
5649 kfree(phba->vpi_bmask);
5650 kfree(phba->vpi_ids);
5651 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5652 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5653 &phba->lpfc_vpi_blk_list, list) {
5654 list_del_init(&rsrc_blk->list);
5655 kfree(rsrc_blk);
5656 }
16a3a208 5657 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
5658 break;
5659 case LPFC_RSC_TYPE_FCOE_XRI:
5660 kfree(phba->sli4_hba.xri_bmask);
5661 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
5662 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5663 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5664 list_del_init(&rsrc_blk->list);
5665 kfree(rsrc_blk);
5666 }
5667 break;
5668 case LPFC_RSC_TYPE_FCOE_VFI:
5669 kfree(phba->sli4_hba.vfi_bmask);
5670 kfree(phba->sli4_hba.vfi_ids);
5671 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5672 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5673 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5674 list_del_init(&rsrc_blk->list);
5675 kfree(rsrc_blk);
5676 }
5677 break;
5678 case LPFC_RSC_TYPE_FCOE_RPI:
5679 /* RPI bitmask and physical id array are cleaned up earlier. */
5680 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5681 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5682 list_del_init(&rsrc_blk->list);
5683 kfree(rsrc_blk);
5684 }
5685 break;
5686 default:
5687 break;
5688 }
5689
5690 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5691
5692 out_free_mbox:
5693 mempool_free(mbox, phba->mbox_mem_pool);
5694 return rc;
5695}
5696
bd4b3e5c 5697static void
7bdedb34
JS
5698lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5699 uint32_t feature)
65791f1f 5700{
65791f1f 5701 uint32_t len;
65791f1f 5702
65791f1f
JS
5703 len = sizeof(struct lpfc_mbx_set_feature) -
5704 sizeof(struct lpfc_sli4_cfg_mhdr);
5705 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5706 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5707 LPFC_SLI4_MBX_EMBED);
7bdedb34
JS
5708
5709 switch (feature) {
5710 case LPFC_SET_UE_RECOVERY:
5711 bf_set(lpfc_mbx_set_feature_UER,
5712 &mbox->u.mqe.un.set_feature, 1);
5713 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5714 mbox->u.mqe.un.set_feature.param_len = 8;
5715 break;
5716 case LPFC_SET_MDS_DIAGS:
5717 bf_set(lpfc_mbx_set_feature_mds,
5718 &mbox->u.mqe.un.set_feature, 1);
5719 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5720 &mbox->u.mqe.un.set_feature, 0);
5721 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5722 mbox->u.mqe.un.set_feature.param_len = 8;
5723 break;
65791f1f 5724 }
7bdedb34
JS
5725
5726 return;
65791f1f
JS
5727}
5728
6d368e53
JS
5729/**
5730 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5731 * @phba: Pointer to HBA context object.
5732 *
5733 * This function allocates all SLI4 resource identifiers.
5734 **/
5735int
5736lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5737{
5738 int i, rc, error = 0;
5739 uint16_t count, base;
5740 unsigned long longs;
5741
ff78d8f9
JS
5742 if (!phba->sli4_hba.rpi_hdrs_in_use)
5743 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6d368e53
JS
5744 if (phba->sli4_hba.extents_in_use) {
5745 /*
5746 * The port supports resource extents. The XRI, VPI, VFI, RPI
5747 * resource extent count must be read and allocated before
5748 * provisioning the resource id arrays.
5749 */
5750 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5751 LPFC_IDX_RSRC_RDY) {
5752 /*
5753 * Extent-based resources are set - the driver could
5754 * be in a port reset. Figure out if any corrective
5755 * actions need to be taken.
5756 */
5757 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5758 LPFC_RSC_TYPE_FCOE_VFI);
5759 if (rc != 0)
5760 error++;
5761 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5762 LPFC_RSC_TYPE_FCOE_VPI);
5763 if (rc != 0)
5764 error++;
5765 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5766 LPFC_RSC_TYPE_FCOE_XRI);
5767 if (rc != 0)
5768 error++;
5769 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5770 LPFC_RSC_TYPE_FCOE_RPI);
5771 if (rc != 0)
5772 error++;
5773
5774 /*
5775 * It's possible that the number of resources
5776 * provided to this port instance changed between
5777 * resets. Detect this condition and reallocate
5778 * resources. Otherwise, there is no action.
5779 */
5780 if (error) {
5781 lpfc_printf_log(phba, KERN_INFO,
5782 LOG_MBOX | LOG_INIT,
5783 "2931 Detected extent resource "
5784 "change. Reallocating all "
5785 "extents.\n");
5786 rc = lpfc_sli4_dealloc_extent(phba,
5787 LPFC_RSC_TYPE_FCOE_VFI);
5788 rc = lpfc_sli4_dealloc_extent(phba,
5789 LPFC_RSC_TYPE_FCOE_VPI);
5790 rc = lpfc_sli4_dealloc_extent(phba,
5791 LPFC_RSC_TYPE_FCOE_XRI);
5792 rc = lpfc_sli4_dealloc_extent(phba,
5793 LPFC_RSC_TYPE_FCOE_RPI);
5794 } else
5795 return 0;
5796 }
5797
5798 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5799 if (unlikely(rc))
5800 goto err_exit;
5801
5802 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5803 if (unlikely(rc))
5804 goto err_exit;
5805
5806 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5807 if (unlikely(rc))
5808 goto err_exit;
5809
5810 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5811 if (unlikely(rc))
5812 goto err_exit;
5813 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5814 LPFC_IDX_RSRC_RDY);
5815 return rc;
5816 } else {
5817 /*
5818 * The port does not support resource extents. The XRI, VPI,
5819 * VFI, RPI resource ids were determined from READ_CONFIG.
5820 * Just allocate the bitmasks and provision the resource id
5821 * arrays. If a port reset is active, the resources don't
5822 * need any action - just exit.
5823 */
5824 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
ff78d8f9
JS
5825 LPFC_IDX_RSRC_RDY) {
5826 lpfc_sli4_dealloc_resource_identifiers(phba);
5827 lpfc_sli4_remove_rpis(phba);
5828 }
6d368e53
JS
5829 /* RPIs. */
5830 count = phba->sli4_hba.max_cfg_param.max_rpi;
0a630c27
JS
5831 if (count <= 0) {
5832 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5833 "3279 Invalid provisioning of "
5834 "rpi:%d\n", count);
5835 rc = -EINVAL;
5836 goto err_exit;
5837 }
6d368e53
JS
5838 base = phba->sli4_hba.max_cfg_param.rpi_base;
5839 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5840 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5841 sizeof(unsigned long),
5842 GFP_KERNEL);
5843 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5844 rc = -ENOMEM;
5845 goto err_exit;
5846 }
5847 phba->sli4_hba.rpi_ids = kzalloc(count *
5848 sizeof(uint16_t),
5849 GFP_KERNEL);
5850 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5851 rc = -ENOMEM;
5852 goto free_rpi_bmask;
5853 }
5854
5855 for (i = 0; i < count; i++)
5856 phba->sli4_hba.rpi_ids[i] = base + i;
5857
5858 /* VPIs. */
5859 count = phba->sli4_hba.max_cfg_param.max_vpi;
0a630c27
JS
5860 if (count <= 0) {
5861 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5862 "3280 Invalid provisioning of "
5863 "vpi:%d\n", count);
5864 rc = -EINVAL;
5865 goto free_rpi_ids;
5866 }
6d368e53
JS
5867 base = phba->sli4_hba.max_cfg_param.vpi_base;
5868 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5869 phba->vpi_bmask = kzalloc(longs *
5870 sizeof(unsigned long),
5871 GFP_KERNEL);
5872 if (unlikely(!phba->vpi_bmask)) {
5873 rc = -ENOMEM;
5874 goto free_rpi_ids;
5875 }
5876 phba->vpi_ids = kzalloc(count *
5877 sizeof(uint16_t),
5878 GFP_KERNEL);
5879 if (unlikely(!phba->vpi_ids)) {
5880 rc = -ENOMEM;
5881 goto free_vpi_bmask;
5882 }
5883
5884 for (i = 0; i < count; i++)
5885 phba->vpi_ids[i] = base + i;
5886
5887 /* XRIs. */
5888 count = phba->sli4_hba.max_cfg_param.max_xri;
0a630c27
JS
5889 if (count <= 0) {
5890 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5891 "3281 Invalid provisioning of "
5892 "xri:%d\n", count);
5893 rc = -EINVAL;
5894 goto free_vpi_ids;
5895 }
6d368e53
JS
5896 base = phba->sli4_hba.max_cfg_param.xri_base;
5897 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5898 phba->sli4_hba.xri_bmask = kzalloc(longs *
5899 sizeof(unsigned long),
5900 GFP_KERNEL);
5901 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5902 rc = -ENOMEM;
5903 goto free_vpi_ids;
5904 }
41899be7 5905 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
5906 phba->sli4_hba.xri_ids = kzalloc(count *
5907 sizeof(uint16_t),
5908 GFP_KERNEL);
5909 if (unlikely(!phba->sli4_hba.xri_ids)) {
5910 rc = -ENOMEM;
5911 goto free_xri_bmask;
5912 }
5913
5914 for (i = 0; i < count; i++)
5915 phba->sli4_hba.xri_ids[i] = base + i;
5916
5917 /* VFIs. */
5918 count = phba->sli4_hba.max_cfg_param.max_vfi;
0a630c27
JS
5919 if (count <= 0) {
5920 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5921 "3282 Invalid provisioning of "
5922 "vfi:%d\n", count);
5923 rc = -EINVAL;
5924 goto free_xri_ids;
5925 }
6d368e53
JS
5926 base = phba->sli4_hba.max_cfg_param.vfi_base;
5927 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5928 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5929 sizeof(unsigned long),
5930 GFP_KERNEL);
5931 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5932 rc = -ENOMEM;
5933 goto free_xri_ids;
5934 }
5935 phba->sli4_hba.vfi_ids = kzalloc(count *
5936 sizeof(uint16_t),
5937 GFP_KERNEL);
5938 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5939 rc = -ENOMEM;
5940 goto free_vfi_bmask;
5941 }
5942
5943 for (i = 0; i < count; i++)
5944 phba->sli4_hba.vfi_ids[i] = base + i;
5945
5946 /*
5947 * Mark all resources ready. An HBA reset doesn't need
5948 * to reset the initialization.
5949 */
5950 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5951 LPFC_IDX_RSRC_RDY);
5952 return 0;
5953 }
5954
5955 free_vfi_bmask:
5956 kfree(phba->sli4_hba.vfi_bmask);
5957 free_xri_ids:
5958 kfree(phba->sli4_hba.xri_ids);
5959 free_xri_bmask:
5960 kfree(phba->sli4_hba.xri_bmask);
5961 free_vpi_ids:
5962 kfree(phba->vpi_ids);
5963 free_vpi_bmask:
5964 kfree(phba->vpi_bmask);
5965 free_rpi_ids:
5966 kfree(phba->sli4_hba.rpi_ids);
5967 free_rpi_bmask:
5968 kfree(phba->sli4_hba.rpi_bmask);
5969 err_exit:
5970 return rc;
5971}
5972
5973/**
5974 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5975 * @phba: Pointer to HBA context object.
5976 *
5977 * This function allocates the number of elements for the specified
5978 * resource type.
5979 **/
5980int
5981lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5982{
5983 if (phba->sli4_hba.extents_in_use) {
5984 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5985 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5986 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5987 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5988 } else {
5989 kfree(phba->vpi_bmask);
16a3a208 5990 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
5991 kfree(phba->vpi_ids);
5992 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5993 kfree(phba->sli4_hba.xri_bmask);
5994 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
5995 kfree(phba->sli4_hba.vfi_bmask);
5996 kfree(phba->sli4_hba.vfi_ids);
5997 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5998 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5999 }
6000
6001 return 0;
6002}
6003
b76f2dc9
JS
6004/**
6005 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6006 * @phba: Pointer to HBA context object.
6007 * @type: The resource extent type.
6008 * @extnt_count: buffer to hold port extent count response
6009 * @extnt_size: buffer to hold port extent size response.
6010 *
6011 * This function calls the port to read the host allocated extents
6012 * for a particular type.
6013 **/
6014int
6015lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6016 uint16_t *extnt_cnt, uint16_t *extnt_size)
6017{
6018 bool emb;
6019 int rc = 0;
6020 uint16_t curr_blks = 0;
6021 uint32_t req_len, emb_len;
6022 uint32_t alloc_len, mbox_tmo;
6023 struct list_head *blk_list_head;
6024 struct lpfc_rsrc_blks *rsrc_blk;
6025 LPFC_MBOXQ_t *mbox;
6026 void *virtaddr = NULL;
6027 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6028 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6029 union lpfc_sli4_cfg_shdr *shdr;
6030
6031 switch (type) {
6032 case LPFC_RSC_TYPE_FCOE_VPI:
6033 blk_list_head = &phba->lpfc_vpi_blk_list;
6034 break;
6035 case LPFC_RSC_TYPE_FCOE_XRI:
6036 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6037 break;
6038 case LPFC_RSC_TYPE_FCOE_VFI:
6039 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6040 break;
6041 case LPFC_RSC_TYPE_FCOE_RPI:
6042 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6043 break;
6044 default:
6045 return -EIO;
6046 }
6047
6048 /* Count the number of extents currently allocatd for this type. */
6049 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6050 if (curr_blks == 0) {
6051 /*
6052 * The GET_ALLOCATED mailbox does not return the size,
6053 * just the count. The size should be just the size
6054 * stored in the current allocated block and all sizes
6055 * for an extent type are the same so set the return
6056 * value now.
6057 */
6058 *extnt_size = rsrc_blk->rsrc_size;
6059 }
6060 curr_blks++;
6061 }
6062
b76f2dc9
JS
6063 /*
6064 * Calculate the size of an embedded mailbox. The uint32_t
6065 * accounts for extents-specific word.
6066 */
6067 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6068 sizeof(uint32_t);
6069
6070 /*
6071 * Presume the allocation and response will fit into an embedded
6072 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6073 */
6074 emb = LPFC_SLI4_MBX_EMBED;
6075 req_len = emb_len;
6076 if (req_len > emb_len) {
6077 req_len = curr_blks * sizeof(uint16_t) +
6078 sizeof(union lpfc_sli4_cfg_shdr) +
6079 sizeof(uint32_t);
6080 emb = LPFC_SLI4_MBX_NEMBED;
6081 }
6082
6083 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6084 if (!mbox)
6085 return -ENOMEM;
6086 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6087
6088 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6089 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6090 req_len, emb);
6091 if (alloc_len < req_len) {
6092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6093 "2983 Allocated DMA memory size (x%x) is "
6094 "less than the requested DMA memory "
6095 "size (x%x)\n", alloc_len, req_len);
6096 rc = -ENOMEM;
6097 goto err_exit;
6098 }
6099 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6100 if (unlikely(rc)) {
6101 rc = -EIO;
6102 goto err_exit;
6103 }
6104
6105 if (!phba->sli4_hba.intr_enable)
6106 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6107 else {
a183a15f 6108 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
b76f2dc9
JS
6109 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6110 }
6111
6112 if (unlikely(rc)) {
6113 rc = -EIO;
6114 goto err_exit;
6115 }
6116
6117 /*
6118 * Figure out where the response is located. Then get local pointers
6119 * to the response data. The port does not guarantee to respond to
6120 * all extents counts request so update the local variable with the
6121 * allocated count from the port.
6122 */
6123 if (emb == LPFC_SLI4_MBX_EMBED) {
6124 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6125 shdr = &rsrc_ext->header.cfg_shdr;
6126 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6127 } else {
6128 virtaddr = mbox->sge_array->addr[0];
6129 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6130 shdr = &n_rsrc->cfg_shdr;
6131 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6132 }
6133
6134 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6135 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6136 "2984 Failed to read allocated resources "
6137 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6138 type,
6139 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6140 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6141 rc = -EIO;
6142 goto err_exit;
6143 }
6144 err_exit:
6145 lpfc_sli4_mbox_cmd_free(phba, mbox);
6146 return rc;
6147}
6148
8a9d2e80
JS
6149/**
6150 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
6151 * @phba: pointer to lpfc hba data structure.
6152 *
6153 * This routine walks the list of els buffers that have been allocated and
6154 * repost them to the port by using SGL block post. This is needed after a
6155 * pci_function_reset/warm_start or start. It attempts to construct blocks
6156 * of els buffer sgls which contains contiguous xris and uses the non-embedded
6157 * SGL block post mailbox commands to post them to the port. For single els
6158 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6159 * mailbox command for posting.
6160 *
6161 * Returns: 0 = success, non-zero failure.
6162 **/
6163static int
6164lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
6165{
6166 struct lpfc_sglq *sglq_entry = NULL;
6167 struct lpfc_sglq *sglq_entry_next = NULL;
6168 struct lpfc_sglq *sglq_entry_first = NULL;
711ea882 6169 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
8a9d2e80 6170 int last_xritag = NO_XRI;
dafe8cea 6171 struct lpfc_sli_ring *pring;
8a9d2e80
JS
6172 LIST_HEAD(prep_sgl_list);
6173 LIST_HEAD(blck_sgl_list);
6174 LIST_HEAD(allc_sgl_list);
6175 LIST_HEAD(post_sgl_list);
6176 LIST_HEAD(free_sgl_list);
6177
dafe8cea 6178 pring = &phba->sli.ring[LPFC_ELS_RING];
38c20673 6179 spin_lock_irq(&phba->hbalock);
dafe8cea 6180 spin_lock(&pring->ring_lock);
8a9d2e80 6181 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
dafe8cea 6182 spin_unlock(&pring->ring_lock);
38c20673 6183 spin_unlock_irq(&phba->hbalock);
8a9d2e80 6184
711ea882 6185 total_cnt = phba->sli4_hba.els_xri_cnt;
8a9d2e80
JS
6186 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6187 &allc_sgl_list, list) {
6188 list_del_init(&sglq_entry->list);
6189 block_cnt++;
6190 if ((last_xritag != NO_XRI) &&
6191 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6192 /* a hole in xri block, form a sgl posting block */
6193 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6194 post_cnt = block_cnt - 1;
6195 /* prepare list for next posting block */
6196 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6197 block_cnt = 1;
6198 } else {
6199 /* prepare list for next posting block */
6200 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6201 /* enough sgls for non-embed sgl mbox command */
6202 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6203 list_splice_init(&prep_sgl_list,
6204 &blck_sgl_list);
6205 post_cnt = block_cnt;
6206 block_cnt = 0;
6207 }
6208 }
6209 num_posted++;
6210
6211 /* keep track of last sgl's xritag */
6212 last_xritag = sglq_entry->sli4_xritag;
6213
6214 /* end of repost sgl list condition for els buffers */
6215 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6216 if (post_cnt == 0) {
6217 list_splice_init(&prep_sgl_list,
6218 &blck_sgl_list);
6219 post_cnt = block_cnt;
6220 } else if (block_cnt == 1) {
6221 status = lpfc_sli4_post_sgl(phba,
6222 sglq_entry->phys, 0,
6223 sglq_entry->sli4_xritag);
6224 if (!status) {
6225 /* successful, put sgl to posted list */
6226 list_add_tail(&sglq_entry->list,
6227 &post_sgl_list);
6228 } else {
6229 /* Failure, put sgl to free list */
6230 lpfc_printf_log(phba, KERN_WARNING,
6231 LOG_SLI,
6232 "3159 Failed to post els "
6233 "sgl, xritag:x%x\n",
6234 sglq_entry->sli4_xritag);
6235 list_add_tail(&sglq_entry->list,
6236 &free_sgl_list);
711ea882 6237 total_cnt--;
8a9d2e80
JS
6238 }
6239 }
6240 }
6241
6242 /* continue until a nembed page worth of sgls */
6243 if (post_cnt == 0)
6244 continue;
6245
6246 /* post the els buffer list sgls as a block */
6247 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6248 post_cnt);
6249
6250 if (!status) {
6251 /* success, put sgl list to posted sgl list */
6252 list_splice_init(&blck_sgl_list, &post_sgl_list);
6253 } else {
6254 /* Failure, put sgl list to free sgl list */
6255 sglq_entry_first = list_first_entry(&blck_sgl_list,
6256 struct lpfc_sglq,
6257 list);
6258 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6259 "3160 Failed to post els sgl-list, "
6260 "xritag:x%x-x%x\n",
6261 sglq_entry_first->sli4_xritag,
6262 (sglq_entry_first->sli4_xritag +
6263 post_cnt - 1));
6264 list_splice_init(&blck_sgl_list, &free_sgl_list);
711ea882 6265 total_cnt -= post_cnt;
8a9d2e80
JS
6266 }
6267
6268 /* don't reset xirtag due to hole in xri block */
6269 if (block_cnt == 0)
6270 last_xritag = NO_XRI;
6271
6272 /* reset els sgl post count for next round of posting */
6273 post_cnt = 0;
6274 }
711ea882
JS
6275 /* update the number of XRIs posted for ELS */
6276 phba->sli4_hba.els_xri_cnt = total_cnt;
8a9d2e80
JS
6277
6278 /* free the els sgls failed to post */
6279 lpfc_free_sgl_list(phba, &free_sgl_list);
6280
6281 /* push els sgls posted to the availble list */
6282 if (!list_empty(&post_sgl_list)) {
38c20673 6283 spin_lock_irq(&phba->hbalock);
dafe8cea 6284 spin_lock(&pring->ring_lock);
8a9d2e80
JS
6285 list_splice_init(&post_sgl_list,
6286 &phba->sli4_hba.lpfc_sgl_list);
dafe8cea 6287 spin_unlock(&pring->ring_lock);
38c20673 6288 spin_unlock_irq(&phba->hbalock);
8a9d2e80
JS
6289 } else {
6290 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6291 "3161 Failure to post els sgl to port.\n");
6292 return -EIO;
6293 }
6294 return 0;
6295}
6296
61bda8f7
JS
6297void
6298lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6299{
6300 uint32_t len;
6301
6302 len = sizeof(struct lpfc_mbx_set_host_data) -
6303 sizeof(struct lpfc_sli4_cfg_mhdr);
6304 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6305 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6306 LPFC_SLI4_MBX_EMBED);
6307
6308 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
b2fd103b
JS
6309 mbox->u.mqe.un.set_host_data.param_len =
6310 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
61bda8f7
JS
6311 snprintf(mbox->u.mqe.un.set_host_data.data,
6312 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6313 "Linux %s v"LPFC_DRIVER_VERSION,
6314 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6315}
6316
da0436e9
JS
6317/**
6318 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6319 * @phba: Pointer to HBA context object.
6320 *
6321 * This function is the main SLI4 device intialization PCI function. This
6322 * function is called by the HBA intialization code, HBA reset code and
6323 * HBA error attention handler code. Caller is not required to hold any
6324 * locks.
6325 **/
6326int
6327lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6328{
6329 int rc;
6330 LPFC_MBOXQ_t *mboxq;
6331 struct lpfc_mqe *mqe;
6332 uint8_t *vpd;
6333 uint32_t vpd_size;
6334 uint32_t ftr_rsp = 0;
6335 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6336 struct lpfc_vport *vport = phba->pport;
6337 struct lpfc_dmabuf *mp;
6338
6339 /* Perform a PCI function reset to start from clean */
6340 rc = lpfc_pci_function_reset(phba);
6341 if (unlikely(rc))
6342 return -ENODEV;
6343
6344 /* Check the HBA Host Status Register for readyness */
6345 rc = lpfc_sli4_post_status_check(phba);
6346 if (unlikely(rc))
6347 return -ENODEV;
6348 else {
6349 spin_lock_irq(&phba->hbalock);
6350 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6351 spin_unlock_irq(&phba->hbalock);
6352 }
6353
6354 /*
6355 * Allocate a single mailbox container for initializing the
6356 * port.
6357 */
6358 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6359 if (!mboxq)
6360 return -ENOMEM;
6361
da0436e9 6362 /* Issue READ_REV to collect vpd and FW information. */
49198b37 6363 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
6364 vpd = kzalloc(vpd_size, GFP_KERNEL);
6365 if (!vpd) {
6366 rc = -ENOMEM;
6367 goto out_free_mbox;
6368 }
6369
6370 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
76a95d75
JS
6371 if (unlikely(rc)) {
6372 kfree(vpd);
6373 goto out_free_mbox;
6374 }
572709e2 6375
da0436e9 6376 mqe = &mboxq->u.mqe;
f1126688 6377 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
b5c53958 6378 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
76a95d75 6379 phba->hba_flag |= HBA_FCOE_MODE;
b5c53958
JS
6380 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6381 } else {
76a95d75 6382 phba->hba_flag &= ~HBA_FCOE_MODE;
b5c53958 6383 }
45ed1190
JS
6384
6385 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6386 LPFC_DCBX_CEE_MODE)
6387 phba->hba_flag |= HBA_FIP_SUPPORT;
6388 else
6389 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6390
4f2e66c6
JS
6391 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6392
c31098ce 6393 if (phba->sli_rev != LPFC_SLI_REV4) {
da0436e9
JS
6394 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6395 "0376 READ_REV Error. SLI Level %d "
6396 "FCoE enabled %d\n",
76a95d75 6397 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
da0436e9 6398 rc = -EIO;
76a95d75
JS
6399 kfree(vpd);
6400 goto out_free_mbox;
da0436e9 6401 }
cd1c8301 6402
ff78d8f9
JS
6403 /*
6404 * Continue initialization with default values even if driver failed
6405 * to read FCoE param config regions, only read parameters if the
6406 * board is FCoE
6407 */
6408 if (phba->hba_flag & HBA_FCOE_MODE &&
6409 lpfc_sli4_read_fcoe_params(phba))
6410 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6411 "2570 Failed to read FCoE parameters\n");
6412
cd1c8301
JS
6413 /*
6414 * Retrieve sli4 device physical port name, failure of doing it
6415 * is considered as non-fatal.
6416 */
6417 rc = lpfc_sli4_retrieve_pport_name(phba);
6418 if (!rc)
6419 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6420 "3080 Successful retrieving SLI4 device "
6421 "physical port name: %s.\n", phba->Port);
6422
da0436e9
JS
6423 /*
6424 * Evaluate the read rev and vpd data. Populate the driver
6425 * state with the results. If this routine fails, the failure
6426 * is not fatal as the driver will use generic values.
6427 */
6428 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6429 if (unlikely(!rc)) {
6430 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6431 "0377 Error %d parsing vpd. "
6432 "Using defaults.\n", rc);
6433 rc = 0;
6434 }
76a95d75 6435 kfree(vpd);
da0436e9 6436
f1126688
JS
6437 /* Save information as VPD data */
6438 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6439 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6440 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6441 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6442 &mqe->un.read_rev);
6443 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6444 &mqe->un.read_rev);
6445 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6446 &mqe->un.read_rev);
6447 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6448 &mqe->un.read_rev);
6449 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6450 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6451 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6452 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6453 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6454 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6455 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6456 "(%d):0380 READ_REV Status x%x "
6457 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6458 mboxq->vport ? mboxq->vport->vpi : 0,
6459 bf_get(lpfc_mqe_status, mqe),
6460 phba->vpd.rev.opFwName,
6461 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6462 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9 6463
572709e2
JS
6464 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6465 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6466 if (phba->pport->cfg_lun_queue_depth > rc) {
6467 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6468 "3362 LUN queue depth changed from %d to %d\n",
6469 phba->pport->cfg_lun_queue_depth, rc);
6470 phba->pport->cfg_lun_queue_depth = rc;
6471 }
6472
65791f1f 6473 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7bdedb34
JS
6474 LPFC_SLI_INTF_IF_TYPE_0) {
6475 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6476 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6477 if (rc == MBX_SUCCESS) {
6478 phba->hba_flag |= HBA_RECOVERABLE_UE;
6479 /* Set 1Sec interval to detect UE */
6480 phba->eratt_poll_interval = 1;
6481 phba->sli4_hba.ue_to_sr = bf_get(
6482 lpfc_mbx_set_feature_UESR,
6483 &mboxq->u.mqe.un.set_feature);
6484 phba->sli4_hba.ue_to_rp = bf_get(
6485 lpfc_mbx_set_feature_UERP,
6486 &mboxq->u.mqe.un.set_feature);
6487 }
6488 }
6489
6490 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6491 /* Enable MDS Diagnostics only if the SLI Port supports it */
6492 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6493 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6494 if (rc != MBX_SUCCESS)
6495 phba->mds_diags_support = 0;
6496 }
572709e2 6497
da0436e9
JS
6498 /*
6499 * Discover the port's supported feature set and match it against the
6500 * hosts requests.
6501 */
6502 lpfc_request_features(phba, mboxq);
6503 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6504 if (unlikely(rc)) {
6505 rc = -EIO;
76a95d75 6506 goto out_free_mbox;
da0436e9
JS
6507 }
6508
6509 /*
6510 * The port must support FCP initiator mode as this is the
6511 * only mode running in the host.
6512 */
6513 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6514 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6515 "0378 No support for fcpi mode.\n");
6516 ftr_rsp++;
6517 }
fedd3b7b
JS
6518 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6519 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6520 else
6521 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
da0436e9
JS
6522 /*
6523 * If the port cannot support the host's requested features
6524 * then turn off the global config parameters to disable the
6525 * feature in the driver. This is not a fatal error.
6526 */
bf08611b
JS
6527 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6528 if (phba->cfg_enable_bg) {
6529 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6530 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6531 else
6532 ftr_rsp++;
6533 }
da0436e9
JS
6534
6535 if (phba->max_vpi && phba->cfg_enable_npiv &&
6536 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6537 ftr_rsp++;
6538
6539 if (ftr_rsp) {
6540 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6541 "0379 Feature Mismatch Data: x%08x %08x "
6542 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6543 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6544 phba->cfg_enable_npiv, phba->max_vpi);
6545 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6546 phba->cfg_enable_bg = 0;
6547 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6548 phba->cfg_enable_npiv = 0;
6549 }
6550
6551 /* These SLI3 features are assumed in SLI4 */
6552 spin_lock_irq(&phba->hbalock);
6553 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6554 spin_unlock_irq(&phba->hbalock);
6555
6d368e53
JS
6556 /*
6557 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6558 * calls depends on these resources to complete port setup.
6559 */
6560 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6561 if (rc) {
6562 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6563 "2920 Failed to alloc Resource IDs "
6564 "rc = x%x\n", rc);
6565 goto out_free_mbox;
6566 }
6567
61bda8f7
JS
6568 lpfc_set_host_data(phba, mboxq);
6569
6570 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6571 if (rc) {
6572 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6573 "2134 Failed to set host os driver version %x",
6574 rc);
6575 }
6576
da0436e9 6577 /* Read the port's service parameters. */
9f1177a3
JS
6578 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6579 if (rc) {
6580 phba->link_state = LPFC_HBA_ERROR;
6581 rc = -ENOMEM;
76a95d75 6582 goto out_free_mbox;
9f1177a3
JS
6583 }
6584
da0436e9
JS
6585 mboxq->vport = vport;
6586 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6587 mp = (struct lpfc_dmabuf *) mboxq->context1;
6588 if (rc == MBX_SUCCESS) {
6589 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6590 rc = 0;
6591 }
6592
6593 /*
6594 * This memory was allocated by the lpfc_read_sparam routine. Release
6595 * it to the mbuf pool.
6596 */
6597 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6598 kfree(mp);
6599 mboxq->context1 = NULL;
6600 if (unlikely(rc)) {
6601 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6602 "0382 READ_SPARAM command failed "
6603 "status %d, mbxStatus x%x\n",
6604 rc, bf_get(lpfc_mqe_status, mqe));
6605 phba->link_state = LPFC_HBA_ERROR;
6606 rc = -EIO;
76a95d75 6607 goto out_free_mbox;
da0436e9
JS
6608 }
6609
0558056c 6610 lpfc_update_vport_wwn(vport);
da0436e9
JS
6611
6612 /* Update the fc_host data structures with new wwn. */
6613 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6614 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6615
8a9d2e80
JS
6616 /* update host els and scsi xri-sgl sizes and mappings */
6617 rc = lpfc_sli4_xri_sgl_update(phba);
6618 if (unlikely(rc)) {
6619 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6620 "1400 Failed to update xri-sgl size and "
6621 "mapping: %d\n", rc);
6622 goto out_free_mbox;
da0436e9
JS
6623 }
6624
8a9d2e80
JS
6625 /* register the els sgl pool to the port */
6626 rc = lpfc_sli4_repost_els_sgl_list(phba);
6627 if (unlikely(rc)) {
6628 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6629 "0582 Error %d during els sgl post "
6630 "operation\n", rc);
6631 rc = -ENODEV;
6632 goto out_free_mbox;
6633 }
6634
6635 /* register the allocated scsi sgl pool to the port */
da0436e9
JS
6636 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6637 if (unlikely(rc)) {
6d368e53 6638 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
6639 "0383 Error %d during scsi sgl post "
6640 "operation\n", rc);
da0436e9
JS
6641 /* Some Scsi buffers were moved to the abort scsi list */
6642 /* A pci function reset will repost them */
6643 rc = -ENODEV;
76a95d75 6644 goto out_free_mbox;
da0436e9
JS
6645 }
6646
6647 /* Post the rpi header region to the device. */
6648 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6649 if (unlikely(rc)) {
6650 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6651 "0393 Error %d during rpi post operation\n",
6652 rc);
6653 rc = -ENODEV;
76a95d75 6654 goto out_free_mbox;
da0436e9 6655 }
97f2ecf1 6656 lpfc_sli4_node_prep(phba);
da0436e9 6657
5350d872
JS
6658 /* Create all the SLI4 queues */
6659 rc = lpfc_sli4_queue_create(phba);
6660 if (rc) {
6661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6662 "3089 Failed to allocate queues\n");
6663 rc = -ENODEV;
6664 goto out_stop_timers;
6665 }
da0436e9
JS
6666 /* Set up all the queues to the device */
6667 rc = lpfc_sli4_queue_setup(phba);
6668 if (unlikely(rc)) {
6669 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6670 "0381 Error %d during queue setup.\n ", rc);
5350d872 6671 goto out_destroy_queue;
da0436e9
JS
6672 }
6673
6674 /* Arm the CQs and then EQs on device */
6675 lpfc_sli4_arm_cqeq_intr(phba);
6676
6677 /* Indicate device interrupt mode */
6678 phba->sli4_hba.intr_enable = 1;
6679
6680 /* Allow asynchronous mailbox command to go through */
6681 spin_lock_irq(&phba->hbalock);
6682 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6683 spin_unlock_irq(&phba->hbalock);
6684
6685 /* Post receive buffers to the device */
6686 lpfc_sli4_rb_setup(phba);
6687
fc2b989b
JS
6688 /* Reset HBA FCF states after HBA reset */
6689 phba->fcf.fcf_flag = 0;
6690 phba->fcf.current_rec.flag = 0;
6691
da0436e9 6692 /* Start the ELS watchdog timer */
8fa38513 6693 mod_timer(&vport->els_tmofunc,
256ec0d0 6694 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
da0436e9
JS
6695
6696 /* Start heart beat timer */
6697 mod_timer(&phba->hb_tmofunc,
256ec0d0 6698 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
da0436e9
JS
6699 phba->hb_outstanding = 0;
6700 phba->last_completion_time = jiffies;
6701
6702 /* Start error attention (ERATT) polling timer */
256ec0d0 6703 mod_timer(&phba->eratt_poll,
65791f1f 6704 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
da0436e9 6705
75baf696
JS
6706 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6707 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6708 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6709 if (!rc) {
6710 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6711 "2829 This device supports "
6712 "Advanced Error Reporting (AER)\n");
6713 spin_lock_irq(&phba->hbalock);
6714 phba->hba_flag |= HBA_AER_ENABLED;
6715 spin_unlock_irq(&phba->hbalock);
6716 } else {
6717 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6718 "2830 This device does not support "
6719 "Advanced Error Reporting (AER)\n");
6720 phba->cfg_aer_support = 0;
6721 }
0a96e975 6722 rc = 0;
75baf696
JS
6723 }
6724
76a95d75
JS
6725 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6726 /*
6727 * The FC Port needs to register FCFI (index 0)
6728 */
6729 lpfc_reg_fcfi(phba, mboxq);
6730 mboxq->vport = phba->pport;
6731 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9589b062 6732 if (rc != MBX_SUCCESS)
76a95d75 6733 goto out_unset_queue;
9589b062
JS
6734 rc = 0;
6735 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6736 &mboxq->u.mqe.un.reg_fcfi);
026abb87
JS
6737
6738 /* Check if the port is configured to be disabled */
6739 lpfc_sli_read_link_ste(phba);
76a95d75 6740 }
026abb87 6741
da0436e9
JS
6742 /*
6743 * The port is ready, set the host's link state to LINK_DOWN
6744 * in preparation for link interrupts.
6745 */
da0436e9
JS
6746 spin_lock_irq(&phba->hbalock);
6747 phba->link_state = LPFC_LINK_DOWN;
6748 spin_unlock_irq(&phba->hbalock);
026abb87
JS
6749 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6750 (phba->hba_flag & LINK_DISABLED)) {
6751 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6752 "3103 Adapter Link is disabled.\n");
6753 lpfc_down_link(phba, mboxq);
6754 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6755 if (rc != MBX_SUCCESS) {
6756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6757 "3104 Adapter failed to issue "
6758 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6759 goto out_unset_queue;
6760 }
6761 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
1b51197d
JS
6762 /* don't perform init_link on SLI4 FC port loopback test */
6763 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6764 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6765 if (rc)
6766 goto out_unset_queue;
6767 }
5350d872
JS
6768 }
6769 mempool_free(mboxq, phba->mbox_mem_pool);
6770 return rc;
76a95d75 6771out_unset_queue:
da0436e9 6772 /* Unset all the queues set up in this routine when error out */
5350d872
JS
6773 lpfc_sli4_queue_unset(phba);
6774out_destroy_queue:
6775 lpfc_sli4_queue_destroy(phba);
da0436e9 6776out_stop_timers:
5350d872 6777 lpfc_stop_hba_timers(phba);
da0436e9
JS
6778out_free_mbox:
6779 mempool_free(mboxq, phba->mbox_mem_pool);
6780 return rc;
6781}
6782
6783/**
6784 * lpfc_mbox_timeout - Timeout call back function for mbox timer
6785 * @ptr: context object - pointer to hba structure.
6786 *
6787 * This is the callback function for mailbox timer. The mailbox
6788 * timer is armed when a new mailbox command is issued and the timer
6789 * is deleted when the mailbox complete. The function is called by
6790 * the kernel timer code when a mailbox does not complete within
6791 * expected time. This function wakes up the worker thread to
6792 * process the mailbox timeout and returns. All the processing is
6793 * done by the worker thread function lpfc_mbox_timeout_handler.
6794 **/
6795void
6796lpfc_mbox_timeout(unsigned long ptr)
6797{
6798 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
6799 unsigned long iflag;
6800 uint32_t tmo_posted;
6801
6802 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6803 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6804 if (!tmo_posted)
6805 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6806 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6807
6808 if (!tmo_posted)
6809 lpfc_worker_wake_up(phba);
6810 return;
6811}
6812
e8d3c3b1
JS
6813/**
6814 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6815 * are pending
6816 * @phba: Pointer to HBA context object.
6817 *
6818 * This function checks if any mailbox completions are present on the mailbox
6819 * completion queue.
6820 **/
3bb11fc5 6821static bool
e8d3c3b1
JS
6822lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6823{
6824
6825 uint32_t idx;
6826 struct lpfc_queue *mcq;
6827 struct lpfc_mcqe *mcqe;
6828 bool pending_completions = false;
6829
6830 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6831 return false;
6832
6833 /* Check for completions on mailbox completion queue */
6834
6835 mcq = phba->sli4_hba.mbx_cq;
6836 idx = mcq->hba_index;
6837 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6838 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6839 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6840 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6841 pending_completions = true;
6842 break;
6843 }
6844 idx = (idx + 1) % mcq->entry_count;
6845 if (mcq->hba_index == idx)
6846 break;
6847 }
6848 return pending_completions;
6849
6850}
6851
6852/**
6853 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6854 * that were missed.
6855 * @phba: Pointer to HBA context object.
6856 *
6857 * For sli4, it is possible to miss an interrupt. As such mbox completions
6858 * maybe missed causing erroneous mailbox timeouts to occur. This function
6859 * checks to see if mbox completions are on the mailbox completion queue
6860 * and will process all the completions associated with the eq for the
6861 * mailbox completion queue.
6862 **/
6863bool
6864lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6865{
6866
6867 uint32_t eqidx;
6868 struct lpfc_queue *fpeq = NULL;
6869 struct lpfc_eqe *eqe;
6870 bool mbox_pending;
6871
6872 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6873 return false;
6874
6875 /* Find the eq associated with the mcq */
6876
6877 if (phba->sli4_hba.hba_eq)
6878 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6879 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6880 phba->sli4_hba.mbx_cq->assoc_qid) {
6881 fpeq = phba->sli4_hba.hba_eq[eqidx];
6882 break;
6883 }
6884 if (!fpeq)
6885 return false;
6886
6887 /* Turn off interrupts from this EQ */
6888
6889 lpfc_sli4_eq_clr_intr(fpeq);
6890
6891 /* Check to see if a mbox completion is pending */
6892
6893 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6894
6895 /*
6896 * If a mbox completion is pending, process all the events on EQ
6897 * associated with the mbox completion queue (this could include
6898 * mailbox commands, async events, els commands, receive queue data
6899 * and fcp commands)
6900 */
6901
6902 if (mbox_pending)
6903 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6904 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6905 fpeq->EQ_processed++;
6906 }
6907
6908 /* Always clear and re-arm the EQ */
6909
6910 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6911
6912 return mbox_pending;
6913
6914}
da0436e9
JS
6915
6916/**
6917 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6918 * @phba: Pointer to HBA context object.
6919 *
6920 * This function is called from worker thread when a mailbox command times out.
6921 * The caller is not required to hold any locks. This function will reset the
6922 * HBA and recover all the pending commands.
6923 **/
6924void
6925lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6926{
6927 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
eb016566
JS
6928 MAILBOX_t *mb = NULL;
6929
da0436e9 6930 struct lpfc_sli *psli = &phba->sli;
da0436e9 6931
e8d3c3b1
JS
6932 /* If the mailbox completed, process the completion and return */
6933 if (lpfc_sli4_process_missed_mbox_completions(phba))
6934 return;
6935
eb016566
JS
6936 if (pmbox != NULL)
6937 mb = &pmbox->u.mb;
da0436e9
JS
6938 /* Check the pmbox pointer first. There is a race condition
6939 * between the mbox timeout handler getting executed in the
6940 * worklist and the mailbox actually completing. When this
6941 * race condition occurs, the mbox_active will be NULL.
6942 */
6943 spin_lock_irq(&phba->hbalock);
6944 if (pmbox == NULL) {
6945 lpfc_printf_log(phba, KERN_WARNING,
6946 LOG_MBOX | LOG_SLI,
6947 "0353 Active Mailbox cleared - mailbox timeout "
6948 "exiting\n");
6949 spin_unlock_irq(&phba->hbalock);
6950 return;
6951 }
6952
6953 /* Mbox cmd <mbxCommand> timeout */
6954 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6955 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6956 mb->mbxCommand,
6957 phba->pport->port_state,
6958 phba->sli.sli_flag,
6959 phba->sli.mbox_active);
6960 spin_unlock_irq(&phba->hbalock);
6961
6962 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6963 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
25985edc 6964 * it to fail all outstanding SCSI IO.
da0436e9
JS
6965 */
6966 spin_lock_irq(&phba->pport->work_port_lock);
6967 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6968 spin_unlock_irq(&phba->pport->work_port_lock);
6969 spin_lock_irq(&phba->hbalock);
6970 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 6971 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
6972 spin_unlock_irq(&phba->hbalock);
6973
db55fba8 6974 lpfc_sli_abort_fcp_rings(phba);
da0436e9
JS
6975
6976 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6977 "0345 Resetting board due to mailbox timeout\n");
6978
6979 /* Reset the HBA device */
6980 lpfc_reset_hba(phba);
6981}
6982
6983/**
6984 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6985 * @phba: Pointer to HBA context object.
6986 * @pmbox: Pointer to mailbox object.
6987 * @flag: Flag indicating how the mailbox need to be processed.
6988 *
6989 * This function is called by discovery code and HBA management code
6990 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6991 * function gets the hbalock to protect the data structures.
6992 * The mailbox command can be submitted in polling mode, in which case
6993 * this function will wait in a polling loop for the completion of the
6994 * mailbox.
6995 * If the mailbox is submitted in no_wait mode (not polling) the
6996 * function will submit the command and returns immediately without waiting
6997 * for the mailbox completion. The no_wait is supported only when HBA
6998 * is in SLI2/SLI3 mode - interrupts are enabled.
6999 * The SLI interface allows only one mailbox pending at a time. If the
7000 * mailbox is issued in polling mode and there is already a mailbox
7001 * pending, then the function will return an error. If the mailbox is issued
7002 * in NO_WAIT mode and there is a mailbox pending already, the function
7003 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7004 * The sli layer owns the mailbox object until the completion of mailbox
7005 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7006 * return codes the caller owns the mailbox command after the return of
7007 * the function.
e59058c4 7008 **/
3772a991
JS
7009static int
7010lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7011 uint32_t flag)
dea3101e 7012{
bf07bdea 7013 MAILBOX_t *mbx;
2e0fef85 7014 struct lpfc_sli *psli = &phba->sli;
dea3101e 7015 uint32_t status, evtctr;
9940b97b 7016 uint32_t ha_copy, hc_copy;
dea3101e 7017 int i;
09372820 7018 unsigned long timeout;
dea3101e 7019 unsigned long drvr_flag = 0;
34b02dcd 7020 uint32_t word0, ldata;
dea3101e 7021 void __iomem *to_slim;
58da1ffb
JS
7022 int processing_queue = 0;
7023
7024 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7025 if (!pmbox) {
8568a4d2 7026 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 7027 /* processing mbox queue from intr_handler */
3772a991
JS
7028 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7029 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7030 return MBX_SUCCESS;
7031 }
58da1ffb 7032 processing_queue = 1;
58da1ffb
JS
7033 pmbox = lpfc_mbox_get(phba);
7034 if (!pmbox) {
7035 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7036 return MBX_SUCCESS;
7037 }
7038 }
dea3101e 7039
ed957684 7040 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 7041 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 7042 if(!pmbox->vport) {
58da1ffb 7043 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 7044 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 7045 LOG_MBOX | LOG_VPORT,
e8b62011 7046 "1806 Mbox x%x failed. No vport\n",
3772a991 7047 pmbox->u.mb.mbxCommand);
ed957684 7048 dump_stack();
58da1ffb 7049 goto out_not_finished;
ed957684
JS
7050 }
7051 }
7052
8d63f375 7053 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
7054 if (unlikely(pci_channel_offline(phba->pcidev))) {
7055 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7056 goto out_not_finished;
7057 }
8d63f375 7058
a257bf90
JS
7059 /* If HBA has a deferred error attention, fail the iocb. */
7060 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7061 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7062 goto out_not_finished;
7063 }
7064
dea3101e 7065 psli = &phba->sli;
92d7f7b0 7066
bf07bdea 7067 mbx = &pmbox->u.mb;
dea3101e
JB
7068 status = MBX_SUCCESS;
7069
2e0fef85
JS
7070 if (phba->link_state == LPFC_HBA_ERROR) {
7071 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
7072
7073 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7074 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7075 "(%d):0311 Mailbox command x%x cannot "
7076 "issue Data: x%x x%x\n",
7077 pmbox->vport ? pmbox->vport->vpi : 0,
7078 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 7079 goto out_not_finished;
41415862
JW
7080 }
7081
bf07bdea 7082 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9940b97b
JS
7083 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7084 !(hc_copy & HC_MBINT_ENA)) {
7085 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7086 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3772a991
JS
7087 "(%d):2528 Mailbox command x%x cannot "
7088 "issue Data: x%x x%x\n",
7089 pmbox->vport ? pmbox->vport->vpi : 0,
7090 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9940b97b
JS
7091 goto out_not_finished;
7092 }
9290831f
JS
7093 }
7094
dea3101e
JB
7095 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7096 /* Polling for a mbox command when another one is already active
7097 * is not allowed in SLI. Also, the driver must have established
7098 * SLI2 mode to queue and process multiple mbox commands.
7099 */
7100
7101 if (flag & MBX_POLL) {
2e0fef85 7102 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
7103
7104 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7105 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7106 "(%d):2529 Mailbox command x%x "
7107 "cannot issue Data: x%x x%x\n",
7108 pmbox->vport ? pmbox->vport->vpi : 0,
7109 pmbox->u.mb.mbxCommand,
7110 psli->sli_flag, flag);
58da1ffb 7111 goto out_not_finished;
dea3101e
JB
7112 }
7113
3772a991 7114 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 7115 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7116 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7117 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7118 "(%d):2530 Mailbox command x%x "
7119 "cannot issue Data: x%x x%x\n",
7120 pmbox->vport ? pmbox->vport->vpi : 0,
7121 pmbox->u.mb.mbxCommand,
7122 psli->sli_flag, flag);
58da1ffb 7123 goto out_not_finished;
dea3101e
JB
7124 }
7125
dea3101e
JB
7126 /* Another mailbox command is still being processed, queue this
7127 * command to be processed later.
7128 */
7129 lpfc_mbox_put(phba, pmbox);
7130
7131 /* Mbox cmd issue - BUSY */
ed957684 7132 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7133 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 7134 "x%x x%x x%x x%x\n",
92d7f7b0 7135 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
bf07bdea 7136 mbx->mbxCommand, phba->pport->port_state,
92d7f7b0 7137 psli->sli_flag, flag);
dea3101e
JB
7138
7139 psli->slistat.mbox_busy++;
2e0fef85 7140 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7141
858c9f6c
JS
7142 if (pmbox->vport) {
7143 lpfc_debugfs_disc_trc(pmbox->vport,
7144 LPFC_DISC_TRC_MBOX_VPORT,
7145 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7146 (uint32_t)mbx->mbxCommand,
7147 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7148 }
7149 else {
7150 lpfc_debugfs_disc_trc(phba->pport,
7151 LPFC_DISC_TRC_MBOX,
7152 "MBOX Bsy: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7153 (uint32_t)mbx->mbxCommand,
7154 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7155 }
7156
2e0fef85 7157 return MBX_BUSY;
dea3101e
JB
7158 }
7159
dea3101e
JB
7160 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7161
7162 /* If we are not polling, we MUST be in SLI2 mode */
7163 if (flag != MBX_POLL) {
3772a991 7164 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
bf07bdea 7165 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 7166 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7167 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7168 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7169 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7170 "(%d):2531 Mailbox command x%x "
7171 "cannot issue Data: x%x x%x\n",
7172 pmbox->vport ? pmbox->vport->vpi : 0,
7173 pmbox->u.mb.mbxCommand,
7174 psli->sli_flag, flag);
58da1ffb 7175 goto out_not_finished;
dea3101e
JB
7176 }
7177 /* timeout active mbox command */
256ec0d0
JS
7178 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7179 1000);
7180 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea3101e
JB
7181 }
7182
7183 /* Mailbox cmd <cmd> issue */
ed957684 7184 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7185 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 7186 "x%x\n",
e8b62011 7187 pmbox->vport ? pmbox->vport->vpi : 0,
bf07bdea 7188 mbx->mbxCommand, phba->pport->port_state,
92d7f7b0 7189 psli->sli_flag, flag);
dea3101e 7190
bf07bdea 7191 if (mbx->mbxCommand != MBX_HEARTBEAT) {
858c9f6c
JS
7192 if (pmbox->vport) {
7193 lpfc_debugfs_disc_trc(pmbox->vport,
7194 LPFC_DISC_TRC_MBOX_VPORT,
7195 "MBOX Send vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7196 (uint32_t)mbx->mbxCommand,
7197 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7198 }
7199 else {
7200 lpfc_debugfs_disc_trc(phba->pport,
7201 LPFC_DISC_TRC_MBOX,
7202 "MBOX Send: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7203 (uint32_t)mbx->mbxCommand,
7204 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7205 }
7206 }
7207
dea3101e
JB
7208 psli->slistat.mbox_cmd++;
7209 evtctr = psli->slistat.mbox_event;
7210
7211 /* next set own bit for the adapter and copy over command word */
bf07bdea 7212 mbx->mbxOwner = OWN_CHIP;
dea3101e 7213
3772a991 7214 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
7215 /* Populate mbox extension offset word. */
7216 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
bf07bdea 7217 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7218 = (uint8_t *)phba->mbox_ext
7219 - (uint8_t *)phba->mbox;
7220 }
7221
7222 /* Copy the mailbox extension data */
7223 if (pmbox->in_ext_byte_len && pmbox->context2) {
7224 lpfc_sli_pcimem_bcopy(pmbox->context2,
7225 (uint8_t *)phba->mbox_ext,
7226 pmbox->in_ext_byte_len);
7227 }
7228 /* Copy command data to host SLIM area */
bf07bdea 7229 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 7230 } else {
7a470277
JS
7231 /* Populate mbox extension offset word. */
7232 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
bf07bdea 7233 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7234 = MAILBOX_HBA_EXT_OFFSET;
7235
7236 /* Copy the mailbox extension data */
7237 if (pmbox->in_ext_byte_len && pmbox->context2) {
7238 lpfc_memcpy_to_slim(phba->MBslimaddr +
7239 MAILBOX_HBA_EXT_OFFSET,
7240 pmbox->context2, pmbox->in_ext_byte_len);
7241
7242 }
bf07bdea 7243 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 7244 /* copy command data into host mbox for cmpl */
bf07bdea 7245 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e
JB
7246 }
7247
7248 /* First copy mbox command data to HBA SLIM, skip past first
7249 word */
7250 to_slim = phba->MBslimaddr + sizeof (uint32_t);
bf07bdea 7251 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea3101e
JB
7252 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7253
7254 /* Next copy over first word, with mbxOwner set */
bf07bdea 7255 ldata = *((uint32_t *)mbx);
dea3101e
JB
7256 to_slim = phba->MBslimaddr;
7257 writel(ldata, to_slim);
7258 readl(to_slim); /* flush */
7259
bf07bdea 7260 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 7261 /* switch over to host mailbox */
3772a991 7262 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e
JB
7263 }
7264 }
7265
7266 wmb();
dea3101e
JB
7267
7268 switch (flag) {
7269 case MBX_NOWAIT:
09372820 7270 /* Set up reference to mailbox command */
dea3101e 7271 psli->mbox_active = pmbox;
09372820
JS
7272 /* Interrupt board to do it */
7273 writel(CA_MBATT, phba->CAregaddr);
7274 readl(phba->CAregaddr); /* flush */
7275 /* Don't wait for it to finish, just return */
dea3101e
JB
7276 break;
7277
7278 case MBX_POLL:
09372820 7279 /* Set up null reference to mailbox command */
dea3101e 7280 psli->mbox_active = NULL;
09372820
JS
7281 /* Interrupt board to do it */
7282 writel(CA_MBATT, phba->CAregaddr);
7283 readl(phba->CAregaddr); /* flush */
7284
3772a991 7285 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7286 /* First read mbox status word */
34b02dcd 7287 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
7288 word0 = le32_to_cpu(word0);
7289 } else {
7290 /* First read mbox status word */
9940b97b
JS
7291 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7292 spin_unlock_irqrestore(&phba->hbalock,
7293 drvr_flag);
7294 goto out_not_finished;
7295 }
dea3101e
JB
7296 }
7297
7298 /* Read the HBA Host Attention Register */
9940b97b
JS
7299 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7300 spin_unlock_irqrestore(&phba->hbalock,
7301 drvr_flag);
7302 goto out_not_finished;
7303 }
a183a15f
JS
7304 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7305 1000) + jiffies;
09372820 7306 i = 0;
dea3101e 7307 /* Wait for command to complete */
41415862
JW
7308 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7309 (!(ha_copy & HA_MBATT) &&
2e0fef85 7310 (phba->link_state > LPFC_WARM_START))) {
09372820 7311 if (time_after(jiffies, timeout)) {
dea3101e 7312 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7313 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 7314 drvr_flag);
58da1ffb 7315 goto out_not_finished;
dea3101e
JB
7316 }
7317
7318 /* Check if we took a mbox interrupt while we were
7319 polling */
7320 if (((word0 & OWN_CHIP) != OWN_CHIP)
7321 && (evtctr != psli->slistat.mbox_event))
7322 break;
7323
09372820
JS
7324 if (i++ > 10) {
7325 spin_unlock_irqrestore(&phba->hbalock,
7326 drvr_flag);
7327 msleep(1);
7328 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7329 }
dea3101e 7330
3772a991 7331 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7332 /* First copy command data */
34b02dcd 7333 word0 = *((uint32_t *)phba->mbox);
dea3101e 7334 word0 = le32_to_cpu(word0);
bf07bdea 7335 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 7336 MAILBOX_t *slimmb;
34b02dcd 7337 uint32_t slimword0;
dea3101e
JB
7338 /* Check real SLIM for any errors */
7339 slimword0 = readl(phba->MBslimaddr);
7340 slimmb = (MAILBOX_t *) & slimword0;
7341 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7342 && slimmb->mbxStatus) {
7343 psli->sli_flag &=
3772a991 7344 ~LPFC_SLI_ACTIVE;
dea3101e
JB
7345 word0 = slimword0;
7346 }
7347 }
7348 } else {
7349 /* First copy command data */
7350 word0 = readl(phba->MBslimaddr);
7351 }
7352 /* Read the HBA Host Attention Register */
9940b97b
JS
7353 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7354 spin_unlock_irqrestore(&phba->hbalock,
7355 drvr_flag);
7356 goto out_not_finished;
7357 }
dea3101e
JB
7358 }
7359
3772a991 7360 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7361 /* copy results back to user */
bf07bdea 7362 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
7a470277
JS
7363 /* Copy the mailbox extension data */
7364 if (pmbox->out_ext_byte_len && pmbox->context2) {
7365 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7366 pmbox->context2,
7367 pmbox->out_ext_byte_len);
7368 }
dea3101e
JB
7369 } else {
7370 /* First copy command data */
bf07bdea 7371 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea3101e 7372 MAILBOX_CMD_SIZE);
7a470277
JS
7373 /* Copy the mailbox extension data */
7374 if (pmbox->out_ext_byte_len && pmbox->context2) {
7375 lpfc_memcpy_from_slim(pmbox->context2,
7376 phba->MBslimaddr +
7377 MAILBOX_HBA_EXT_OFFSET,
7378 pmbox->out_ext_byte_len);
dea3101e
JB
7379 }
7380 }
7381
7382 writel(HA_MBATT, phba->HAregaddr);
7383 readl(phba->HAregaddr); /* flush */
7384
7385 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
bf07bdea 7386 status = mbx->mbxStatus;
dea3101e
JB
7387 }
7388
2e0fef85
JS
7389 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7390 return status;
58da1ffb
JS
7391
7392out_not_finished:
7393 if (processing_queue) {
da0436e9 7394 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
7395 lpfc_mbox_cmpl_put(phba, pmbox);
7396 }
7397 return MBX_NOT_FINISHED;
dea3101e
JB
7398}
7399
f1126688
JS
7400/**
7401 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7402 * @phba: Pointer to HBA context object.
7403 *
7404 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7405 * the driver internal pending mailbox queue. It will then try to wait out the
7406 * possible outstanding mailbox command before return.
7407 *
7408 * Returns:
7409 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7410 * the outstanding mailbox command timed out.
7411 **/
7412static int
7413lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7414{
7415 struct lpfc_sli *psli = &phba->sli;
f1126688 7416 int rc = 0;
a183a15f 7417 unsigned long timeout = 0;
f1126688
JS
7418
7419 /* Mark the asynchronous mailbox command posting as blocked */
7420 spin_lock_irq(&phba->hbalock);
7421 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
f1126688
JS
7422 /* Determine how long we might wait for the active mailbox
7423 * command to be gracefully completed by firmware.
7424 */
a183a15f
JS
7425 if (phba->sli.mbox_active)
7426 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7427 phba->sli.mbox_active) *
7428 1000) + jiffies;
7429 spin_unlock_irq(&phba->hbalock);
7430
e8d3c3b1
JS
7431 /* Make sure the mailbox is really active */
7432 if (timeout)
7433 lpfc_sli4_process_missed_mbox_completions(phba);
7434
f1126688
JS
7435 /* Wait for the outstnading mailbox command to complete */
7436 while (phba->sli.mbox_active) {
7437 /* Check active mailbox complete status every 2ms */
7438 msleep(2);
7439 if (time_after(jiffies, timeout)) {
7440 /* Timeout, marked the outstanding cmd not complete */
7441 rc = 1;
7442 break;
7443 }
7444 }
7445
7446 /* Can not cleanly block async mailbox command, fails it */
7447 if (rc) {
7448 spin_lock_irq(&phba->hbalock);
7449 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7450 spin_unlock_irq(&phba->hbalock);
7451 }
7452 return rc;
7453}
7454
7455/**
7456 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7457 * @phba: Pointer to HBA context object.
7458 *
7459 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7460 * commands from the driver internal pending mailbox queue. It makes sure
7461 * that there is no outstanding mailbox command before resuming posting
7462 * asynchronous mailbox commands. If, for any reason, there is outstanding
7463 * mailbox command, it will try to wait it out before resuming asynchronous
7464 * mailbox command posting.
7465 **/
7466static void
7467lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7468{
7469 struct lpfc_sli *psli = &phba->sli;
7470
7471 spin_lock_irq(&phba->hbalock);
7472 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7473 /* Asynchronous mailbox posting is not blocked, do nothing */
7474 spin_unlock_irq(&phba->hbalock);
7475 return;
7476 }
7477
7478 /* Outstanding synchronous mailbox command is guaranteed to be done,
7479 * successful or timeout, after timing-out the outstanding mailbox
7480 * command shall always be removed, so just unblock posting async
7481 * mailbox command and resume
7482 */
7483 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7484 spin_unlock_irq(&phba->hbalock);
7485
7486 /* wake up worker thread to post asynchronlous mailbox command */
7487 lpfc_worker_wake_up(phba);
7488}
7489
2d843edc
JS
7490/**
7491 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7492 * @phba: Pointer to HBA context object.
7493 * @mboxq: Pointer to mailbox object.
7494 *
7495 * The function waits for the bootstrap mailbox register ready bit from
7496 * port for twice the regular mailbox command timeout value.
7497 *
7498 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7499 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7500 **/
7501static int
7502lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7503{
7504 uint32_t db_ready;
7505 unsigned long timeout;
7506 struct lpfc_register bmbx_reg;
7507
7508 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7509 * 1000) + jiffies;
7510
7511 do {
7512 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7513 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7514 if (!db_ready)
7515 msleep(2);
7516
7517 if (time_after(jiffies, timeout))
7518 return MBXERR_ERROR;
7519 } while (!db_ready);
7520
7521 return 0;
7522}
7523
da0436e9
JS
7524/**
7525 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7526 * @phba: Pointer to HBA context object.
7527 * @mboxq: Pointer to mailbox object.
7528 *
7529 * The function posts a mailbox to the port. The mailbox is expected
7530 * to be comletely filled in and ready for the port to operate on it.
7531 * This routine executes a synchronous completion operation on the
7532 * mailbox by polling for its completion.
7533 *
7534 * The caller must not be holding any locks when calling this routine.
7535 *
7536 * Returns:
7537 * MBX_SUCCESS - mailbox posted successfully
7538 * Any of the MBX error values.
7539 **/
7540static int
7541lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7542{
7543 int rc = MBX_SUCCESS;
7544 unsigned long iflag;
da0436e9
JS
7545 uint32_t mcqe_status;
7546 uint32_t mbx_cmnd;
da0436e9
JS
7547 struct lpfc_sli *psli = &phba->sli;
7548 struct lpfc_mqe *mb = &mboxq->u.mqe;
7549 struct lpfc_bmbx_create *mbox_rgn;
7550 struct dma_address *dma_address;
da0436e9
JS
7551
7552 /*
7553 * Only one mailbox can be active to the bootstrap mailbox region
7554 * at a time and there is no queueing provided.
7555 */
7556 spin_lock_irqsave(&phba->hbalock, iflag);
7557 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7558 spin_unlock_irqrestore(&phba->hbalock, iflag);
7559 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7560 "(%d):2532 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7561 "cannot issue Data: x%x x%x\n",
7562 mboxq->vport ? mboxq->vport->vpi : 0,
7563 mboxq->u.mb.mbxCommand,
a183a15f
JS
7564 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7565 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7566 psli->sli_flag, MBX_POLL);
7567 return MBXERR_ERROR;
7568 }
7569 /* The server grabs the token and owns it until release */
7570 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7571 phba->sli.mbox_active = mboxq;
7572 spin_unlock_irqrestore(&phba->hbalock, iflag);
7573
2d843edc
JS
7574 /* wait for bootstrap mbox register for readyness */
7575 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7576 if (rc)
7577 goto exit;
7578
da0436e9
JS
7579 /*
7580 * Initialize the bootstrap memory region to avoid stale data areas
7581 * in the mailbox post. Then copy the caller's mailbox contents to
7582 * the bmbx mailbox region.
7583 */
7584 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7585 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7586 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7587 sizeof(struct lpfc_mqe));
7588
7589 /* Post the high mailbox dma address to the port and wait for ready. */
7590 dma_address = &phba->sli4_hba.bmbx.dma_address;
7591 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7592
2d843edc
JS
7593 /* wait for bootstrap mbox register for hi-address write done */
7594 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7595 if (rc)
7596 goto exit;
da0436e9
JS
7597
7598 /* Post the low mailbox dma address to the port. */
7599 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
da0436e9 7600
2d843edc
JS
7601 /* wait for bootstrap mbox register for low address write done */
7602 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7603 if (rc)
7604 goto exit;
da0436e9
JS
7605
7606 /*
7607 * Read the CQ to ensure the mailbox has completed.
7608 * If so, update the mailbox status so that the upper layers
7609 * can complete the request normally.
7610 */
7611 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7612 sizeof(struct lpfc_mqe));
7613 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7614 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7615 sizeof(struct lpfc_mcqe));
7616 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
0558056c
JS
7617 /*
7618 * When the CQE status indicates a failure and the mailbox status
7619 * indicates success then copy the CQE status into the mailbox status
7620 * (and prefix it with x4000).
7621 */
da0436e9 7622 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
0558056c
JS
7623 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7624 bf_set(lpfc_mqe_status, mb,
7625 (LPFC_MBX_ERROR_RANGE | mcqe_status));
da0436e9 7626 rc = MBXERR_ERROR;
d7c47992
JS
7627 } else
7628 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
7629
7630 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 7631 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
da0436e9
JS
7632 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7633 " x%x x%x CQ: x%x x%x x%x x%x\n",
a183a15f
JS
7634 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7635 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7636 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7637 bf_get(lpfc_mqe_status, mb),
7638 mb->un.mb_words[0], mb->un.mb_words[1],
7639 mb->un.mb_words[2], mb->un.mb_words[3],
7640 mb->un.mb_words[4], mb->un.mb_words[5],
7641 mb->un.mb_words[6], mb->un.mb_words[7],
7642 mb->un.mb_words[8], mb->un.mb_words[9],
7643 mb->un.mb_words[10], mb->un.mb_words[11],
7644 mb->un.mb_words[12], mboxq->mcqe.word0,
7645 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7646 mboxq->mcqe.trailer);
7647exit:
7648 /* We are holding the token, no needed for lock when release */
7649 spin_lock_irqsave(&phba->hbalock, iflag);
7650 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7651 phba->sli.mbox_active = NULL;
7652 spin_unlock_irqrestore(&phba->hbalock, iflag);
7653 return rc;
7654}
7655
7656/**
7657 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7658 * @phba: Pointer to HBA context object.
7659 * @pmbox: Pointer to mailbox object.
7660 * @flag: Flag indicating how the mailbox need to be processed.
7661 *
7662 * This function is called by discovery code and HBA management code to submit
7663 * a mailbox command to firmware with SLI-4 interface spec.
7664 *
7665 * Return codes the caller owns the mailbox command after the return of the
7666 * function.
7667 **/
7668static int
7669lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7670 uint32_t flag)
7671{
7672 struct lpfc_sli *psli = &phba->sli;
7673 unsigned long iflags;
7674 int rc;
7675
b76f2dc9
JS
7676 /* dump from issue mailbox command if setup */
7677 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7678
8fa38513
JS
7679 rc = lpfc_mbox_dev_check(phba);
7680 if (unlikely(rc)) {
7681 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7682 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8fa38513
JS
7683 "cannot issue Data: x%x x%x\n",
7684 mboxq->vport ? mboxq->vport->vpi : 0,
7685 mboxq->u.mb.mbxCommand,
a183a15f
JS
7686 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7687 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8fa38513
JS
7688 psli->sli_flag, flag);
7689 goto out_not_finished;
7690 }
7691
da0436e9
JS
7692 /* Detect polling mode and jump to a handler */
7693 if (!phba->sli4_hba.intr_enable) {
7694 if (flag == MBX_POLL)
7695 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7696 else
7697 rc = -EIO;
7698 if (rc != MBX_SUCCESS)
0558056c 7699 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
da0436e9 7700 "(%d):2541 Mailbox command x%x "
cc459f19
JS
7701 "(x%x/x%x) failure: "
7702 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7703 "Data: x%x x%x\n,",
da0436e9
JS
7704 mboxq->vport ? mboxq->vport->vpi : 0,
7705 mboxq->u.mb.mbxCommand,
a183a15f
JS
7706 lpfc_sli_config_mbox_subsys_get(phba,
7707 mboxq),
7708 lpfc_sli_config_mbox_opcode_get(phba,
7709 mboxq),
cc459f19
JS
7710 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7711 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7712 bf_get(lpfc_mcqe_ext_status,
7713 &mboxq->mcqe),
da0436e9
JS
7714 psli->sli_flag, flag);
7715 return rc;
7716 } else if (flag == MBX_POLL) {
f1126688
JS
7717 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7718 "(%d):2542 Try to issue mailbox command "
a183a15f 7719 "x%x (x%x/x%x) synchronously ahead of async"
f1126688 7720 "mailbox command queue: x%x x%x\n",
da0436e9
JS
7721 mboxq->vport ? mboxq->vport->vpi : 0,
7722 mboxq->u.mb.mbxCommand,
a183a15f
JS
7723 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7724 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9 7725 psli->sli_flag, flag);
f1126688
JS
7726 /* Try to block the asynchronous mailbox posting */
7727 rc = lpfc_sli4_async_mbox_block(phba);
7728 if (!rc) {
7729 /* Successfully blocked, now issue sync mbox cmd */
7730 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7731 if (rc != MBX_SUCCESS)
cc459f19 7732 lpfc_printf_log(phba, KERN_WARNING,
a183a15f 7733 LOG_MBOX | LOG_SLI,
cc459f19
JS
7734 "(%d):2597 Sync Mailbox command "
7735 "x%x (x%x/x%x) failure: "
7736 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7737 "Data: x%x x%x\n,",
7738 mboxq->vport ? mboxq->vport->vpi : 0,
a183a15f
JS
7739 mboxq->u.mb.mbxCommand,
7740 lpfc_sli_config_mbox_subsys_get(phba,
7741 mboxq),
7742 lpfc_sli_config_mbox_opcode_get(phba,
7743 mboxq),
cc459f19
JS
7744 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7745 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7746 bf_get(lpfc_mcqe_ext_status,
7747 &mboxq->mcqe),
a183a15f 7748 psli->sli_flag, flag);
f1126688
JS
7749 /* Unblock the async mailbox posting afterward */
7750 lpfc_sli4_async_mbox_unblock(phba);
7751 }
7752 return rc;
da0436e9
JS
7753 }
7754
7755 /* Now, interrupt mode asynchrous mailbox command */
7756 rc = lpfc_mbox_cmd_check(phba, mboxq);
7757 if (rc) {
7758 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7759 "(%d):2543 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7760 "cannot issue Data: x%x x%x\n",
7761 mboxq->vport ? mboxq->vport->vpi : 0,
7762 mboxq->u.mb.mbxCommand,
a183a15f
JS
7763 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7764 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7765 psli->sli_flag, flag);
7766 goto out_not_finished;
7767 }
da0436e9
JS
7768
7769 /* Put the mailbox command to the driver internal FIFO */
7770 psli->slistat.mbox_busy++;
7771 spin_lock_irqsave(&phba->hbalock, iflags);
7772 lpfc_mbox_put(phba, mboxq);
7773 spin_unlock_irqrestore(&phba->hbalock, iflags);
7774 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7775 "(%d):0354 Mbox cmd issue - Enqueue Data: "
a183a15f 7776 "x%x (x%x/x%x) x%x x%x x%x\n",
da0436e9
JS
7777 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7778 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
a183a15f
JS
7779 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7780 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7781 phba->pport->port_state,
7782 psli->sli_flag, MBX_NOWAIT);
7783 /* Wake up worker thread to transport mailbox command from head */
7784 lpfc_worker_wake_up(phba);
7785
7786 return MBX_BUSY;
7787
7788out_not_finished:
7789 return MBX_NOT_FINISHED;
7790}
7791
7792/**
7793 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7794 * @phba: Pointer to HBA context object.
7795 *
7796 * This function is called by worker thread to send a mailbox command to
7797 * SLI4 HBA firmware.
7798 *
7799 **/
7800int
7801lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7802{
7803 struct lpfc_sli *psli = &phba->sli;
7804 LPFC_MBOXQ_t *mboxq;
7805 int rc = MBX_SUCCESS;
7806 unsigned long iflags;
7807 struct lpfc_mqe *mqe;
7808 uint32_t mbx_cmnd;
7809
7810 /* Check interrupt mode before post async mailbox command */
7811 if (unlikely(!phba->sli4_hba.intr_enable))
7812 return MBX_NOT_FINISHED;
7813
7814 /* Check for mailbox command service token */
7815 spin_lock_irqsave(&phba->hbalock, iflags);
7816 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7817 spin_unlock_irqrestore(&phba->hbalock, iflags);
7818 return MBX_NOT_FINISHED;
7819 }
7820 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7821 spin_unlock_irqrestore(&phba->hbalock, iflags);
7822 return MBX_NOT_FINISHED;
7823 }
7824 if (unlikely(phba->sli.mbox_active)) {
7825 spin_unlock_irqrestore(&phba->hbalock, iflags);
7826 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7827 "0384 There is pending active mailbox cmd\n");
7828 return MBX_NOT_FINISHED;
7829 }
7830 /* Take the mailbox command service token */
7831 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7832
7833 /* Get the next mailbox command from head of queue */
7834 mboxq = lpfc_mbox_get(phba);
7835
7836 /* If no more mailbox command waiting for post, we're done */
7837 if (!mboxq) {
7838 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7839 spin_unlock_irqrestore(&phba->hbalock, iflags);
7840 return MBX_SUCCESS;
7841 }
7842 phba->sli.mbox_active = mboxq;
7843 spin_unlock_irqrestore(&phba->hbalock, iflags);
7844
7845 /* Check device readiness for posting mailbox command */
7846 rc = lpfc_mbox_dev_check(phba);
7847 if (unlikely(rc))
7848 /* Driver clean routine will clean up pending mailbox */
7849 goto out_not_finished;
7850
7851 /* Prepare the mbox command to be posted */
7852 mqe = &mboxq->u.mqe;
7853 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7854
7855 /* Start timer for the mbox_tmo and log some mailbox post messages */
7856 mod_timer(&psli->mbox_tmo, (jiffies +
256ec0d0 7857 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
da0436e9
JS
7858
7859 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 7860 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
da0436e9
JS
7861 "x%x x%x\n",
7862 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
a183a15f
JS
7863 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7864 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7865 phba->pport->port_state, psli->sli_flag);
7866
7867 if (mbx_cmnd != MBX_HEARTBEAT) {
7868 if (mboxq->vport) {
7869 lpfc_debugfs_disc_trc(mboxq->vport,
7870 LPFC_DISC_TRC_MBOX_VPORT,
7871 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7872 mbx_cmnd, mqe->un.mb_words[0],
7873 mqe->un.mb_words[1]);
7874 } else {
7875 lpfc_debugfs_disc_trc(phba->pport,
7876 LPFC_DISC_TRC_MBOX,
7877 "MBOX Send: cmd:x%x mb:x%x x%x",
7878 mbx_cmnd, mqe->un.mb_words[0],
7879 mqe->un.mb_words[1]);
7880 }
7881 }
7882 psli->slistat.mbox_cmd++;
7883
7884 /* Post the mailbox command to the port */
7885 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7886 if (rc != MBX_SUCCESS) {
7887 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7888 "(%d):2533 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7889 "cannot issue Data: x%x x%x\n",
7890 mboxq->vport ? mboxq->vport->vpi : 0,
7891 mboxq->u.mb.mbxCommand,
a183a15f
JS
7892 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7893 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7894 psli->sli_flag, MBX_NOWAIT);
7895 goto out_not_finished;
7896 }
7897
7898 return rc;
7899
7900out_not_finished:
7901 spin_lock_irqsave(&phba->hbalock, iflags);
d7069f09
JS
7902 if (phba->sli.mbox_active) {
7903 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7904 __lpfc_mbox_cmpl_put(phba, mboxq);
7905 /* Release the token */
7906 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7907 phba->sli.mbox_active = NULL;
7908 }
da0436e9
JS
7909 spin_unlock_irqrestore(&phba->hbalock, iflags);
7910
7911 return MBX_NOT_FINISHED;
7912}
7913
7914/**
7915 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7916 * @phba: Pointer to HBA context object.
7917 * @pmbox: Pointer to mailbox object.
7918 * @flag: Flag indicating how the mailbox need to be processed.
7919 *
7920 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7921 * the API jump table function pointer from the lpfc_hba struct.
7922 *
7923 * Return codes the caller owns the mailbox command after the return of the
7924 * function.
7925 **/
7926int
7927lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7928{
7929 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7930}
7931
7932/**
25985edc 7933 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
da0436e9
JS
7934 * @phba: The hba struct for which this call is being executed.
7935 * @dev_grp: The HBA PCI-Device group number.
7936 *
7937 * This routine sets up the mbox interface API function jump table in @phba
7938 * struct.
7939 * Returns: 0 - success, -ENODEV - failure.
7940 **/
7941int
7942lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7943{
7944
7945 switch (dev_grp) {
7946 case LPFC_PCI_DEV_LP:
7947 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7948 phba->lpfc_sli_handle_slow_ring_event =
7949 lpfc_sli_handle_slow_ring_event_s3;
7950 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7951 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7952 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7953 break;
7954 case LPFC_PCI_DEV_OC:
7955 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7956 phba->lpfc_sli_handle_slow_ring_event =
7957 lpfc_sli_handle_slow_ring_event_s4;
7958 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7959 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7960 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7961 break;
7962 default:
7963 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7964 "1420 Invalid HBA PCI-device group: 0x%x\n",
7965 dev_grp);
7966 return -ENODEV;
7967 break;
7968 }
7969 return 0;
7970}
7971
e59058c4 7972/**
3621a710 7973 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
7974 * @phba: Pointer to HBA context object.
7975 * @pring: Pointer to driver SLI ring object.
7976 * @piocb: Pointer to address of newly added command iocb.
7977 *
7978 * This function is called with hbalock held to add a command
7979 * iocb to the txq when SLI layer cannot submit the command iocb
7980 * to the ring.
7981 **/
2a9bf3d0 7982void
92d7f7b0 7983__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 7984 struct lpfc_iocbq *piocb)
dea3101e 7985{
1c2ba475 7986 lockdep_assert_held(&phba->hbalock);
dea3101e
JB
7987 /* Insert the caller's iocb in the txq tail for later processing. */
7988 list_add_tail(&piocb->list, &pring->txq);
dea3101e
JB
7989}
7990
e59058c4 7991/**
3621a710 7992 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
7993 * @phba: Pointer to HBA context object.
7994 * @pring: Pointer to driver SLI ring object.
7995 * @piocb: Pointer to address of newly added command iocb.
7996 *
7997 * This function is called with hbalock held before a new
7998 * iocb is submitted to the firmware. This function checks
7999 * txq to flush the iocbs in txq to Firmware before
8000 * submitting new iocbs to the Firmware.
8001 * If there are iocbs in the txq which need to be submitted
8002 * to firmware, lpfc_sli_next_iocb returns the first element
8003 * of the txq after dequeuing it from txq.
8004 * If there is no iocb in the txq then the function will return
8005 * *piocb and *piocb is set to NULL. Caller needs to check
8006 * *piocb to find if there are more commands in the txq.
8007 **/
dea3101e
JB
8008static struct lpfc_iocbq *
8009lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8010 struct lpfc_iocbq **piocb)
dea3101e
JB
8011{
8012 struct lpfc_iocbq * nextiocb;
8013
1c2ba475
JT
8014 lockdep_assert_held(&phba->hbalock);
8015
dea3101e
JB
8016 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8017 if (!nextiocb) {
8018 nextiocb = *piocb;
8019 *piocb = NULL;
8020 }
8021
8022 return nextiocb;
8023}
8024
e59058c4 8025/**
3772a991 8026 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 8027 * @phba: Pointer to HBA context object.
3772a991 8028 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
8029 * @piocb: Pointer to command iocb.
8030 * @flag: Flag indicating if this command can be put into txq.
8031 *
3772a991
JS
8032 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8033 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8034 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8035 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8036 * this function allows only iocbs for posting buffers. This function finds
8037 * next available slot in the command ring and posts the command to the
8038 * available slot and writes the port attention register to request HBA start
8039 * processing new iocb. If there is no slot available in the ring and
8040 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8041 * the function returns IOCB_BUSY.
e59058c4 8042 *
3772a991
JS
8043 * This function is called with hbalock held. The function will return success
8044 * after it successfully submit the iocb to firmware or after adding to the
8045 * txq.
e59058c4 8046 **/
98c9ea5c 8047static int
3772a991 8048__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e
JB
8049 struct lpfc_iocbq *piocb, uint32_t flag)
8050{
8051 struct lpfc_iocbq *nextiocb;
8052 IOCB_t *iocb;
3772a991 8053 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea3101e 8054
1c2ba475
JT
8055 lockdep_assert_held(&phba->hbalock);
8056
92d7f7b0
JS
8057 if (piocb->iocb_cmpl && (!piocb->vport) &&
8058 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8059 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8060 lpfc_printf_log(phba, KERN_ERR,
8061 LOG_SLI | LOG_VPORT,
e8b62011 8062 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
8063 piocb->iocb.ulpCommand);
8064 dump_stack();
8065 return IOCB_ERROR;
8066 }
8067
8068
8d63f375
LV
8069 /* If the PCI channel is in offline state, do not post iocbs. */
8070 if (unlikely(pci_channel_offline(phba->pcidev)))
8071 return IOCB_ERROR;
8072
a257bf90
JS
8073 /* If HBA has a deferred error attention, fail the iocb. */
8074 if (unlikely(phba->hba_flag & DEFER_ERATT))
8075 return IOCB_ERROR;
8076
dea3101e
JB
8077 /*
8078 * We should never get an IOCB if we are in a < LINK_DOWN state
8079 */
2e0fef85 8080 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e
JB
8081 return IOCB_ERROR;
8082
8083 /*
8084 * Check to see if we are blocking IOCB processing because of a
0b727fea 8085 * outstanding event.
dea3101e 8086 */
0b727fea 8087 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e
JB
8088 goto iocb_busy;
8089
2e0fef85 8090 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 8091 /*
2680eeaa 8092 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e
JB
8093 * can be issued if the link is not up.
8094 */
8095 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
8096 case CMD_GEN_REQUEST64_CR:
8097 case CMD_GEN_REQUEST64_CX:
8098 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8099 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 8100 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
8101 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8102 MENLO_TRANSPORT_TYPE))
8103
8104 goto iocb_busy;
8105 break;
dea3101e
JB
8106 case CMD_QUE_RING_BUF_CN:
8107 case CMD_QUE_RING_BUF64_CN:
dea3101e
JB
8108 /*
8109 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8110 * completion, iocb_cmpl MUST be 0.
8111 */
8112 if (piocb->iocb_cmpl)
8113 piocb->iocb_cmpl = NULL;
8114 /*FALLTHROUGH*/
8115 case CMD_CREATE_XRI_CR:
2680eeaa
JS
8116 case CMD_CLOSE_XRI_CN:
8117 case CMD_CLOSE_XRI_CX:
dea3101e
JB
8118 break;
8119 default:
8120 goto iocb_busy;
8121 }
8122
8123 /*
8124 * For FCP commands, we must be in a state where we can process link
8125 * attention events.
8126 */
8127 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
92d7f7b0 8128 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 8129 goto iocb_busy;
92d7f7b0 8130 }
dea3101e 8131
dea3101e
JB
8132 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8133 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8134 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8135
8136 if (iocb)
8137 lpfc_sli_update_ring(phba, pring);
8138 else
8139 lpfc_sli_update_full_ring(phba, pring);
8140
8141 if (!piocb)
8142 return IOCB_SUCCESS;
8143
8144 goto out_busy;
8145
8146 iocb_busy:
8147 pring->stats.iocb_cmd_delay++;
8148
8149 out_busy:
8150
8151 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 8152 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e
JB
8153 return IOCB_SUCCESS;
8154 }
8155
8156 return IOCB_BUSY;
8157}
8158
3772a991 8159/**
4f774513
JS
8160 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8161 * @phba: Pointer to HBA context object.
8162 * @piocb: Pointer to command iocb.
8163 * @sglq: Pointer to the scatter gather queue object.
8164 *
8165 * This routine converts the bpl or bde that is in the IOCB
8166 * to a sgl list for the sli4 hardware. The physical address
8167 * of the bpl/bde is converted back to a virtual address.
8168 * If the IOCB contains a BPL then the list of BDE's is
8169 * converted to sli4_sge's. If the IOCB contains a single
8170 * BDE then it is converted to a single sli_sge.
8171 * The IOCB is still in cpu endianess so the contents of
8172 * the bpl can be used without byte swapping.
8173 *
8174 * Returns valid XRI = Success, NO_XRI = Failure.
8175**/
8176static uint16_t
8177lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8178 struct lpfc_sglq *sglq)
3772a991 8179{
4f774513
JS
8180 uint16_t xritag = NO_XRI;
8181 struct ulp_bde64 *bpl = NULL;
8182 struct ulp_bde64 bde;
8183 struct sli4_sge *sgl = NULL;
1b51197d 8184 struct lpfc_dmabuf *dmabuf;
4f774513
JS
8185 IOCB_t *icmd;
8186 int numBdes = 0;
8187 int i = 0;
63e801ce
JS
8188 uint32_t offset = 0; /* accumulated offset in the sg request list */
8189 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 8190
4f774513
JS
8191 if (!piocbq || !sglq)
8192 return xritag;
8193
8194 sgl = (struct sli4_sge *)sglq->sgl;
8195 icmd = &piocbq->iocb;
6b5151fd
JS
8196 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8197 return sglq->sli4_xritag;
4f774513
JS
8198 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8199 numBdes = icmd->un.genreq64.bdl.bdeSize /
8200 sizeof(struct ulp_bde64);
8201 /* The addrHigh and addrLow fields within the IOCB
8202 * have not been byteswapped yet so there is no
8203 * need to swap them back.
8204 */
1b51197d
JS
8205 if (piocbq->context3)
8206 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8207 else
8208 return xritag;
4f774513 8209
1b51197d 8210 bpl = (struct ulp_bde64 *)dmabuf->virt;
4f774513
JS
8211 if (!bpl)
8212 return xritag;
8213
8214 for (i = 0; i < numBdes; i++) {
8215 /* Should already be byte swapped. */
28baac74
JS
8216 sgl->addr_hi = bpl->addrHigh;
8217 sgl->addr_lo = bpl->addrLow;
8218
0558056c 8219 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8220 if ((i+1) == numBdes)
8221 bf_set(lpfc_sli4_sge_last, sgl, 1);
8222 else
8223 bf_set(lpfc_sli4_sge_last, sgl, 0);
28baac74
JS
8224 /* swap the size field back to the cpu so we
8225 * can assign it to the sgl.
8226 */
8227 bde.tus.w = le32_to_cpu(bpl->tus.w);
8228 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
8229 /* The offsets in the sgl need to be accumulated
8230 * separately for the request and reply lists.
8231 * The request is always first, the reply follows.
8232 */
8233 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8234 /* add up the reply sg entries */
8235 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8236 inbound++;
8237 /* first inbound? reset the offset */
8238 if (inbound == 1)
8239 offset = 0;
8240 bf_set(lpfc_sli4_sge_offset, sgl, offset);
f9bb2da1
JS
8241 bf_set(lpfc_sli4_sge_type, sgl,
8242 LPFC_SGE_TYPE_DATA);
63e801ce
JS
8243 offset += bde.tus.f.bdeSize;
8244 }
546fc854 8245 sgl->word2 = cpu_to_le32(sgl->word2);
4f774513
JS
8246 bpl++;
8247 sgl++;
8248 }
8249 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8250 /* The addrHigh and addrLow fields of the BDE have not
8251 * been byteswapped yet so they need to be swapped
8252 * before putting them in the sgl.
8253 */
8254 sgl->addr_hi =
8255 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8256 sgl->addr_lo =
8257 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
0558056c 8258 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8259 bf_set(lpfc_sli4_sge_last, sgl, 1);
8260 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
8261 sgl->sge_len =
8262 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
8263 }
8264 return sglq->sli4_xritag;
3772a991 8265}
92d7f7b0 8266
e59058c4 8267/**
4f774513 8268 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 8269 * @phba: Pointer to HBA context object.
4f774513
JS
8270 * @piocb: Pointer to command iocb.
8271 * @wqe: Pointer to the work queue entry.
e59058c4 8272 *
4f774513
JS
8273 * This routine converts the iocb command to its Work Queue Entry
8274 * equivalent. The wqe pointer should not have any fields set when
8275 * this routine is called because it will memcpy over them.
8276 * This routine does not set the CQ_ID or the WQEC bits in the
8277 * wqe.
e59058c4 8278 *
4f774513 8279 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 8280 **/
cf5bf97e 8281static int
4f774513
JS
8282lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8283 union lpfc_wqe *wqe)
cf5bf97e 8284{
5ffc266e 8285 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
8286 uint8_t ct = 0;
8287 uint32_t fip;
8288 uint32_t abort_tag;
8289 uint8_t command_type = ELS_COMMAND_NON_FIP;
8290 uint8_t cmnd;
8291 uint16_t xritag;
dcf2a4e0
JS
8292 uint16_t abrt_iotag;
8293 struct lpfc_iocbq *abrtiocbq;
4f774513 8294 struct ulp_bde64 *bpl = NULL;
f0d9bccc 8295 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
8296 int numBdes, i;
8297 struct ulp_bde64 bde;
c31098ce 8298 struct lpfc_nodelist *ndlp;
ff78d8f9 8299 uint32_t *pcmd;
1b51197d 8300 uint32_t if_type;
4f774513 8301
45ed1190 8302 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 8303 /* The fcp commands will set command type */
0c287589 8304 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 8305 command_type = FCP_COMMAND;
c868595d 8306 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
8307 command_type = ELS_COMMAND_FIP;
8308 else
8309 command_type = ELS_COMMAND_NON_FIP;
8310
b5c53958
JS
8311 if (phba->fcp_embed_io)
8312 memset(wqe, 0, sizeof(union lpfc_wqe128));
4f774513
JS
8313 /* Some of the fields are in the right position already */
8314 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
f0d9bccc 8315 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
28d7f3df 8316 wqe->generic.wqe_com.word10 = 0;
b5c53958
JS
8317
8318 abort_tag = (uint32_t) iocbq->iotag;
8319 xritag = iocbq->sli4_xritag;
4f774513
JS
8320 /* words0-2 bpl convert bde */
8321 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
8322 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8323 sizeof(struct ulp_bde64);
4f774513
JS
8324 bpl = (struct ulp_bde64 *)
8325 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8326 if (!bpl)
8327 return IOCB_ERROR;
cf5bf97e 8328
4f774513
JS
8329 /* Should already be byte swapped. */
8330 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8331 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8332 /* swap the size field back to the cpu so we
8333 * can assign it to the sgl.
8334 */
8335 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
8336 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8337 total_len = 0;
8338 for (i = 0; i < numBdes; i++) {
8339 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8340 total_len += bde.tus.f.bdeSize;
8341 }
4f774513 8342 } else
5ffc266e 8343 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 8344
4f774513
JS
8345 iocbq->iocb.ulpIoTag = iocbq->iotag;
8346 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 8347
4f774513
JS
8348 switch (iocbq->iocb.ulpCommand) {
8349 case CMD_ELS_REQUEST64_CR:
93d1379e
JS
8350 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8351 ndlp = iocbq->context_un.ndlp;
8352 else
8353 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513
JS
8354 if (!iocbq->iocb.ulpLe) {
8355 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8356 "2007 Only Limited Edition cmd Format"
8357 " supported 0x%x\n",
8358 iocbq->iocb.ulpCommand);
8359 return IOCB_ERROR;
8360 }
ff78d8f9 8361
5ffc266e 8362 wqe->els_req.payload_len = xmit_len;
4f774513
JS
8363 /* Els_reguest64 has a TMO */
8364 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8365 iocbq->iocb.ulpTimeout);
8366 /* Need a VF for word 4 set the vf bit*/
8367 bf_set(els_req64_vf, &wqe->els_req, 0);
8368 /* And a VFID for word 12 */
8369 bf_set(els_req64_vfid, &wqe->els_req, 0);
4f774513 8370 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
8371 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8372 iocbq->iocb.ulpContext);
8373 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8374 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 8375 /* CCP CCPE PV PRI in word10 were set in the memcpy */
ff78d8f9 8376 if (command_type == ELS_COMMAND_FIP)
c868595d
JS
8377 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8378 >> LPFC_FIP_ELS_ID_SHIFT);
ff78d8f9
JS
8379 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8380 iocbq->context2)->virt);
1b51197d
JS
8381 if_type = bf_get(lpfc_sli_intf_if_type,
8382 &phba->sli4_hba.sli_intf);
8383 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
ff78d8f9 8384 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
cb69f7de 8385 *pcmd == ELS_CMD_SCR ||
6b5151fd 8386 *pcmd == ELS_CMD_FDISC ||
bdcd2b92 8387 *pcmd == ELS_CMD_LOGO ||
ff78d8f9
JS
8388 *pcmd == ELS_CMD_PLOGI)) {
8389 bf_set(els_req64_sp, &wqe->els_req, 1);
8390 bf_set(els_req64_sid, &wqe->els_req,
8391 iocbq->vport->fc_myDID);
939723a4
JS
8392 if ((*pcmd == ELS_CMD_FLOGI) &&
8393 !(phba->fc_topology ==
8394 LPFC_TOPOLOGY_LOOP))
8395 bf_set(els_req64_sid, &wqe->els_req, 0);
ff78d8f9
JS
8396 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8397 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
a7dd9c0f 8398 phba->vpi_ids[iocbq->vport->vpi]);
3ef6d24c 8399 } else if (pcmd && iocbq->context1) {
ff78d8f9
JS
8400 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8401 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8402 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8403 }
c868595d 8404 }
6d368e53
JS
8405 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8406 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
f0d9bccc
JS
8407 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8408 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8409 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8410 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8411 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8412 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
af22741c 8413 wqe->els_req.max_response_payload_len = total_len - xmit_len;
7851fe2c 8414 break;
5ffc266e 8415 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
8416 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8417 iocbq->iocb.un.ulpWord[3]);
8418 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7851fe2c 8419 iocbq->iocb.unsli3.rcvsli3.ox_id);
5ffc266e
JS
8420 /* The entire sequence is transmitted for this IOCB */
8421 xmit_len = total_len;
8422 cmnd = CMD_XMIT_SEQUENCE64_CR;
1b51197d
JS
8423 if (phba->link_flag & LS_LOOPBACK_MODE)
8424 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
4f774513 8425 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
8426 /* word3 iocb=io_tag32 wqe=reserved */
8427 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
8428 /* word4 relative_offset memcpy */
8429 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
8430 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8431 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8432 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8433 LPFC_WQE_IOD_WRITE);
8434 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8435 LPFC_WQE_LENLOC_WORD12);
8436 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
8437 wqe->xmit_sequence.xmit_len = xmit_len;
8438 command_type = OTHER_COMMAND;
7851fe2c 8439 break;
4f774513 8440 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
8441 /* word3 iocb=iotag32 wqe=seq_payload_len */
8442 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
8443 /* word4 iocb=rsvd wqe=rsvd */
8444 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8445 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 8446 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 8447 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
8448 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8449 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8450 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8451 LPFC_WQE_LENLOC_WORD3);
8452 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7851fe2c 8453 break;
4f774513
JS
8454 case CMD_FCP_IWRITE64_CR:
8455 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
8456 /* word3 iocb=iotag wqe=payload_offset_len */
8457 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
8458 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8459 xmit_len + sizeof(struct fcp_rsp));
8460 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8461 0);
f0d9bccc
JS
8462 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8463 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8464 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8465 iocbq->iocb.ulpFCP2Rcvy);
8466 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8467 /* Always open the exchange */
f0d9bccc
JS
8468 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8469 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8470 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 8471 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8472 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
1ba981fd
JS
8473 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8474 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
c92c841c
JS
8475 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8476 if (iocbq->priority) {
8477 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8478 (iocbq->priority << 1));
8479 } else {
1ba981fd
JS
8480 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8481 (phba->cfg_XLanePriority << 1));
8482 }
8483 }
b5c53958
JS
8484 /* Note, word 10 is already initialized to 0 */
8485
8486 if (phba->fcp_embed_io) {
8487 struct lpfc_scsi_buf *lpfc_cmd;
8488 struct sli4_sge *sgl;
8489 union lpfc_wqe128 *wqe128;
8490 struct fcp_cmnd *fcp_cmnd;
8491 uint32_t *ptr;
8492
8493 /* 128 byte wqe support here */
8494 wqe128 = (union lpfc_wqe128 *)wqe;
8495
8496 lpfc_cmd = iocbq->context1;
8497 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8498 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8499
8500 /* Word 0-2 - FCP_CMND */
8501 wqe128->generic.bde.tus.f.bdeFlags =
8502 BUFF_TYPE_BDE_IMMED;
8503 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8504 wqe128->generic.bde.addrHigh = 0;
8505 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8506
8507 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8508
8509 /* Word 22-29 FCP CMND Payload */
8510 ptr = &wqe128->words[22];
8511 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8512 }
7851fe2c 8513 break;
4f774513 8514 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
8515 /* word3 iocb=iotag wqe=payload_offset_len */
8516 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
8517 bf_set(payload_offset_len, &wqe->fcp_iread,
8518 xmit_len + sizeof(struct fcp_rsp));
8519 bf_set(cmd_buff_len, &wqe->fcp_iread,
8520 0);
f0d9bccc
JS
8521 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8522 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8523 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8524 iocbq->iocb.ulpFCP2Rcvy);
8525 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688 8526 /* Always open the exchange */
f0d9bccc
JS
8527 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8528 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8529 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 8530 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8531 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
1ba981fd
JS
8532 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8533 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
c92c841c
JS
8534 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8535 if (iocbq->priority) {
8536 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8537 (iocbq->priority << 1));
8538 } else {
1ba981fd
JS
8539 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8540 (phba->cfg_XLanePriority << 1));
8541 }
8542 }
b5c53958
JS
8543 /* Note, word 10 is already initialized to 0 */
8544
8545 if (phba->fcp_embed_io) {
8546 struct lpfc_scsi_buf *lpfc_cmd;
8547 struct sli4_sge *sgl;
8548 union lpfc_wqe128 *wqe128;
8549 struct fcp_cmnd *fcp_cmnd;
8550 uint32_t *ptr;
8551
8552 /* 128 byte wqe support here */
8553 wqe128 = (union lpfc_wqe128 *)wqe;
8554
8555 lpfc_cmd = iocbq->context1;
8556 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8557 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8558
8559 /* Word 0-2 - FCP_CMND */
8560 wqe128->generic.bde.tus.f.bdeFlags =
8561 BUFF_TYPE_BDE_IMMED;
8562 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8563 wqe128->generic.bde.addrHigh = 0;
8564 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8565
8566 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8567
8568 /* Word 22-29 FCP CMND Payload */
8569 ptr = &wqe128->words[22];
8570 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8571 }
7851fe2c 8572 break;
4f774513 8573 case CMD_FCP_ICMND64_CR:
0ba4b219
JS
8574 /* word3 iocb=iotag wqe=payload_offset_len */
8575 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8576 bf_set(payload_offset_len, &wqe->fcp_icmd,
8577 xmit_len + sizeof(struct fcp_rsp));
8578 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8579 0);
f0d9bccc 8580 /* word3 iocb=IO_TAG wqe=reserved */
f0d9bccc 8581 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 8582 /* Always open the exchange */
f0d9bccc
JS
8583 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8584 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8585 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8586 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8587 LPFC_WQE_LENLOC_NONE);
2a94aea4
JS
8588 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8589 iocbq->iocb.ulpFCP2Rcvy);
1ba981fd
JS
8590 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8591 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
c92c841c
JS
8592 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8593 if (iocbq->priority) {
8594 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8595 (iocbq->priority << 1));
8596 } else {
1ba981fd
JS
8597 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8598 (phba->cfg_XLanePriority << 1));
8599 }
8600 }
b5c53958
JS
8601 /* Note, word 10 is already initialized to 0 */
8602
8603 if (phba->fcp_embed_io) {
8604 struct lpfc_scsi_buf *lpfc_cmd;
8605 struct sli4_sge *sgl;
8606 union lpfc_wqe128 *wqe128;
8607 struct fcp_cmnd *fcp_cmnd;
8608 uint32_t *ptr;
8609
8610 /* 128 byte wqe support here */
8611 wqe128 = (union lpfc_wqe128 *)wqe;
8612
8613 lpfc_cmd = iocbq->context1;
8614 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8615 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8616
8617 /* Word 0-2 - FCP_CMND */
8618 wqe128->generic.bde.tus.f.bdeFlags =
8619 BUFF_TYPE_BDE_IMMED;
8620 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8621 wqe128->generic.bde.addrHigh = 0;
8622 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8623
8624 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8625
8626 /* Word 22-29 FCP CMND Payload */
8627 ptr = &wqe128->words[22];
8628 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8629 }
7851fe2c 8630 break;
4f774513 8631 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
8632 /* For this command calculate the xmit length of the
8633 * request bde.
8634 */
8635 xmit_len = 0;
8636 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8637 sizeof(struct ulp_bde64);
8638 for (i = 0; i < numBdes; i++) {
63e801ce 8639 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
546fc854
JS
8640 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8641 break;
63e801ce
JS
8642 xmit_len += bde.tus.f.bdeSize;
8643 }
f0d9bccc
JS
8644 /* word3 iocb=IO_TAG wqe=request_payload_len */
8645 wqe->gen_req.request_payload_len = xmit_len;
8646 /* word4 iocb=parameter wqe=relative_offset memcpy */
8647 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
8648 /* word6 context tag copied in memcpy */
8649 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8650 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8651 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8652 "2015 Invalid CT %x command 0x%x\n",
8653 ct, iocbq->iocb.ulpCommand);
8654 return IOCB_ERROR;
8655 }
f0d9bccc
JS
8656 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8657 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8658 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8659 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8660 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8661 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8662 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8663 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
af22741c 8664 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
4f774513 8665 command_type = OTHER_COMMAND;
7851fe2c 8666 break;
4f774513 8667 case CMD_XMIT_ELS_RSP64_CX:
c31098ce 8668 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513 8669 /* words0-2 BDE memcpy */
f0d9bccc
JS
8670 /* word3 iocb=iotag32 wqe=response_payload_len */
8671 wqe->xmit_els_rsp.response_payload_len = xmit_len;
939723a4
JS
8672 /* word4 */
8673 wqe->xmit_els_rsp.word4 = 0;
4f774513
JS
8674 /* word5 iocb=rsvd wge=did */
8675 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
939723a4
JS
8676 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8677
8678 if_type = bf_get(lpfc_sli_intf_if_type,
8679 &phba->sli4_hba.sli_intf);
8680 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8681 if (iocbq->vport->fc_flag & FC_PT2PT) {
8682 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8683 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8684 iocbq->vport->fc_myDID);
8685 if (iocbq->vport->fc_myDID == Fabric_DID) {
8686 bf_set(wqe_els_did,
8687 &wqe->xmit_els_rsp.wqe_dest, 0);
8688 }
8689 }
8690 }
f0d9bccc
JS
8691 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8692 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8693 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8694 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7851fe2c 8695 iocbq->iocb.unsli3.rcvsli3.ox_id);
4f774513 8696 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 8697 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6d368e53 8698 phba->vpi_ids[iocbq->vport->vpi]);
f0d9bccc
JS
8699 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8700 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8701 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8702 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8703 LPFC_WQE_LENLOC_WORD3);
8704 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6d368e53
JS
8705 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8706 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
ff78d8f9
JS
8707 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8708 iocbq->context2)->virt);
8709 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
939723a4
JS
8710 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8711 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
ff78d8f9 8712 iocbq->vport->fc_myDID);
939723a4
JS
8713 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8714 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
ff78d8f9
JS
8715 phba->vpi_ids[phba->pport->vpi]);
8716 }
4f774513 8717 command_type = OTHER_COMMAND;
7851fe2c 8718 break;
4f774513
JS
8719 case CMD_CLOSE_XRI_CN:
8720 case CMD_ABORT_XRI_CN:
8721 case CMD_ABORT_XRI_CX:
8722 /* words 0-2 memcpy should be 0 rserved */
8723 /* port will send abts */
dcf2a4e0
JS
8724 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8725 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8726 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8727 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8728 } else
8729 fip = 0;
8730
8731 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 8732 /*
dcf2a4e0
JS
8733 * The link is down, or the command was ELS_FIP
8734 * so the fw does not need to send abts
4f774513
JS
8735 * on the wire.
8736 */
8737 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8738 else
8739 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8740 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
8741 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8742 wqe->abort_cmd.rsrvd5 = 0;
8743 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
8744 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8745 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
8746 /*
8747 * The abort handler will send us CMD_ABORT_XRI_CN or
8748 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8749 */
f0d9bccc
JS
8750 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8751 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8752 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8753 LPFC_WQE_LENLOC_NONE);
4f774513
JS
8754 cmnd = CMD_ABORT_XRI_CX;
8755 command_type = OTHER_COMMAND;
8756 xritag = 0;
7851fe2c 8757 break;
6669f9bb 8758 case CMD_XMIT_BLS_RSP64_CX:
6b5151fd 8759 ndlp = (struct lpfc_nodelist *)iocbq->context1;
546fc854 8760 /* As BLS ABTS RSP WQE is very different from other WQEs,
6669f9bb
JS
8761 * we re-construct this WQE here based on information in
8762 * iocbq from scratch.
8763 */
8764 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 8765 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 8766 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
546fc854
JS
8767 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8768 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
5ffc266e
JS
8769 LPFC_ABTS_UNSOL_INT) {
8770 /* ABTS sent by initiator to CT exchange, the
8771 * RX_ID field will be filled with the newly
8772 * allocated responder XRI.
8773 */
8774 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8775 iocbq->sli4_xritag);
8776 } else {
8777 /* ABTS sent by responder to CT exchange, the
8778 * RX_ID field will be filled with the responder
8779 * RX_ID from ABTS.
8780 */
8781 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
546fc854 8782 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
5ffc266e 8783 }
6669f9bb
JS
8784 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8785 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6b5151fd
JS
8786
8787 /* Use CT=VPI */
8788 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8789 ndlp->nlp_DID);
8790 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8791 iocbq->iocb.ulpContext);
8792 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
6669f9bb 8793 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6b5151fd 8794 phba->vpi_ids[phba->pport->vpi]);
f0d9bccc
JS
8795 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8796 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8797 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
8798 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8799 command_type = OTHER_COMMAND;
546fc854
JS
8800 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8801 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8802 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8803 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8804 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8805 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8806 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8807 }
8808
7851fe2c 8809 break;
4f774513
JS
8810 case CMD_XRI_ABORTED_CX:
8811 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
8812 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8813 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8814 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8815 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8816 default:
8817 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8818 "2014 Invalid command 0x%x\n",
8819 iocbq->iocb.ulpCommand);
8820 return IOCB_ERROR;
7851fe2c 8821 break;
4f774513 8822 }
6d368e53 8823
8012cc38
JS
8824 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8825 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8826 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8827 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8828 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8829 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8830 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8831 LPFC_IO_DIF_INSERT);
f0d9bccc
JS
8832 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8833 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8834 wqe->generic.wqe_com.abort_tag = abort_tag;
8835 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8836 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8837 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8838 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
8839 return 0;
8840}
8841
8842/**
8843 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8844 * @phba: Pointer to HBA context object.
8845 * @ring_number: SLI ring number to issue iocb on.
8846 * @piocb: Pointer to command iocb.
8847 * @flag: Flag indicating if this command can be put into txq.
8848 *
8849 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8850 * an iocb command to an HBA with SLI-4 interface spec.
8851 *
8852 * This function is called with hbalock held. The function will return success
8853 * after it successfully submit the iocb to firmware or after adding to the
8854 * txq.
8855 **/
8856static int
8857__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8858 struct lpfc_iocbq *piocb, uint32_t flag)
8859{
8860 struct lpfc_sglq *sglq;
b5c53958
JS
8861 union lpfc_wqe *wqe;
8862 union lpfc_wqe128 wqe128;
1ba981fd 8863 struct lpfc_queue *wq;
4f774513 8864 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
4f774513 8865
1c2ba475
JT
8866 lockdep_assert_held(&phba->hbalock);
8867
b5c53958
JS
8868 /*
8869 * The WQE can be either 64 or 128 bytes,
8870 * so allocate space on the stack assuming the largest.
8871 */
8872 wqe = (union lpfc_wqe *)&wqe128;
8873
4f774513
JS
8874 if (piocb->sli4_xritag == NO_XRI) {
8875 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6b5151fd 8876 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
8877 sglq = NULL;
8878 else {
0e9bb8d7 8879 if (!list_empty(&pring->txq)) {
2a9bf3d0
JS
8880 if (!(flag & SLI_IOCB_RET_IOCB)) {
8881 __lpfc_sli_ringtx_put(phba,
8882 pring, piocb);
8883 return IOCB_SUCCESS;
8884 } else {
8885 return IOCB_BUSY;
8886 }
8887 } else {
6d368e53 8888 sglq = __lpfc_sli_get_sglq(phba, piocb);
2a9bf3d0
JS
8889 if (!sglq) {
8890 if (!(flag & SLI_IOCB_RET_IOCB)) {
8891 __lpfc_sli_ringtx_put(phba,
8892 pring,
8893 piocb);
8894 return IOCB_SUCCESS;
8895 } else
8896 return IOCB_BUSY;
8897 }
8898 }
4f774513
JS
8899 }
8900 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6d368e53
JS
8901 /* These IO's already have an XRI and a mapped sgl. */
8902 sglq = NULL;
4f774513 8903 } else {
6d368e53
JS
8904 /*
8905 * This is a continuation of a commandi,(CX) so this
4f774513
JS
8906 * sglq is on the active list
8907 */
edccdc17 8908 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
4f774513
JS
8909 if (!sglq)
8910 return IOCB_ERROR;
8911 }
8912
8913 if (sglq) {
6d368e53 8914 piocb->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0 8915 piocb->sli4_xritag = sglq->sli4_xritag;
2a9bf3d0 8916 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
8917 return IOCB_ERROR;
8918 }
8919
b5c53958 8920 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
4f774513
JS
8921 return IOCB_ERROR;
8922
341af102 8923 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
1ba981fd 8924 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
f38fa0bb 8925 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
1ba981fd
JS
8926 wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8927 } else {
8928 wq = phba->sli4_hba.oas_wq;
8929 }
b5c53958 8930 if (lpfc_sli4_wq_put(wq, wqe))
4f774513
JS
8931 return IOCB_ERROR;
8932 } else {
ea714f3d
JS
8933 if (unlikely(!phba->sli4_hba.els_wq))
8934 return IOCB_ERROR;
b5c53958 8935 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
4f774513
JS
8936 return IOCB_ERROR;
8937 }
8938 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8939
8940 return 0;
8941}
8942
8943/**
8944 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8945 *
8946 * This routine wraps the actual lockless version for issusing IOCB function
8947 * pointer from the lpfc_hba struct.
8948 *
8949 * Return codes:
b5c53958
JS
8950 * IOCB_ERROR - Error
8951 * IOCB_SUCCESS - Success
8952 * IOCB_BUSY - Busy
4f774513 8953 **/
2a9bf3d0 8954int
4f774513
JS
8955__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8956 struct lpfc_iocbq *piocb, uint32_t flag)
8957{
8958 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8959}
8960
8961/**
25985edc 8962 * lpfc_sli_api_table_setup - Set up sli api function jump table
4f774513
JS
8963 * @phba: The hba struct for which this call is being executed.
8964 * @dev_grp: The HBA PCI-Device group number.
8965 *
8966 * This routine sets up the SLI interface API function jump table in @phba
8967 * struct.
8968 * Returns: 0 - success, -ENODEV - failure.
8969 **/
8970int
8971lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8972{
8973
8974 switch (dev_grp) {
8975 case LPFC_PCI_DEV_LP:
8976 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8977 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8978 break;
8979 case LPFC_PCI_DEV_OC:
8980 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8981 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8982 break;
8983 default:
8984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8985 "1419 Invalid HBA PCI-device group: 0x%x\n",
8986 dev_grp);
8987 return -ENODEV;
8988 break;
8989 }
8990 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8991 return 0;
8992}
8993
a1efe163
JS
8994/**
8995 * lpfc_sli_calc_ring - Calculates which ring to use
8996 * @phba: Pointer to HBA context object.
8997 * @ring_number: Initial ring
8998 * @piocb: Pointer to command iocb.
8999 *
9000 * For SLI4, FCP IO can deferred to one fo many WQs, based on
9001 * fcp_wqidx, thus we need to calculate the corresponding ring.
9002 * Since ABORTS must go on the same WQ of the command they are
9003 * aborting, we use command's fcp_wqidx.
9004 */
bd4b3e5c 9005static int
9bd2bff5
JS
9006lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
9007 struct lpfc_iocbq *piocb)
9008{
8b0dff14
JS
9009 if (phba->sli_rev < LPFC_SLI_REV4)
9010 return ring_number;
9011
9012 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
9013 if (!(phba->cfg_fof) ||
9014 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
9015 if (unlikely(!phba->sli4_hba.fcp_wq))
9016 return LPFC_HBA_ERROR;
9017 /*
9018 * for abort iocb fcp_wqidx should already
9019 * be setup based on what work queue we used.
9020 */
9021 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
9022 piocb->fcp_wqidx =
9023 lpfc_sli4_scmd_to_wqidx_distr(phba,
9024 piocb->context1);
9025 ring_number = MAX_SLI3_CONFIGURED_RINGS +
9026 piocb->fcp_wqidx;
9027 } else {
9028 if (unlikely(!phba->sli4_hba.oas_wq))
9029 return LPFC_HBA_ERROR;
9030 piocb->fcp_wqidx = 0;
9031 ring_number = LPFC_FCP_OAS_RING;
9bd2bff5
JS
9032 }
9033 }
9034 return ring_number;
9035}
9036
4f774513
JS
9037/**
9038 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9039 * @phba: Pointer to HBA context object.
9040 * @pring: Pointer to driver SLI ring object.
9041 * @piocb: Pointer to command iocb.
9042 * @flag: Flag indicating if this command can be put into txq.
9043 *
9044 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9045 * function. This function gets the hbalock and calls
9046 * __lpfc_sli_issue_iocb function and will return the error returned
9047 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9048 * functions which do not hold hbalock.
9049 **/
9050int
9051lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9052 struct lpfc_iocbq *piocb, uint32_t flag)
9053{
ba20c853 9054 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
2a76a283 9055 struct lpfc_sli_ring *pring;
ba20c853
JS
9056 struct lpfc_queue *fpeq;
9057 struct lpfc_eqe *eqe;
4f774513 9058 unsigned long iflags;
2a76a283 9059 int rc, idx;
4f774513 9060
7e56aa25 9061 if (phba->sli_rev == LPFC_SLI_REV4) {
9bd2bff5
JS
9062 ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
9063 if (unlikely(ring_number == LPFC_HBA_ERROR))
9064 return IOCB_ERROR;
9065 idx = piocb->fcp_wqidx;
ba20c853 9066
9bd2bff5
JS
9067 pring = &phba->sli.ring[ring_number];
9068 spin_lock_irqsave(&pring->ring_lock, iflags);
9069 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9070 spin_unlock_irqrestore(&pring->ring_lock, iflags);
ba20c853 9071
9bd2bff5
JS
9072 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
9073 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
4f774513 9074
9bd2bff5
JS
9075 if (atomic_dec_and_test(&fcp_eq_hdl->
9076 fcp_eq_in_use)) {
ba20c853 9077
9bd2bff5
JS
9078 /* Get associated EQ with this index */
9079 fpeq = phba->sli4_hba.hba_eq[idx];
ba20c853 9080
9bd2bff5
JS
9081 /* Turn off interrupts from this EQ */
9082 lpfc_sli4_eq_clr_intr(fpeq);
ba20c853 9083
9bd2bff5
JS
9084 /*
9085 * Process all the events on FCP EQ
9086 */
9087 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9088 lpfc_sli4_hba_handle_eqe(phba,
9089 eqe, idx);
9090 fpeq->EQ_processed++;
ba20c853 9091 }
ba20c853 9092
9bd2bff5
JS
9093 /* Always clear and re-arm the EQ */
9094 lpfc_sli4_eq_release(fpeq,
9095 LPFC_QUEUE_REARM);
9096 }
9097 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
2a76a283 9098 }
7e56aa25
JS
9099 } else {
9100 /* For now, SLI2/3 will still use hbalock */
9101 spin_lock_irqsave(&phba->hbalock, iflags);
9102 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9103 spin_unlock_irqrestore(&phba->hbalock, iflags);
9104 }
4f774513
JS
9105 return rc;
9106}
9107
9108/**
9109 * lpfc_extra_ring_setup - Extra ring setup function
9110 * @phba: Pointer to HBA context object.
9111 *
9112 * This function is called while driver attaches with the
9113 * HBA to setup the extra ring. The extra ring is used
9114 * only when driver needs to support target mode functionality
9115 * or IP over FC functionalities.
9116 *
9117 * This function is called with no lock held.
9118 **/
9119static int
9120lpfc_extra_ring_setup( struct lpfc_hba *phba)
9121{
9122 struct lpfc_sli *psli;
9123 struct lpfc_sli_ring *pring;
9124
9125 psli = &phba->sli;
9126
9127 /* Adjust cmd/rsp ring iocb entries more evenly */
9128
9129 /* Take some away from the FCP ring */
9130 pring = &psli->ring[psli->fcp_ring];
7e56aa25
JS
9131 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9132 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9133 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9134 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e 9135
a4bc3379
JS
9136 /* and give them to the extra ring */
9137 pring = &psli->ring[psli->extra_ring];
9138
7e56aa25
JS
9139 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9140 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9141 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9142 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e
JW
9143
9144 /* Setup default profile for this ring */
9145 pring->iotag_max = 4096;
9146 pring->num_mask = 1;
9147 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
9148 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9149 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
9150 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9151 return 0;
9152}
9153
cb69f7de
JS
9154/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9155 * @phba: Pointer to HBA context object.
9156 * @iocbq: Pointer to iocb object.
9157 *
9158 * The async_event handler calls this routine when it receives
9159 * an ASYNC_STATUS_CN event from the port. The port generates
9160 * this event when an Abort Sequence request to an rport fails
9161 * twice in succession. The abort could be originated by the
9162 * driver or by the port. The ABTS could have been for an ELS
9163 * or FCP IO. The port only generates this event when an ABTS
9164 * fails to complete after one retry.
9165 */
9166static void
9167lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9168 struct lpfc_iocbq *iocbq)
9169{
9170 struct lpfc_nodelist *ndlp = NULL;
9171 uint16_t rpi = 0, vpi = 0;
9172 struct lpfc_vport *vport = NULL;
9173
9174 /* The rpi in the ulpContext is vport-sensitive. */
9175 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9176 rpi = iocbq->iocb.ulpContext;
9177
9178 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9179 "3092 Port generated ABTS async event "
9180 "on vpi %d rpi %d status 0x%x\n",
9181 vpi, rpi, iocbq->iocb.ulpStatus);
9182
9183 vport = lpfc_find_vport_by_vpid(phba, vpi);
9184 if (!vport)
9185 goto err_exit;
9186 ndlp = lpfc_findnode_rpi(vport, rpi);
9187 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9188 goto err_exit;
9189
9190 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9191 lpfc_sli_abts_recover_port(vport, ndlp);
9192 return;
9193
9194 err_exit:
9195 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9196 "3095 Event Context not found, no "
9197 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9198 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9199 vpi, rpi);
9200}
9201
9202/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9203 * @phba: pointer to HBA context object.
9204 * @ndlp: nodelist pointer for the impacted rport.
9205 * @axri: pointer to the wcqe containing the failed exchange.
9206 *
9207 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9208 * port. The port generates this event when an abort exchange request to an
9209 * rport fails twice in succession with no reply. The abort could be originated
9210 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9211 */
9212void
9213lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9214 struct lpfc_nodelist *ndlp,
9215 struct sli4_wcqe_xri_aborted *axri)
9216{
9217 struct lpfc_vport *vport;
5c1db2ac 9218 uint32_t ext_status = 0;
cb69f7de 9219
6b5151fd 9220 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
cb69f7de
JS
9221 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9222 "3115 Node Context not found, driver "
9223 "ignoring abts err event\n");
6b5151fd
JS
9224 return;
9225 }
9226
cb69f7de
JS
9227 vport = ndlp->vport;
9228 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9229 "3116 Port generated FCP XRI ABORT event on "
5c1db2ac 9230 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
8e668af5 9231 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
cb69f7de 9232 bf_get(lpfc_wcqe_xa_xri, axri),
5c1db2ac
JS
9233 bf_get(lpfc_wcqe_xa_status, axri),
9234 axri->parameter);
cb69f7de 9235
5c1db2ac
JS
9236 /*
9237 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9238 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9239 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9240 */
e3d2b802 9241 ext_status = axri->parameter & IOERR_PARAM_MASK;
5c1db2ac
JS
9242 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9243 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
cb69f7de
JS
9244 lpfc_sli_abts_recover_port(vport, ndlp);
9245}
9246
e59058c4 9247/**
3621a710 9248 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
9249 * @phba: Pointer to HBA context object.
9250 * @pring: Pointer to driver SLI ring object.
9251 * @iocbq: Pointer to iocb object.
9252 *
9253 * This function is called by the slow ring event handler
9254 * function when there is an ASYNC event iocb in the ring.
9255 * This function is called with no lock held.
9256 * Currently this function handles only temperature related
9257 * ASYNC events. The function decodes the temperature sensor
9258 * event message and posts events for the management applications.
9259 **/
98c9ea5c 9260static void
57127f15
JS
9261lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9262 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9263{
9264 IOCB_t *icmd;
9265 uint16_t evt_code;
57127f15
JS
9266 struct temp_event temp_event_data;
9267 struct Scsi_Host *shost;
a257bf90 9268 uint32_t *iocb_w;
57127f15
JS
9269
9270 icmd = &iocbq->iocb;
9271 evt_code = icmd->un.asyncstat.evt_code;
57127f15 9272
cb69f7de
JS
9273 switch (evt_code) {
9274 case ASYNC_TEMP_WARN:
9275 case ASYNC_TEMP_SAFE:
9276 temp_event_data.data = (uint32_t) icmd->ulpContext;
9277 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9278 if (evt_code == ASYNC_TEMP_WARN) {
9279 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9280 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9281 "0347 Adapter is very hot, please take "
9282 "corrective action. temperature : %d Celsius\n",
9283 (uint32_t) icmd->ulpContext);
9284 } else {
9285 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9286 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9287 "0340 Adapter temperature is OK now. "
9288 "temperature : %d Celsius\n",
9289 (uint32_t) icmd->ulpContext);
9290 }
9291
9292 /* Send temperature change event to applications */
9293 shost = lpfc_shost_from_vport(phba->pport);
9294 fc_host_post_vendor_event(shost, fc_get_event_number(),
9295 sizeof(temp_event_data), (char *) &temp_event_data,
9296 LPFC_NL_VENDOR_ID);
9297 break;
9298 case ASYNC_STATUS_CN:
9299 lpfc_sli_abts_err_handler(phba, iocbq);
9300 break;
9301 default:
a257bf90 9302 iocb_w = (uint32_t *) icmd;
cb69f7de 9303 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
76bb24ef 9304 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 9305 " evt_code 0x%x\n"
a257bf90
JS
9306 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9307 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9308 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9309 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
cb69f7de 9310 pring->ringno, icmd->un.asyncstat.evt_code,
a257bf90
JS
9311 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9312 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9313 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9314 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9315
cb69f7de 9316 break;
57127f15 9317 }
57127f15
JS
9318}
9319
9320
e59058c4 9321/**
3621a710 9322 * lpfc_sli_setup - SLI ring setup function
e59058c4
JS
9323 * @phba: Pointer to HBA context object.
9324 *
9325 * lpfc_sli_setup sets up rings of the SLI interface with
9326 * number of iocbs per ring and iotags. This function is
9327 * called while driver attach to the HBA and before the
9328 * interrupts are enabled. So there is no need for locking.
9329 *
9330 * This function always returns 0.
9331 **/
dea3101e
JB
9332int
9333lpfc_sli_setup(struct lpfc_hba *phba)
9334{
ed957684 9335 int i, totiocbsize = 0;
dea3101e
JB
9336 struct lpfc_sli *psli = &phba->sli;
9337 struct lpfc_sli_ring *pring;
9338
2a76a283
JS
9339 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9340 if (phba->sli_rev == LPFC_SLI_REV4)
67d12733 9341 psli->num_rings += phba->cfg_fcp_io_channel;
dea3101e
JB
9342 psli->sli_flag = 0;
9343 psli->fcp_ring = LPFC_FCP_RING;
9344 psli->next_ring = LPFC_FCP_NEXT_RING;
a4bc3379 9345 psli->extra_ring = LPFC_EXTRA_RING;
dea3101e 9346
604a3e30
JB
9347 psli->iocbq_lookup = NULL;
9348 psli->iocbq_lookup_len = 0;
9349 psli->last_iotag = 0;
9350
dea3101e
JB
9351 for (i = 0; i < psli->num_rings; i++) {
9352 pring = &psli->ring[i];
9353 switch (i) {
9354 case LPFC_FCP_RING: /* ring 0 - FCP */
9355 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9356 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9357 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9358 pring->sli.sli3.numCiocb +=
9359 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9360 pring->sli.sli3.numRiocb +=
9361 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9362 pring->sli.sli3.numCiocb +=
9363 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9364 pring->sli.sli3.numRiocb +=
9365 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9366 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9367 SLI3_IOCB_CMD_SIZE :
9368 SLI2_IOCB_CMD_SIZE;
7e56aa25 9369 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9370 SLI3_IOCB_RSP_SIZE :
9371 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
9372 pring->iotag_ctr = 0;
9373 pring->iotag_max =
92d7f7b0 9374 (phba->cfg_hba_queue_depth * 2);
dea3101e
JB
9375 pring->fast_iotag = pring->iotag_max;
9376 pring->num_mask = 0;
9377 break;
a4bc3379 9378 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 9379 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9380 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9381 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9382 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9383 SLI3_IOCB_CMD_SIZE :
9384 SLI2_IOCB_CMD_SIZE;
7e56aa25 9385 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9386 SLI3_IOCB_RSP_SIZE :
9387 SLI2_IOCB_RSP_SIZE;
2e0fef85 9388 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e
JB
9389 pring->num_mask = 0;
9390 break;
9391 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9392 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9393 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9394 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9395 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9396 SLI3_IOCB_CMD_SIZE :
9397 SLI2_IOCB_CMD_SIZE;
7e56aa25 9398 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9399 SLI3_IOCB_RSP_SIZE :
9400 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
9401 pring->fast_iotag = 0;
9402 pring->iotag_ctr = 0;
9403 pring->iotag_max = 4096;
57127f15
JS
9404 pring->lpfc_sli_rcv_async_status =
9405 lpfc_sli_async_event_handler;
6669f9bb 9406 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 9407 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
9408 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9409 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 9410 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 9411 lpfc_els_unsol_event;
dea3101e 9412 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
9413 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9414 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 9415 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 9416 lpfc_els_unsol_event;
dea3101e
JB
9417 pring->prt[2].profile = 0; /* Mask 2 */
9418 /* NameServer Inquiry */
6a9c52cf 9419 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 9420 /* NameServer */
6a9c52cf 9421 pring->prt[2].type = FC_TYPE_CT;
dea3101e 9422 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 9423 lpfc_ct_unsol_event;
dea3101e
JB
9424 pring->prt[3].profile = 0; /* Mask 3 */
9425 /* NameServer response */
6a9c52cf 9426 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 9427 /* NameServer */
6a9c52cf 9428 pring->prt[3].type = FC_TYPE_CT;
dea3101e 9429 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 9430 lpfc_ct_unsol_event;
dea3101e
JB
9431 break;
9432 }
7e56aa25
JS
9433 totiocbsize += (pring->sli.sli3.numCiocb *
9434 pring->sli.sli3.sizeCiocb) +
9435 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea3101e 9436 }
ed957684 9437 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 9438 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
9439 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9440 "SLI2 SLIM Data: x%x x%lx\n",
9441 phba->brd_no, totiocbsize,
9442 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 9443 }
cf5bf97e
JW
9444 if (phba->cfg_multi_ring_support == 2)
9445 lpfc_extra_ring_setup(phba);
dea3101e
JB
9446
9447 return 0;
9448}
9449
e59058c4 9450/**
3621a710 9451 * lpfc_sli_queue_setup - Queue initialization function
e59058c4
JS
9452 * @phba: Pointer to HBA context object.
9453 *
9454 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9455 * ring. This function also initializes ring indices of each ring.
9456 * This function is called during the initialization of the SLI
9457 * interface of an HBA.
9458 * This function is called with no lock held and always returns
9459 * 1.
9460 **/
dea3101e 9461int
2e0fef85 9462lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea3101e
JB
9463{
9464 struct lpfc_sli *psli;
9465 struct lpfc_sli_ring *pring;
604a3e30 9466 int i;
dea3101e
JB
9467
9468 psli = &phba->sli;
2e0fef85 9469 spin_lock_irq(&phba->hbalock);
dea3101e 9470 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 9471 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e
JB
9472 /* Initialize list headers for txq and txcmplq as double linked lists */
9473 for (i = 0; i < psli->num_rings; i++) {
9474 pring = &psli->ring[i];
9475 pring->ringno = i;
7e56aa25
JS
9476 pring->sli.sli3.next_cmdidx = 0;
9477 pring->sli.sli3.local_getidx = 0;
9478 pring->sli.sli3.cmdidx = 0;
68e814f5 9479 pring->flag = 0;
dea3101e
JB
9480 INIT_LIST_HEAD(&pring->txq);
9481 INIT_LIST_HEAD(&pring->txcmplq);
9482 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 9483 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 9484 INIT_LIST_HEAD(&pring->postbufq);
7e56aa25 9485 spin_lock_init(&pring->ring_lock);
dea3101e 9486 }
2e0fef85
JS
9487 spin_unlock_irq(&phba->hbalock);
9488 return 1;
dea3101e
JB
9489}
9490
04c68496
JS
9491/**
9492 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9493 * @phba: Pointer to HBA context object.
9494 *
9495 * This routine flushes the mailbox command subsystem. It will unconditionally
9496 * flush all the mailbox commands in the three possible stages in the mailbox
9497 * command sub-system: pending mailbox command queue; the outstanding mailbox
9498 * command; and completed mailbox command queue. It is caller's responsibility
9499 * to make sure that the driver is in the proper state to flush the mailbox
9500 * command sub-system. Namely, the posting of mailbox commands into the
9501 * pending mailbox command queue from the various clients must be stopped;
9502 * either the HBA is in a state that it will never works on the outstanding
9503 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9504 * mailbox command has been completed.
9505 **/
9506static void
9507lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9508{
9509 LIST_HEAD(completions);
9510 struct lpfc_sli *psli = &phba->sli;
9511 LPFC_MBOXQ_t *pmb;
9512 unsigned long iflag;
9513
9514 /* Flush all the mailbox commands in the mbox system */
9515 spin_lock_irqsave(&phba->hbalock, iflag);
9516 /* The pending mailbox command queue */
9517 list_splice_init(&phba->sli.mboxq, &completions);
9518 /* The outstanding active mailbox command */
9519 if (psli->mbox_active) {
9520 list_add_tail(&psli->mbox_active->list, &completions);
9521 psli->mbox_active = NULL;
9522 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9523 }
9524 /* The completed mailbox command queue */
9525 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9526 spin_unlock_irqrestore(&phba->hbalock, iflag);
9527
9528 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9529 while (!list_empty(&completions)) {
9530 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9531 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9532 if (pmb->mbox_cmpl)
9533 pmb->mbox_cmpl(phba, pmb);
9534 }
9535}
9536
e59058c4 9537/**
3621a710 9538 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
9539 * @vport: Pointer to virtual port object.
9540 *
9541 * lpfc_sli_host_down is called to clean up the resources
9542 * associated with a vport before destroying virtual
9543 * port data structures.
9544 * This function does following operations:
9545 * - Free discovery resources associated with this virtual
9546 * port.
9547 * - Free iocbs associated with this virtual port in
9548 * the txq.
9549 * - Send abort for all iocb commands associated with this
9550 * vport in txcmplq.
9551 *
9552 * This function is called with no lock held and always returns 1.
9553 **/
92d7f7b0
JS
9554int
9555lpfc_sli_host_down(struct lpfc_vport *vport)
9556{
858c9f6c 9557 LIST_HEAD(completions);
92d7f7b0
JS
9558 struct lpfc_hba *phba = vport->phba;
9559 struct lpfc_sli *psli = &phba->sli;
9560 struct lpfc_sli_ring *pring;
9561 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
9562 int i;
9563 unsigned long flags = 0;
9564 uint16_t prev_pring_flag;
9565
9566 lpfc_cleanup_discovery_resources(vport);
9567
9568 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0
JS
9569 for (i = 0; i < psli->num_rings; i++) {
9570 pring = &psli->ring[i];
9571 prev_pring_flag = pring->flag;
5e9d9b82
JS
9572 /* Only slow rings */
9573 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 9574 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
9575 /* Set the lpfc data pending flag */
9576 set_bit(LPFC_DATA_READY, &phba->data_flags);
9577 }
92d7f7b0
JS
9578 /*
9579 * Error everything on the txq since these iocbs have not been
9580 * given to the FW yet.
9581 */
92d7f7b0
JS
9582 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9583 if (iocb->vport != vport)
9584 continue;
858c9f6c 9585 list_move_tail(&iocb->list, &completions);
92d7f7b0
JS
9586 }
9587
9588 /* Next issue ABTS for everything on the txcmplq */
9589 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9590 list) {
9591 if (iocb->vport != vport)
9592 continue;
9593 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9594 }
9595
9596 pring->flag = prev_pring_flag;
9597 }
9598
9599 spin_unlock_irqrestore(&phba->hbalock, flags);
9600
a257bf90
JS
9601 /* Cancel all the IOCBs from the completions list */
9602 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9603 IOERR_SLI_DOWN);
92d7f7b0
JS
9604 return 1;
9605}
9606
e59058c4 9607/**
3621a710 9608 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
9609 * @phba: Pointer to HBA context object.
9610 *
9611 * This function cleans up all iocb, buffers, mailbox commands
9612 * while shutting down the HBA. This function is called with no
9613 * lock held and always returns 1.
9614 * This function does the following to cleanup driver resources:
9615 * - Free discovery resources for each virtual port
9616 * - Cleanup any pending fabric iocbs
9617 * - Iterate through the iocb txq and free each entry
9618 * in the list.
9619 * - Free up any buffer posted to the HBA
9620 * - Free mailbox commands in the mailbox queue.
9621 **/
dea3101e 9622int
2e0fef85 9623lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 9624{
2534ba75 9625 LIST_HEAD(completions);
2e0fef85 9626 struct lpfc_sli *psli = &phba->sli;
dea3101e 9627 struct lpfc_sli_ring *pring;
0ff10d46 9628 struct lpfc_dmabuf *buf_ptr;
dea3101e 9629 unsigned long flags = 0;
04c68496
JS
9630 int i;
9631
9632 /* Shutdown the mailbox command sub-system */
618a5230 9633 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea3101e 9634
dea3101e
JB
9635 lpfc_hba_down_prep(phba);
9636
92d7f7b0
JS
9637 lpfc_fabric_abort_hba(phba);
9638
2e0fef85 9639 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e
JB
9640 for (i = 0; i < psli->num_rings; i++) {
9641 pring = &psli->ring[i];
5e9d9b82
JS
9642 /* Only slow rings */
9643 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 9644 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
9645 /* Set the lpfc data pending flag */
9646 set_bit(LPFC_DATA_READY, &phba->data_flags);
9647 }
dea3101e
JB
9648
9649 /*
9650 * Error everything on the txq since these iocbs have not been
9651 * given to the FW yet.
9652 */
2534ba75 9653 list_splice_init(&pring->txq, &completions);
2534ba75 9654 }
2e0fef85 9655 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 9656
a257bf90
JS
9657 /* Cancel all the IOCBs from the completions list */
9658 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9659 IOERR_SLI_DOWN);
dea3101e 9660
0ff10d46
JS
9661 spin_lock_irqsave(&phba->hbalock, flags);
9662 list_splice_init(&phba->elsbuf, &completions);
9663 phba->elsbuf_cnt = 0;
9664 phba->elsbuf_prev_cnt = 0;
9665 spin_unlock_irqrestore(&phba->hbalock, flags);
9666
9667 while (!list_empty(&completions)) {
9668 list_remove_head(&completions, buf_ptr,
9669 struct lpfc_dmabuf, list);
9670 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9671 kfree(buf_ptr);
9672 }
9673
dea3101e
JB
9674 /* Return any active mbox cmds */
9675 del_timer_sync(&psli->mbox_tmo);
2e0fef85 9676
da0436e9 9677 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 9678 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 9679 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 9680
da0436e9
JS
9681 return 1;
9682}
9683
e59058c4 9684/**
3621a710 9685 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
9686 * @srcp: Source memory pointer.
9687 * @destp: Destination memory pointer.
9688 * @cnt: Number of words required to be copied.
9689 *
9690 * This function is used for copying data between driver memory
9691 * and the SLI memory. This function also changes the endianness
9692 * of each word if native endianness is different from SLI
9693 * endianness. This function can be called with or without
9694 * lock.
9695 **/
dea3101e
JB
9696void
9697lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9698{
9699 uint32_t *src = srcp;
9700 uint32_t *dest = destp;
9701 uint32_t ldata;
9702 int i;
9703
9704 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9705 ldata = *src;
9706 ldata = le32_to_cpu(ldata);
9707 *dest = ldata;
9708 src++;
9709 dest++;
9710 }
9711}
9712
e59058c4 9713
a0c87cbd
JS
9714/**
9715 * lpfc_sli_bemem_bcopy - SLI memory copy function
9716 * @srcp: Source memory pointer.
9717 * @destp: Destination memory pointer.
9718 * @cnt: Number of words required to be copied.
9719 *
9720 * This function is used for copying data between a data structure
9721 * with big endian representation to local endianness.
9722 * This function can be called with or without lock.
9723 **/
9724void
9725lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9726{
9727 uint32_t *src = srcp;
9728 uint32_t *dest = destp;
9729 uint32_t ldata;
9730 int i;
9731
9732 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9733 ldata = *src;
9734 ldata = be32_to_cpu(ldata);
9735 *dest = ldata;
9736 src++;
9737 dest++;
9738 }
9739}
9740
e59058c4 9741/**
3621a710 9742 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
9743 * @phba: Pointer to HBA context object.
9744 * @pring: Pointer to driver SLI ring object.
9745 * @mp: Pointer to driver buffer object.
9746 *
9747 * This function is called with no lock held.
9748 * It always return zero after adding the buffer to the postbufq
9749 * buffer list.
9750 **/
dea3101e 9751int
2e0fef85
JS
9752lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9753 struct lpfc_dmabuf *mp)
dea3101e
JB
9754{
9755 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9756 later */
2e0fef85 9757 spin_lock_irq(&phba->hbalock);
dea3101e 9758 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 9759 pring->postbufq_cnt++;
2e0fef85 9760 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
9761 return 0;
9762}
9763
e59058c4 9764/**
3621a710 9765 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
9766 * @phba: Pointer to HBA context object.
9767 *
9768 * When HBQ is enabled, buffers are searched based on tags. This function
9769 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9770 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9771 * does not conflict with tags of buffer posted for unsolicited events.
9772 * The function returns the allocated tag. The function is called with
9773 * no locks held.
9774 **/
76bb24ef
JS
9775uint32_t
9776lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9777{
9778 spin_lock_irq(&phba->hbalock);
9779 phba->buffer_tag_count++;
9780 /*
9781 * Always set the QUE_BUFTAG_BIT to distiguish between
9782 * a tag assigned by HBQ.
9783 */
9784 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9785 spin_unlock_irq(&phba->hbalock);
9786 return phba->buffer_tag_count;
9787}
9788
e59058c4 9789/**
3621a710 9790 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
9791 * @phba: Pointer to HBA context object.
9792 * @pring: Pointer to driver SLI ring object.
9793 * @tag: Buffer tag.
9794 *
9795 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9796 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9797 * iocb is posted to the response ring with the tag of the buffer.
9798 * This function searches the pring->postbufq list using the tag
9799 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9800 * iocb. If the buffer is found then lpfc_dmabuf object of the
9801 * buffer is returned to the caller else NULL is returned.
9802 * This function is called with no lock held.
9803 **/
76bb24ef
JS
9804struct lpfc_dmabuf *
9805lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9806 uint32_t tag)
9807{
9808 struct lpfc_dmabuf *mp, *next_mp;
9809 struct list_head *slp = &pring->postbufq;
9810
25985edc 9811 /* Search postbufq, from the beginning, looking for a match on tag */
76bb24ef
JS
9812 spin_lock_irq(&phba->hbalock);
9813 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9814 if (mp->buffer_tag == tag) {
9815 list_del_init(&mp->list);
9816 pring->postbufq_cnt--;
9817 spin_unlock_irq(&phba->hbalock);
9818 return mp;
9819 }
9820 }
9821
9822 spin_unlock_irq(&phba->hbalock);
9823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 9824 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
9825 "ring %d Data x%lx x%p x%p x%x\n",
9826 pring->ringno, (unsigned long) tag,
9827 slp->next, slp->prev, pring->postbufq_cnt);
9828
9829 return NULL;
9830}
dea3101e 9831
e59058c4 9832/**
3621a710 9833 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
9834 * @phba: Pointer to HBA context object.
9835 * @pring: Pointer to driver SLI ring object.
9836 * @phys: DMA address of the buffer.
9837 *
9838 * This function searches the buffer list using the dma_address
9839 * of unsolicited event to find the driver's lpfc_dmabuf object
9840 * corresponding to the dma_address. The function returns the
9841 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9842 * This function is called by the ct and els unsolicited event
9843 * handlers to get the buffer associated with the unsolicited
9844 * event.
9845 *
9846 * This function is called with no lock held.
9847 **/
dea3101e
JB
9848struct lpfc_dmabuf *
9849lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9850 dma_addr_t phys)
9851{
9852 struct lpfc_dmabuf *mp, *next_mp;
9853 struct list_head *slp = &pring->postbufq;
9854
25985edc 9855 /* Search postbufq, from the beginning, looking for a match on phys */
2e0fef85 9856 spin_lock_irq(&phba->hbalock);
dea3101e
JB
9857 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9858 if (mp->phys == phys) {
9859 list_del_init(&mp->list);
9860 pring->postbufq_cnt--;
2e0fef85 9861 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
9862 return mp;
9863 }
9864 }
9865
2e0fef85 9866 spin_unlock_irq(&phba->hbalock);
dea3101e 9867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 9868 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 9869 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 9870 pring->ringno, (unsigned long long)phys,
dea3101e
JB
9871 slp->next, slp->prev, pring->postbufq_cnt);
9872 return NULL;
9873}
9874
e59058c4 9875/**
3621a710 9876 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
9877 * @phba: Pointer to HBA context object.
9878 * @cmdiocb: Pointer to driver command iocb object.
9879 * @rspiocb: Pointer to driver response iocb object.
9880 *
9881 * This function is the completion handler for the abort iocbs for
9882 * ELS commands. This function is called from the ELS ring event
9883 * handler with no lock held. This function frees memory resources
9884 * associated with the abort iocb.
9885 **/
dea3101e 9886static void
2e0fef85
JS
9887lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9888 struct lpfc_iocbq *rspiocb)
dea3101e 9889{
2e0fef85 9890 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 9891 uint16_t abort_iotag, abort_context;
ff78d8f9 9892 struct lpfc_iocbq *abort_iocb = NULL;
2680eeaa
JS
9893
9894 if (irsp->ulpStatus) {
ff78d8f9
JS
9895
9896 /*
9897 * Assume that the port already completed and returned, or
9898 * will return the iocb. Just Log the message.
9899 */
2680eeaa
JS
9900 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9901 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9902
2e0fef85 9903 spin_lock_irq(&phba->hbalock);
45ed1190
JS
9904 if (phba->sli_rev < LPFC_SLI_REV4) {
9905 if (abort_iotag != 0 &&
9906 abort_iotag <= phba->sli.last_iotag)
9907 abort_iocb =
9908 phba->sli.iocbq_lookup[abort_iotag];
9909 } else
9910 /* For sli4 the abort_tag is the XRI,
9911 * so the abort routine puts the iotag of the iocb
9912 * being aborted in the context field of the abort
9913 * IOCB.
9914 */
9915 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 9916
2a9bf3d0
JS
9917 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9918 "0327 Cannot abort els iocb %p "
9919 "with tag %x context %x, abort status %x, "
9920 "abort code %x\n",
9921 abort_iocb, abort_iotag, abort_context,
9922 irsp->ulpStatus, irsp->un.ulpWord[4]);
341af102 9923
ff78d8f9 9924 spin_unlock_irq(&phba->hbalock);
2680eeaa 9925 }
604a3e30 9926 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e
JB
9927 return;
9928}
9929
e59058c4 9930/**
3621a710 9931 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
9932 * @phba: Pointer to HBA context object.
9933 * @cmdiocb: Pointer to driver command iocb object.
9934 * @rspiocb: Pointer to driver response iocb object.
9935 *
9936 * The function is called from SLI ring event handler with no
9937 * lock held. This function is the completion handler for ELS commands
9938 * which are aborted. The function frees memory resources used for
9939 * the aborted ELS commands.
9940 **/
92d7f7b0
JS
9941static void
9942lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9943 struct lpfc_iocbq *rspiocb)
9944{
9945 IOCB_t *irsp = &rspiocb->iocb;
9946
9947 /* ELS cmd tag <ulpIoTag> completes */
9948 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 9949 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 9950 "x%x x%x x%x\n",
e8b62011 9951 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 9952 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
9953 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9954 lpfc_ct_free_iocb(phba, cmdiocb);
9955 else
9956 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
9957 return;
9958}
9959
e59058c4 9960/**
5af5eee7 9961 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
9962 * @phba: Pointer to HBA context object.
9963 * @pring: Pointer to driver SLI ring object.
9964 * @cmdiocb: Pointer to driver command iocb object.
9965 *
5af5eee7
JS
9966 * This function issues an abort iocb for the provided command iocb down to
9967 * the port. Other than the case the outstanding command iocb is an abort
9968 * request, this function issues abort out unconditionally. This function is
9969 * called with hbalock held. The function returns 0 when it fails due to
9970 * memory allocation failure or when the command iocb is an abort request.
e59058c4 9971 **/
5af5eee7
JS
9972static int
9973lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 9974 struct lpfc_iocbq *cmdiocb)
dea3101e 9975{
2e0fef85 9976 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 9977 struct lpfc_iocbq *abtsiocbp;
dea3101e
JB
9978 IOCB_t *icmd = NULL;
9979 IOCB_t *iabt = NULL;
9bd2bff5 9980 int ring_number;
5af5eee7 9981 int retval;
7e56aa25 9982 unsigned long iflags;
07951076 9983
1c2ba475
JT
9984 lockdep_assert_held(&phba->hbalock);
9985
92d7f7b0
JS
9986 /*
9987 * There are certain command types we don't want to abort. And we
9988 * don't want to abort commands that are already in the process of
9989 * being aborted.
07951076
JS
9990 */
9991 icmd = &cmdiocb->iocb;
2e0fef85 9992 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
9993 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9994 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
9995 return 0;
9996
dea3101e 9997 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 9998 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
9999 if (abtsiocbp == NULL)
10000 return 0;
dea3101e 10001
07951076 10002 /* This signals the response to set the correct status
341af102 10003 * before calling the completion handler
07951076
JS
10004 */
10005 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10006
dea3101e 10007 iabt = &abtsiocbp->iocb;
07951076
JS
10008 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10009 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 10010 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 10011 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
10012 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10013 }
da0436e9
JS
10014 else
10015 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
10016 iabt->ulpLe = 1;
10017 iabt->ulpClass = icmd->ulpClass;
dea3101e 10018
5ffc266e
JS
10019 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10020 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
341af102
JS
10021 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10022 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
10023 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10024 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
5ffc266e 10025
2e0fef85 10026 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
10027 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10028 else
10029 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 10030
07951076 10031 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
e6c6acc0 10032 abtsiocbp->vport = vport;
5b8bd0c9 10033
e8b62011
JS
10034 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10035 "0339 Abort xri x%x, original iotag x%x, "
10036 "abort cmd iotag x%x\n",
2a9bf3d0 10037 iabt->un.acxri.abortIoTag,
e8b62011 10038 iabt->un.acxri.abortContextTag,
2a9bf3d0 10039 abtsiocbp->iotag);
7e56aa25
JS
10040
10041 if (phba->sli_rev == LPFC_SLI_REV4) {
9bd2bff5
JS
10042 ring_number =
10043 lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
10044 if (unlikely(ring_number == LPFC_HBA_ERROR))
10045 return 0;
10046 pring = &phba->sli.ring[ring_number];
7e56aa25
JS
10047 /* Note: both hbalock and ring_lock need to be set here */
10048 spin_lock_irqsave(&pring->ring_lock, iflags);
10049 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10050 abtsiocbp, 0);
10051 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10052 } else {
10053 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10054 abtsiocbp, 0);
10055 }
dea3101e 10056
d7c255b2
JS
10057 if (retval)
10058 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
10059
10060 /*
10061 * Caller to this routine should check for IOCB_ERROR
10062 * and handle it properly. This routine no longer removes
10063 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10064 */
10065 return retval;
10066}
10067
10068/**
10069 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10070 * @phba: Pointer to HBA context object.
10071 * @pring: Pointer to driver SLI ring object.
10072 * @cmdiocb: Pointer to driver command iocb object.
10073 *
10074 * This function issues an abort iocb for the provided command iocb. In case
10075 * of unloading, the abort iocb will not be issued to commands on the ELS
10076 * ring. Instead, the callback function shall be changed to those commands
10077 * so that nothing happens when them finishes. This function is called with
10078 * hbalock held. The function returns 0 when the command iocb is an abort
10079 * request.
10080 **/
10081int
10082lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10083 struct lpfc_iocbq *cmdiocb)
10084{
10085 struct lpfc_vport *vport = cmdiocb->vport;
10086 int retval = IOCB_ERROR;
10087 IOCB_t *icmd = NULL;
10088
1c2ba475
JT
10089 lockdep_assert_held(&phba->hbalock);
10090
5af5eee7
JS
10091 /*
10092 * There are certain command types we don't want to abort. And we
10093 * don't want to abort commands that are already in the process of
10094 * being aborted.
10095 */
10096 icmd = &cmdiocb->iocb;
10097 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10098 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10099 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10100 return 0;
10101
10102 /*
10103 * If we're unloading, don't abort iocb on the ELS ring, but change
10104 * the callback so that nothing happens when it finishes.
10105 */
10106 if ((vport->load_flag & FC_UNLOADING) &&
10107 (pring->ringno == LPFC_ELS_RING)) {
10108 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10109 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10110 else
10111 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10112 goto abort_iotag_exit;
10113 }
10114
10115 /* Now, we try to issue the abort to the cmdiocb out */
10116 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10117
07951076 10118abort_iotag_exit:
2e0fef85
JS
10119 /*
10120 * Caller to this routine should check for IOCB_ERROR
10121 * and handle it properly. This routine no longer removes
10122 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 10123 */
2e0fef85 10124 return retval;
dea3101e
JB
10125}
10126
5af5eee7
JS
10127/**
10128 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10129 * @phba: pointer to lpfc HBA data structure.
10130 *
10131 * This routine will abort all pending and outstanding iocbs to an HBA.
10132 **/
10133void
10134lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10135{
10136 struct lpfc_sli *psli = &phba->sli;
10137 struct lpfc_sli_ring *pring;
10138 int i;
10139
10140 for (i = 0; i < psli->num_rings; i++) {
10141 pring = &psli->ring[i];
db55fba8 10142 lpfc_sli_abort_iocb_ring(phba, pring);
5af5eee7
JS
10143 }
10144}
10145
e59058c4 10146/**
3621a710 10147 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
10148 * @iocbq: Pointer to driver iocb object.
10149 * @vport: Pointer to driver virtual port object.
10150 * @tgt_id: SCSI ID of the target.
10151 * @lun_id: LUN ID of the scsi device.
10152 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10153 *
3621a710 10154 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
10155 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10156 * 0 if the filtering criteria is met for the given iocb and will return
10157 * 1 if the filtering criteria is not met.
10158 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10159 * given iocb is for the SCSI device specified by vport, tgt_id and
10160 * lun_id parameter.
10161 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10162 * given iocb is for the SCSI target specified by vport and tgt_id
10163 * parameters.
10164 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10165 * given iocb is for the SCSI host associated with the given vport.
10166 * This function is called with no locks held.
10167 **/
dea3101e 10168static int
51ef4c26
JS
10169lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10170 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 10171 lpfc_ctx_cmd ctx_cmd)
dea3101e 10172{
0bd4ca25 10173 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e
JB
10174 int rc = 1;
10175
0bd4ca25
JSEC
10176 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10177 return rc;
10178
51ef4c26
JS
10179 if (iocbq->vport != vport)
10180 return rc;
10181
0bd4ca25 10182 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 10183
495a714c 10184 if (lpfc_cmd->pCmd == NULL)
dea3101e
JB
10185 return rc;
10186
10187 switch (ctx_cmd) {
10188 case LPFC_CTX_LUN:
495a714c
JS
10189 if ((lpfc_cmd->rdata->pnode) &&
10190 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10191 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e
JB
10192 rc = 0;
10193 break;
10194 case LPFC_CTX_TGT:
495a714c
JS
10195 if ((lpfc_cmd->rdata->pnode) &&
10196 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e
JB
10197 rc = 0;
10198 break;
dea3101e
JB
10199 case LPFC_CTX_HOST:
10200 rc = 0;
10201 break;
10202 default:
10203 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 10204 __func__, ctx_cmd);
dea3101e
JB
10205 break;
10206 }
10207
10208 return rc;
10209}
10210
e59058c4 10211/**
3621a710 10212 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
10213 * @vport: Pointer to virtual port.
10214 * @tgt_id: SCSI ID of the target.
10215 * @lun_id: LUN ID of the scsi device.
10216 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10217 *
10218 * This function returns number of FCP commands pending for the vport.
10219 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10220 * commands pending on the vport associated with SCSI device specified
10221 * by tgt_id and lun_id parameters.
10222 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10223 * commands pending on the vport associated with SCSI target specified
10224 * by tgt_id parameter.
10225 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10226 * commands pending on the vport.
10227 * This function returns the number of iocbs which satisfy the filter.
10228 * This function is called without any lock held.
10229 **/
dea3101e 10230int
51ef4c26
JS
10231lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10232 lpfc_ctx_cmd ctx_cmd)
dea3101e 10233{
51ef4c26 10234 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
10235 struct lpfc_iocbq *iocbq;
10236 int sum, i;
dea3101e 10237
31979008 10238 spin_lock_irq(&phba->hbalock);
0bd4ca25
JSEC
10239 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10240 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 10241
51ef4c26
JS
10242 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10243 ctx_cmd) == 0)
0bd4ca25 10244 sum++;
dea3101e 10245 }
31979008 10246 spin_unlock_irq(&phba->hbalock);
0bd4ca25 10247
dea3101e
JB
10248 return sum;
10249}
10250
e59058c4 10251/**
3621a710 10252 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
10253 * @phba: Pointer to HBA context object
10254 * @cmdiocb: Pointer to command iocb object.
10255 * @rspiocb: Pointer to response iocb object.
10256 *
10257 * This function is called when an aborted FCP iocb completes. This
10258 * function is called by the ring event handler with no lock held.
10259 * This function frees the iocb.
10260 **/
5eb95af0 10261void
2e0fef85
JS
10262lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10263 struct lpfc_iocbq *rspiocb)
5eb95af0 10264{
cb69f7de 10265 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8e668af5 10266 "3096 ABORT_XRI_CN completing on rpi x%x "
cb69f7de
JS
10267 "original iotag x%x, abort cmd iotag x%x "
10268 "status 0x%x, reason 0x%x\n",
10269 cmdiocb->iocb.un.acxri.abortContextTag,
10270 cmdiocb->iocb.un.acxri.abortIoTag,
10271 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10272 rspiocb->iocb.un.ulpWord[4]);
604a3e30 10273 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
10274 return;
10275}
10276
e59058c4 10277/**
3621a710 10278 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
10279 * @vport: Pointer to virtual port.
10280 * @pring: Pointer to driver SLI ring object.
10281 * @tgt_id: SCSI ID of the target.
10282 * @lun_id: LUN ID of the scsi device.
10283 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10284 *
10285 * This function sends an abort command for every SCSI command
10286 * associated with the given virtual port pending on the ring
10287 * filtered by lpfc_sli_validate_fcp_iocb function.
10288 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10289 * FCP iocbs associated with lun specified by tgt_id and lun_id
10290 * parameters
10291 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10292 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10293 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10294 * FCP iocbs associated with virtual port.
10295 * This function returns number of iocbs it failed to abort.
10296 * This function is called with no locks held.
10297 **/
dea3101e 10298int
51ef4c26
JS
10299lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10300 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 10301{
51ef4c26 10302 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
10303 struct lpfc_iocbq *iocbq;
10304 struct lpfc_iocbq *abtsiocb;
dea3101e 10305 IOCB_t *cmd = NULL;
dea3101e 10306 int errcnt = 0, ret_val = 0;
0bd4ca25 10307 int i;
dea3101e 10308
0bd4ca25
JSEC
10309 for (i = 1; i <= phba->sli.last_iotag; i++) {
10310 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 10311
51ef4c26 10312 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 10313 abort_cmd) != 0)
dea3101e
JB
10314 continue;
10315
afbd8d88
JS
10316 /*
10317 * If the iocbq is already being aborted, don't take a second
10318 * action, but do count it.
10319 */
10320 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10321 continue;
10322
dea3101e 10323 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 10324 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
10325 if (abtsiocb == NULL) {
10326 errcnt++;
10327 continue;
10328 }
dea3101e 10329
afbd8d88
JS
10330 /* indicate the IO is being aborted by the driver. */
10331 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10332
0bd4ca25 10333 cmd = &iocbq->iocb;
dea3101e
JB
10334 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10335 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
10336 if (phba->sli_rev == LPFC_SLI_REV4)
10337 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10338 else
10339 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e
JB
10340 abtsiocb->iocb.ulpLe = 1;
10341 abtsiocb->iocb.ulpClass = cmd->ulpClass;
afbd8d88 10342 abtsiocb->vport = vport;
dea3101e 10343
5ffc266e
JS
10344 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10345 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
341af102
JS
10346 if (iocbq->iocb_flag & LPFC_IO_FCP)
10347 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
10348 if (iocbq->iocb_flag & LPFC_IO_FOF)
10349 abtsiocb->iocb_flag |= LPFC_IO_FOF;
5ffc266e 10350
2e0fef85 10351 if (lpfc_is_link_up(phba))
dea3101e
JB
10352 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10353 else
10354 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10355
5eb95af0
JSEC
10356 /* Setup callback routine and issue the command. */
10357 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
da0436e9
JS
10358 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10359 abtsiocb, 0);
dea3101e 10360 if (ret_val == IOCB_ERROR) {
604a3e30 10361 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e
JB
10362 errcnt++;
10363 continue;
10364 }
10365 }
10366
10367 return errcnt;
10368}
10369
98912dda
JS
10370/**
10371 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10372 * @vport: Pointer to virtual port.
10373 * @pring: Pointer to driver SLI ring object.
10374 * @tgt_id: SCSI ID of the target.
10375 * @lun_id: LUN ID of the scsi device.
10376 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10377 *
10378 * This function sends an abort command for every SCSI command
10379 * associated with the given virtual port pending on the ring
10380 * filtered by lpfc_sli_validate_fcp_iocb function.
10381 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10382 * FCP iocbs associated with lun specified by tgt_id and lun_id
10383 * parameters
10384 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10385 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10386 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10387 * FCP iocbs associated with virtual port.
10388 * This function returns number of iocbs it aborted .
10389 * This function is called with no locks held right after a taskmgmt
10390 * command is sent.
10391 **/
10392int
10393lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10394 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10395{
10396 struct lpfc_hba *phba = vport->phba;
8c50d25c 10397 struct lpfc_scsi_buf *lpfc_cmd;
98912dda 10398 struct lpfc_iocbq *abtsiocbq;
8c50d25c 10399 struct lpfc_nodelist *ndlp;
98912dda
JS
10400 struct lpfc_iocbq *iocbq;
10401 IOCB_t *icmd;
10402 int sum, i, ret_val;
10403 unsigned long iflags;
10404 struct lpfc_sli_ring *pring_s4;
10405 uint32_t ring_number;
10406
10407 spin_lock_irq(&phba->hbalock);
10408
10409 /* all I/Os are in process of being flushed */
10410 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10411 spin_unlock_irq(&phba->hbalock);
10412 return 0;
10413 }
10414 sum = 0;
10415
10416 for (i = 1; i <= phba->sli.last_iotag; i++) {
10417 iocbq = phba->sli.iocbq_lookup[i];
10418
10419 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10420 cmd) != 0)
10421 continue;
10422
10423 /*
10424 * If the iocbq is already being aborted, don't take a second
10425 * action, but do count it.
10426 */
10427 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10428 continue;
10429
10430 /* issue ABTS for this IOCB based on iotag */
10431 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10432 if (abtsiocbq == NULL)
10433 continue;
10434
10435 icmd = &iocbq->iocb;
10436 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10437 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10438 if (phba->sli_rev == LPFC_SLI_REV4)
10439 abtsiocbq->iocb.un.acxri.abortIoTag =
10440 iocbq->sli4_xritag;
10441 else
10442 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10443 abtsiocbq->iocb.ulpLe = 1;
10444 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10445 abtsiocbq->vport = vport;
10446
10447 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10448 abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
10449 if (iocbq->iocb_flag & LPFC_IO_FCP)
10450 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
10451 if (iocbq->iocb_flag & LPFC_IO_FOF)
10452 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
98912dda 10453
8c50d25c
JS
10454 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10455 ndlp = lpfc_cmd->rdata->pnode;
10456
10457 if (lpfc_is_link_up(phba) &&
10458 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
98912dda
JS
10459 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10460 else
10461 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10462
10463 /* Setup callback routine and issue the command. */
10464 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10465
10466 /*
10467 * Indicate the IO is being aborted by the driver and set
10468 * the caller's flag into the aborted IO.
10469 */
10470 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10471
10472 if (phba->sli_rev == LPFC_SLI_REV4) {
10473 ring_number = MAX_SLI3_CONFIGURED_RINGS +
10474 iocbq->fcp_wqidx;
10475 pring_s4 = &phba->sli.ring[ring_number];
10476 /* Note: both hbalock and ring_lock must be set here */
10477 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10478 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10479 abtsiocbq, 0);
10480 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10481 } else {
10482 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10483 abtsiocbq, 0);
10484 }
10485
10486
10487 if (ret_val == IOCB_ERROR)
10488 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10489 else
10490 sum++;
10491 }
10492 spin_unlock_irq(&phba->hbalock);
10493 return sum;
10494}
10495
e59058c4 10496/**
3621a710 10497 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
10498 * @phba: Pointer to HBA context object.
10499 * @cmdiocbq: Pointer to command iocb.
10500 * @rspiocbq: Pointer to response iocb.
10501 *
10502 * This function is the completion handler for iocbs issued using
10503 * lpfc_sli_issue_iocb_wait function. This function is called by the
10504 * ring event handler function without any lock held. This function
10505 * can be called from both worker thread context and interrupt
10506 * context. This function also can be called from other thread which
10507 * cleans up the SLI layer objects.
10508 * This function copy the contents of the response iocb to the
10509 * response iocb memory object provided by the caller of
10510 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10511 * sleeps for the iocb completion.
10512 **/
68876920
JSEC
10513static void
10514lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10515 struct lpfc_iocbq *cmdiocbq,
10516 struct lpfc_iocbq *rspiocbq)
dea3101e 10517{
68876920
JSEC
10518 wait_queue_head_t *pdone_q;
10519 unsigned long iflags;
0f65ff68 10520 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 10521
2e0fef85 10522 spin_lock_irqsave(&phba->hbalock, iflags);
5a0916b4
JS
10523 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10524
10525 /*
10526 * A time out has occurred for the iocb. If a time out
10527 * completion handler has been supplied, call it. Otherwise,
10528 * just free the iocbq.
10529 */
10530
10531 spin_unlock_irqrestore(&phba->hbalock, iflags);
10532 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10533 cmdiocbq->wait_iocb_cmpl = NULL;
10534 if (cmdiocbq->iocb_cmpl)
10535 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10536 else
10537 lpfc_sli_release_iocbq(phba, cmdiocbq);
10538 return;
10539 }
10540
68876920
JSEC
10541 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10542 if (cmdiocbq->context2 && rspiocbq)
10543 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10544 &rspiocbq->iocb, sizeof(IOCB_t));
10545
0f65ff68
JS
10546 /* Set the exchange busy flag for task management commands */
10547 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10548 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10549 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10550 cur_iocbq);
10551 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10552 }
10553
68876920 10554 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
10555 if (pdone_q)
10556 wake_up(pdone_q);
858c9f6c 10557 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e
JB
10558 return;
10559}
10560
d11e31dd
JS
10561/**
10562 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10563 * @phba: Pointer to HBA context object..
10564 * @piocbq: Pointer to command iocb.
10565 * @flag: Flag to test.
10566 *
10567 * This routine grabs the hbalock and then test the iocb_flag to
10568 * see if the passed in flag is set.
10569 * Returns:
10570 * 1 if flag is set.
10571 * 0 if flag is not set.
10572 **/
10573static int
10574lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10575 struct lpfc_iocbq *piocbq, uint32_t flag)
10576{
10577 unsigned long iflags;
10578 int ret;
10579
10580 spin_lock_irqsave(&phba->hbalock, iflags);
10581 ret = piocbq->iocb_flag & flag;
10582 spin_unlock_irqrestore(&phba->hbalock, iflags);
10583 return ret;
10584
10585}
10586
e59058c4 10587/**
3621a710 10588 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
10589 * @phba: Pointer to HBA context object..
10590 * @pring: Pointer to sli ring.
10591 * @piocb: Pointer to command iocb.
10592 * @prspiocbq: Pointer to response iocb.
10593 * @timeout: Timeout in number of seconds.
10594 *
10595 * This function issues the iocb to firmware and waits for the
5a0916b4
JS
10596 * iocb to complete. The iocb_cmpl field of the shall be used
10597 * to handle iocbs which time out. If the field is NULL, the
10598 * function shall free the iocbq structure. If more clean up is
10599 * needed, the caller is expected to provide a completion function
10600 * that will provide the needed clean up. If the iocb command is
10601 * not completed within timeout seconds, the function will either
10602 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10603 * completion function set in the iocb_cmpl field and then return
10604 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10605 * resources if this function returns IOCB_TIMEDOUT.
e59058c4
JS
10606 * The function waits for the iocb completion using an
10607 * non-interruptible wait.
10608 * This function will sleep while waiting for iocb completion.
10609 * So, this function should not be called from any context which
10610 * does not allow sleeping. Due to the same reason, this function
10611 * cannot be called with interrupt disabled.
10612 * This function assumes that the iocb completions occur while
10613 * this function sleep. So, this function cannot be called from
10614 * the thread which process iocb completion for this ring.
10615 * This function clears the iocb_flag of the iocb object before
10616 * issuing the iocb and the iocb completion handler sets this
10617 * flag and wakes this thread when the iocb completes.
10618 * The contents of the response iocb will be copied to prspiocbq
10619 * by the completion handler when the command completes.
10620 * This function returns IOCB_SUCCESS when success.
10621 * This function is called with no lock held.
10622 **/
dea3101e 10623int
2e0fef85 10624lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 10625 uint32_t ring_number,
2e0fef85
JS
10626 struct lpfc_iocbq *piocb,
10627 struct lpfc_iocbq *prspiocbq,
68876920 10628 uint32_t timeout)
dea3101e 10629{
7259f0d0 10630 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
10631 long timeleft, timeout_req = 0;
10632 int retval = IOCB_SUCCESS;
875fbdfe 10633 uint32_t creg_val;
0e9bb8d7
JS
10634 struct lpfc_iocbq *iocb;
10635 int txq_cnt = 0;
10636 int txcmplq_cnt = 0;
2a9bf3d0 10637 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5a0916b4
JS
10638 unsigned long iflags;
10639 bool iocb_completed = true;
10640
dea3101e 10641 /*
68876920
JSEC
10642 * If the caller has provided a response iocbq buffer, then context2
10643 * is NULL or its an error.
dea3101e 10644 */
68876920
JSEC
10645 if (prspiocbq) {
10646 if (piocb->context2)
10647 return IOCB_ERROR;
10648 piocb->context2 = prspiocbq;
dea3101e
JB
10649 }
10650
5a0916b4 10651 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
68876920
JSEC
10652 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10653 piocb->context_un.wait_queue = &done_q;
5a0916b4 10654 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea3101e 10655
875fbdfe 10656 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
10657 if (lpfc_readl(phba->HCregaddr, &creg_val))
10658 return IOCB_ERROR;
875fbdfe
JSEC
10659 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10660 writel(creg_val, phba->HCregaddr);
10661 readl(phba->HCregaddr); /* flush */
10662 }
10663
2a9bf3d0
JS
10664 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10665 SLI_IOCB_RET_IOCB);
68876920 10666 if (retval == IOCB_SUCCESS) {
256ec0d0 10667 timeout_req = msecs_to_jiffies(timeout * 1000);
68876920 10668 timeleft = wait_event_timeout(done_q,
d11e31dd 10669 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 10670 timeout_req);
5a0916b4
JS
10671 spin_lock_irqsave(&phba->hbalock, iflags);
10672 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
10673
10674 /*
10675 * IOCB timed out. Inform the wake iocb wait
10676 * completion function and set local status
10677 */
dea3101e 10678
5a0916b4
JS
10679 iocb_completed = false;
10680 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10681 }
10682 spin_unlock_irqrestore(&phba->hbalock, iflags);
10683 if (iocb_completed) {
7054a606 10684 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 10685 "0331 IOCB wake signaled\n");
53151bbb
JS
10686 /* Note: we are not indicating if the IOCB has a success
10687 * status or not - that's for the caller to check.
10688 * IOCB_SUCCESS means just that the command was sent and
10689 * completed. Not that it completed successfully.
10690 * */
7054a606 10691 } else if (timeleft == 0) {
68876920 10692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
10693 "0338 IOCB wait timeout error - no "
10694 "wake response Data x%x\n", timeout);
68876920 10695 retval = IOCB_TIMEDOUT;
7054a606 10696 } else {
68876920 10697 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
10698 "0330 IOCB wake NOT set, "
10699 "Data x%x x%lx\n",
68876920
JSEC
10700 timeout, (timeleft / jiffies));
10701 retval = IOCB_TIMEDOUT;
dea3101e 10702 }
2a9bf3d0 10703 } else if (retval == IOCB_BUSY) {
0e9bb8d7
JS
10704 if (phba->cfg_log_verbose & LOG_SLI) {
10705 list_for_each_entry(iocb, &pring->txq, list) {
10706 txq_cnt++;
10707 }
10708 list_for_each_entry(iocb, &pring->txcmplq, list) {
10709 txcmplq_cnt++;
10710 }
10711 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10712 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10713 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10714 }
2a9bf3d0 10715 return retval;
68876920
JSEC
10716 } else {
10717 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 10718 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 10719 retval);
68876920 10720 retval = IOCB_ERROR;
dea3101e
JB
10721 }
10722
875fbdfe 10723 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
10724 if (lpfc_readl(phba->HCregaddr, &creg_val))
10725 return IOCB_ERROR;
875fbdfe
JSEC
10726 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10727 writel(creg_val, phba->HCregaddr);
10728 readl(phba->HCregaddr); /* flush */
10729 }
10730
68876920
JSEC
10731 if (prspiocbq)
10732 piocb->context2 = NULL;
10733
10734 piocb->context_un.wait_queue = NULL;
10735 piocb->iocb_cmpl = NULL;
dea3101e
JB
10736 return retval;
10737}
68876920 10738
e59058c4 10739/**
3621a710 10740 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
10741 * @phba: Pointer to HBA context object.
10742 * @pmboxq: Pointer to driver mailbox object.
10743 * @timeout: Timeout in number of seconds.
10744 *
10745 * This function issues the mailbox to firmware and waits for the
10746 * mailbox command to complete. If the mailbox command is not
10747 * completed within timeout seconds, it returns MBX_TIMEOUT.
10748 * The function waits for the mailbox completion using an
10749 * interruptible wait. If the thread is woken up due to a
10750 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10751 * should not free the mailbox resources, if this function returns
10752 * MBX_TIMEOUT.
10753 * This function will sleep while waiting for mailbox completion.
10754 * So, this function should not be called from any context which
10755 * does not allow sleeping. Due to the same reason, this function
10756 * cannot be called with interrupt disabled.
10757 * This function assumes that the mailbox completion occurs while
10758 * this function sleep. So, this function cannot be called from
10759 * the worker thread which processes mailbox completion.
10760 * This function is called in the context of HBA management
10761 * applications.
10762 * This function returns MBX_SUCCESS when successful.
10763 * This function is called with no lock held.
10764 **/
dea3101e 10765int
2e0fef85 10766lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e
JB
10767 uint32_t timeout)
10768{
7259f0d0 10769 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
b230b8a2 10770 MAILBOX_t *mb = NULL;
dea3101e 10771 int retval;
858c9f6c 10772 unsigned long flag;
dea3101e 10773
b230b8a2 10774 /* The caller might set context1 for extended buffer */
98c9ea5c 10775 if (pmboxq->context1)
b230b8a2 10776 mb = (MAILBOX_t *)pmboxq->context1;
dea3101e 10777
495a714c 10778 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e
JB
10779 /* setup wake call as IOCB callback */
10780 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10781 /* setup context field to pass wait_queue pointer to wake function */
10782 pmboxq->context1 = &done_q;
10783
dea3101e
JB
10784 /* now issue the command */
10785 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea3101e 10786 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
10787 wait_event_interruptible_timeout(done_q,
10788 pmboxq->mbox_flag & LPFC_MBX_WAKE,
256ec0d0 10789 msecs_to_jiffies(timeout * 1000));
7054a606 10790
858c9f6c 10791 spin_lock_irqsave(&phba->hbalock, flag);
b230b8a2
JS
10792 /* restore the possible extended buffer for free resource */
10793 pmboxq->context1 = (uint8_t *)mb;
7054a606
JS
10794 /*
10795 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10796 * else do not free the resources.
10797 */
d7c47992 10798 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 10799 retval = MBX_SUCCESS;
d7c47992 10800 } else {
7054a606 10801 retval = MBX_TIMEOUT;
858c9f6c
JS
10802 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10803 }
10804 spin_unlock_irqrestore(&phba->hbalock, flag);
b230b8a2
JS
10805 } else {
10806 /* restore the possible extended buffer for free resource */
10807 pmboxq->context1 = (uint8_t *)mb;
dea3101e
JB
10808 }
10809
dea3101e
JB
10810 return retval;
10811}
10812
e59058c4 10813/**
3772a991 10814 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
10815 * @phba: Pointer to HBA context.
10816 *
3772a991
JS
10817 * This function is called to shutdown the driver's mailbox sub-system.
10818 * It first marks the mailbox sub-system is in a block state to prevent
10819 * the asynchronous mailbox command from issued off the pending mailbox
10820 * command queue. If the mailbox command sub-system shutdown is due to
10821 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10822 * the mailbox sub-system flush routine to forcefully bring down the
10823 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10824 * as with offline or HBA function reset), this routine will wait for the
10825 * outstanding mailbox command to complete before invoking the mailbox
10826 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 10827 **/
3772a991 10828void
618a5230 10829lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
b4c02652 10830{
3772a991 10831 struct lpfc_sli *psli = &phba->sli;
3772a991 10832 unsigned long timeout;
b4c02652 10833
618a5230
JS
10834 if (mbx_action == LPFC_MBX_NO_WAIT) {
10835 /* delay 100ms for port state */
10836 msleep(100);
10837 lpfc_sli_mbox_sys_flush(phba);
10838 return;
10839 }
a183a15f 10840 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
d7069f09 10841
3772a991
JS
10842 spin_lock_irq(&phba->hbalock);
10843 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
b4c02652 10844
3772a991 10845 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
3772a991
JS
10846 /* Determine how long we might wait for the active mailbox
10847 * command to be gracefully completed by firmware.
10848 */
a183a15f
JS
10849 if (phba->sli.mbox_active)
10850 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10851 phba->sli.mbox_active) *
10852 1000) + jiffies;
10853 spin_unlock_irq(&phba->hbalock);
10854
3772a991
JS
10855 while (phba->sli.mbox_active) {
10856 /* Check active mailbox complete status every 2ms */
10857 msleep(2);
10858 if (time_after(jiffies, timeout))
10859 /* Timeout, let the mailbox flush routine to
10860 * forcefully release active mailbox command
10861 */
10862 break;
10863 }
d7069f09
JS
10864 } else
10865 spin_unlock_irq(&phba->hbalock);
10866
3772a991
JS
10867 lpfc_sli_mbox_sys_flush(phba);
10868}
ed957684 10869
3772a991
JS
10870/**
10871 * lpfc_sli_eratt_read - read sli-3 error attention events
10872 * @phba: Pointer to HBA context.
10873 *
10874 * This function is called to read the SLI3 device error attention registers
10875 * for possible error attention events. The caller must hold the hostlock
10876 * with spin_lock_irq().
10877 *
25985edc 10878 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
10879 * Register and returns 0 otherwise.
10880 **/
10881static int
10882lpfc_sli_eratt_read(struct lpfc_hba *phba)
10883{
10884 uint32_t ha_copy;
b4c02652 10885
3772a991 10886 /* Read chip Host Attention (HA) register */
9940b97b
JS
10887 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10888 goto unplug_err;
10889
3772a991
JS
10890 if (ha_copy & HA_ERATT) {
10891 /* Read host status register to retrieve error event */
9940b97b
JS
10892 if (lpfc_sli_read_hs(phba))
10893 goto unplug_err;
b4c02652 10894
3772a991
JS
10895 /* Check if there is a deferred error condition is active */
10896 if ((HS_FFER1 & phba->work_hs) &&
10897 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 10898 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 10899 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
10900 /* Clear all interrupt enable conditions */
10901 writel(0, phba->HCregaddr);
10902 readl(phba->HCregaddr);
10903 }
10904
10905 /* Set the driver HA work bitmap */
3772a991
JS
10906 phba->work_ha |= HA_ERATT;
10907 /* Indicate polling handles this ERATT */
10908 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
10909 return 1;
10910 }
10911 return 0;
9940b97b
JS
10912
10913unplug_err:
10914 /* Set the driver HS work bitmap */
10915 phba->work_hs |= UNPLUG_ERR;
10916 /* Set the driver HA work bitmap */
10917 phba->work_ha |= HA_ERATT;
10918 /* Indicate polling handles this ERATT */
10919 phba->hba_flag |= HBA_ERATT_HANDLED;
10920 return 1;
b4c02652
JS
10921}
10922
da0436e9
JS
10923/**
10924 * lpfc_sli4_eratt_read - read sli-4 error attention events
10925 * @phba: Pointer to HBA context.
10926 *
10927 * This function is called to read the SLI4 device error attention registers
10928 * for possible error attention events. The caller must hold the hostlock
10929 * with spin_lock_irq().
10930 *
25985edc 10931 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
10932 * Register and returns 0 otherwise.
10933 **/
10934static int
10935lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10936{
10937 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
10938 uint32_t if_type, portsmphr;
10939 struct lpfc_register portstat_reg;
da0436e9 10940
2fcee4bf
JS
10941 /*
10942 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
10943 * registers for error attention. This can be changed later.
10944 */
2fcee4bf
JS
10945 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10946 switch (if_type) {
10947 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
10948 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10949 &uerr_sta_lo) ||
10950 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10951 &uerr_sta_hi)) {
10952 phba->work_hs |= UNPLUG_ERR;
10953 phba->work_ha |= HA_ERATT;
10954 phba->hba_flag |= HBA_ERATT_HANDLED;
10955 return 1;
10956 }
2fcee4bf
JS
10957 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10958 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10960 "1423 HBA Unrecoverable error: "
10961 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10962 "ue_mask_lo_reg=0x%x, "
10963 "ue_mask_hi_reg=0x%x\n",
10964 uerr_sta_lo, uerr_sta_hi,
10965 phba->sli4_hba.ue_mask_lo,
10966 phba->sli4_hba.ue_mask_hi);
10967 phba->work_status[0] = uerr_sta_lo;
10968 phba->work_status[1] = uerr_sta_hi;
10969 phba->work_ha |= HA_ERATT;
10970 phba->hba_flag |= HBA_ERATT_HANDLED;
10971 return 1;
10972 }
10973 break;
10974 case LPFC_SLI_INTF_IF_TYPE_2:
9940b97b
JS
10975 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10976 &portstat_reg.word0) ||
10977 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10978 &portsmphr)){
10979 phba->work_hs |= UNPLUG_ERR;
10980 phba->work_ha |= HA_ERATT;
10981 phba->hba_flag |= HBA_ERATT_HANDLED;
10982 return 1;
10983 }
2fcee4bf
JS
10984 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10985 phba->work_status[0] =
10986 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10987 phba->work_status[1] =
10988 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10989 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2e90f4b5 10990 "2885 Port Status Event: "
2fcee4bf
JS
10991 "port status reg 0x%x, "
10992 "port smphr reg 0x%x, "
10993 "error 1=0x%x, error 2=0x%x\n",
10994 portstat_reg.word0,
10995 portsmphr,
10996 phba->work_status[0],
10997 phba->work_status[1]);
10998 phba->work_ha |= HA_ERATT;
10999 phba->hba_flag |= HBA_ERATT_HANDLED;
11000 return 1;
11001 }
11002 break;
11003 case LPFC_SLI_INTF_IF_TYPE_1:
11004 default:
a747c9ce 11005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
11006 "2886 HBA Error Attention on unsupported "
11007 "if type %d.", if_type);
a747c9ce 11008 return 1;
da0436e9 11009 }
2fcee4bf 11010
da0436e9
JS
11011 return 0;
11012}
11013
e59058c4 11014/**
3621a710 11015 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
11016 * @phba: Pointer to HBA context.
11017 *
3772a991 11018 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
11019 * error attention register bit for error attention events.
11020 *
25985edc 11021 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
11022 * Register and returns 0 otherwise.
11023 **/
11024int
11025lpfc_sli_check_eratt(struct lpfc_hba *phba)
11026{
11027 uint32_t ha_copy;
11028
11029 /* If somebody is waiting to handle an eratt, don't process it
11030 * here. The brdkill function will do this.
11031 */
11032 if (phba->link_flag & LS_IGNORE_ERATT)
11033 return 0;
11034
11035 /* Check if interrupt handler handles this ERATT */
11036 spin_lock_irq(&phba->hbalock);
11037 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11038 /* Interrupt handler has handled ERATT */
11039 spin_unlock_irq(&phba->hbalock);
11040 return 0;
11041 }
11042
a257bf90
JS
11043 /*
11044 * If there is deferred error attention, do not check for error
11045 * attention
11046 */
11047 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11048 spin_unlock_irq(&phba->hbalock);
11049 return 0;
11050 }
11051
3772a991
JS
11052 /* If PCI channel is offline, don't process it */
11053 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 11054 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11055 return 0;
11056 }
11057
11058 switch (phba->sli_rev) {
11059 case LPFC_SLI_REV2:
11060 case LPFC_SLI_REV3:
11061 /* Read chip Host Attention (HA) register */
11062 ha_copy = lpfc_sli_eratt_read(phba);
11063 break;
da0436e9 11064 case LPFC_SLI_REV4:
2fcee4bf 11065 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
11066 ha_copy = lpfc_sli4_eratt_read(phba);
11067 break;
3772a991
JS
11068 default:
11069 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11070 "0299 Invalid SLI revision (%d)\n",
11071 phba->sli_rev);
11072 ha_copy = 0;
11073 break;
9399627f
JS
11074 }
11075 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11076
11077 return ha_copy;
11078}
11079
11080/**
11081 * lpfc_intr_state_check - Check device state for interrupt handling
11082 * @phba: Pointer to HBA context.
11083 *
11084 * This inline routine checks whether a device or its PCI slot is in a state
11085 * that the interrupt should be handled.
11086 *
11087 * This function returns 0 if the device or the PCI slot is in a state that
11088 * interrupt should be handled, otherwise -EIO.
11089 */
11090static inline int
11091lpfc_intr_state_check(struct lpfc_hba *phba)
11092{
11093 /* If the pci channel is offline, ignore all the interrupts */
11094 if (unlikely(pci_channel_offline(phba->pcidev)))
11095 return -EIO;
11096
11097 /* Update device level interrupt statistics */
11098 phba->sli.slistat.sli_intr++;
11099
11100 /* Ignore all interrupts during initialization. */
11101 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11102 return -EIO;
11103
9399627f
JS
11104 return 0;
11105}
11106
11107/**
3772a991 11108 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
11109 * @irq: Interrupt number.
11110 * @dev_id: The device context pointer.
11111 *
9399627f 11112 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
11113 * service routine when device with SLI-3 interface spec is enabled with
11114 * MSI-X multi-message interrupt mode and there are slow-path events in
11115 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11116 * interrupt mode, this function is called as part of the device-level
11117 * interrupt handler. When the PCI slot is in error recovery or the HBA
11118 * is undergoing initialization, the interrupt handler will not process
11119 * the interrupt. The link attention and ELS ring attention events are
11120 * handled by the worker thread. The interrupt handler signals the worker
11121 * thread and returns for these events. This function is called without
11122 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
11123 * structures.
11124 *
11125 * This function returns IRQ_HANDLED when interrupt is handled else it
11126 * returns IRQ_NONE.
e59058c4 11127 **/
dea3101e 11128irqreturn_t
3772a991 11129lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 11130{
2e0fef85 11131 struct lpfc_hba *phba;
a747c9ce 11132 uint32_t ha_copy, hc_copy;
dea3101e
JB
11133 uint32_t work_ha_copy;
11134 unsigned long status;
5b75da2f 11135 unsigned long iflag;
dea3101e
JB
11136 uint32_t control;
11137
92d7f7b0 11138 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
11139 struct lpfc_vport *vport;
11140 struct lpfc_nodelist *ndlp;
11141 struct lpfc_dmabuf *mp;
92d7f7b0
JS
11142 LPFC_MBOXQ_t *pmb;
11143 int rc;
11144
dea3101e
JB
11145 /*
11146 * Get the driver's phba structure from the dev_id and
11147 * assume the HBA is not interrupting.
11148 */
9399627f 11149 phba = (struct lpfc_hba *)dev_id;
dea3101e
JB
11150
11151 if (unlikely(!phba))
11152 return IRQ_NONE;
11153
dea3101e 11154 /*
9399627f
JS
11155 * Stuff needs to be attented to when this function is invoked as an
11156 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 11157 */
9399627f 11158 if (phba->intr_type == MSIX) {
3772a991
JS
11159 /* Check device state for handling interrupt */
11160 if (lpfc_intr_state_check(phba))
9399627f
JS
11161 return IRQ_NONE;
11162 /* Need to read HA REG for slow-path events */
5b75da2f 11163 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
11164 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11165 goto unplug_error;
9399627f
JS
11166 /* If somebody is waiting to handle an eratt don't process it
11167 * here. The brdkill function will do this.
11168 */
11169 if (phba->link_flag & LS_IGNORE_ERATT)
11170 ha_copy &= ~HA_ERATT;
11171 /* Check the need for handling ERATT in interrupt handler */
11172 if (ha_copy & HA_ERATT) {
11173 if (phba->hba_flag & HBA_ERATT_HANDLED)
11174 /* ERATT polling has handled ERATT */
11175 ha_copy &= ~HA_ERATT;
11176 else
11177 /* Indicate interrupt handler handles ERATT */
11178 phba->hba_flag |= HBA_ERATT_HANDLED;
11179 }
a257bf90
JS
11180
11181 /*
11182 * If there is deferred error attention, do not check for any
11183 * interrupt.
11184 */
11185 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 11186 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
11187 return IRQ_NONE;
11188 }
11189
9399627f 11190 /* Clear up only attention source related to slow-path */
9940b97b
JS
11191 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11192 goto unplug_error;
11193
a747c9ce
JS
11194 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11195 HC_LAINT_ENA | HC_ERINT_ENA),
11196 phba->HCregaddr);
9399627f
JS
11197 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11198 phba->HAregaddr);
a747c9ce 11199 writel(hc_copy, phba->HCregaddr);
9399627f 11200 readl(phba->HAregaddr); /* flush */
5b75da2f 11201 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
11202 } else
11203 ha_copy = phba->ha_copy;
dea3101e 11204
dea3101e
JB
11205 work_ha_copy = ha_copy & phba->work_ha_mask;
11206
9399627f 11207 if (work_ha_copy) {
dea3101e
JB
11208 if (work_ha_copy & HA_LATT) {
11209 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11210 /*
11211 * Turn off Link Attention interrupts
11212 * until CLEAR_LA done
11213 */
5b75da2f 11214 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 11215 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
11216 if (lpfc_readl(phba->HCregaddr, &control))
11217 goto unplug_error;
dea3101e
JB
11218 control &= ~HC_LAINT_ENA;
11219 writel(control, phba->HCregaddr);
11220 readl(phba->HCregaddr); /* flush */
5b75da2f 11221 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
11222 }
11223 else
11224 work_ha_copy &= ~HA_LATT;
11225 }
11226
9399627f 11227 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
11228 /*
11229 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11230 * the only slow ring.
11231 */
11232 status = (work_ha_copy &
11233 (HA_RXMASK << (4*LPFC_ELS_RING)));
11234 status >>= (4*LPFC_ELS_RING);
11235 if (status & HA_RXMASK) {
5b75da2f 11236 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
11237 if (lpfc_readl(phba->HCregaddr, &control))
11238 goto unplug_error;
a58cbd52
JS
11239
11240 lpfc_debugfs_slow_ring_trc(phba,
11241 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11242 control, status,
11243 (uint32_t)phba->sli.slistat.sli_intr);
11244
858c9f6c 11245 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
11246 lpfc_debugfs_slow_ring_trc(phba,
11247 "ISR Disable ring:"
11248 "pwork:x%x hawork:x%x wait:x%x",
11249 phba->work_ha, work_ha_copy,
11250 (uint32_t)((unsigned long)
5e9d9b82 11251 &phba->work_waitq));
a58cbd52 11252
858c9f6c
JS
11253 control &=
11254 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e
JB
11255 writel(control, phba->HCregaddr);
11256 readl(phba->HCregaddr); /* flush */
dea3101e 11257 }
a58cbd52
JS
11258 else {
11259 lpfc_debugfs_slow_ring_trc(phba,
11260 "ISR slow ring: pwork:"
11261 "x%x hawork:x%x wait:x%x",
11262 phba->work_ha, work_ha_copy,
11263 (uint32_t)((unsigned long)
5e9d9b82 11264 &phba->work_waitq));
a58cbd52 11265 }
5b75da2f 11266 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
11267 }
11268 }
5b75da2f 11269 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 11270 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
11271 if (lpfc_sli_read_hs(phba))
11272 goto unplug_error;
a257bf90
JS
11273 /*
11274 * Check if there is a deferred error condition
11275 * is active
11276 */
11277 if ((HS_FFER1 & phba->work_hs) &&
11278 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
11279 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11280 phba->work_hs)) {
a257bf90
JS
11281 phba->hba_flag |= DEFER_ERATT;
11282 /* Clear all interrupt enable conditions */
11283 writel(0, phba->HCregaddr);
11284 readl(phba->HCregaddr);
11285 }
11286 }
11287
9399627f 11288 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 11289 pmb = phba->sli.mbox_active;
04c68496 11290 pmbox = &pmb->u.mb;
34b02dcd 11291 mbox = phba->mbox;
858c9f6c 11292 vport = pmb->vport;
92d7f7b0
JS
11293
11294 /* First check out the status word */
11295 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11296 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 11297 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
11298 /*
11299 * Stray Mailbox Interrupt, mbxCommand <cmd>
11300 * mbxStatus <status>
11301 */
09372820 11302 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 11303 LOG_SLI,
e8b62011 11304 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
11305 "Interrupt mbxCommand x%x "
11306 "mbxStatus x%x\n",
e8b62011 11307 (vport ? vport->vpi : 0),
92d7f7b0
JS
11308 pmbox->mbxCommand,
11309 pmbox->mbxStatus);
09372820
JS
11310 /* clear mailbox attention bit */
11311 work_ha_copy &= ~HA_MBATT;
11312 } else {
97eab634 11313 phba->sli.mbox_active = NULL;
5b75da2f 11314 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
11315 phba->last_completion_time = jiffies;
11316 del_timer(&phba->sli.mbox_tmo);
09372820
JS
11317 if (pmb->mbox_cmpl) {
11318 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11319 MAILBOX_CMD_SIZE);
7a470277
JS
11320 if (pmb->out_ext_byte_len &&
11321 pmb->context2)
11322 lpfc_sli_pcimem_bcopy(
11323 phba->mbox_ext,
11324 pmb->context2,
11325 pmb->out_ext_byte_len);
09372820
JS
11326 }
11327 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11328 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11329
11330 lpfc_debugfs_disc_trc(vport,
11331 LPFC_DISC_TRC_MBOX_VPORT,
11332 "MBOX dflt rpi: : "
11333 "status:x%x rpi:x%x",
11334 (uint32_t)pmbox->mbxStatus,
11335 pmbox->un.varWords[0], 0);
11336
11337 if (!pmbox->mbxStatus) {
11338 mp = (struct lpfc_dmabuf *)
11339 (pmb->context1);
11340 ndlp = (struct lpfc_nodelist *)
11341 pmb->context2;
11342
11343 /* Reg_LOGIN of dflt RPI was
11344 * successful. new lets get
11345 * rid of the RPI using the
11346 * same mbox buffer.
11347 */
11348 lpfc_unreg_login(phba,
11349 vport->vpi,
11350 pmbox->un.varWords[0],
11351 pmb);
11352 pmb->mbox_cmpl =
11353 lpfc_mbx_cmpl_dflt_rpi;
11354 pmb->context1 = mp;
11355 pmb->context2 = ndlp;
11356 pmb->vport = vport;
58da1ffb
JS
11357 rc = lpfc_sli_issue_mbox(phba,
11358 pmb,
11359 MBX_NOWAIT);
11360 if (rc != MBX_BUSY)
11361 lpfc_printf_log(phba,
11362 KERN_ERR,
11363 LOG_MBOX | LOG_SLI,
d7c255b2 11364 "0350 rc should have"
6a9c52cf 11365 "been MBX_BUSY\n");
3772a991
JS
11366 if (rc != MBX_NOT_FINISHED)
11367 goto send_current_mbox;
09372820 11368 }
858c9f6c 11369 }
5b75da2f
JS
11370 spin_lock_irqsave(
11371 &phba->pport->work_port_lock,
11372 iflag);
09372820
JS
11373 phba->pport->work_port_events &=
11374 ~WORKER_MBOX_TMO;
5b75da2f
JS
11375 spin_unlock_irqrestore(
11376 &phba->pport->work_port_lock,
11377 iflag);
09372820 11378 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 11379 }
97eab634 11380 } else
5b75da2f 11381 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 11382
92d7f7b0
JS
11383 if ((work_ha_copy & HA_MBATT) &&
11384 (phba->sli.mbox_active == NULL)) {
858c9f6c 11385send_current_mbox:
92d7f7b0 11386 /* Process next mailbox command if there is one */
58da1ffb
JS
11387 do {
11388 rc = lpfc_sli_issue_mbox(phba, NULL,
11389 MBX_NOWAIT);
11390 } while (rc == MBX_NOT_FINISHED);
11391 if (rc != MBX_SUCCESS)
11392 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11393 LOG_SLI, "0349 rc should be "
6a9c52cf 11394 "MBX_SUCCESS\n");
92d7f7b0
JS
11395 }
11396
5b75da2f 11397 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 11398 phba->work_ha |= work_ha_copy;
5b75da2f 11399 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 11400 lpfc_worker_wake_up(phba);
dea3101e 11401 }
9399627f 11402 return IRQ_HANDLED;
9940b97b
JS
11403unplug_error:
11404 spin_unlock_irqrestore(&phba->hbalock, iflag);
11405 return IRQ_HANDLED;
dea3101e 11406
3772a991 11407} /* lpfc_sli_sp_intr_handler */
9399627f
JS
11408
11409/**
3772a991 11410 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
11411 * @irq: Interrupt number.
11412 * @dev_id: The device context pointer.
11413 *
11414 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
11415 * service routine when device with SLI-3 interface spec is enabled with
11416 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11417 * ring event in the HBA. However, when the device is enabled with either
11418 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11419 * device-level interrupt handler. When the PCI slot is in error recovery
11420 * or the HBA is undergoing initialization, the interrupt handler will not
11421 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11422 * the intrrupt context. This function is called without any lock held.
11423 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
11424 *
11425 * This function returns IRQ_HANDLED when interrupt is handled else it
11426 * returns IRQ_NONE.
11427 **/
11428irqreturn_t
3772a991 11429lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
11430{
11431 struct lpfc_hba *phba;
11432 uint32_t ha_copy;
11433 unsigned long status;
5b75da2f 11434 unsigned long iflag;
9399627f
JS
11435
11436 /* Get the driver's phba structure from the dev_id and
11437 * assume the HBA is not interrupting.
11438 */
11439 phba = (struct lpfc_hba *) dev_id;
11440
11441 if (unlikely(!phba))
11442 return IRQ_NONE;
11443
11444 /*
11445 * Stuff needs to be attented to when this function is invoked as an
11446 * individual interrupt handler in MSI-X multi-message interrupt mode
11447 */
11448 if (phba->intr_type == MSIX) {
3772a991
JS
11449 /* Check device state for handling interrupt */
11450 if (lpfc_intr_state_check(phba))
9399627f
JS
11451 return IRQ_NONE;
11452 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
11453 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11454 return IRQ_HANDLED;
9399627f 11455 /* Clear up only attention source related to fast-path */
5b75da2f 11456 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
11457 /*
11458 * If there is deferred error attention, do not check for
11459 * any interrupt.
11460 */
11461 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 11462 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
11463 return IRQ_NONE;
11464 }
9399627f
JS
11465 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11466 phba->HAregaddr);
11467 readl(phba->HAregaddr); /* flush */
5b75da2f 11468 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
11469 } else
11470 ha_copy = phba->ha_copy;
dea3101e
JB
11471
11472 /*
9399627f 11473 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 11474 */
9399627f
JS
11475 ha_copy &= ~(phba->work_ha_mask);
11476
11477 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 11478 status >>= (4*LPFC_FCP_RING);
858c9f6c 11479 if (status & HA_RXMASK)
dea3101e
JB
11480 lpfc_sli_handle_fast_ring_event(phba,
11481 &phba->sli.ring[LPFC_FCP_RING],
11482 status);
a4bc3379
JS
11483
11484 if (phba->cfg_multi_ring_support == 2) {
11485 /*
9399627f
JS
11486 * Process all events on extra ring. Take the optimized path
11487 * for extra ring IO.
a4bc3379 11488 */
9399627f 11489 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 11490 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 11491 if (status & HA_RXMASK) {
a4bc3379
JS
11492 lpfc_sli_handle_fast_ring_event(phba,
11493 &phba->sli.ring[LPFC_EXTRA_RING],
11494 status);
11495 }
11496 }
dea3101e 11497 return IRQ_HANDLED;
3772a991 11498} /* lpfc_sli_fp_intr_handler */
9399627f
JS
11499
11500/**
3772a991 11501 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
11502 * @irq: Interrupt number.
11503 * @dev_id: The device context pointer.
11504 *
3772a991
JS
11505 * This function is the HBA device-level interrupt handler to device with
11506 * SLI-3 interface spec, called from the PCI layer when either MSI or
11507 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11508 * requires driver attention. This function invokes the slow-path interrupt
11509 * attention handling function and fast-path interrupt attention handling
11510 * function in turn to process the relevant HBA attention events. This
11511 * function is called without any lock held. It gets the hbalock to access
11512 * and update SLI data structures.
9399627f
JS
11513 *
11514 * This function returns IRQ_HANDLED when interrupt is handled, else it
11515 * returns IRQ_NONE.
11516 **/
11517irqreturn_t
3772a991 11518lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
11519{
11520 struct lpfc_hba *phba;
11521 irqreturn_t sp_irq_rc, fp_irq_rc;
11522 unsigned long status1, status2;
a747c9ce 11523 uint32_t hc_copy;
9399627f
JS
11524
11525 /*
11526 * Get the driver's phba structure from the dev_id and
11527 * assume the HBA is not interrupting.
11528 */
11529 phba = (struct lpfc_hba *) dev_id;
11530
11531 if (unlikely(!phba))
11532 return IRQ_NONE;
11533
3772a991
JS
11534 /* Check device state for handling interrupt */
11535 if (lpfc_intr_state_check(phba))
9399627f
JS
11536 return IRQ_NONE;
11537
11538 spin_lock(&phba->hbalock);
9940b97b
JS
11539 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11540 spin_unlock(&phba->hbalock);
11541 return IRQ_HANDLED;
11542 }
11543
9399627f
JS
11544 if (unlikely(!phba->ha_copy)) {
11545 spin_unlock(&phba->hbalock);
11546 return IRQ_NONE;
11547 } else if (phba->ha_copy & HA_ERATT) {
11548 if (phba->hba_flag & HBA_ERATT_HANDLED)
11549 /* ERATT polling has handled ERATT */
11550 phba->ha_copy &= ~HA_ERATT;
11551 else
11552 /* Indicate interrupt handler handles ERATT */
11553 phba->hba_flag |= HBA_ERATT_HANDLED;
11554 }
11555
a257bf90
JS
11556 /*
11557 * If there is deferred error attention, do not check for any interrupt.
11558 */
11559 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 11560 spin_unlock(&phba->hbalock);
a257bf90
JS
11561 return IRQ_NONE;
11562 }
11563
9399627f 11564 /* Clear attention sources except link and error attentions */
9940b97b
JS
11565 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11566 spin_unlock(&phba->hbalock);
11567 return IRQ_HANDLED;
11568 }
a747c9ce
JS
11569 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11570 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11571 phba->HCregaddr);
9399627f 11572 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 11573 writel(hc_copy, phba->HCregaddr);
9399627f
JS
11574 readl(phba->HAregaddr); /* flush */
11575 spin_unlock(&phba->hbalock);
11576
11577 /*
11578 * Invokes slow-path host attention interrupt handling as appropriate.
11579 */
11580
11581 /* status of events with mailbox and link attention */
11582 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11583
11584 /* status of events with ELS ring */
11585 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11586 status2 >>= (4*LPFC_ELS_RING);
11587
11588 if (status1 || (status2 & HA_RXMASK))
3772a991 11589 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
11590 else
11591 sp_irq_rc = IRQ_NONE;
11592
11593 /*
11594 * Invoke fast-path host attention interrupt handling as appropriate.
11595 */
11596
11597 /* status of events with FCP ring */
11598 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11599 status1 >>= (4*LPFC_FCP_RING);
11600
11601 /* status of events with extra ring */
11602 if (phba->cfg_multi_ring_support == 2) {
11603 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11604 status2 >>= (4*LPFC_EXTRA_RING);
11605 } else
11606 status2 = 0;
11607
11608 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 11609 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
11610 else
11611 fp_irq_rc = IRQ_NONE;
dea3101e 11612
9399627f
JS
11613 /* Return device-level interrupt handling status */
11614 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 11615} /* lpfc_sli_intr_handler */
4f774513
JS
11616
11617/**
11618 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11619 * @phba: pointer to lpfc hba data structure.
11620 *
11621 * This routine is invoked by the worker thread to process all the pending
11622 * SLI4 FCP abort XRI events.
11623 **/
11624void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11625{
11626 struct lpfc_cq_event *cq_event;
11627
11628 /* First, declare the fcp xri abort event has been handled */
11629 spin_lock_irq(&phba->hbalock);
11630 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11631 spin_unlock_irq(&phba->hbalock);
11632 /* Now, handle all the fcp xri abort events */
11633 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11634 /* Get the first event from the head of the event queue */
11635 spin_lock_irq(&phba->hbalock);
11636 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11637 cq_event, struct lpfc_cq_event, list);
11638 spin_unlock_irq(&phba->hbalock);
11639 /* Notify aborted XRI for FCP work queue */
11640 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11641 /* Free the event processed back to the free pool */
11642 lpfc_sli4_cq_event_release(phba, cq_event);
11643 }
11644}
11645
11646/**
11647 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11648 * @phba: pointer to lpfc hba data structure.
11649 *
11650 * This routine is invoked by the worker thread to process all the pending
11651 * SLI4 els abort xri events.
11652 **/
11653void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11654{
11655 struct lpfc_cq_event *cq_event;
11656
11657 /* First, declare the els xri abort event has been handled */
11658 spin_lock_irq(&phba->hbalock);
11659 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11660 spin_unlock_irq(&phba->hbalock);
11661 /* Now, handle all the els xri abort events */
11662 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11663 /* Get the first event from the head of the event queue */
11664 spin_lock_irq(&phba->hbalock);
11665 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11666 cq_event, struct lpfc_cq_event, list);
11667 spin_unlock_irq(&phba->hbalock);
11668 /* Notify aborted XRI for ELS work queue */
11669 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11670 /* Free the event processed back to the free pool */
11671 lpfc_sli4_cq_event_release(phba, cq_event);
11672 }
11673}
11674
341af102
JS
11675/**
11676 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11677 * @phba: pointer to lpfc hba data structure
11678 * @pIocbIn: pointer to the rspiocbq
11679 * @pIocbOut: pointer to the cmdiocbq
11680 * @wcqe: pointer to the complete wcqe
11681 *
11682 * This routine transfers the fields of a command iocbq to a response iocbq
11683 * by copying all the IOCB fields from command iocbq and transferring the
11684 * completion status information from the complete wcqe.
11685 **/
4f774513 11686static void
341af102
JS
11687lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11688 struct lpfc_iocbq *pIocbIn,
4f774513
JS
11689 struct lpfc_iocbq *pIocbOut,
11690 struct lpfc_wcqe_complete *wcqe)
11691{
af22741c 11692 int numBdes, i;
341af102 11693 unsigned long iflags;
af22741c
JS
11694 uint32_t status, max_response;
11695 struct lpfc_dmabuf *dmabuf;
11696 struct ulp_bde64 *bpl, bde;
4f774513
JS
11697 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11698
11699 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11700 sizeof(struct lpfc_iocbq) - offset);
4f774513 11701 /* Map WCQE parameters into irspiocb parameters */
acd6859b
JS
11702 status = bf_get(lpfc_wcqe_c_status, wcqe);
11703 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
4f774513
JS
11704 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11705 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11706 pIocbIn->iocb.un.fcpi.fcpi_parm =
11707 pIocbOut->iocb.un.fcpi.fcpi_parm -
11708 wcqe->total_data_placed;
11709 else
11710 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 11711 else {
4f774513 11712 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
af22741c
JS
11713 switch (pIocbOut->iocb.ulpCommand) {
11714 case CMD_ELS_REQUEST64_CR:
11715 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11716 bpl = (struct ulp_bde64 *)dmabuf->virt;
11717 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11718 max_response = bde.tus.f.bdeSize;
11719 break;
11720 case CMD_GEN_REQUEST64_CR:
11721 max_response = 0;
11722 if (!pIocbOut->context3)
11723 break;
11724 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11725 sizeof(struct ulp_bde64);
11726 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11727 bpl = (struct ulp_bde64 *)dmabuf->virt;
11728 for (i = 0; i < numBdes; i++) {
11729 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11730 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11731 max_response += bde.tus.f.bdeSize;
11732 }
11733 break;
11734 default:
11735 max_response = wcqe->total_data_placed;
11736 break;
11737 }
11738 if (max_response < wcqe->total_data_placed)
11739 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11740 else
11741 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11742 wcqe->total_data_placed;
695a814e 11743 }
341af102 11744
acd6859b
JS
11745 /* Convert BG errors for completion status */
11746 if (status == CQE_STATUS_DI_ERROR) {
11747 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11748
11749 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11750 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11751 else
11752 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11753
11754 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11755 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11756 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11757 BGS_GUARD_ERR_MASK;
11758 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11759 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11760 BGS_APPTAG_ERR_MASK;
11761 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11762 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11763 BGS_REFTAG_ERR_MASK;
11764
11765 /* Check to see if there was any good data before the error */
11766 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11767 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11768 BGS_HI_WATER_MARK_PRESENT_MASK;
11769 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11770 wcqe->total_data_placed;
11771 }
11772
11773 /*
11774 * Set ALL the error bits to indicate we don't know what
11775 * type of error it is.
11776 */
11777 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11778 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11779 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11780 BGS_GUARD_ERR_MASK);
11781 }
11782
341af102
JS
11783 /* Pick up HBA exchange busy condition */
11784 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11785 spin_lock_irqsave(&phba->hbalock, iflags);
11786 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11787 spin_unlock_irqrestore(&phba->hbalock, iflags);
11788 }
4f774513
JS
11789}
11790
45ed1190
JS
11791/**
11792 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11793 * @phba: Pointer to HBA context object.
11794 * @wcqe: Pointer to work-queue completion queue entry.
11795 *
11796 * This routine handles an ELS work-queue completion event and construct
11797 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11798 * discovery engine to handle.
11799 *
11800 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11801 **/
11802static struct lpfc_iocbq *
11803lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11804 struct lpfc_iocbq *irspiocbq)
11805{
11806 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11807 struct lpfc_iocbq *cmdiocbq;
11808 struct lpfc_wcqe_complete *wcqe;
11809 unsigned long iflags;
11810
11811 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
7e56aa25 11812 spin_lock_irqsave(&pring->ring_lock, iflags);
45ed1190
JS
11813 pring->stats.iocb_event++;
11814 /* Look up the ELS command IOCB and create pseudo response IOCB */
11815 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11816 bf_get(lpfc_wcqe_c_request_tag, wcqe));
89533e9b
JS
11817 /* Put the iocb back on the txcmplq */
11818 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
7e56aa25 11819 spin_unlock_irqrestore(&pring->ring_lock, iflags);
45ed1190
JS
11820
11821 if (unlikely(!cmdiocbq)) {
11822 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11823 "0386 ELS complete with no corresponding "
11824 "cmdiocb: iotag (%d)\n",
11825 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11826 lpfc_sli_release_iocbq(phba, irspiocbq);
11827 return NULL;
11828 }
11829
11830 /* Fake the irspiocbq and copy necessary response information */
341af102 11831 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
11832
11833 return irspiocbq;
11834}
11835
04c68496
JS
11836/**
11837 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11838 * @phba: Pointer to HBA context object.
11839 * @cqe: Pointer to mailbox completion queue entry.
11840 *
11841 * This routine process a mailbox completion queue entry with asynchrous
11842 * event.
11843 *
11844 * Return: true if work posted to worker thread, otherwise false.
11845 **/
11846static bool
11847lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11848{
11849 struct lpfc_cq_event *cq_event;
11850 unsigned long iflags;
11851
11852 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11853 "0392 Async Event: word0:x%x, word1:x%x, "
11854 "word2:x%x, word3:x%x\n", mcqe->word0,
11855 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11856
11857 /* Allocate a new internal CQ_EVENT entry */
11858 cq_event = lpfc_sli4_cq_event_alloc(phba);
11859 if (!cq_event) {
11860 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11861 "0394 Failed to allocate CQ_EVENT entry\n");
11862 return false;
11863 }
11864
11865 /* Move the CQE into an asynchronous event entry */
11866 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11867 spin_lock_irqsave(&phba->hbalock, iflags);
11868 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11869 /* Set the async event flag */
11870 phba->hba_flag |= ASYNC_EVENT;
11871 spin_unlock_irqrestore(&phba->hbalock, iflags);
11872
11873 return true;
11874}
11875
11876/**
11877 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11878 * @phba: Pointer to HBA context object.
11879 * @cqe: Pointer to mailbox completion queue entry.
11880 *
11881 * This routine process a mailbox completion queue entry with mailbox
11882 * completion event.
11883 *
11884 * Return: true if work posted to worker thread, otherwise false.
11885 **/
11886static bool
11887lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11888{
11889 uint32_t mcqe_status;
11890 MAILBOX_t *mbox, *pmbox;
11891 struct lpfc_mqe *mqe;
11892 struct lpfc_vport *vport;
11893 struct lpfc_nodelist *ndlp;
11894 struct lpfc_dmabuf *mp;
11895 unsigned long iflags;
11896 LPFC_MBOXQ_t *pmb;
11897 bool workposted = false;
11898 int rc;
11899
11900 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11901 if (!bf_get(lpfc_trailer_completed, mcqe))
11902 goto out_no_mqe_complete;
11903
11904 /* Get the reference to the active mbox command */
11905 spin_lock_irqsave(&phba->hbalock, iflags);
11906 pmb = phba->sli.mbox_active;
11907 if (unlikely(!pmb)) {
11908 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11909 "1832 No pending MBOX command to handle\n");
11910 spin_unlock_irqrestore(&phba->hbalock, iflags);
11911 goto out_no_mqe_complete;
11912 }
11913 spin_unlock_irqrestore(&phba->hbalock, iflags);
11914 mqe = &pmb->u.mqe;
11915 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11916 mbox = phba->mbox;
11917 vport = pmb->vport;
11918
11919 /* Reset heartbeat timer */
11920 phba->last_completion_time = jiffies;
11921 del_timer(&phba->sli.mbox_tmo);
11922
11923 /* Move mbox data to caller's mailbox region, do endian swapping */
11924 if (pmb->mbox_cmpl && mbox)
11925 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 11926
73d91e50
JS
11927 /*
11928 * For mcqe errors, conditionally move a modified error code to
11929 * the mbox so that the error will not be missed.
11930 */
11931 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11932 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11933 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11934 bf_set(lpfc_mqe_status, mqe,
11935 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11936 }
04c68496
JS
11937 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11938 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11939 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11940 "MBOX dflt rpi: status:x%x rpi:x%x",
11941 mcqe_status,
11942 pmbox->un.varWords[0], 0);
11943 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11944 mp = (struct lpfc_dmabuf *)(pmb->context1);
11945 ndlp = (struct lpfc_nodelist *)pmb->context2;
11946 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11947 * RID of the PPI using the same mbox buffer.
11948 */
11949 lpfc_unreg_login(phba, vport->vpi,
11950 pmbox->un.varWords[0], pmb);
11951 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11952 pmb->context1 = mp;
11953 pmb->context2 = ndlp;
11954 pmb->vport = vport;
11955 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11956 if (rc != MBX_BUSY)
11957 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11958 LOG_SLI, "0385 rc should "
11959 "have been MBX_BUSY\n");
11960 if (rc != MBX_NOT_FINISHED)
11961 goto send_current_mbox;
11962 }
11963 }
11964 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11965 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11966 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11967
11968 /* There is mailbox completion work to do */
11969 spin_lock_irqsave(&phba->hbalock, iflags);
11970 __lpfc_mbox_cmpl_put(phba, pmb);
11971 phba->work_ha |= HA_MBATT;
11972 spin_unlock_irqrestore(&phba->hbalock, iflags);
11973 workposted = true;
11974
11975send_current_mbox:
11976 spin_lock_irqsave(&phba->hbalock, iflags);
11977 /* Release the mailbox command posting token */
11978 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11979 /* Setting active mailbox pointer need to be in sync to flag clear */
11980 phba->sli.mbox_active = NULL;
11981 spin_unlock_irqrestore(&phba->hbalock, iflags);
11982 /* Wake up worker thread to post the next pending mailbox command */
11983 lpfc_worker_wake_up(phba);
11984out_no_mqe_complete:
11985 if (bf_get(lpfc_trailer_consumed, mcqe))
11986 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11987 return workposted;
11988}
11989
11990/**
11991 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11992 * @phba: Pointer to HBA context object.
11993 * @cqe: Pointer to mailbox completion queue entry.
11994 *
11995 * This routine process a mailbox completion queue entry, it invokes the
11996 * proper mailbox complete handling or asynchrous event handling routine
11997 * according to the MCQE's async bit.
11998 *
11999 * Return: true if work posted to worker thread, otherwise false.
12000 **/
12001static bool
12002lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12003{
12004 struct lpfc_mcqe mcqe;
12005 bool workposted;
12006
12007 /* Copy the mailbox MCQE and convert endian order as needed */
12008 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12009
12010 /* Invoke the proper event handling routine */
12011 if (!bf_get(lpfc_trailer_async, &mcqe))
12012 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12013 else
12014 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12015 return workposted;
12016}
12017
4f774513
JS
12018/**
12019 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12020 * @phba: Pointer to HBA context object.
2a76a283 12021 * @cq: Pointer to associated CQ
4f774513
JS
12022 * @wcqe: Pointer to work-queue completion queue entry.
12023 *
12024 * This routine handles an ELS work-queue completion event.
12025 *
12026 * Return: true if work posted to worker thread, otherwise false.
12027 **/
12028static bool
2a76a283 12029lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
12030 struct lpfc_wcqe_complete *wcqe)
12031{
4f774513
JS
12032 struct lpfc_iocbq *irspiocbq;
12033 unsigned long iflags;
2a76a283 12034 struct lpfc_sli_ring *pring = cq->pring;
0e9bb8d7
JS
12035 int txq_cnt = 0;
12036 int txcmplq_cnt = 0;
12037 int fcp_txcmplq_cnt = 0;
4f774513 12038
45ed1190 12039 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
12040 irspiocbq = lpfc_sli_get_iocbq(phba);
12041 if (!irspiocbq) {
0e9bb8d7
JS
12042 if (!list_empty(&pring->txq))
12043 txq_cnt++;
12044 if (!list_empty(&pring->txcmplq))
12045 txcmplq_cnt++;
12046 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
12047 fcp_txcmplq_cnt++;
4f774513 12048 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
12049 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12050 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
0e9bb8d7
JS
12051 txq_cnt, phba->iocb_cnt,
12052 fcp_txcmplq_cnt,
12053 txcmplq_cnt);
45ed1190 12054 return false;
4f774513 12055 }
4f774513 12056
45ed1190
JS
12057 /* Save off the slow-path queue event for work thread to process */
12058 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 12059 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 12060 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
12061 &phba->sli4_hba.sp_queue_event);
12062 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 12063 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 12064
45ed1190 12065 return true;
4f774513
JS
12066}
12067
12068/**
12069 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12070 * @phba: Pointer to HBA context object.
12071 * @wcqe: Pointer to work-queue completion queue entry.
12072 *
12073 * This routine handles slow-path WQ entry comsumed event by invoking the
12074 * proper WQ release routine to the slow-path WQ.
12075 **/
12076static void
12077lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12078 struct lpfc_wcqe_release *wcqe)
12079{
2e90f4b5
JS
12080 /* sanity check on queue memory */
12081 if (unlikely(!phba->sli4_hba.els_wq))
12082 return;
4f774513
JS
12083 /* Check for the slow-path ELS work queue */
12084 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12085 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12086 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12087 else
12088 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12089 "2579 Slow-path wqe consume event carries "
12090 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12091 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12092 phba->sli4_hba.els_wq->queue_id);
12093}
12094
12095/**
12096 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12097 * @phba: Pointer to HBA context object.
12098 * @cq: Pointer to a WQ completion queue.
12099 * @wcqe: Pointer to work-queue completion queue entry.
12100 *
12101 * This routine handles an XRI abort event.
12102 *
12103 * Return: true if work posted to worker thread, otherwise false.
12104 **/
12105static bool
12106lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12107 struct lpfc_queue *cq,
12108 struct sli4_wcqe_xri_aborted *wcqe)
12109{
12110 bool workposted = false;
12111 struct lpfc_cq_event *cq_event;
12112 unsigned long iflags;
12113
12114 /* Allocate a new internal CQ_EVENT entry */
12115 cq_event = lpfc_sli4_cq_event_alloc(phba);
12116 if (!cq_event) {
12117 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12118 "0602 Failed to allocate CQ_EVENT entry\n");
12119 return false;
12120 }
12121
12122 /* Move the CQE into the proper xri abort event list */
12123 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12124 switch (cq->subtype) {
12125 case LPFC_FCP:
12126 spin_lock_irqsave(&phba->hbalock, iflags);
12127 list_add_tail(&cq_event->list,
12128 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12129 /* Set the fcp xri abort event flag */
12130 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12131 spin_unlock_irqrestore(&phba->hbalock, iflags);
12132 workposted = true;
12133 break;
12134 case LPFC_ELS:
12135 spin_lock_irqsave(&phba->hbalock, iflags);
12136 list_add_tail(&cq_event->list,
12137 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12138 /* Set the els xri abort event flag */
12139 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12140 spin_unlock_irqrestore(&phba->hbalock, iflags);
12141 workposted = true;
12142 break;
12143 default:
12144 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12145 "0603 Invalid work queue CQE subtype (x%x)\n",
12146 cq->subtype);
12147 workposted = false;
12148 break;
12149 }
12150 return workposted;
12151}
12152
4f774513
JS
12153/**
12154 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
12155 * @phba: Pointer to HBA context object.
12156 * @rcqe: Pointer to receive-queue completion queue entry.
12157 *
12158 * This routine process a receive-queue completion queue entry.
12159 *
12160 * Return: true if work posted to worker thread, otherwise false.
12161 **/
12162static bool
4d9ab994 12163lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 12164{
4f774513
JS
12165 bool workposted = false;
12166 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12167 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12168 struct hbq_dmabuf *dma_buf;
7851fe2c 12169 uint32_t status, rq_id;
4f774513
JS
12170 unsigned long iflags;
12171
2e90f4b5
JS
12172 /* sanity check on queue memory */
12173 if (unlikely(!hrq) || unlikely(!drq))
12174 return workposted;
12175
7851fe2c
JS
12176 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12177 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12178 else
12179 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12180 if (rq_id != hrq->queue_id)
4f774513
JS
12181 goto out;
12182
4d9ab994 12183 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
12184 switch (status) {
12185 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12186 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12187 "2537 Receive Frame Truncated!!\n");
b84daac9 12188 hrq->RQ_buf_trunc++;
4f774513 12189 case FC_STATUS_RQ_SUCCESS:
5ffc266e 12190 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
12191 spin_lock_irqsave(&phba->hbalock, iflags);
12192 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12193 if (!dma_buf) {
b84daac9 12194 hrq->RQ_no_buf_found++;
4f774513
JS
12195 spin_unlock_irqrestore(&phba->hbalock, iflags);
12196 goto out;
12197 }
b84daac9 12198 hrq->RQ_rcv_buf++;
4d9ab994 12199 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
4f774513 12200 /* save off the frame for the word thread to process */
4d9ab994 12201 list_add_tail(&dma_buf->cq_event.list,
45ed1190 12202 &phba->sli4_hba.sp_queue_event);
4f774513 12203 /* Frame received */
45ed1190 12204 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
12205 spin_unlock_irqrestore(&phba->hbalock, iflags);
12206 workposted = true;
12207 break;
12208 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12209 case FC_STATUS_INSUFF_BUF_FRM_DISC:
b84daac9 12210 hrq->RQ_no_posted_buf++;
4f774513
JS
12211 /* Post more buffers if possible */
12212 spin_lock_irqsave(&phba->hbalock, iflags);
12213 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12214 spin_unlock_irqrestore(&phba->hbalock, iflags);
12215 workposted = true;
12216 break;
12217 }
12218out:
12219 return workposted;
4f774513
JS
12220}
12221
4d9ab994
JS
12222/**
12223 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12224 * @phba: Pointer to HBA context object.
12225 * @cq: Pointer to the completion queue.
12226 * @wcqe: Pointer to a completion queue entry.
12227 *
25985edc 12228 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
12229 * entry.
12230 *
12231 * Return: true if work posted to worker thread, otherwise false.
12232 **/
12233static bool
12234lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12235 struct lpfc_cqe *cqe)
12236{
45ed1190 12237 struct lpfc_cqe cqevt;
4d9ab994
JS
12238 bool workposted = false;
12239
12240 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 12241 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
12242
12243 /* Check and process for different type of WCQE and dispatch */
45ed1190 12244 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 12245 case CQE_CODE_COMPL_WQE:
45ed1190 12246 /* Process the WQ/RQ complete event */
bc73905a 12247 phba->last_completion_time = jiffies;
2a76a283 12248 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
45ed1190 12249 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
12250 break;
12251 case CQE_CODE_RELEASE_WQE:
12252 /* Process the WQ release event */
12253 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 12254 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
12255 break;
12256 case CQE_CODE_XRI_ABORTED:
12257 /* Process the WQ XRI abort event */
bc73905a 12258 phba->last_completion_time = jiffies;
4d9ab994 12259 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 12260 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
12261 break;
12262 case CQE_CODE_RECEIVE:
7851fe2c 12263 case CQE_CODE_RECEIVE_V1:
4d9ab994 12264 /* Process the RQ event */
bc73905a 12265 phba->last_completion_time = jiffies;
4d9ab994 12266 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 12267 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
12268 break;
12269 default:
12270 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12271 "0388 Not a valid WCQE code: x%x\n",
45ed1190 12272 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
12273 break;
12274 }
12275 return workposted;
12276}
12277
4f774513
JS
12278/**
12279 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12280 * @phba: Pointer to HBA context object.
12281 * @eqe: Pointer to fast-path event queue entry.
12282 *
12283 * This routine process a event queue entry from the slow-path event queue.
12284 * It will check the MajorCode and MinorCode to determine this is for a
12285 * completion event on a completion queue, if not, an error shall be logged
12286 * and just return. Otherwise, it will get to the corresponding completion
12287 * queue and process all the entries on that completion queue, rearm the
12288 * completion queue, and then return.
12289 *
12290 **/
12291static void
67d12733
JS
12292lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12293 struct lpfc_queue *speq)
4f774513 12294{
67d12733 12295 struct lpfc_queue *cq = NULL, *childq;
4f774513
JS
12296 struct lpfc_cqe *cqe;
12297 bool workposted = false;
12298 int ecount = 0;
12299 uint16_t cqid;
12300
4f774513 12301 /* Get the reference to the corresponding CQ */
cb5172ea 12302 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513 12303
4f774513
JS
12304 list_for_each_entry(childq, &speq->child_list, list) {
12305 if (childq->queue_id == cqid) {
12306 cq = childq;
12307 break;
12308 }
12309 }
12310 if (unlikely(!cq)) {
75baf696
JS
12311 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12312 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12313 "0365 Slow-path CQ identifier "
12314 "(%d) does not exist\n", cqid);
4f774513
JS
12315 return;
12316 }
12317
12318 /* Process all the entries to the CQ */
12319 switch (cq->type) {
12320 case LPFC_MCQ:
12321 while ((cqe = lpfc_sli4_cq_get(cq))) {
12322 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
73d91e50 12323 if (!(++ecount % cq->entry_repost))
4f774513 12324 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
b84daac9 12325 cq->CQ_mbox++;
4f774513
JS
12326 }
12327 break;
12328 case LPFC_WCQ:
12329 while ((cqe = lpfc_sli4_cq_get(cq))) {
0558056c
JS
12330 if (cq->subtype == LPFC_FCP)
12331 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
12332 cqe);
12333 else
12334 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12335 cqe);
73d91e50 12336 if (!(++ecount % cq->entry_repost))
4f774513
JS
12337 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12338 }
b84daac9
JS
12339
12340 /* Track the max number of CQEs processed in 1 EQ */
12341 if (ecount > cq->CQ_max_cqe)
12342 cq->CQ_max_cqe = ecount;
4f774513
JS
12343 break;
12344 default:
12345 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12346 "0370 Invalid completion queue type (%d)\n",
12347 cq->type);
12348 return;
12349 }
12350
12351 /* Catch the no cq entry condition, log an error */
12352 if (unlikely(ecount == 0))
12353 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12354 "0371 No entry from the CQ: identifier "
12355 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12356
12357 /* In any case, flash and re-arm the RCQ */
12358 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12359
12360 /* wake up worker thread if there are works to be done */
12361 if (workposted)
12362 lpfc_worker_wake_up(phba);
12363}
12364
12365/**
12366 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
2a76a283
JS
12367 * @phba: Pointer to HBA context object.
12368 * @cq: Pointer to associated CQ
12369 * @wcqe: Pointer to work-queue completion queue entry.
4f774513
JS
12370 *
12371 * This routine process a fast-path work queue completion entry from fast-path
12372 * event queue for FCP command response completion.
12373 **/
12374static void
2a76a283 12375lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
12376 struct lpfc_wcqe_complete *wcqe)
12377{
2a76a283 12378 struct lpfc_sli_ring *pring = cq->pring;
4f774513
JS
12379 struct lpfc_iocbq *cmdiocbq;
12380 struct lpfc_iocbq irspiocbq;
12381 unsigned long iflags;
12382
4f774513
JS
12383 /* Check for response status */
12384 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12385 /* If resource errors reported from HBA, reduce queue
12386 * depth of the SCSI device.
12387 */
e3d2b802
JS
12388 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12389 IOSTAT_LOCAL_REJECT)) &&
12390 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12391 IOERR_NO_RESOURCES))
4f774513 12392 phba->lpfc_rampdown_queue_depth(phba);
e3d2b802 12393
4f774513
JS
12394 /* Log the error status */
12395 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12396 "0373 FCP complete error: status=x%x, "
12397 "hw_status=x%x, total_data_specified=%d, "
12398 "parameter=x%x, word3=x%x\n",
12399 bf_get(lpfc_wcqe_c_status, wcqe),
12400 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12401 wcqe->total_data_placed, wcqe->parameter,
12402 wcqe->word3);
12403 }
12404
12405 /* Look up the FCP command IOCB and create pseudo response IOCB */
7e56aa25
JS
12406 spin_lock_irqsave(&pring->ring_lock, iflags);
12407 pring->stats.iocb_event++;
4f774513
JS
12408 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12409 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 12410 spin_unlock_irqrestore(&pring->ring_lock, iflags);
4f774513
JS
12411 if (unlikely(!cmdiocbq)) {
12412 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12413 "0374 FCP complete with no corresponding "
12414 "cmdiocb: iotag (%d)\n",
12415 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12416 return;
12417 }
12418 if (unlikely(!cmdiocbq->iocb_cmpl)) {
12419 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12420 "0375 FCP cmdiocb not callback function "
12421 "iotag: (%d)\n",
12422 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12423 return;
12424 }
12425
12426 /* Fake the irspiocb and copy necessary response information */
341af102 12427 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 12428
0f65ff68
JS
12429 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12430 spin_lock_irqsave(&phba->hbalock, iflags);
12431 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12432 spin_unlock_irqrestore(&phba->hbalock, iflags);
12433 }
12434
4f774513
JS
12435 /* Pass the cmd_iocb and the rsp state to the upper layer */
12436 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12437}
12438
12439/**
12440 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12441 * @phba: Pointer to HBA context object.
12442 * @cq: Pointer to completion queue.
12443 * @wcqe: Pointer to work-queue completion queue entry.
12444 *
12445 * This routine handles an fast-path WQ entry comsumed event by invoking the
12446 * proper WQ release routine to the slow-path WQ.
12447 **/
12448static void
12449lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12450 struct lpfc_wcqe_release *wcqe)
12451{
12452 struct lpfc_queue *childwq;
12453 bool wqid_matched = false;
12454 uint16_t fcp_wqid;
12455
12456 /* Check for fast-path FCP work queue release */
12457 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12458 list_for_each_entry(childwq, &cq->child_list, list) {
12459 if (childwq->queue_id == fcp_wqid) {
12460 lpfc_sli4_wq_release(childwq,
12461 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12462 wqid_matched = true;
12463 break;
12464 }
12465 }
12466 /* Report warning log message if no match found */
12467 if (wqid_matched != true)
12468 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12469 "2580 Fast-path wqe consume event carries "
12470 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12471}
12472
12473/**
12474 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12475 * @cq: Pointer to the completion queue.
12476 * @eqe: Pointer to fast-path completion queue entry.
12477 *
12478 * This routine process a fast-path work queue completion entry from fast-path
12479 * event queue for FCP command response completion.
12480 **/
12481static int
12482lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12483 struct lpfc_cqe *cqe)
12484{
12485 struct lpfc_wcqe_release wcqe;
12486 bool workposted = false;
12487
12488 /* Copy the work queue CQE and convert endian order if needed */
12489 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12490
12491 /* Check and process for different type of WCQE and dispatch */
12492 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12493 case CQE_CODE_COMPL_WQE:
b84daac9 12494 cq->CQ_wq++;
4f774513 12495 /* Process the WQ complete event */
98fc5dd9 12496 phba->last_completion_time = jiffies;
2a76a283 12497 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
4f774513
JS
12498 (struct lpfc_wcqe_complete *)&wcqe);
12499 break;
12500 case CQE_CODE_RELEASE_WQE:
b84daac9 12501 cq->CQ_release_wqe++;
4f774513
JS
12502 /* Process the WQ release event */
12503 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12504 (struct lpfc_wcqe_release *)&wcqe);
12505 break;
12506 case CQE_CODE_XRI_ABORTED:
b84daac9 12507 cq->CQ_xri_aborted++;
4f774513 12508 /* Process the WQ XRI abort event */
bc73905a 12509 phba->last_completion_time = jiffies;
4f774513
JS
12510 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12511 (struct sli4_wcqe_xri_aborted *)&wcqe);
12512 break;
12513 default:
12514 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12515 "0144 Not a valid WCQE code: x%x\n",
12516 bf_get(lpfc_wcqe_c_code, &wcqe));
12517 break;
12518 }
12519 return workposted;
12520}
12521
12522/**
67d12733 12523 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
4f774513
JS
12524 * @phba: Pointer to HBA context object.
12525 * @eqe: Pointer to fast-path event queue entry.
12526 *
12527 * This routine process a event queue entry from the fast-path event queue.
12528 * It will check the MajorCode and MinorCode to determine this is for a
12529 * completion event on a completion queue, if not, an error shall be logged
12530 * and just return. Otherwise, it will get to the corresponding completion
12531 * queue and process all the entries on the completion queue, rearm the
12532 * completion queue, and then return.
12533 **/
12534static void
67d12733
JS
12535lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12536 uint32_t qidx)
4f774513
JS
12537{
12538 struct lpfc_queue *cq;
12539 struct lpfc_cqe *cqe;
12540 bool workposted = false;
12541 uint16_t cqid;
12542 int ecount = 0;
12543
cb5172ea 12544 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513 12545 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
67d12733 12546 "0366 Not a valid completion "
4f774513 12547 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
12548 bf_get_le32(lpfc_eqe_major_code, eqe),
12549 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
12550 return;
12551 }
12552
67d12733
JS
12553 /* Get the reference to the corresponding CQ */
12554 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12555
12556 /* Check if this is a Slow path event */
12557 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12558 lpfc_sli4_sp_handle_eqe(phba, eqe,
12559 phba->sli4_hba.hba_eq[qidx]);
12560 return;
12561 }
12562
2e90f4b5
JS
12563 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12564 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12565 "3146 Fast-path completion queues "
12566 "does not exist\n");
12567 return;
12568 }
67d12733 12569 cq = phba->sli4_hba.fcp_cq[qidx];
4f774513 12570 if (unlikely(!cq)) {
75baf696
JS
12571 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12572 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12573 "0367 Fast-path completion queue "
67d12733 12574 "(%d) does not exist\n", qidx);
4f774513
JS
12575 return;
12576 }
12577
4f774513
JS
12578 if (unlikely(cqid != cq->queue_id)) {
12579 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12580 "0368 Miss-matched fast-path completion "
12581 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12582 cqid, cq->queue_id);
12583 return;
12584 }
12585
12586 /* Process all the entries to the CQ */
12587 while ((cqe = lpfc_sli4_cq_get(cq))) {
12588 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
73d91e50 12589 if (!(++ecount % cq->entry_repost))
4f774513
JS
12590 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12591 }
12592
b84daac9
JS
12593 /* Track the max number of CQEs processed in 1 EQ */
12594 if (ecount > cq->CQ_max_cqe)
12595 cq->CQ_max_cqe = ecount;
12596
4f774513
JS
12597 /* Catch the no cq entry condition */
12598 if (unlikely(ecount == 0))
12599 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12600 "0369 No entry from fast-path completion "
12601 "queue fcpcqid=%d\n", cq->queue_id);
12602
12603 /* In any case, flash and re-arm the CQ */
12604 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12605
12606 /* wake up worker thread if there are works to be done */
12607 if (workposted)
12608 lpfc_worker_wake_up(phba);
12609}
12610
12611static void
12612lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12613{
12614 struct lpfc_eqe *eqe;
12615
12616 /* walk all the EQ entries and drop on the floor */
12617 while ((eqe = lpfc_sli4_eq_get(eq)))
12618 ;
12619
12620 /* Clear and re-arm the EQ */
12621 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12622}
12623
1ba981fd
JS
12624
12625/**
12626 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12627 * entry
12628 * @phba: Pointer to HBA context object.
12629 * @eqe: Pointer to fast-path event queue entry.
12630 *
12631 * This routine process a event queue entry from the Flash Optimized Fabric
12632 * event queue. It will check the MajorCode and MinorCode to determine this
12633 * is for a completion event on a completion queue, if not, an error shall be
12634 * logged and just return. Otherwise, it will get to the corresponding
12635 * completion queue and process all the entries on the completion queue, rearm
12636 * the completion queue, and then return.
12637 **/
12638static void
12639lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12640{
12641 struct lpfc_queue *cq;
12642 struct lpfc_cqe *cqe;
12643 bool workposted = false;
12644 uint16_t cqid;
12645 int ecount = 0;
12646
12647 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12648 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12649 "9147 Not a valid completion "
12650 "event: majorcode=x%x, minorcode=x%x\n",
12651 bf_get_le32(lpfc_eqe_major_code, eqe),
12652 bf_get_le32(lpfc_eqe_minor_code, eqe));
12653 return;
12654 }
12655
12656 /* Get the reference to the corresponding CQ */
12657 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12658
12659 /* Next check for OAS */
12660 cq = phba->sli4_hba.oas_cq;
12661 if (unlikely(!cq)) {
12662 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12663 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12664 "9148 OAS completion queue "
12665 "does not exist\n");
12666 return;
12667 }
12668
12669 if (unlikely(cqid != cq->queue_id)) {
12670 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12671 "9149 Miss-matched fast-path compl "
12672 "queue id: eqcqid=%d, fcpcqid=%d\n",
12673 cqid, cq->queue_id);
12674 return;
12675 }
12676
12677 /* Process all the entries to the OAS CQ */
12678 while ((cqe = lpfc_sli4_cq_get(cq))) {
12679 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12680 if (!(++ecount % cq->entry_repost))
12681 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12682 }
12683
12684 /* Track the max number of CQEs processed in 1 EQ */
12685 if (ecount > cq->CQ_max_cqe)
12686 cq->CQ_max_cqe = ecount;
12687
12688 /* Catch the no cq entry condition */
12689 if (unlikely(ecount == 0))
12690 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12691 "9153 No entry from fast-path completion "
12692 "queue fcpcqid=%d\n", cq->queue_id);
12693
12694 /* In any case, flash and re-arm the CQ */
12695 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12696
12697 /* wake up worker thread if there are works to be done */
12698 if (workposted)
12699 lpfc_worker_wake_up(phba);
12700}
12701
12702/**
12703 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12704 * @irq: Interrupt number.
12705 * @dev_id: The device context pointer.
12706 *
12707 * This function is directly called from the PCI layer as an interrupt
12708 * service routine when device with SLI-4 interface spec is enabled with
12709 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12710 * IOCB ring event in the HBA. However, when the device is enabled with either
12711 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12712 * device-level interrupt handler. When the PCI slot is in error recovery
12713 * or the HBA is undergoing initialization, the interrupt handler will not
12714 * process the interrupt. The Flash Optimized Fabric ring event are handled in
12715 * the intrrupt context. This function is called without any lock held.
12716 * It gets the hbalock to access and update SLI data structures. Note that,
12717 * the EQ to CQ are one-to-one map such that the EQ index is
12718 * equal to that of CQ index.
12719 *
12720 * This function returns IRQ_HANDLED when interrupt is handled else it
12721 * returns IRQ_NONE.
12722 **/
12723irqreturn_t
12724lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12725{
12726 struct lpfc_hba *phba;
12727 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12728 struct lpfc_queue *eq;
12729 struct lpfc_eqe *eqe;
12730 unsigned long iflag;
12731 int ecount = 0;
1ba981fd
JS
12732
12733 /* Get the driver's phba structure from the dev_id */
12734 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12735 phba = fcp_eq_hdl->phba;
1ba981fd
JS
12736
12737 if (unlikely(!phba))
12738 return IRQ_NONE;
12739
12740 /* Get to the EQ struct associated with this vector */
12741 eq = phba->sli4_hba.fof_eq;
12742 if (unlikely(!eq))
12743 return IRQ_NONE;
12744
12745 /* Check device state for handling interrupt */
12746 if (unlikely(lpfc_intr_state_check(phba))) {
12747 eq->EQ_badstate++;
12748 /* Check again for link_state with lock held */
12749 spin_lock_irqsave(&phba->hbalock, iflag);
12750 if (phba->link_state < LPFC_LINK_DOWN)
12751 /* Flush, clear interrupt, and rearm the EQ */
12752 lpfc_sli4_eq_flush(phba, eq);
12753 spin_unlock_irqrestore(&phba->hbalock, iflag);
12754 return IRQ_NONE;
12755 }
12756
12757 /*
12758 * Process all the event on FCP fast-path EQ
12759 */
12760 while ((eqe = lpfc_sli4_eq_get(eq))) {
12761 lpfc_sli4_fof_handle_eqe(phba, eqe);
12762 if (!(++ecount % eq->entry_repost))
12763 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12764 eq->EQ_processed++;
12765 }
12766
12767 /* Track the max number of EQEs processed in 1 intr */
12768 if (ecount > eq->EQ_max_eqe)
12769 eq->EQ_max_eqe = ecount;
12770
12771
12772 if (unlikely(ecount == 0)) {
12773 eq->EQ_no_entry++;
12774
12775 if (phba->intr_type == MSIX)
12776 /* MSI-X treated interrupt served as no EQ share INT */
12777 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12778 "9145 MSI-X interrupt with no EQE\n");
12779 else {
12780 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12781 "9146 ISR interrupt with no EQE\n");
12782 /* Non MSI-X treated on interrupt as EQ share INT */
12783 return IRQ_NONE;
12784 }
12785 }
12786 /* Always clear and re-arm the fast-path EQ */
12787 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12788 return IRQ_HANDLED;
12789}
12790
4f774513 12791/**
67d12733 12792 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
4f774513
JS
12793 * @irq: Interrupt number.
12794 * @dev_id: The device context pointer.
12795 *
12796 * This function is directly called from the PCI layer as an interrupt
12797 * service routine when device with SLI-4 interface spec is enabled with
12798 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12799 * ring event in the HBA. However, when the device is enabled with either
12800 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12801 * device-level interrupt handler. When the PCI slot is in error recovery
12802 * or the HBA is undergoing initialization, the interrupt handler will not
12803 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12804 * the intrrupt context. This function is called without any lock held.
12805 * It gets the hbalock to access and update SLI data structures. Note that,
12806 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12807 * equal to that of FCP CQ index.
12808 *
67d12733
JS
12809 * The link attention and ELS ring attention events are handled
12810 * by the worker thread. The interrupt handler signals the worker thread
12811 * and returns for these events. This function is called without any lock
12812 * held. It gets the hbalock to access and update SLI data structures.
12813 *
4f774513
JS
12814 * This function returns IRQ_HANDLED when interrupt is handled else it
12815 * returns IRQ_NONE.
12816 **/
12817irqreturn_t
67d12733 12818lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
4f774513
JS
12819{
12820 struct lpfc_hba *phba;
12821 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12822 struct lpfc_queue *fpeq;
12823 struct lpfc_eqe *eqe;
12824 unsigned long iflag;
12825 int ecount = 0;
962bc51b 12826 int fcp_eqidx;
4f774513
JS
12827
12828 /* Get the driver's phba structure from the dev_id */
12829 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12830 phba = fcp_eq_hdl->phba;
12831 fcp_eqidx = fcp_eq_hdl->idx;
12832
12833 if (unlikely(!phba))
12834 return IRQ_NONE;
67d12733 12835 if (unlikely(!phba->sli4_hba.hba_eq))
5350d872 12836 return IRQ_NONE;
4f774513
JS
12837
12838 /* Get to the EQ struct associated with this vector */
67d12733 12839 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
2e90f4b5
JS
12840 if (unlikely(!fpeq))
12841 return IRQ_NONE;
4f774513 12842
ba20c853
JS
12843 if (lpfc_fcp_look_ahead) {
12844 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12845 lpfc_sli4_eq_clr_intr(fpeq);
12846 else {
12847 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12848 return IRQ_NONE;
12849 }
12850 }
12851
4f774513
JS
12852 /* Check device state for handling interrupt */
12853 if (unlikely(lpfc_intr_state_check(phba))) {
b84daac9 12854 fpeq->EQ_badstate++;
4f774513
JS
12855 /* Check again for link_state with lock held */
12856 spin_lock_irqsave(&phba->hbalock, iflag);
12857 if (phba->link_state < LPFC_LINK_DOWN)
12858 /* Flush, clear interrupt, and rearm the EQ */
12859 lpfc_sli4_eq_flush(phba, fpeq);
12860 spin_unlock_irqrestore(&phba->hbalock, iflag);
ba20c853
JS
12861 if (lpfc_fcp_look_ahead)
12862 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
4f774513
JS
12863 return IRQ_NONE;
12864 }
12865
12866 /*
12867 * Process all the event on FCP fast-path EQ
12868 */
12869 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
eb016566
JS
12870 if (eqe == NULL)
12871 break;
12872
67d12733 12873 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
73d91e50 12874 if (!(++ecount % fpeq->entry_repost))
4f774513 12875 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
b84daac9 12876 fpeq->EQ_processed++;
4f774513
JS
12877 }
12878
b84daac9
JS
12879 /* Track the max number of EQEs processed in 1 intr */
12880 if (ecount > fpeq->EQ_max_eqe)
12881 fpeq->EQ_max_eqe = ecount;
12882
4f774513
JS
12883 /* Always clear and re-arm the fast-path EQ */
12884 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12885
12886 if (unlikely(ecount == 0)) {
b84daac9 12887 fpeq->EQ_no_entry++;
ba20c853
JS
12888
12889 if (lpfc_fcp_look_ahead) {
12890 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12891 return IRQ_NONE;
12892 }
12893
4f774513
JS
12894 if (phba->intr_type == MSIX)
12895 /* MSI-X treated interrupt served as no EQ share INT */
12896 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12897 "0358 MSI-X interrupt with no EQE\n");
12898 else
12899 /* Non MSI-X treated on interrupt as EQ share INT */
12900 return IRQ_NONE;
12901 }
12902
ba20c853
JS
12903 if (lpfc_fcp_look_ahead)
12904 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
4f774513
JS
12905 return IRQ_HANDLED;
12906} /* lpfc_sli4_fp_intr_handler */
12907
12908/**
12909 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12910 * @irq: Interrupt number.
12911 * @dev_id: The device context pointer.
12912 *
12913 * This function is the device-level interrupt handler to device with SLI-4
12914 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12915 * interrupt mode is enabled and there is an event in the HBA which requires
12916 * driver attention. This function invokes the slow-path interrupt attention
12917 * handling function and fast-path interrupt attention handling function in
12918 * turn to process the relevant HBA attention events. This function is called
12919 * without any lock held. It gets the hbalock to access and update SLI data
12920 * structures.
12921 *
12922 * This function returns IRQ_HANDLED when interrupt is handled, else it
12923 * returns IRQ_NONE.
12924 **/
12925irqreturn_t
12926lpfc_sli4_intr_handler(int irq, void *dev_id)
12927{
12928 struct lpfc_hba *phba;
67d12733
JS
12929 irqreturn_t hba_irq_rc;
12930 bool hba_handled = false;
962bc51b 12931 int fcp_eqidx;
4f774513
JS
12932
12933 /* Get the driver's phba structure from the dev_id */
12934 phba = (struct lpfc_hba *)dev_id;
12935
12936 if (unlikely(!phba))
12937 return IRQ_NONE;
12938
4f774513
JS
12939 /*
12940 * Invoke fast-path host attention interrupt handling as appropriate.
12941 */
67d12733
JS
12942 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12943 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
4f774513 12944 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
67d12733
JS
12945 if (hba_irq_rc == IRQ_HANDLED)
12946 hba_handled |= true;
4f774513
JS
12947 }
12948
1ba981fd
JS
12949 if (phba->cfg_fof) {
12950 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12951 &phba->sli4_hba.fcp_eq_hdl[0]);
12952 if (hba_irq_rc == IRQ_HANDLED)
12953 hba_handled |= true;
12954 }
12955
67d12733 12956 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
4f774513
JS
12957} /* lpfc_sli4_intr_handler */
12958
12959/**
12960 * lpfc_sli4_queue_free - free a queue structure and associated memory
12961 * @queue: The queue structure to free.
12962 *
b595076a 12963 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
12964 * the host resident queue. This function must be called after destroying the
12965 * queue on the HBA.
12966 **/
12967void
12968lpfc_sli4_queue_free(struct lpfc_queue *queue)
12969{
12970 struct lpfc_dmabuf *dmabuf;
12971
12972 if (!queue)
12973 return;
12974
12975 while (!list_empty(&queue->page_list)) {
12976 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12977 list);
49198b37 12978 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
4f774513
JS
12979 dmabuf->virt, dmabuf->phys);
12980 kfree(dmabuf);
12981 }
12982 kfree(queue);
12983 return;
12984}
12985
12986/**
12987 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12988 * @phba: The HBA that this queue is being created on.
12989 * @entry_size: The size of each queue entry for this queue.
12990 * @entry count: The number of entries that this queue will handle.
12991 *
12992 * This function allocates a queue structure and the DMAable memory used for
12993 * the host resident queue. This function must be called before creating the
12994 * queue on the HBA.
12995 **/
12996struct lpfc_queue *
12997lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12998 uint32_t entry_count)
12999{
13000 struct lpfc_queue *queue;
13001 struct lpfc_dmabuf *dmabuf;
13002 int x, total_qe_count;
13003 void *dma_pointer;
cb5172ea 13004 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 13005
cb5172ea
JS
13006 if (!phba->sli4_hba.pc_sli4_params.supported)
13007 hw_page_size = SLI4_PAGE_SIZE;
13008
4f774513
JS
13009 queue = kzalloc(sizeof(struct lpfc_queue) +
13010 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13011 if (!queue)
13012 return NULL;
cb5172ea
JS
13013 queue->page_count = (ALIGN(entry_size * entry_count,
13014 hw_page_size))/hw_page_size;
4f774513
JS
13015 INIT_LIST_HEAD(&queue->list);
13016 INIT_LIST_HEAD(&queue->page_list);
13017 INIT_LIST_HEAD(&queue->child_list);
13018 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13019 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13020 if (!dmabuf)
13021 goto out_fail;
1aee383d
JP
13022 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13023 hw_page_size, &dmabuf->phys,
13024 GFP_KERNEL);
4f774513
JS
13025 if (!dmabuf->virt) {
13026 kfree(dmabuf);
13027 goto out_fail;
13028 }
13029 dmabuf->buffer_tag = x;
13030 list_add_tail(&dmabuf->list, &queue->page_list);
13031 /* initialize queue's entry array */
13032 dma_pointer = dmabuf->virt;
13033 for (; total_qe_count < entry_count &&
cb5172ea 13034 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
13035 total_qe_count++, dma_pointer += entry_size) {
13036 queue->qe[total_qe_count].address = dma_pointer;
13037 }
13038 }
13039 queue->entry_size = entry_size;
13040 queue->entry_count = entry_count;
73d91e50
JS
13041
13042 /*
13043 * entry_repost is calculated based on the number of entries in the
13044 * queue. This works out except for RQs. If buffers are NOT initially
13045 * posted for every RQE, entry_repost should be adjusted accordingly.
13046 */
13047 queue->entry_repost = (entry_count >> 3);
13048 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13049 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
4f774513
JS
13050 queue->phba = phba;
13051
13052 return queue;
13053out_fail:
13054 lpfc_sli4_queue_free(queue);
13055 return NULL;
13056}
13057
962bc51b
JS
13058/**
13059 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13060 * @phba: HBA structure that indicates port to create a queue on.
13061 * @pci_barset: PCI BAR set flag.
13062 *
13063 * This function shall perform iomap of the specified PCI BAR address to host
13064 * memory address if not already done so and return it. The returned host
13065 * memory address can be NULL.
13066 */
13067static void __iomem *
13068lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13069{
962bc51b
JS
13070 if (!phba->pcidev)
13071 return NULL;
962bc51b
JS
13072
13073 switch (pci_barset) {
13074 case WQ_PCI_BAR_0_AND_1:
962bc51b
JS
13075 return phba->pci_bar0_memmap_p;
13076 case WQ_PCI_BAR_2_AND_3:
962bc51b
JS
13077 return phba->pci_bar2_memmap_p;
13078 case WQ_PCI_BAR_4_AND_5:
962bc51b
JS
13079 return phba->pci_bar4_memmap_p;
13080 default:
13081 break;
13082 }
13083 return NULL;
13084}
13085
173edbb2
JS
13086/**
13087 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
13088 * @phba: HBA structure that indicates port to create a queue on.
13089 * @startq: The starting FCP EQ to modify
13090 *
13091 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13092 *
13093 * The @phba struct is used to send mailbox command to HBA. The @startq
13094 * is used to get the starting FCP EQ to change.
13095 * This function is asynchronous and will wait for the mailbox
13096 * command to finish before continuing.
13097 *
13098 * On success this function will return a zero. If unable to allocate enough
13099 * memory this function will return -ENOMEM. If the queue create mailbox command
13100 * fails this function will return -ENXIO.
13101 **/
a2fc4aef 13102int
2c9c5a00 13103lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
173edbb2
JS
13104{
13105 struct lpfc_mbx_modify_eq_delay *eq_delay;
13106 LPFC_MBOXQ_t *mbox;
13107 struct lpfc_queue *eq;
13108 int cnt, rc, length, status = 0;
13109 uint32_t shdr_status, shdr_add_status;
ee02006b 13110 uint32_t result;
173edbb2
JS
13111 int fcp_eqidx;
13112 union lpfc_sli4_cfg_shdr *shdr;
13113 uint16_t dmult;
13114
67d12733 13115 if (startq >= phba->cfg_fcp_io_channel)
173edbb2
JS
13116 return 0;
13117
13118 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13119 if (!mbox)
13120 return -ENOMEM;
13121 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13122 sizeof(struct lpfc_sli4_cfg_mhdr));
13123 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13124 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13125 length, LPFC_SLI4_MBX_EMBED);
13126 eq_delay = &mbox->u.mqe.un.eq_delay;
13127
13128 /* Calculate delay multiper from maximum interrupt per second */
ee02006b
JS
13129 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
13130 if (result > LPFC_DMULT_CONST)
13131 dmult = 0;
13132 else
13133 dmult = LPFC_DMULT_CONST/result - 1;
173edbb2
JS
13134
13135 cnt = 0;
67d12733 13136 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
173edbb2 13137 fcp_eqidx++) {
67d12733 13138 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
173edbb2
JS
13139 if (!eq)
13140 continue;
13141 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13142 eq_delay->u.request.eq[cnt].phase = 0;
13143 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13144 cnt++;
13145 if (cnt >= LPFC_MAX_EQ_DELAY)
13146 break;
13147 }
13148 eq_delay->u.request.num_eq = cnt;
13149
13150 mbox->vport = phba->pport;
13151 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13152 mbox->context1 = NULL;
13153 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13154 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13155 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13156 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13157 if (shdr_status || shdr_add_status || rc) {
13158 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13159 "2512 MODIFY_EQ_DELAY mailbox failed with "
13160 "status x%x add_status x%x, mbx status x%x\n",
13161 shdr_status, shdr_add_status, rc);
13162 status = -ENXIO;
13163 }
13164 mempool_free(mbox, phba->mbox_mem_pool);
13165 return status;
13166}
13167
4f774513
JS
13168/**
13169 * lpfc_eq_create - Create an Event Queue on the HBA
13170 * @phba: HBA structure that indicates port to create a queue on.
13171 * @eq: The queue structure to use to create the event queue.
13172 * @imax: The maximum interrupt per second limit.
13173 *
13174 * This function creates an event queue, as detailed in @eq, on a port,
13175 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13176 *
13177 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13178 * is used to get the entry count and entry size that are necessary to
13179 * determine the number of pages to allocate and use for this queue. This
13180 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13181 * event queue. This function is asynchronous and will wait for the mailbox
13182 * command to finish before continuing.
13183 *
13184 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
13185 * memory this function will return -ENOMEM. If the queue create mailbox command
13186 * fails this function will return -ENXIO.
4f774513 13187 **/
a2fc4aef 13188int
ee02006b 13189lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
4f774513
JS
13190{
13191 struct lpfc_mbx_eq_create *eq_create;
13192 LPFC_MBOXQ_t *mbox;
13193 int rc, length, status = 0;
13194 struct lpfc_dmabuf *dmabuf;
13195 uint32_t shdr_status, shdr_add_status;
13196 union lpfc_sli4_cfg_shdr *shdr;
13197 uint16_t dmult;
49198b37
JS
13198 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13199
2e90f4b5
JS
13200 /* sanity check on queue memory */
13201 if (!eq)
13202 return -ENODEV;
49198b37
JS
13203 if (!phba->sli4_hba.pc_sli4_params.supported)
13204 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
13205
13206 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13207 if (!mbox)
13208 return -ENOMEM;
13209 length = (sizeof(struct lpfc_mbx_eq_create) -
13210 sizeof(struct lpfc_sli4_cfg_mhdr));
13211 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13212 LPFC_MBOX_OPCODE_EQ_CREATE,
13213 length, LPFC_SLI4_MBX_EMBED);
13214 eq_create = &mbox->u.mqe.un.eq_create;
13215 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13216 eq->page_count);
13217 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13218 LPFC_EQE_SIZE);
13219 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
2c9c5a00
JS
13220 /* don't setup delay multiplier using EQ_CREATE */
13221 dmult = 0;
4f774513
JS
13222 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13223 dmult);
13224 switch (eq->entry_count) {
13225 default:
13226 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13227 "0360 Unsupported EQ count. (%d)\n",
13228 eq->entry_count);
13229 if (eq->entry_count < 256)
13230 return -EINVAL;
13231 /* otherwise default to smallest count (drop through) */
13232 case 256:
13233 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13234 LPFC_EQ_CNT_256);
13235 break;
13236 case 512:
13237 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13238 LPFC_EQ_CNT_512);
13239 break;
13240 case 1024:
13241 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13242 LPFC_EQ_CNT_1024);
13243 break;
13244 case 2048:
13245 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13246 LPFC_EQ_CNT_2048);
13247 break;
13248 case 4096:
13249 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13250 LPFC_EQ_CNT_4096);
13251 break;
13252 }
13253 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 13254 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
13255 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13256 putPaddrLow(dmabuf->phys);
13257 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13258 putPaddrHigh(dmabuf->phys);
13259 }
13260 mbox->vport = phba->pport;
13261 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13262 mbox->context1 = NULL;
13263 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13264 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13265 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13266 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13267 if (shdr_status || shdr_add_status || rc) {
13268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13269 "2500 EQ_CREATE mailbox failed with "
13270 "status x%x add_status x%x, mbx status x%x\n",
13271 shdr_status, shdr_add_status, rc);
13272 status = -ENXIO;
13273 }
13274 eq->type = LPFC_EQ;
13275 eq->subtype = LPFC_NONE;
13276 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13277 if (eq->queue_id == 0xFFFF)
13278 status = -ENXIO;
13279 eq->host_index = 0;
13280 eq->hba_index = 0;
13281
8fa38513 13282 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
13283 return status;
13284}
13285
13286/**
13287 * lpfc_cq_create - Create a Completion Queue on the HBA
13288 * @phba: HBA structure that indicates port to create a queue on.
13289 * @cq: The queue structure to use to create the completion queue.
13290 * @eq: The event queue to bind this completion queue to.
13291 *
13292 * This function creates a completion queue, as detailed in @wq, on a port,
13293 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13294 *
13295 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13296 * is used to get the entry count and entry size that are necessary to
13297 * determine the number of pages to allocate and use for this queue. The @eq
13298 * is used to indicate which event queue to bind this completion queue to. This
13299 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13300 * completion queue. This function is asynchronous and will wait for the mailbox
13301 * command to finish before continuing.
13302 *
13303 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
13304 * memory this function will return -ENOMEM. If the queue create mailbox command
13305 * fails this function will return -ENXIO.
4f774513 13306 **/
a2fc4aef 13307int
4f774513
JS
13308lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13309 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13310{
13311 struct lpfc_mbx_cq_create *cq_create;
13312 struct lpfc_dmabuf *dmabuf;
13313 LPFC_MBOXQ_t *mbox;
13314 int rc, length, status = 0;
13315 uint32_t shdr_status, shdr_add_status;
13316 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
13317 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13318
2e90f4b5
JS
13319 /* sanity check on queue memory */
13320 if (!cq || !eq)
13321 return -ENODEV;
49198b37
JS
13322 if (!phba->sli4_hba.pc_sli4_params.supported)
13323 hw_page_size = SLI4_PAGE_SIZE;
13324
4f774513
JS
13325 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13326 if (!mbox)
13327 return -ENOMEM;
13328 length = (sizeof(struct lpfc_mbx_cq_create) -
13329 sizeof(struct lpfc_sli4_cfg_mhdr));
13330 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13331 LPFC_MBOX_OPCODE_CQ_CREATE,
13332 length, LPFC_SLI4_MBX_EMBED);
13333 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 13334 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
13335 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13336 cq->page_count);
13337 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13338 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
13339 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13340 phba->sli4_hba.pc_sli4_params.cqv);
13341 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
c31098ce
JS
13342 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13343 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
5a6f133e
JS
13344 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13345 eq->queue_id);
13346 } else {
13347 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13348 eq->queue_id);
13349 }
4f774513
JS
13350 switch (cq->entry_count) {
13351 default:
13352 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13353 "0361 Unsupported CQ count. (%d)\n",
13354 cq->entry_count);
4f4c1863
JS
13355 if (cq->entry_count < 256) {
13356 status = -EINVAL;
13357 goto out;
13358 }
4f774513
JS
13359 /* otherwise default to smallest count (drop through) */
13360 case 256:
13361 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13362 LPFC_CQ_CNT_256);
13363 break;
13364 case 512:
13365 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13366 LPFC_CQ_CNT_512);
13367 break;
13368 case 1024:
13369 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13370 LPFC_CQ_CNT_1024);
13371 break;
13372 }
13373 list_for_each_entry(dmabuf, &cq->page_list, list) {
49198b37 13374 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
13375 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13376 putPaddrLow(dmabuf->phys);
13377 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13378 putPaddrHigh(dmabuf->phys);
13379 }
13380 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13381
13382 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
13383 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13384 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13385 if (shdr_status || shdr_add_status || rc) {
13386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13387 "2501 CQ_CREATE mailbox failed with "
13388 "status x%x add_status x%x, mbx status x%x\n",
13389 shdr_status, shdr_add_status, rc);
13390 status = -ENXIO;
13391 goto out;
13392 }
13393 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13394 if (cq->queue_id == 0xFFFF) {
13395 status = -ENXIO;
13396 goto out;
13397 }
13398 /* link the cq onto the parent eq child list */
13399 list_add_tail(&cq->list, &eq->child_list);
13400 /* Set up completion queue's type and subtype */
13401 cq->type = type;
13402 cq->subtype = subtype;
13403 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 13404 cq->assoc_qid = eq->queue_id;
4f774513
JS
13405 cq->host_index = 0;
13406 cq->hba_index = 0;
4f774513 13407
8fa38513
JS
13408out:
13409 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
13410 return status;
13411}
13412
b19a061a
JS
13413/**
13414 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
13415 * @phba: HBA structure that indicates port to create a queue on.
13416 * @mq: The queue structure to use to create the mailbox queue.
13417 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13418 * @cq: The completion queue to associate with this cq.
13419 *
13420 * This function provides failback (fb) functionality when the
13421 * mq_create_ext fails on older FW generations. It's purpose is identical
13422 * to mq_create_ext otherwise.
13423 *
13424 * This routine cannot fail as all attributes were previously accessed and
13425 * initialized in mq_create_ext.
13426 **/
13427static void
13428lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13429 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
13430{
13431 struct lpfc_mbx_mq_create *mq_create;
13432 struct lpfc_dmabuf *dmabuf;
13433 int length;
13434
13435 length = (sizeof(struct lpfc_mbx_mq_create) -
13436 sizeof(struct lpfc_sli4_cfg_mhdr));
13437 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13438 LPFC_MBOX_OPCODE_MQ_CREATE,
13439 length, LPFC_SLI4_MBX_EMBED);
13440 mq_create = &mbox->u.mqe.un.mq_create;
13441 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
13442 mq->page_count);
13443 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
13444 cq->queue_id);
13445 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13446 switch (mq->entry_count) {
13447 case 16:
5a6f133e
JS
13448 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13449 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
13450 break;
13451 case 32:
5a6f133e
JS
13452 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13453 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
13454 break;
13455 case 64:
5a6f133e
JS
13456 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13457 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
13458 break;
13459 case 128:
5a6f133e
JS
13460 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13461 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
13462 break;
13463 }
13464 list_for_each_entry(dmabuf, &mq->page_list, list) {
13465 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13466 putPaddrLow(dmabuf->phys);
13467 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13468 putPaddrHigh(dmabuf->phys);
13469 }
13470}
13471
04c68496
JS
13472/**
13473 * lpfc_mq_create - Create a mailbox Queue on the HBA
13474 * @phba: HBA structure that indicates port to create a queue on.
13475 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
13476 * @cq: The completion queue to associate with this cq.
13477 * @subtype: The queue's subtype.
04c68496
JS
13478 *
13479 * This function creates a mailbox queue, as detailed in @mq, on a port,
13480 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13481 *
13482 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13483 * is used to get the entry count and entry size that are necessary to
13484 * determine the number of pages to allocate and use for this queue. This
13485 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13486 * mailbox queue. This function is asynchronous and will wait for the mailbox
13487 * command to finish before continuing.
13488 *
13489 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
13490 * memory this function will return -ENOMEM. If the queue create mailbox command
13491 * fails this function will return -ENXIO.
04c68496 13492 **/
b19a061a 13493int32_t
04c68496
JS
13494lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13495 struct lpfc_queue *cq, uint32_t subtype)
13496{
13497 struct lpfc_mbx_mq_create *mq_create;
b19a061a 13498 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
13499 struct lpfc_dmabuf *dmabuf;
13500 LPFC_MBOXQ_t *mbox;
13501 int rc, length, status = 0;
13502 uint32_t shdr_status, shdr_add_status;
13503 union lpfc_sli4_cfg_shdr *shdr;
49198b37 13504 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 13505
2e90f4b5
JS
13506 /* sanity check on queue memory */
13507 if (!mq || !cq)
13508 return -ENODEV;
49198b37
JS
13509 if (!phba->sli4_hba.pc_sli4_params.supported)
13510 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 13511
04c68496
JS
13512 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13513 if (!mbox)
13514 return -ENOMEM;
b19a061a 13515 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
13516 sizeof(struct lpfc_sli4_cfg_mhdr));
13517 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 13518 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 13519 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
13520
13521 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 13522 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
13523 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13524 &mq_create_ext->u.request, mq->page_count);
13525 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13526 &mq_create_ext->u.request, 1);
13527 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
13528 &mq_create_ext->u.request, 1);
13529 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13530 &mq_create_ext->u.request, 1);
70f3c073
JS
13531 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13532 &mq_create_ext->u.request, 1);
13533 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13534 &mq_create_ext->u.request, 1);
b19a061a 13535 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
13536 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13537 phba->sli4_hba.pc_sli4_params.mqv);
13538 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13539 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13540 cq->queue_id);
13541 else
13542 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13543 cq->queue_id);
04c68496
JS
13544 switch (mq->entry_count) {
13545 default:
13546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13547 "0362 Unsupported MQ count. (%d)\n",
13548 mq->entry_count);
4f4c1863
JS
13549 if (mq->entry_count < 16) {
13550 status = -EINVAL;
13551 goto out;
13552 }
04c68496
JS
13553 /* otherwise default to smallest count (drop through) */
13554 case 16:
5a6f133e
JS
13555 bf_set(lpfc_mq_context_ring_size,
13556 &mq_create_ext->u.request.context,
13557 LPFC_MQ_RING_SIZE_16);
04c68496
JS
13558 break;
13559 case 32:
5a6f133e
JS
13560 bf_set(lpfc_mq_context_ring_size,
13561 &mq_create_ext->u.request.context,
13562 LPFC_MQ_RING_SIZE_32);
04c68496
JS
13563 break;
13564 case 64:
5a6f133e
JS
13565 bf_set(lpfc_mq_context_ring_size,
13566 &mq_create_ext->u.request.context,
13567 LPFC_MQ_RING_SIZE_64);
04c68496
JS
13568 break;
13569 case 128:
5a6f133e
JS
13570 bf_set(lpfc_mq_context_ring_size,
13571 &mq_create_ext->u.request.context,
13572 LPFC_MQ_RING_SIZE_128);
04c68496
JS
13573 break;
13574 }
13575 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 13576 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 13577 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 13578 putPaddrLow(dmabuf->phys);
b19a061a 13579 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
13580 putPaddrHigh(dmabuf->phys);
13581 }
13582 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
13583 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13584 &mq_create_ext->u.response);
13585 if (rc != MBX_SUCCESS) {
13586 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13587 "2795 MQ_CREATE_EXT failed with "
13588 "status x%x. Failback to MQ_CREATE.\n",
13589 rc);
13590 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13591 mq_create = &mbox->u.mqe.un.mq_create;
13592 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13593 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13594 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13595 &mq_create->u.response);
13596 }
13597
04c68496 13598 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
13599 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13600 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13601 if (shdr_status || shdr_add_status || rc) {
13602 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13603 "2502 MQ_CREATE mailbox failed with "
13604 "status x%x add_status x%x, mbx status x%x\n",
13605 shdr_status, shdr_add_status, rc);
13606 status = -ENXIO;
13607 goto out;
13608 }
04c68496
JS
13609 if (mq->queue_id == 0xFFFF) {
13610 status = -ENXIO;
13611 goto out;
13612 }
13613 mq->type = LPFC_MQ;
2a622bfb 13614 mq->assoc_qid = cq->queue_id;
04c68496
JS
13615 mq->subtype = subtype;
13616 mq->host_index = 0;
13617 mq->hba_index = 0;
13618
13619 /* link the mq onto the parent cq child list */
13620 list_add_tail(&mq->list, &cq->child_list);
13621out:
8fa38513 13622 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
13623 return status;
13624}
13625
4f774513
JS
13626/**
13627 * lpfc_wq_create - Create a Work Queue on the HBA
13628 * @phba: HBA structure that indicates port to create a queue on.
13629 * @wq: The queue structure to use to create the work queue.
13630 * @cq: The completion queue to bind this work queue to.
13631 * @subtype: The subtype of the work queue indicating its functionality.
13632 *
13633 * This function creates a work queue, as detailed in @wq, on a port, described
13634 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13635 *
13636 * The @phba struct is used to send mailbox command to HBA. The @wq struct
13637 * is used to get the entry count and entry size that are necessary to
13638 * determine the number of pages to allocate and use for this queue. The @cq
13639 * is used to indicate which completion queue to bind this work queue to. This
13640 * function will send the WQ_CREATE mailbox command to the HBA to setup the
13641 * work queue. This function is asynchronous and will wait for the mailbox
13642 * command to finish before continuing.
13643 *
13644 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
13645 * memory this function will return -ENOMEM. If the queue create mailbox command
13646 * fails this function will return -ENXIO.
4f774513 13647 **/
a2fc4aef 13648int
4f774513
JS
13649lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13650 struct lpfc_queue *cq, uint32_t subtype)
13651{
13652 struct lpfc_mbx_wq_create *wq_create;
13653 struct lpfc_dmabuf *dmabuf;
13654 LPFC_MBOXQ_t *mbox;
13655 int rc, length, status = 0;
13656 uint32_t shdr_status, shdr_add_status;
13657 union lpfc_sli4_cfg_shdr *shdr;
49198b37 13658 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 13659 struct dma_address *page;
962bc51b
JS
13660 void __iomem *bar_memmap_p;
13661 uint32_t db_offset;
13662 uint16_t pci_barset;
49198b37 13663
2e90f4b5
JS
13664 /* sanity check on queue memory */
13665 if (!wq || !cq)
13666 return -ENODEV;
49198b37
JS
13667 if (!phba->sli4_hba.pc_sli4_params.supported)
13668 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
13669
13670 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13671 if (!mbox)
13672 return -ENOMEM;
13673 length = (sizeof(struct lpfc_mbx_wq_create) -
13674 sizeof(struct lpfc_sli4_cfg_mhdr));
13675 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13676 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13677 length, LPFC_SLI4_MBX_EMBED);
13678 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 13679 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
13680 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13681 wq->page_count);
13682 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13683 cq->queue_id);
0c651878
JS
13684
13685 /* wqv is the earliest version supported, NOT the latest */
5a6f133e
JS
13686 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13687 phba->sli4_hba.pc_sli4_params.wqv);
962bc51b 13688
0c651878
JS
13689 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13690 case LPFC_Q_CREATE_VERSION_0:
13691 switch (wq->entry_size) {
13692 default:
13693 case 64:
13694 /* Nothing to do, version 0 ONLY supports 64 byte */
13695 page = wq_create->u.request.page;
13696 break;
13697 case 128:
13698 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13699 LPFC_WQ_SZ128_SUPPORT)) {
13700 status = -ERANGE;
13701 goto out;
13702 }
13703 /* If we get here the HBA MUST also support V1 and
13704 * we MUST use it
13705 */
13706 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13707 LPFC_Q_CREATE_VERSION_1);
13708
13709 bf_set(lpfc_mbx_wq_create_wqe_count,
13710 &wq_create->u.request_1, wq->entry_count);
13711 bf_set(lpfc_mbx_wq_create_wqe_size,
13712 &wq_create->u.request_1,
13713 LPFC_WQ_WQE_SIZE_128);
13714 bf_set(lpfc_mbx_wq_create_page_size,
13715 &wq_create->u.request_1,
13716 (PAGE_SIZE/SLI4_PAGE_SIZE));
13717 page = wq_create->u.request_1.page;
13718 break;
13719 }
13720 break;
13721 case LPFC_Q_CREATE_VERSION_1:
5a6f133e
JS
13722 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13723 wq->entry_count);
13724 switch (wq->entry_size) {
13725 default:
13726 case 64:
13727 bf_set(lpfc_mbx_wq_create_wqe_size,
13728 &wq_create->u.request_1,
13729 LPFC_WQ_WQE_SIZE_64);
13730 break;
13731 case 128:
0c651878
JS
13732 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13733 LPFC_WQ_SZ128_SUPPORT)) {
13734 status = -ERANGE;
13735 goto out;
13736 }
5a6f133e
JS
13737 bf_set(lpfc_mbx_wq_create_wqe_size,
13738 &wq_create->u.request_1,
13739 LPFC_WQ_WQE_SIZE_128);
13740 break;
13741 }
13742 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
13743 (PAGE_SIZE/SLI4_PAGE_SIZE));
13744 page = wq_create->u.request_1.page;
0c651878
JS
13745 break;
13746 default:
13747 status = -ERANGE;
13748 goto out;
5a6f133e 13749 }
0c651878 13750
4f774513 13751 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 13752 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
13753 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13754 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513 13755 }
962bc51b
JS
13756
13757 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13758 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13759
4f774513
JS
13760 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13761 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
13762 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13763 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13764 if (shdr_status || shdr_add_status || rc) {
13765 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13766 "2503 WQ_CREATE mailbox failed with "
13767 "status x%x add_status x%x, mbx status x%x\n",
13768 shdr_status, shdr_add_status, rc);
13769 status = -ENXIO;
13770 goto out;
13771 }
13772 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13773 if (wq->queue_id == 0xFFFF) {
13774 status = -ENXIO;
13775 goto out;
13776 }
962bc51b
JS
13777 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13778 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13779 &wq_create->u.response);
13780 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13781 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13782 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13783 "3265 WQ[%d] doorbell format not "
13784 "supported: x%x\n", wq->queue_id,
13785 wq->db_format);
13786 status = -EINVAL;
13787 goto out;
13788 }
13789 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13790 &wq_create->u.response);
13791 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13792 if (!bar_memmap_p) {
13793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13794 "3263 WQ[%d] failed to memmap pci "
13795 "barset:x%x\n", wq->queue_id,
13796 pci_barset);
13797 status = -ENOMEM;
13798 goto out;
13799 }
13800 db_offset = wq_create->u.response.doorbell_offset;
13801 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13802 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13803 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13804 "3252 WQ[%d] doorbell offset not "
13805 "supported: x%x\n", wq->queue_id,
13806 db_offset);
13807 status = -EINVAL;
13808 goto out;
13809 }
13810 wq->db_regaddr = bar_memmap_p + db_offset;
13811 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
13812 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13813 "format:x%x\n", wq->queue_id, pci_barset,
13814 db_offset, wq->db_format);
962bc51b
JS
13815 } else {
13816 wq->db_format = LPFC_DB_LIST_FORMAT;
13817 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13818 }
4f774513 13819 wq->type = LPFC_WQ;
2a622bfb 13820 wq->assoc_qid = cq->queue_id;
4f774513
JS
13821 wq->subtype = subtype;
13822 wq->host_index = 0;
13823 wq->hba_index = 0;
ff78d8f9 13824 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
4f774513
JS
13825
13826 /* link the wq onto the parent cq child list */
13827 list_add_tail(&wq->list, &cq->child_list);
13828out:
8fa38513 13829 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
13830 return status;
13831}
13832
73d91e50
JS
13833/**
13834 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13835 * @phba: HBA structure that indicates port to create a queue on.
13836 * @rq: The queue structure to use for the receive queue.
13837 * @qno: The associated HBQ number
13838 *
13839 *
13840 * For SLI4 we need to adjust the RQ repost value based on
13841 * the number of buffers that are initially posted to the RQ.
13842 */
13843void
13844lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13845{
13846 uint32_t cnt;
13847
2e90f4b5
JS
13848 /* sanity check on queue memory */
13849 if (!rq)
13850 return;
73d91e50
JS
13851 cnt = lpfc_hbq_defs[qno]->entry_count;
13852
13853 /* Recalc repost for RQs based on buffers initially posted */
13854 cnt = (cnt >> 3);
13855 if (cnt < LPFC_QUEUE_MIN_REPOST)
13856 cnt = LPFC_QUEUE_MIN_REPOST;
13857
13858 rq->entry_repost = cnt;
13859}
13860
4f774513
JS
13861/**
13862 * lpfc_rq_create - Create a Receive Queue on the HBA
13863 * @phba: HBA structure that indicates port to create a queue on.
13864 * @hrq: The queue structure to use to create the header receive queue.
13865 * @drq: The queue structure to use to create the data receive queue.
13866 * @cq: The completion queue to bind this work queue to.
13867 *
13868 * This function creates a receive buffer queue pair , as detailed in @hrq and
13869 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13870 * to the HBA.
13871 *
13872 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13873 * struct is used to get the entry count that is necessary to determine the
13874 * number of pages to use for this queue. The @cq is used to indicate which
13875 * completion queue to bind received buffers that are posted to these queues to.
13876 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13877 * receive queue pair. This function is asynchronous and will wait for the
13878 * mailbox command to finish before continuing.
13879 *
13880 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
13881 * memory this function will return -ENOMEM. If the queue create mailbox command
13882 * fails this function will return -ENXIO.
4f774513 13883 **/
a2fc4aef 13884int
4f774513
JS
13885lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13886 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13887{
13888 struct lpfc_mbx_rq_create *rq_create;
13889 struct lpfc_dmabuf *dmabuf;
13890 LPFC_MBOXQ_t *mbox;
13891 int rc, length, status = 0;
13892 uint32_t shdr_status, shdr_add_status;
13893 union lpfc_sli4_cfg_shdr *shdr;
49198b37 13894 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
962bc51b
JS
13895 void __iomem *bar_memmap_p;
13896 uint32_t db_offset;
13897 uint16_t pci_barset;
49198b37 13898
2e90f4b5
JS
13899 /* sanity check on queue memory */
13900 if (!hrq || !drq || !cq)
13901 return -ENODEV;
49198b37
JS
13902 if (!phba->sli4_hba.pc_sli4_params.supported)
13903 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
13904
13905 if (hrq->entry_count != drq->entry_count)
13906 return -EINVAL;
13907 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13908 if (!mbox)
13909 return -ENOMEM;
13910 length = (sizeof(struct lpfc_mbx_rq_create) -
13911 sizeof(struct lpfc_sli4_cfg_mhdr));
13912 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13913 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13914 length, LPFC_SLI4_MBX_EMBED);
13915 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
13916 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13917 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13918 phba->sli4_hba.pc_sli4_params.rqv);
13919 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13920 bf_set(lpfc_rq_context_rqe_count_1,
13921 &rq_create->u.request.context,
13922 hrq->entry_count);
13923 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
13924 bf_set(lpfc_rq_context_rqe_size,
13925 &rq_create->u.request.context,
13926 LPFC_RQE_SIZE_8);
13927 bf_set(lpfc_rq_context_page_size,
13928 &rq_create->u.request.context,
13929 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
13930 } else {
13931 switch (hrq->entry_count) {
13932 default:
13933 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13934 "2535 Unsupported RQ count. (%d)\n",
13935 hrq->entry_count);
4f4c1863
JS
13936 if (hrq->entry_count < 512) {
13937 status = -EINVAL;
13938 goto out;
13939 }
5a6f133e
JS
13940 /* otherwise default to smallest count (drop through) */
13941 case 512:
13942 bf_set(lpfc_rq_context_rqe_count,
13943 &rq_create->u.request.context,
13944 LPFC_RQ_RING_SIZE_512);
13945 break;
13946 case 1024:
13947 bf_set(lpfc_rq_context_rqe_count,
13948 &rq_create->u.request.context,
13949 LPFC_RQ_RING_SIZE_1024);
13950 break;
13951 case 2048:
13952 bf_set(lpfc_rq_context_rqe_count,
13953 &rq_create->u.request.context,
13954 LPFC_RQ_RING_SIZE_2048);
13955 break;
13956 case 4096:
13957 bf_set(lpfc_rq_context_rqe_count,
13958 &rq_create->u.request.context,
13959 LPFC_RQ_RING_SIZE_4096);
13960 break;
13961 }
13962 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13963 LPFC_HDR_BUF_SIZE);
4f774513
JS
13964 }
13965 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13966 cq->queue_id);
13967 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13968 hrq->page_count);
4f774513 13969 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 13970 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
13971 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13972 putPaddrLow(dmabuf->phys);
13973 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13974 putPaddrHigh(dmabuf->phys);
13975 }
962bc51b
JS
13976 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13977 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13978
4f774513
JS
13979 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13980 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
13981 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13982 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13983 if (shdr_status || shdr_add_status || rc) {
13984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13985 "2504 RQ_CREATE mailbox failed with "
13986 "status x%x add_status x%x, mbx status x%x\n",
13987 shdr_status, shdr_add_status, rc);
13988 status = -ENXIO;
13989 goto out;
13990 }
13991 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13992 if (hrq->queue_id == 0xFFFF) {
13993 status = -ENXIO;
13994 goto out;
13995 }
962bc51b
JS
13996
13997 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13998 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13999 &rq_create->u.response);
14000 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
14001 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
14002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14003 "3262 RQ [%d] doorbell format not "
14004 "supported: x%x\n", hrq->queue_id,
14005 hrq->db_format);
14006 status = -EINVAL;
14007 goto out;
14008 }
14009
14010 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
14011 &rq_create->u.response);
14012 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14013 if (!bar_memmap_p) {
14014 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14015 "3269 RQ[%d] failed to memmap pci "
14016 "barset:x%x\n", hrq->queue_id,
14017 pci_barset);
14018 status = -ENOMEM;
14019 goto out;
14020 }
14021
14022 db_offset = rq_create->u.response.doorbell_offset;
14023 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
14024 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
14025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14026 "3270 RQ[%d] doorbell offset not "
14027 "supported: x%x\n", hrq->queue_id,
14028 db_offset);
14029 status = -EINVAL;
14030 goto out;
14031 }
14032 hrq->db_regaddr = bar_memmap_p + db_offset;
14033 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
14034 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
14035 "format:x%x\n", hrq->queue_id, pci_barset,
14036 db_offset, hrq->db_format);
962bc51b
JS
14037 } else {
14038 hrq->db_format = LPFC_DB_RING_FORMAT;
14039 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
14040 }
4f774513 14041 hrq->type = LPFC_HRQ;
2a622bfb 14042 hrq->assoc_qid = cq->queue_id;
4f774513
JS
14043 hrq->subtype = subtype;
14044 hrq->host_index = 0;
14045 hrq->hba_index = 0;
14046
14047 /* now create the data queue */
14048 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14049 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14050 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
14051 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14052 phba->sli4_hba.pc_sli4_params.rqv);
14053 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14054 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 14055 &rq_create->u.request.context, hrq->entry_count);
5a6f133e 14056 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
c31098ce
JS
14057 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
14058 LPFC_RQE_SIZE_8);
14059 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
14060 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
14061 } else {
14062 switch (drq->entry_count) {
14063 default:
14064 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14065 "2536 Unsupported RQ count. (%d)\n",
14066 drq->entry_count);
4f4c1863
JS
14067 if (drq->entry_count < 512) {
14068 status = -EINVAL;
14069 goto out;
14070 }
5a6f133e
JS
14071 /* otherwise default to smallest count (drop through) */
14072 case 512:
14073 bf_set(lpfc_rq_context_rqe_count,
14074 &rq_create->u.request.context,
14075 LPFC_RQ_RING_SIZE_512);
14076 break;
14077 case 1024:
14078 bf_set(lpfc_rq_context_rqe_count,
14079 &rq_create->u.request.context,
14080 LPFC_RQ_RING_SIZE_1024);
14081 break;
14082 case 2048:
14083 bf_set(lpfc_rq_context_rqe_count,
14084 &rq_create->u.request.context,
14085 LPFC_RQ_RING_SIZE_2048);
14086 break;
14087 case 4096:
14088 bf_set(lpfc_rq_context_rqe_count,
14089 &rq_create->u.request.context,
14090 LPFC_RQ_RING_SIZE_4096);
14091 break;
14092 }
14093 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14094 LPFC_DATA_BUF_SIZE);
4f774513
JS
14095 }
14096 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14097 cq->queue_id);
14098 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14099 drq->page_count);
4f774513
JS
14100 list_for_each_entry(dmabuf, &drq->page_list, list) {
14101 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14102 putPaddrLow(dmabuf->phys);
14103 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14104 putPaddrHigh(dmabuf->phys);
14105 }
962bc51b
JS
14106 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14107 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
4f774513
JS
14108 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14109 /* The IOCTL status is embedded in the mailbox subheader. */
14110 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14111 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14112 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14113 if (shdr_status || shdr_add_status || rc) {
14114 status = -ENXIO;
14115 goto out;
14116 }
14117 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14118 if (drq->queue_id == 0xFFFF) {
14119 status = -ENXIO;
14120 goto out;
14121 }
14122 drq->type = LPFC_DRQ;
2a622bfb 14123 drq->assoc_qid = cq->queue_id;
4f774513
JS
14124 drq->subtype = subtype;
14125 drq->host_index = 0;
14126 drq->hba_index = 0;
14127
14128 /* link the header and data RQs onto the parent cq child list */
14129 list_add_tail(&hrq->list, &cq->child_list);
14130 list_add_tail(&drq->list, &cq->child_list);
14131
14132out:
8fa38513 14133 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14134 return status;
14135}
14136
14137/**
14138 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14139 * @eq: The queue structure associated with the queue to destroy.
14140 *
14141 * This function destroys a queue, as detailed in @eq by sending an mailbox
14142 * command, specific to the type of queue, to the HBA.
14143 *
14144 * The @eq struct is used to get the queue ID of the queue to destroy.
14145 *
14146 * On success this function will return a zero. If the queue destroy mailbox
d439d286 14147 * command fails this function will return -ENXIO.
4f774513 14148 **/
a2fc4aef 14149int
4f774513
JS
14150lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14151{
14152 LPFC_MBOXQ_t *mbox;
14153 int rc, length, status = 0;
14154 uint32_t shdr_status, shdr_add_status;
14155 union lpfc_sli4_cfg_shdr *shdr;
14156
2e90f4b5 14157 /* sanity check on queue memory */
4f774513
JS
14158 if (!eq)
14159 return -ENODEV;
14160 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14161 if (!mbox)
14162 return -ENOMEM;
14163 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14164 sizeof(struct lpfc_sli4_cfg_mhdr));
14165 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14166 LPFC_MBOX_OPCODE_EQ_DESTROY,
14167 length, LPFC_SLI4_MBX_EMBED);
14168 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14169 eq->queue_id);
14170 mbox->vport = eq->phba->pport;
14171 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14172
14173 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14174 /* The IOCTL status is embedded in the mailbox subheader. */
14175 shdr = (union lpfc_sli4_cfg_shdr *)
14176 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14177 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14178 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14179 if (shdr_status || shdr_add_status || rc) {
14180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14181 "2505 EQ_DESTROY mailbox failed with "
14182 "status x%x add_status x%x, mbx status x%x\n",
14183 shdr_status, shdr_add_status, rc);
14184 status = -ENXIO;
14185 }
14186
14187 /* Remove eq from any list */
14188 list_del_init(&eq->list);
8fa38513 14189 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
14190 return status;
14191}
14192
14193/**
14194 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14195 * @cq: The queue structure associated with the queue to destroy.
14196 *
14197 * This function destroys a queue, as detailed in @cq by sending an mailbox
14198 * command, specific to the type of queue, to the HBA.
14199 *
14200 * The @cq struct is used to get the queue ID of the queue to destroy.
14201 *
14202 * On success this function will return a zero. If the queue destroy mailbox
d439d286 14203 * command fails this function will return -ENXIO.
4f774513 14204 **/
a2fc4aef 14205int
4f774513
JS
14206lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14207{
14208 LPFC_MBOXQ_t *mbox;
14209 int rc, length, status = 0;
14210 uint32_t shdr_status, shdr_add_status;
14211 union lpfc_sli4_cfg_shdr *shdr;
14212
2e90f4b5 14213 /* sanity check on queue memory */
4f774513
JS
14214 if (!cq)
14215 return -ENODEV;
14216 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14217 if (!mbox)
14218 return -ENOMEM;
14219 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14220 sizeof(struct lpfc_sli4_cfg_mhdr));
14221 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14222 LPFC_MBOX_OPCODE_CQ_DESTROY,
14223 length, LPFC_SLI4_MBX_EMBED);
14224 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14225 cq->queue_id);
14226 mbox->vport = cq->phba->pport;
14227 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14228 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14229 /* The IOCTL status is embedded in the mailbox subheader. */
14230 shdr = (union lpfc_sli4_cfg_shdr *)
14231 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14232 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14233 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14234 if (shdr_status || shdr_add_status || rc) {
14235 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14236 "2506 CQ_DESTROY mailbox failed with "
14237 "status x%x add_status x%x, mbx status x%x\n",
14238 shdr_status, shdr_add_status, rc);
14239 status = -ENXIO;
14240 }
14241 /* Remove cq from any list */
14242 list_del_init(&cq->list);
8fa38513 14243 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
14244 return status;
14245}
14246
04c68496
JS
14247/**
14248 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14249 * @qm: The queue structure associated with the queue to destroy.
14250 *
14251 * This function destroys a queue, as detailed in @mq by sending an mailbox
14252 * command, specific to the type of queue, to the HBA.
14253 *
14254 * The @mq struct is used to get the queue ID of the queue to destroy.
14255 *
14256 * On success this function will return a zero. If the queue destroy mailbox
d439d286 14257 * command fails this function will return -ENXIO.
04c68496 14258 **/
a2fc4aef 14259int
04c68496
JS
14260lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14261{
14262 LPFC_MBOXQ_t *mbox;
14263 int rc, length, status = 0;
14264 uint32_t shdr_status, shdr_add_status;
14265 union lpfc_sli4_cfg_shdr *shdr;
14266
2e90f4b5 14267 /* sanity check on queue memory */
04c68496
JS
14268 if (!mq)
14269 return -ENODEV;
14270 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14271 if (!mbox)
14272 return -ENOMEM;
14273 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14274 sizeof(struct lpfc_sli4_cfg_mhdr));
14275 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14276 LPFC_MBOX_OPCODE_MQ_DESTROY,
14277 length, LPFC_SLI4_MBX_EMBED);
14278 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14279 mq->queue_id);
14280 mbox->vport = mq->phba->pport;
14281 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14282 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14283 /* The IOCTL status is embedded in the mailbox subheader. */
14284 shdr = (union lpfc_sli4_cfg_shdr *)
14285 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14286 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14287 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14288 if (shdr_status || shdr_add_status || rc) {
14289 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14290 "2507 MQ_DESTROY mailbox failed with "
14291 "status x%x add_status x%x, mbx status x%x\n",
14292 shdr_status, shdr_add_status, rc);
14293 status = -ENXIO;
14294 }
14295 /* Remove mq from any list */
14296 list_del_init(&mq->list);
8fa38513 14297 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
14298 return status;
14299}
14300
4f774513
JS
14301/**
14302 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14303 * @wq: The queue structure associated with the queue to destroy.
14304 *
14305 * This function destroys a queue, as detailed in @wq by sending an mailbox
14306 * command, specific to the type of queue, to the HBA.
14307 *
14308 * The @wq struct is used to get the queue ID of the queue to destroy.
14309 *
14310 * On success this function will return a zero. If the queue destroy mailbox
d439d286 14311 * command fails this function will return -ENXIO.
4f774513 14312 **/
a2fc4aef 14313int
4f774513
JS
14314lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14315{
14316 LPFC_MBOXQ_t *mbox;
14317 int rc, length, status = 0;
14318 uint32_t shdr_status, shdr_add_status;
14319 union lpfc_sli4_cfg_shdr *shdr;
14320
2e90f4b5 14321 /* sanity check on queue memory */
4f774513
JS
14322 if (!wq)
14323 return -ENODEV;
14324 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14325 if (!mbox)
14326 return -ENOMEM;
14327 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14328 sizeof(struct lpfc_sli4_cfg_mhdr));
14329 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14330 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14331 length, LPFC_SLI4_MBX_EMBED);
14332 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14333 wq->queue_id);
14334 mbox->vport = wq->phba->pport;
14335 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14336 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14337 shdr = (union lpfc_sli4_cfg_shdr *)
14338 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14339 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14340 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14341 if (shdr_status || shdr_add_status || rc) {
14342 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14343 "2508 WQ_DESTROY mailbox failed with "
14344 "status x%x add_status x%x, mbx status x%x\n",
14345 shdr_status, shdr_add_status, rc);
14346 status = -ENXIO;
14347 }
14348 /* Remove wq from any list */
14349 list_del_init(&wq->list);
8fa38513 14350 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
14351 return status;
14352}
14353
14354/**
14355 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14356 * @rq: The queue structure associated with the queue to destroy.
14357 *
14358 * This function destroys a queue, as detailed in @rq by sending an mailbox
14359 * command, specific to the type of queue, to the HBA.
14360 *
14361 * The @rq struct is used to get the queue ID of the queue to destroy.
14362 *
14363 * On success this function will return a zero. If the queue destroy mailbox
d439d286 14364 * command fails this function will return -ENXIO.
4f774513 14365 **/
a2fc4aef 14366int
4f774513
JS
14367lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14368 struct lpfc_queue *drq)
14369{
14370 LPFC_MBOXQ_t *mbox;
14371 int rc, length, status = 0;
14372 uint32_t shdr_status, shdr_add_status;
14373 union lpfc_sli4_cfg_shdr *shdr;
14374
2e90f4b5 14375 /* sanity check on queue memory */
4f774513
JS
14376 if (!hrq || !drq)
14377 return -ENODEV;
14378 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14379 if (!mbox)
14380 return -ENOMEM;
14381 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 14382 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
14383 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14384 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14385 length, LPFC_SLI4_MBX_EMBED);
14386 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14387 hrq->queue_id);
14388 mbox->vport = hrq->phba->pport;
14389 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14390 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14391 /* The IOCTL status is embedded in the mailbox subheader. */
14392 shdr = (union lpfc_sli4_cfg_shdr *)
14393 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14394 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14395 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14396 if (shdr_status || shdr_add_status || rc) {
14397 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14398 "2509 RQ_DESTROY mailbox failed with "
14399 "status x%x add_status x%x, mbx status x%x\n",
14400 shdr_status, shdr_add_status, rc);
14401 if (rc != MBX_TIMEOUT)
14402 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14403 return -ENXIO;
14404 }
14405 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14406 drq->queue_id);
14407 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14408 shdr = (union lpfc_sli4_cfg_shdr *)
14409 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14410 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14411 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14412 if (shdr_status || shdr_add_status || rc) {
14413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14414 "2510 RQ_DESTROY mailbox failed with "
14415 "status x%x add_status x%x, mbx status x%x\n",
14416 shdr_status, shdr_add_status, rc);
14417 status = -ENXIO;
14418 }
14419 list_del_init(&hrq->list);
14420 list_del_init(&drq->list);
8fa38513 14421 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
14422 return status;
14423}
14424
14425/**
14426 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14427 * @phba: The virtual port for which this call being executed.
14428 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14429 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14430 * @xritag: the xritag that ties this io to the SGL pages.
14431 *
14432 * This routine will post the sgl pages for the IO that has the xritag
14433 * that is in the iocbq structure. The xritag is assigned during iocbq
14434 * creation and persists for as long as the driver is loaded.
14435 * if the caller has fewer than 256 scatter gather segments to map then
14436 * pdma_phys_addr1 should be 0.
14437 * If the caller needs to map more than 256 scatter gather segment then
14438 * pdma_phys_addr1 should be a valid physical address.
14439 * physical address for SGLs must be 64 byte aligned.
14440 * If you are going to map 2 SGL's then the first one must have 256 entries
14441 * the second sgl can have between 1 and 256 entries.
14442 *
14443 * Return codes:
14444 * 0 - Success
14445 * -ENXIO, -ENOMEM - Failure
14446 **/
14447int
14448lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14449 dma_addr_t pdma_phys_addr0,
14450 dma_addr_t pdma_phys_addr1,
14451 uint16_t xritag)
14452{
14453 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14454 LPFC_MBOXQ_t *mbox;
14455 int rc;
14456 uint32_t shdr_status, shdr_add_status;
6d368e53 14457 uint32_t mbox_tmo;
4f774513
JS
14458 union lpfc_sli4_cfg_shdr *shdr;
14459
14460 if (xritag == NO_XRI) {
14461 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14462 "0364 Invalid param:\n");
14463 return -EINVAL;
14464 }
14465
14466 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14467 if (!mbox)
14468 return -ENOMEM;
14469
14470 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14471 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14472 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 14473 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
14474
14475 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14476 &mbox->u.mqe.un.post_sgl_pages;
14477 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14478 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14479
14480 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14481 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14482 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14483 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14484
14485 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14486 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14487 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14488 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14489 if (!phba->sli4_hba.intr_enable)
14490 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 14491 else {
a183a15f 14492 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
14493 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14494 }
4f774513
JS
14495 /* The IOCTL status is embedded in the mailbox subheader. */
14496 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14497 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14498 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14499 if (rc != MBX_TIMEOUT)
14500 mempool_free(mbox, phba->mbox_mem_pool);
14501 if (shdr_status || shdr_add_status || rc) {
14502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14503 "2511 POST_SGL mailbox failed with "
14504 "status x%x add_status x%x, mbx status x%x\n",
14505 shdr_status, shdr_add_status, rc);
4f774513
JS
14506 }
14507 return 0;
14508}
4f774513 14509
6d368e53 14510/**
88a2cfbb 14511 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
14512 * @phba: pointer to lpfc hba data structure.
14513 *
14514 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
14515 * HBA consistent with the SLI-4 interface spec. This routine
14516 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14517 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 14518 *
88a2cfbb
JS
14519 * Returns
14520 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14521 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14522 **/
5d8b8167 14523static uint16_t
6d368e53
JS
14524lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14525{
14526 unsigned long xri;
14527
14528 /*
14529 * Fetch the next logical xri. Because this index is logical,
14530 * the driver starts at 0 each time.
14531 */
14532 spin_lock_irq(&phba->hbalock);
14533 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14534 phba->sli4_hba.max_cfg_param.max_xri, 0);
14535 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14536 spin_unlock_irq(&phba->hbalock);
14537 return NO_XRI;
14538 } else {
14539 set_bit(xri, phba->sli4_hba.xri_bmask);
14540 phba->sli4_hba.max_cfg_param.xri_used++;
6d368e53 14541 }
6d368e53
JS
14542 spin_unlock_irq(&phba->hbalock);
14543 return xri;
14544}
14545
14546/**
14547 * lpfc_sli4_free_xri - Release an xri for reuse.
14548 * @phba: pointer to lpfc hba data structure.
14549 *
14550 * This routine is invoked to release an xri to the pool of
14551 * available rpis maintained by the driver.
14552 **/
5d8b8167 14553static void
6d368e53
JS
14554__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14555{
14556 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
6d368e53
JS
14557 phba->sli4_hba.max_cfg_param.xri_used--;
14558 }
14559}
14560
14561/**
14562 * lpfc_sli4_free_xri - Release an xri for reuse.
14563 * @phba: pointer to lpfc hba data structure.
14564 *
14565 * This routine is invoked to release an xri to the pool of
14566 * available rpis maintained by the driver.
14567 **/
14568void
14569lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14570{
14571 spin_lock_irq(&phba->hbalock);
14572 __lpfc_sli4_free_xri(phba, xri);
14573 spin_unlock_irq(&phba->hbalock);
14574}
14575
4f774513
JS
14576/**
14577 * lpfc_sli4_next_xritag - Get an xritag for the io
14578 * @phba: Pointer to HBA context object.
14579 *
14580 * This function gets an xritag for the iocb. If there is no unused xritag
14581 * it will return 0xffff.
14582 * The function returns the allocated xritag if successful, else returns zero.
14583 * Zero is not a valid xritag.
14584 * The caller is not required to hold any lock.
14585 **/
14586uint16_t
14587lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14588{
6d368e53 14589 uint16_t xri_index;
4f774513 14590
6d368e53 14591 xri_index = lpfc_sli4_alloc_xri(phba);
81378052
JS
14592 if (xri_index == NO_XRI)
14593 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14594 "2004 Failed to allocate XRI.last XRITAG is %d"
14595 " Max XRI is %d, Used XRI is %d\n",
14596 xri_index,
14597 phba->sli4_hba.max_cfg_param.max_xri,
14598 phba->sli4_hba.max_cfg_param.xri_used);
14599 return xri_index;
4f774513
JS
14600}
14601
14602/**
6d368e53 14603 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
4f774513 14604 * @phba: pointer to lpfc hba data structure.
8a9d2e80
JS
14605 * @post_sgl_list: pointer to els sgl entry list.
14606 * @count: number of els sgl entries on the list.
4f774513
JS
14607 *
14608 * This routine is invoked to post a block of driver's sgl pages to the
14609 * HBA using non-embedded mailbox command. No Lock is held. This routine
14610 * is only called when the driver is loading and after all IO has been
14611 * stopped.
14612 **/
8a9d2e80
JS
14613static int
14614lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14615 struct list_head *post_sgl_list,
14616 int post_cnt)
4f774513 14617{
8a9d2e80 14618 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4f774513
JS
14619 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14620 struct sgl_page_pairs *sgl_pg_pairs;
14621 void *viraddr;
14622 LPFC_MBOXQ_t *mbox;
14623 uint32_t reqlen, alloclen, pg_pairs;
14624 uint32_t mbox_tmo;
8a9d2e80
JS
14625 uint16_t xritag_start = 0;
14626 int rc = 0;
4f774513
JS
14627 uint32_t shdr_status, shdr_add_status;
14628 union lpfc_sli4_cfg_shdr *shdr;
14629
8a9d2e80 14630 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
4f774513 14631 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 14632 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
14633 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14634 "2559 Block sgl registration required DMA "
14635 "size (%d) great than a page\n", reqlen);
14636 return -ENOMEM;
14637 }
14638 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 14639 if (!mbox)
4f774513 14640 return -ENOMEM;
4f774513
JS
14641
14642 /* Allocate DMA memory and set up the non-embedded mailbox command */
14643 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14644 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14645 LPFC_SLI4_MBX_NEMBED);
14646
14647 if (alloclen < reqlen) {
14648 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14649 "0285 Allocated DMA memory size (%d) is "
14650 "less than the requested DMA memory "
14651 "size (%d)\n", alloclen, reqlen);
14652 lpfc_sli4_mbox_cmd_free(phba, mbox);
14653 return -ENOMEM;
14654 }
4f774513 14655 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 14656 viraddr = mbox->sge_array->addr[0];
4f774513
JS
14657 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14658 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14659
8a9d2e80
JS
14660 pg_pairs = 0;
14661 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
4f774513
JS
14662 /* Set up the sge entry */
14663 sgl_pg_pairs->sgl_pg0_addr_lo =
14664 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14665 sgl_pg_pairs->sgl_pg0_addr_hi =
14666 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14667 sgl_pg_pairs->sgl_pg1_addr_lo =
14668 cpu_to_le32(putPaddrLow(0));
14669 sgl_pg_pairs->sgl_pg1_addr_hi =
14670 cpu_to_le32(putPaddrHigh(0));
6d368e53 14671
4f774513
JS
14672 /* Keep the first xritag on the list */
14673 if (pg_pairs == 0)
14674 xritag_start = sglq_entry->sli4_xritag;
14675 sgl_pg_pairs++;
8a9d2e80 14676 pg_pairs++;
4f774513 14677 }
6d368e53
JS
14678
14679 /* Complete initialization and perform endian conversion. */
4f774513 14680 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
8a9d2e80 14681 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
4f774513 14682 sgl->word0 = cpu_to_le32(sgl->word0);
4f774513
JS
14683 if (!phba->sli4_hba.intr_enable)
14684 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14685 else {
a183a15f 14686 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
14687 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14688 }
14689 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14690 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14691 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14692 if (rc != MBX_TIMEOUT)
14693 lpfc_sli4_mbox_cmd_free(phba, mbox);
14694 if (shdr_status || shdr_add_status || rc) {
14695 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14696 "2513 POST_SGL_BLOCK mailbox command failed "
14697 "status x%x add_status x%x mbx status x%x\n",
14698 shdr_status, shdr_add_status, rc);
14699 rc = -ENXIO;
14700 }
14701 return rc;
14702}
14703
14704/**
14705 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14706 * @phba: pointer to lpfc hba data structure.
14707 * @sblist: pointer to scsi buffer list.
14708 * @count: number of scsi buffers on the list.
14709 *
14710 * This routine is invoked to post a block of @count scsi sgl pages from a
14711 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14712 * No Lock is held.
14713 *
14714 **/
14715int
8a9d2e80
JS
14716lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14717 struct list_head *sblist,
14718 int count)
4f774513
JS
14719{
14720 struct lpfc_scsi_buf *psb;
14721 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14722 struct sgl_page_pairs *sgl_pg_pairs;
14723 void *viraddr;
14724 LPFC_MBOXQ_t *mbox;
14725 uint32_t reqlen, alloclen, pg_pairs;
14726 uint32_t mbox_tmo;
14727 uint16_t xritag_start = 0;
14728 int rc = 0;
14729 uint32_t shdr_status, shdr_add_status;
14730 dma_addr_t pdma_phys_bpl1;
14731 union lpfc_sli4_cfg_shdr *shdr;
14732
14733 /* Calculate the requested length of the dma memory */
8a9d2e80 14734 reqlen = count * sizeof(struct sgl_page_pairs) +
4f774513 14735 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 14736 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
14737 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14738 "0217 Block sgl registration required DMA "
14739 "size (%d) great than a page\n", reqlen);
14740 return -ENOMEM;
14741 }
14742 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14743 if (!mbox) {
14744 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14745 "0283 Failed to allocate mbox cmd memory\n");
14746 return -ENOMEM;
14747 }
14748
14749 /* Allocate DMA memory and set up the non-embedded mailbox command */
14750 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14751 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14752 LPFC_SLI4_MBX_NEMBED);
14753
14754 if (alloclen < reqlen) {
14755 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14756 "2561 Allocated DMA memory size (%d) is "
14757 "less than the requested DMA memory "
14758 "size (%d)\n", alloclen, reqlen);
14759 lpfc_sli4_mbox_cmd_free(phba, mbox);
14760 return -ENOMEM;
14761 }
6d368e53 14762
4f774513 14763 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
14764 viraddr = mbox->sge_array->addr[0];
14765
14766 /* Set up the SGL pages in the non-embedded DMA pages */
14767 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14768 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14769
14770 pg_pairs = 0;
14771 list_for_each_entry(psb, sblist, list) {
14772 /* Set up the sge entry */
14773 sgl_pg_pairs->sgl_pg0_addr_lo =
14774 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14775 sgl_pg_pairs->sgl_pg0_addr_hi =
14776 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14777 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14778 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14779 else
14780 pdma_phys_bpl1 = 0;
14781 sgl_pg_pairs->sgl_pg1_addr_lo =
14782 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14783 sgl_pg_pairs->sgl_pg1_addr_hi =
14784 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14785 /* Keep the first xritag on the list */
14786 if (pg_pairs == 0)
14787 xritag_start = psb->cur_iocbq.sli4_xritag;
14788 sgl_pg_pairs++;
14789 pg_pairs++;
14790 }
14791 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14792 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14793 /* Perform endian conversion if necessary */
14794 sgl->word0 = cpu_to_le32(sgl->word0);
14795
14796 if (!phba->sli4_hba.intr_enable)
14797 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14798 else {
a183a15f 14799 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
14800 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14801 }
14802 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14803 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14804 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14805 if (rc != MBX_TIMEOUT)
14806 lpfc_sli4_mbox_cmd_free(phba, mbox);
14807 if (shdr_status || shdr_add_status || rc) {
14808 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14809 "2564 POST_SGL_BLOCK mailbox command failed "
14810 "status x%x add_status x%x mbx status x%x\n",
14811 shdr_status, shdr_add_status, rc);
14812 rc = -ENXIO;
14813 }
14814 return rc;
14815}
14816
14817/**
14818 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14819 * @phba: pointer to lpfc_hba struct that the frame was received on
14820 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14821 *
14822 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14823 * valid type of frame that the LPFC driver will handle. This function will
14824 * return a zero if the frame is a valid frame or a non zero value when the
14825 * frame does not pass the check.
14826 **/
14827static int
14828lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14829{
474ffb74
TH
14830 /* make rctl_names static to save stack space */
14831 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
4f774513
JS
14832 char *type_names[] = FC_TYPE_NAMES_INIT;
14833 struct fc_vft_header *fc_vft_hdr;
546fc854 14834 uint32_t *header = (uint32_t *) fc_hdr;
4f774513
JS
14835
14836 switch (fc_hdr->fh_r_ctl) {
14837 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14838 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14839 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14840 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14841 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14842 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14843 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14844 case FC_RCTL_DD_CMD_STATUS: /* command status */
14845 case FC_RCTL_ELS_REQ: /* extended link services request */
14846 case FC_RCTL_ELS_REP: /* extended link services reply */
14847 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14848 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14849 case FC_RCTL_BA_NOP: /* basic link service NOP */
14850 case FC_RCTL_BA_ABTS: /* basic link service abort */
14851 case FC_RCTL_BA_RMC: /* remove connection */
14852 case FC_RCTL_BA_ACC: /* basic accept */
14853 case FC_RCTL_BA_RJT: /* basic reject */
14854 case FC_RCTL_BA_PRMT:
14855 case FC_RCTL_ACK_1: /* acknowledge_1 */
14856 case FC_RCTL_ACK_0: /* acknowledge_0 */
14857 case FC_RCTL_P_RJT: /* port reject */
14858 case FC_RCTL_F_RJT: /* fabric reject */
14859 case FC_RCTL_P_BSY: /* port busy */
14860 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14861 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14862 case FC_RCTL_LCR: /* link credit reset */
14863 case FC_RCTL_END: /* end */
14864 break;
14865 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14866 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14867 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14868 return lpfc_fc_frame_check(phba, fc_hdr);
14869 default:
14870 goto drop;
14871 }
14872 switch (fc_hdr->fh_type) {
14873 case FC_TYPE_BLS:
14874 case FC_TYPE_ELS:
14875 case FC_TYPE_FCP:
14876 case FC_TYPE_CT:
14877 break;
14878 case FC_TYPE_IP:
14879 case FC_TYPE_ILS:
14880 default:
14881 goto drop;
14882 }
546fc854 14883
4f774513 14884 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
88f43a08
JS
14885 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14886 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14887 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14888 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
546fc854
JS
14889 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14890 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
88f43a08
JS
14891 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14892 be32_to_cpu(header[6]));
4f774513
JS
14893 return 0;
14894drop:
14895 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14896 "2539 Dropped frame rctl:%s type:%s\n",
14897 rctl_names[fc_hdr->fh_r_ctl],
14898 type_names[fc_hdr->fh_type]);
14899 return 1;
14900}
14901
14902/**
14903 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14904 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14905 *
14906 * This function processes the FC header to retrieve the VFI from the VF
14907 * header, if one exists. This function will return the VFI if one exists
14908 * or 0 if no VSAN Header exists.
14909 **/
14910static uint32_t
14911lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14912{
14913 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14914
14915 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14916 return 0;
14917 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14918}
14919
14920/**
14921 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14922 * @phba: Pointer to the HBA structure to search for the vport on
14923 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14924 * @fcfi: The FC Fabric ID that the frame came from
14925 *
14926 * This function searches the @phba for a vport that matches the content of the
14927 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14928 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14929 * returns the matching vport pointer or NULL if unable to match frame to a
14930 * vport.
14931 **/
14932static struct lpfc_vport *
14933lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14934 uint16_t fcfi)
14935{
14936 struct lpfc_vport **vports;
14937 struct lpfc_vport *vport = NULL;
14938 int i;
14939 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14940 fc_hdr->fh_d_id[1] << 8 |
14941 fc_hdr->fh_d_id[2]);
939723a4 14942
bf08611b
JS
14943 if (did == Fabric_DID)
14944 return phba->pport;
939723a4
JS
14945 if ((phba->pport->fc_flag & FC_PT2PT) &&
14946 !(phba->link_state == LPFC_HBA_READY))
14947 return phba->pport;
14948
4f774513
JS
14949 vports = lpfc_create_vport_work_array(phba);
14950 if (vports != NULL)
14951 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14952 if (phba->fcf.fcfi == fcfi &&
14953 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14954 vports[i]->fc_myDID == did) {
14955 vport = vports[i];
14956 break;
14957 }
14958 }
14959 lpfc_destroy_vport_work_array(phba, vports);
14960 return vport;
14961}
14962
45ed1190
JS
14963/**
14964 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14965 * @vport: The vport to work on.
14966 *
14967 * This function updates the receive sequence time stamp for this vport. The
14968 * receive sequence time stamp indicates the time that the last frame of the
14969 * the sequence that has been idle for the longest amount of time was received.
14970 * the driver uses this time stamp to indicate if any received sequences have
14971 * timed out.
14972 **/
5d8b8167 14973static void
45ed1190
JS
14974lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14975{
14976 struct lpfc_dmabuf *h_buf;
14977 struct hbq_dmabuf *dmabuf = NULL;
14978
14979 /* get the oldest sequence on the rcv list */
14980 h_buf = list_get_first(&vport->rcv_buffer_list,
14981 struct lpfc_dmabuf, list);
14982 if (!h_buf)
14983 return;
14984 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14985 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14986}
14987
14988/**
14989 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14990 * @vport: The vport that the received sequences were sent to.
14991 *
14992 * This function cleans up all outstanding received sequences. This is called
14993 * by the driver when a link event or user action invalidates all the received
14994 * sequences.
14995 **/
14996void
14997lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14998{
14999 struct lpfc_dmabuf *h_buf, *hnext;
15000 struct lpfc_dmabuf *d_buf, *dnext;
15001 struct hbq_dmabuf *dmabuf = NULL;
15002
15003 /* start with the oldest sequence on the rcv list */
15004 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15005 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15006 list_del_init(&dmabuf->hbuf.list);
15007 list_for_each_entry_safe(d_buf, dnext,
15008 &dmabuf->dbuf.list, list) {
15009 list_del_init(&d_buf->list);
15010 lpfc_in_buf_free(vport->phba, d_buf);
15011 }
15012 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15013 }
15014}
15015
15016/**
15017 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
15018 * @vport: The vport that the received sequences were sent to.
15019 *
15020 * This function determines whether any received sequences have timed out by
15021 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
15022 * indicates that there is at least one timed out sequence this routine will
15023 * go through the received sequences one at a time from most inactive to most
15024 * active to determine which ones need to be cleaned up. Once it has determined
15025 * that a sequence needs to be cleaned up it will simply free up the resources
15026 * without sending an abort.
15027 **/
15028void
15029lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
15030{
15031 struct lpfc_dmabuf *h_buf, *hnext;
15032 struct lpfc_dmabuf *d_buf, *dnext;
15033 struct hbq_dmabuf *dmabuf = NULL;
15034 unsigned long timeout;
15035 int abort_count = 0;
15036
15037 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15038 vport->rcv_buffer_time_stamp);
15039 if (list_empty(&vport->rcv_buffer_list) ||
15040 time_before(jiffies, timeout))
15041 return;
15042 /* start with the oldest sequence on the rcv list */
15043 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15044 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15045 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15046 dmabuf->time_stamp);
15047 if (time_before(jiffies, timeout))
15048 break;
15049 abort_count++;
15050 list_del_init(&dmabuf->hbuf.list);
15051 list_for_each_entry_safe(d_buf, dnext,
15052 &dmabuf->dbuf.list, list) {
15053 list_del_init(&d_buf->list);
15054 lpfc_in_buf_free(vport->phba, d_buf);
15055 }
15056 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15057 }
15058 if (abort_count)
15059 lpfc_update_rcv_time_stamp(vport);
15060}
15061
4f774513
JS
15062/**
15063 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
15064 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
15065 *
15066 * This function searches through the existing incomplete sequences that have
15067 * been sent to this @vport. If the frame matches one of the incomplete
15068 * sequences then the dbuf in the @dmabuf is added to the list of frames that
15069 * make up that sequence. If no sequence is found that matches this frame then
15070 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
15071 * This function returns a pointer to the first dmabuf in the sequence list that
15072 * the frame was linked to.
15073 **/
15074static struct hbq_dmabuf *
15075lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15076{
15077 struct fc_frame_header *new_hdr;
15078 struct fc_frame_header *temp_hdr;
15079 struct lpfc_dmabuf *d_buf;
15080 struct lpfc_dmabuf *h_buf;
15081 struct hbq_dmabuf *seq_dmabuf = NULL;
15082 struct hbq_dmabuf *temp_dmabuf = NULL;
4360ca9c 15083 uint8_t found = 0;
4f774513 15084
4d9ab994 15085 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 15086 dmabuf->time_stamp = jiffies;
4f774513 15087 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
4360ca9c 15088
4f774513
JS
15089 /* Use the hdr_buf to find the sequence that this frame belongs to */
15090 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15091 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15092 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15093 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15094 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15095 continue;
15096 /* found a pending sequence that matches this frame */
15097 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15098 break;
15099 }
15100 if (!seq_dmabuf) {
15101 /*
15102 * This indicates first frame received for this sequence.
15103 * Queue the buffer on the vport's rcv_buffer_list.
15104 */
15105 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 15106 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
15107 return dmabuf;
15108 }
15109 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
15110 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15111 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
15112 list_del_init(&seq_dmabuf->hbuf.list);
15113 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15114 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 15115 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
15116 return dmabuf;
15117 }
45ed1190
JS
15118 /* move this sequence to the tail to indicate a young sequence */
15119 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15120 seq_dmabuf->time_stamp = jiffies;
15121 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
15122 if (list_empty(&seq_dmabuf->dbuf.list)) {
15123 temp_hdr = dmabuf->hbuf.virt;
15124 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15125 return seq_dmabuf;
15126 }
4f774513 15127 /* find the correct place in the sequence to insert this frame */
4360ca9c
JS
15128 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15129 while (!found) {
4f774513
JS
15130 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15131 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15132 /*
15133 * If the frame's sequence count is greater than the frame on
15134 * the list then insert the frame right after this frame
15135 */
eeead811
JS
15136 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15137 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513 15138 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
4360ca9c
JS
15139 found = 1;
15140 break;
4f774513 15141 }
4360ca9c
JS
15142
15143 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15144 break;
15145 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
4f774513 15146 }
4360ca9c
JS
15147
15148 if (found)
15149 return seq_dmabuf;
4f774513
JS
15150 return NULL;
15151}
15152
6669f9bb
JS
15153/**
15154 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15155 * @vport: pointer to a vitural port
15156 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15157 *
15158 * This function tries to abort from the partially assembed sequence, described
15159 * by the information from basic abbort @dmabuf. It checks to see whether such
15160 * partially assembled sequence held by the driver. If so, it shall free up all
15161 * the frames from the partially assembled sequence.
15162 *
15163 * Return
15164 * true -- if there is matching partially assembled sequence present and all
15165 * the frames freed with the sequence;
15166 * false -- if there is no matching partially assembled sequence present so
15167 * nothing got aborted in the lower layer driver
15168 **/
15169static bool
15170lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15171 struct hbq_dmabuf *dmabuf)
15172{
15173 struct fc_frame_header *new_hdr;
15174 struct fc_frame_header *temp_hdr;
15175 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15176 struct hbq_dmabuf *seq_dmabuf = NULL;
15177
15178 /* Use the hdr_buf to find the sequence that matches this frame */
15179 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15180 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15181 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15182 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15183 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15184 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15185 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15186 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15187 continue;
15188 /* found a pending sequence that matches this frame */
15189 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15190 break;
15191 }
15192
15193 /* Free up all the frames from the partially assembled sequence */
15194 if (seq_dmabuf) {
15195 list_for_each_entry_safe(d_buf, n_buf,
15196 &seq_dmabuf->dbuf.list, list) {
15197 list_del_init(&d_buf->list);
15198 lpfc_in_buf_free(vport->phba, d_buf);
15199 }
15200 return true;
15201 }
15202 return false;
15203}
15204
6dd9e31c
JS
15205/**
15206 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15207 * @vport: pointer to a vitural port
15208 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15209 *
15210 * This function tries to abort from the assembed sequence from upper level
15211 * protocol, described by the information from basic abbort @dmabuf. It
15212 * checks to see whether such pending context exists at upper level protocol.
15213 * If so, it shall clean up the pending context.
15214 *
15215 * Return
15216 * true -- if there is matching pending context of the sequence cleaned
15217 * at ulp;
15218 * false -- if there is no matching pending context of the sequence present
15219 * at ulp.
15220 **/
15221static bool
15222lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15223{
15224 struct lpfc_hba *phba = vport->phba;
15225 int handled;
15226
15227 /* Accepting abort at ulp with SLI4 only */
15228 if (phba->sli_rev < LPFC_SLI_REV4)
15229 return false;
15230
15231 /* Register all caring upper level protocols to attend abort */
15232 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15233 if (handled)
15234 return true;
15235
15236 return false;
15237}
15238
6669f9bb 15239/**
546fc854 15240 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
15241 * @phba: Pointer to HBA context object.
15242 * @cmd_iocbq: pointer to the command iocbq structure.
15243 * @rsp_iocbq: pointer to the response iocbq structure.
15244 *
546fc854 15245 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
15246 * event. It properly releases the memory allocated to the sequence abort
15247 * accept iocb.
15248 **/
15249static void
546fc854 15250lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
15251 struct lpfc_iocbq *cmd_iocbq,
15252 struct lpfc_iocbq *rsp_iocbq)
15253{
6dd9e31c
JS
15254 struct lpfc_nodelist *ndlp;
15255
15256 if (cmd_iocbq) {
15257 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15258 lpfc_nlp_put(ndlp);
15259 lpfc_nlp_not_used(ndlp);
6669f9bb 15260 lpfc_sli_release_iocbq(phba, cmd_iocbq);
6dd9e31c 15261 }
6b5151fd
JS
15262
15263 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15264 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15266 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15267 rsp_iocbq->iocb.ulpStatus,
15268 rsp_iocbq->iocb.un.ulpWord[4]);
6669f9bb
JS
15269}
15270
6d368e53
JS
15271/**
15272 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15273 * @phba: Pointer to HBA context object.
15274 * @xri: xri id in transaction.
15275 *
15276 * This function validates the xri maps to the known range of XRIs allocated an
15277 * used by the driver.
15278 **/
7851fe2c 15279uint16_t
6d368e53
JS
15280lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15281 uint16_t xri)
15282{
a2fc4aef 15283 uint16_t i;
6d368e53
JS
15284
15285 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15286 if (xri == phba->sli4_hba.xri_ids[i])
15287 return i;
15288 }
15289 return NO_XRI;
15290}
15291
6669f9bb 15292/**
546fc854 15293 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
15294 * @phba: Pointer to HBA context object.
15295 * @fc_hdr: pointer to a FC frame header.
15296 *
546fc854 15297 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
15298 * event after aborting the sequence handling.
15299 **/
15300static void
6dd9e31c
JS
15301lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15302 struct fc_frame_header *fc_hdr, bool aborted)
6669f9bb 15303{
6dd9e31c 15304 struct lpfc_hba *phba = vport->phba;
6669f9bb
JS
15305 struct lpfc_iocbq *ctiocb = NULL;
15306 struct lpfc_nodelist *ndlp;
ee0f4fe1 15307 uint16_t oxid, rxid, xri, lxri;
5ffc266e 15308 uint32_t sid, fctl;
6669f9bb 15309 IOCB_t *icmd;
546fc854 15310 int rc;
6669f9bb
JS
15311
15312 if (!lpfc_is_link_up(phba))
15313 return;
15314
15315 sid = sli4_sid_from_fc_hdr(fc_hdr);
15316 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 15317 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb 15318
6dd9e31c 15319 ndlp = lpfc_findnode_did(vport, sid);
6669f9bb 15320 if (!ndlp) {
6dd9e31c
JS
15321 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15322 if (!ndlp) {
15323 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15324 "1268 Failed to allocate ndlp for "
15325 "oxid:x%x SID:x%x\n", oxid, sid);
15326 return;
15327 }
15328 lpfc_nlp_init(vport, ndlp, sid);
15329 /* Put ndlp onto pport node list */
15330 lpfc_enqueue_node(vport, ndlp);
15331 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15332 /* re-setup ndlp without removing from node list */
15333 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15334 if (!ndlp) {
15335 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15336 "3275 Failed to active ndlp found "
15337 "for oxid:x%x SID:x%x\n", oxid, sid);
15338 return;
15339 }
6669f9bb
JS
15340 }
15341
546fc854 15342 /* Allocate buffer for rsp iocb */
6669f9bb
JS
15343 ctiocb = lpfc_sli_get_iocbq(phba);
15344 if (!ctiocb)
15345 return;
15346
5ffc266e
JS
15347 /* Extract the F_CTL field from FC_HDR */
15348 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15349
6669f9bb 15350 icmd = &ctiocb->iocb;
6669f9bb 15351 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 15352 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
15353 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15354 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15355 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15356
15357 /* Fill in the rest of iocb fields */
15358 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15359 icmd->ulpBdeCount = 0;
15360 icmd->ulpLe = 1;
15361 icmd->ulpClass = CLASS3;
6d368e53 15362 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
6dd9e31c 15363 ctiocb->context1 = lpfc_nlp_get(ndlp);
6669f9bb 15364
6669f9bb
JS
15365 ctiocb->iocb_cmpl = NULL;
15366 ctiocb->vport = phba->pport;
546fc854 15367 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 15368 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
15369 ctiocb->sli4_xritag = NO_XRI;
15370
ee0f4fe1
JS
15371 if (fctl & FC_FC_EX_CTX)
15372 /* Exchange responder sent the abort so we
15373 * own the oxid.
15374 */
15375 xri = oxid;
15376 else
15377 xri = rxid;
15378 lxri = lpfc_sli4_xri_inrange(phba, xri);
15379 if (lxri != NO_XRI)
15380 lpfc_set_rrq_active(phba, ndlp, lxri,
15381 (xri == oxid) ? rxid : oxid, 0);
6dd9e31c
JS
15382 /* For BA_ABTS from exchange responder, if the logical xri with
15383 * the oxid maps to the FCP XRI range, the port no longer has
15384 * that exchange context, send a BLS_RJT. Override the IOCB for
15385 * a BA_RJT.
15386 */
15387 if ((fctl & FC_FC_EX_CTX) &&
15388 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
15389 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15390 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15391 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15392 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15393 }
15394
15395 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15396 * the driver no longer has that exchange, send a BLS_RJT. Override
15397 * the IOCB for a BA_RJT.
546fc854 15398 */
6dd9e31c 15399 if (aborted == false) {
546fc854
JS
15400 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15401 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15402 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15403 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15404 }
6669f9bb 15405
5ffc266e
JS
15406 if (fctl & FC_FC_EX_CTX) {
15407 /* ABTS sent by responder to CT exchange, construction
15408 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15409 * field and RX_ID from ABTS for RX_ID field.
15410 */
546fc854 15411 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
5ffc266e
JS
15412 } else {
15413 /* ABTS sent by initiator to CT exchange, construction
15414 * of BA_ACC will need to allocate a new XRI as for the
f09c3acc 15415 * XRI_TAG field.
5ffc266e 15416 */
546fc854 15417 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
5ffc266e 15418 }
f09c3acc 15419 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
546fc854 15420 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 15421
546fc854 15422 /* Xmit CT abts response on exchange <xid> */
6dd9e31c
JS
15423 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15424 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15425 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
546fc854
JS
15426
15427 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15428 if (rc == IOCB_ERROR) {
6dd9e31c
JS
15429 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15430 "2925 Failed to issue CT ABTS RSP x%x on "
15431 "xri x%x, Data x%x\n",
15432 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15433 phba->link_state);
15434 lpfc_nlp_put(ndlp);
15435 ctiocb->context1 = NULL;
546fc854
JS
15436 lpfc_sli_release_iocbq(phba, ctiocb);
15437 }
6669f9bb
JS
15438}
15439
15440/**
15441 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15442 * @vport: Pointer to the vport on which this sequence was received
15443 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15444 *
15445 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15446 * receive sequence is only partially assembed by the driver, it shall abort
15447 * the partially assembled frames for the sequence. Otherwise, if the
15448 * unsolicited receive sequence has been completely assembled and passed to
15449 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15450 * unsolicited sequence has been aborted. After that, it will issue a basic
15451 * accept to accept the abort.
15452 **/
5d8b8167 15453static void
6669f9bb
JS
15454lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15455 struct hbq_dmabuf *dmabuf)
15456{
15457 struct lpfc_hba *phba = vport->phba;
15458 struct fc_frame_header fc_hdr;
5ffc266e 15459 uint32_t fctl;
6dd9e31c 15460 bool aborted;
6669f9bb 15461
6669f9bb
JS
15462 /* Make a copy of fc_hdr before the dmabuf being released */
15463 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 15464 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 15465
5ffc266e 15466 if (fctl & FC_FC_EX_CTX) {
6dd9e31c
JS
15467 /* ABTS by responder to exchange, no cleanup needed */
15468 aborted = true;
5ffc266e 15469 } else {
6dd9e31c
JS
15470 /* ABTS by initiator to exchange, need to do cleanup */
15471 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15472 if (aborted == false)
15473 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
5ffc266e 15474 }
6dd9e31c
JS
15475 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15476
15477 /* Respond with BA_ACC or BA_RJT accordingly */
15478 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
6669f9bb
JS
15479}
15480
4f774513
JS
15481/**
15482 * lpfc_seq_complete - Indicates if a sequence is complete
15483 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15484 *
15485 * This function checks the sequence, starting with the frame described by
15486 * @dmabuf, to see if all the frames associated with this sequence are present.
15487 * the frames associated with this sequence are linked to the @dmabuf using the
15488 * dbuf list. This function looks for two major things. 1) That the first frame
15489 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15490 * set. 3) That there are no holes in the sequence count. The function will
15491 * return 1 when the sequence is complete, otherwise it will return 0.
15492 **/
15493static int
15494lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15495{
15496 struct fc_frame_header *hdr;
15497 struct lpfc_dmabuf *d_buf;
15498 struct hbq_dmabuf *seq_dmabuf;
15499 uint32_t fctl;
15500 int seq_count = 0;
15501
15502 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15503 /* make sure first fame of sequence has a sequence count of zero */
15504 if (hdr->fh_seq_cnt != seq_count)
15505 return 0;
15506 fctl = (hdr->fh_f_ctl[0] << 16 |
15507 hdr->fh_f_ctl[1] << 8 |
15508 hdr->fh_f_ctl[2]);
15509 /* If last frame of sequence we can return success. */
15510 if (fctl & FC_FC_END_SEQ)
15511 return 1;
15512 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15513 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15514 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15515 /* If there is a hole in the sequence count then fail. */
eeead811 15516 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
15517 return 0;
15518 fctl = (hdr->fh_f_ctl[0] << 16 |
15519 hdr->fh_f_ctl[1] << 8 |
15520 hdr->fh_f_ctl[2]);
15521 /* If last frame of sequence we can return success. */
15522 if (fctl & FC_FC_END_SEQ)
15523 return 1;
15524 }
15525 return 0;
15526}
15527
15528/**
15529 * lpfc_prep_seq - Prep sequence for ULP processing
15530 * @vport: Pointer to the vport on which this sequence was received
15531 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15532 *
15533 * This function takes a sequence, described by a list of frames, and creates
15534 * a list of iocbq structures to describe the sequence. This iocbq list will be
15535 * used to issue to the generic unsolicited sequence handler. This routine
15536 * returns a pointer to the first iocbq in the list. If the function is unable
15537 * to allocate an iocbq then it throw out the received frames that were not
15538 * able to be described and return a pointer to the first iocbq. If unable to
15539 * allocate any iocbqs (including the first) this function will return NULL.
15540 **/
15541static struct lpfc_iocbq *
15542lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15543{
7851fe2c 15544 struct hbq_dmabuf *hbq_buf;
4f774513
JS
15545 struct lpfc_dmabuf *d_buf, *n_buf;
15546 struct lpfc_iocbq *first_iocbq, *iocbq;
15547 struct fc_frame_header *fc_hdr;
15548 uint32_t sid;
7851fe2c 15549 uint32_t len, tot_len;
eeead811 15550 struct ulp_bde64 *pbde;
4f774513
JS
15551
15552 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15553 /* remove from receive buffer list */
15554 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 15555 lpfc_update_rcv_time_stamp(vport);
4f774513 15556 /* get the Remote Port's SID */
6669f9bb 15557 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 15558 tot_len = 0;
4f774513
JS
15559 /* Get an iocbq struct to fill in. */
15560 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15561 if (first_iocbq) {
15562 /* Initialize the first IOCB. */
8fa38513 15563 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513 15564 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
939723a4
JS
15565
15566 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15567 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15568 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15569 first_iocbq->iocb.un.rcvels.parmRo =
15570 sli4_did_from_fc_hdr(fc_hdr);
15571 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15572 } else
15573 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
15574 first_iocbq->iocb.ulpContext = NO_XRI;
15575 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15576 be16_to_cpu(fc_hdr->fh_ox_id);
15577 /* iocbq is prepped for internal consumption. Physical vpi. */
15578 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15579 vport->phba->vpi_ids[vport->vpi];
4f774513 15580 /* put the first buffer into the first IOCBq */
48a5a664
JS
15581 tot_len = bf_get(lpfc_rcqe_length,
15582 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15583
4f774513
JS
15584 first_iocbq->context2 = &seq_dmabuf->dbuf;
15585 first_iocbq->context3 = NULL;
15586 first_iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
15587 if (tot_len > LPFC_DATA_BUF_SIZE)
15588 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 15589 LPFC_DATA_BUF_SIZE;
48a5a664
JS
15590 else
15591 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15592
4f774513 15593 first_iocbq->iocb.un.rcvels.remoteID = sid;
48a5a664 15594
7851fe2c 15595 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
15596 }
15597 iocbq = first_iocbq;
15598 /*
15599 * Each IOCBq can have two Buffers assigned, so go through the list
15600 * of buffers for this sequence and save two buffers in each IOCBq
15601 */
15602 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15603 if (!iocbq) {
15604 lpfc_in_buf_free(vport->phba, d_buf);
15605 continue;
15606 }
15607 if (!iocbq->context3) {
15608 iocbq->context3 = d_buf;
15609 iocbq->iocb.ulpBdeCount++;
7851fe2c
JS
15610 /* We need to get the size out of the right CQE */
15611 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15612 len = bf_get(lpfc_rcqe_length,
15613 &hbq_buf->cq_event.cqe.rcqe_cmpl);
48a5a664
JS
15614 pbde = (struct ulp_bde64 *)
15615 &iocbq->iocb.unsli3.sli3Words[4];
15616 if (len > LPFC_DATA_BUF_SIZE)
15617 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15618 else
15619 pbde->tus.f.bdeSize = len;
15620
7851fe2c
JS
15621 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15622 tot_len += len;
4f774513
JS
15623 } else {
15624 iocbq = lpfc_sli_get_iocbq(vport->phba);
15625 if (!iocbq) {
15626 if (first_iocbq) {
15627 first_iocbq->iocb.ulpStatus =
15628 IOSTAT_FCP_RSP_ERROR;
15629 first_iocbq->iocb.un.ulpWord[4] =
15630 IOERR_NO_RESOURCES;
15631 }
15632 lpfc_in_buf_free(vport->phba, d_buf);
15633 continue;
15634 }
48a5a664
JS
15635 /* We need to get the size out of the right CQE */
15636 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15637 len = bf_get(lpfc_rcqe_length,
15638 &hbq_buf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
15639 iocbq->context2 = d_buf;
15640 iocbq->context3 = NULL;
15641 iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
15642 if (len > LPFC_DATA_BUF_SIZE)
15643 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 15644 LPFC_DATA_BUF_SIZE;
48a5a664
JS
15645 else
15646 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
7851fe2c 15647
7851fe2c
JS
15648 tot_len += len;
15649 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15650
4f774513
JS
15651 iocbq->iocb.un.rcvels.remoteID = sid;
15652 list_add_tail(&iocbq->list, &first_iocbq->list);
15653 }
15654 }
15655 return first_iocbq;
15656}
15657
6669f9bb
JS
15658static void
15659lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15660 struct hbq_dmabuf *seq_dmabuf)
15661{
15662 struct fc_frame_header *fc_hdr;
15663 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15664 struct lpfc_hba *phba = vport->phba;
15665
15666 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15667 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15668 if (!iocbq) {
15669 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15670 "2707 Ring %d handler: Failed to allocate "
15671 "iocb Rctl x%x Type x%x received\n",
15672 LPFC_ELS_RING,
15673 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15674 return;
15675 }
15676 if (!lpfc_complete_unsol_iocb(phba,
15677 &phba->sli.ring[LPFC_ELS_RING],
15678 iocbq, fc_hdr->fh_r_ctl,
15679 fc_hdr->fh_type))
6d368e53 15680 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
15681 "2540 Ring %d handler: unexpected Rctl "
15682 "x%x Type x%x received\n",
15683 LPFC_ELS_RING,
15684 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15685
15686 /* Free iocb created in lpfc_prep_seq */
15687 list_for_each_entry_safe(curr_iocb, next_iocb,
15688 &iocbq->list, list) {
15689 list_del_init(&curr_iocb->list);
15690 lpfc_sli_release_iocbq(phba, curr_iocb);
15691 }
15692 lpfc_sli_release_iocbq(phba, iocbq);
15693}
15694
4f774513
JS
15695/**
15696 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15697 * @phba: Pointer to HBA context object.
15698 *
15699 * This function is called with no lock held. This function processes all
15700 * the received buffers and gives it to upper layers when a received buffer
15701 * indicates that it is the final frame in the sequence. The interrupt
15702 * service routine processes received buffers at interrupt contexts and adds
15703 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15704 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15705 * appropriate receive function when the final frame in a sequence is received.
15706 **/
4d9ab994
JS
15707void
15708lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15709 struct hbq_dmabuf *dmabuf)
4f774513 15710{
4d9ab994 15711 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
15712 struct fc_frame_header *fc_hdr;
15713 struct lpfc_vport *vport;
15714 uint32_t fcfi;
939723a4 15715 uint32_t did;
4f774513 15716
4f774513 15717 /* Process each received buffer */
4d9ab994
JS
15718 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15719 /* check to see if this a valid type of frame */
15720 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15721 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15722 return;
15723 }
7851fe2c
JS
15724 if ((bf_get(lpfc_cqe_code,
15725 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15726 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15727 &dmabuf->cq_event.cqe.rcqe_cmpl);
15728 else
15729 fcfi = bf_get(lpfc_rcqe_fcf_id,
15730 &dmabuf->cq_event.cqe.rcqe_cmpl);
939723a4 15731
4d9ab994 15732 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
939723a4 15733 if (!vport) {
4d9ab994
JS
15734 /* throw out the frame */
15735 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15736 return;
15737 }
939723a4
JS
15738
15739 /* d_id this frame is directed to */
15740 did = sli4_did_from_fc_hdr(fc_hdr);
15741
15742 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15743 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15744 (did != Fabric_DID)) {
15745 /*
15746 * Throw out the frame if we are not pt2pt.
15747 * The pt2pt protocol allows for discovery frames
15748 * to be received without a registered VPI.
15749 */
15750 if (!(vport->fc_flag & FC_PT2PT) ||
15751 (phba->link_state == LPFC_HBA_READY)) {
15752 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15753 return;
15754 }
15755 }
15756
6669f9bb
JS
15757 /* Handle the basic abort sequence (BA_ABTS) event */
15758 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15759 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15760 return;
15761 }
15762
4d9ab994
JS
15763 /* Link this frame */
15764 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15765 if (!seq_dmabuf) {
15766 /* unable to add frame to vport - throw it out */
15767 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15768 return;
15769 }
15770 /* If not last frame in sequence continue processing frames. */
def9c7a9 15771 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 15772 return;
def9c7a9 15773
6669f9bb
JS
15774 /* Send the complete sequence to the upper layer protocol */
15775 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 15776}
6fb120a7
JS
15777
15778/**
15779 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15780 * @phba: pointer to lpfc hba data structure.
15781 *
15782 * This routine is invoked to post rpi header templates to the
15783 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
15784 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15785 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
15786 *
15787 * This routine does not require any locks. It's usage is expected
15788 * to be driver load or reset recovery when the driver is
15789 * sequential.
15790 *
15791 * Return codes
af901ca1 15792 * 0 - successful
d439d286 15793 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
15794 * When this error occurs, the driver is not guaranteed
15795 * to have any rpi regions posted to the device and
15796 * must either attempt to repost the regions or take a
15797 * fatal error.
15798 **/
15799int
15800lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15801{
15802 struct lpfc_rpi_hdr *rpi_page;
15803 uint32_t rc = 0;
6d368e53
JS
15804 uint16_t lrpi = 0;
15805
15806 /* SLI4 ports that support extents do not require RPI headers. */
15807 if (!phba->sli4_hba.rpi_hdrs_in_use)
15808 goto exit;
15809 if (phba->sli4_hba.extents_in_use)
15810 return -EIO;
6fb120a7 15811
6fb120a7 15812 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
15813 /*
15814 * Assign the rpi headers a physical rpi only if the driver
15815 * has not initialized those resources. A port reset only
15816 * needs the headers posted.
15817 */
15818 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15819 LPFC_RPI_RSRC_RDY)
15820 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15821
6fb120a7
JS
15822 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15823 if (rc != MBX_SUCCESS) {
15824 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15825 "2008 Error %d posting all rpi "
15826 "headers\n", rc);
15827 rc = -EIO;
15828 break;
15829 }
15830 }
15831
6d368e53
JS
15832 exit:
15833 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15834 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
15835 return rc;
15836}
15837
15838/**
15839 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15840 * @phba: pointer to lpfc hba data structure.
15841 * @rpi_page: pointer to the rpi memory region.
15842 *
15843 * This routine is invoked to post a single rpi header to the
15844 * HBA consistent with the SLI-4 interface spec. This memory region
15845 * maps up to 64 rpi context regions.
15846 *
15847 * Return codes
af901ca1 15848 * 0 - successful
d439d286
JS
15849 * -ENOMEM - No available memory
15850 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
15851 **/
15852int
15853lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15854{
15855 LPFC_MBOXQ_t *mboxq;
15856 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15857 uint32_t rc = 0;
6fb120a7
JS
15858 uint32_t shdr_status, shdr_add_status;
15859 union lpfc_sli4_cfg_shdr *shdr;
15860
6d368e53
JS
15861 /* SLI4 ports that support extents do not require RPI headers. */
15862 if (!phba->sli4_hba.rpi_hdrs_in_use)
15863 return rc;
15864 if (phba->sli4_hba.extents_in_use)
15865 return -EIO;
15866
6fb120a7
JS
15867 /* The port is notified of the header region via a mailbox command. */
15868 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15869 if (!mboxq) {
15870 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15871 "2001 Unable to allocate memory for issuing "
15872 "SLI_CONFIG_SPECIAL mailbox command\n");
15873 return -ENOMEM;
15874 }
15875
15876 /* Post all rpi memory regions to the port. */
15877 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
15878 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15879 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15880 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
15881 sizeof(struct lpfc_sli4_cfg_mhdr),
15882 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
15883
15884
15885 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
15886 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15887 rpi_page->start_rpi);
6d368e53
JS
15888 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15889 hdr_tmpl, rpi_page->page_count);
15890
6fb120a7
JS
15891 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15892 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 15893 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
15894 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15895 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15896 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15897 if (rc != MBX_TIMEOUT)
15898 mempool_free(mboxq, phba->mbox_mem_pool);
15899 if (shdr_status || shdr_add_status || rc) {
15900 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15901 "2514 POST_RPI_HDR mailbox failed with "
15902 "status x%x add_status x%x, mbx status x%x\n",
15903 shdr_status, shdr_add_status, rc);
15904 rc = -ENXIO;
15905 }
15906 return rc;
15907}
15908
15909/**
15910 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15911 * @phba: pointer to lpfc hba data structure.
15912 *
15913 * This routine is invoked to post rpi header templates to the
15914 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
15915 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15916 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
15917 *
15918 * Returns
af901ca1 15919 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
15920 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15921 **/
15922int
15923lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15924{
6d368e53
JS
15925 unsigned long rpi;
15926 uint16_t max_rpi, rpi_limit;
15927 uint16_t rpi_remaining, lrpi = 0;
6fb120a7 15928 struct lpfc_rpi_hdr *rpi_hdr;
4902b381 15929 unsigned long iflag;
6fb120a7 15930
6fb120a7 15931 /*
6d368e53
JS
15932 * Fetch the next logical rpi. Because this index is logical,
15933 * the driver starts at 0 each time.
6fb120a7 15934 */
4902b381 15935 spin_lock_irqsave(&phba->hbalock, iflag);
be6bb941
JS
15936 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15937 rpi_limit = phba->sli4_hba.next_rpi;
15938
6d368e53
JS
15939 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15940 if (rpi >= rpi_limit)
6fb120a7
JS
15941 rpi = LPFC_RPI_ALLOC_ERROR;
15942 else {
15943 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15944 phba->sli4_hba.max_cfg_param.rpi_used++;
15945 phba->sli4_hba.rpi_count++;
15946 }
be6bb941
JS
15947 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15948 "0001 rpi:%x max:%x lim:%x\n",
15949 (int) rpi, max_rpi, rpi_limit);
6fb120a7
JS
15950
15951 /*
15952 * Don't try to allocate more rpi header regions if the device limit
6d368e53 15953 * has been exhausted.
6fb120a7
JS
15954 */
15955 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15956 (phba->sli4_hba.rpi_count >= max_rpi)) {
4902b381 15957 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
15958 return rpi;
15959 }
15960
6d368e53
JS
15961 /*
15962 * RPI header postings are not required for SLI4 ports capable of
15963 * extents.
15964 */
15965 if (!phba->sli4_hba.rpi_hdrs_in_use) {
4902b381 15966 spin_unlock_irqrestore(&phba->hbalock, iflag);
6d368e53
JS
15967 return rpi;
15968 }
15969
6fb120a7
JS
15970 /*
15971 * If the driver is running low on rpi resources, allocate another
15972 * page now. Note that the next_rpi value is used because
15973 * it represents how many are actually in use whereas max_rpi notes
15974 * how many are supported max by the device.
15975 */
6d368e53 15976 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
4902b381 15977 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
15978 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15979 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15980 if (!rpi_hdr) {
15981 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15982 "2002 Error Could not grow rpi "
15983 "count\n");
15984 } else {
6d368e53
JS
15985 lrpi = rpi_hdr->start_rpi;
15986 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
15987 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15988 }
15989 }
15990
15991 return rpi;
15992}
15993
d7c47992
JS
15994/**
15995 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15996 * @phba: pointer to lpfc hba data structure.
15997 *
15998 * This routine is invoked to release an rpi to the pool of
15999 * available rpis maintained by the driver.
16000 **/
5d8b8167 16001static void
d7c47992
JS
16002__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16003{
16004 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
16005 phba->sli4_hba.rpi_count--;
16006 phba->sli4_hba.max_cfg_param.rpi_used--;
16007 }
16008}
16009
6fb120a7
JS
16010/**
16011 * lpfc_sli4_free_rpi - Release an rpi for reuse.
16012 * @phba: pointer to lpfc hba data structure.
16013 *
16014 * This routine is invoked to release an rpi to the pool of
16015 * available rpis maintained by the driver.
16016 **/
16017void
16018lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16019{
16020 spin_lock_irq(&phba->hbalock);
d7c47992 16021 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
16022 spin_unlock_irq(&phba->hbalock);
16023}
16024
16025/**
16026 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
16027 * @phba: pointer to lpfc hba data structure.
16028 *
16029 * This routine is invoked to remove the memory region that
16030 * provided rpi via a bitmask.
16031 **/
16032void
16033lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
16034{
16035 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
16036 kfree(phba->sli4_hba.rpi_ids);
16037 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
16038}
16039
16040/**
16041 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
16042 * @phba: pointer to lpfc hba data structure.
16043 *
16044 * This routine is invoked to remove the memory region that
16045 * provided rpi via a bitmask.
16046 **/
16047int
6b5151fd
JS
16048lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
16049 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
6fb120a7
JS
16050{
16051 LPFC_MBOXQ_t *mboxq;
16052 struct lpfc_hba *phba = ndlp->phba;
16053 int rc;
16054
16055 /* The port is notified of the header region via a mailbox command. */
16056 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16057 if (!mboxq)
16058 return -ENOMEM;
16059
16060 /* Post all rpi memory regions to the port. */
16061 lpfc_resume_rpi(mboxq, ndlp);
6b5151fd
JS
16062 if (cmpl) {
16063 mboxq->mbox_cmpl = cmpl;
16064 mboxq->context1 = arg;
16065 mboxq->context2 = ndlp;
72859909
JS
16066 } else
16067 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6b5151fd 16068 mboxq->vport = ndlp->vport;
6fb120a7
JS
16069 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16070 if (rc == MBX_NOT_FINISHED) {
16071 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16072 "2010 Resume RPI Mailbox failed "
16073 "status %d, mbxStatus x%x\n", rc,
16074 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16075 mempool_free(mboxq, phba->mbox_mem_pool);
16076 return -EIO;
16077 }
16078 return 0;
16079}
16080
16081/**
16082 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 16083 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 16084 *
76a95d75 16085 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
16086 *
16087 * Returns:
16088 * 0 success
16089 * -Evalue otherwise
16090 **/
16091int
76a95d75 16092lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
16093{
16094 LPFC_MBOXQ_t *mboxq;
16095 int rc = 0;
6a9c52cf 16096 int retval = MBX_SUCCESS;
6fb120a7 16097 uint32_t mbox_tmo;
76a95d75 16098 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
16099 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16100 if (!mboxq)
16101 return -ENOMEM;
76a95d75 16102 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 16103 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 16104 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 16105 if (rc != MBX_SUCCESS) {
76a95d75 16106 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
16107 "2022 INIT VPI Mailbox failed "
16108 "status %d, mbxStatus x%x\n", rc,
16109 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 16110 retval = -EIO;
6fb120a7 16111 }
6a9c52cf 16112 if (rc != MBX_TIMEOUT)
76a95d75 16113 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
16114
16115 return retval;
6fb120a7
JS
16116}
16117
16118/**
16119 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16120 * @phba: pointer to lpfc hba data structure.
16121 * @mboxq: Pointer to mailbox object.
16122 *
16123 * This routine is invoked to manually add a single FCF record. The caller
16124 * must pass a completely initialized FCF_Record. This routine takes
16125 * care of the nonembedded mailbox operations.
16126 **/
16127static void
16128lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16129{
16130 void *virt_addr;
16131 union lpfc_sli4_cfg_shdr *shdr;
16132 uint32_t shdr_status, shdr_add_status;
16133
16134 virt_addr = mboxq->sge_array->addr[0];
16135 /* The IOCTL status is embedded in the mailbox subheader. */
16136 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16137 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16138 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16139
16140 if ((shdr_status || shdr_add_status) &&
16141 (shdr_status != STATUS_FCF_IN_USE))
16142 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16143 "2558 ADD_FCF_RECORD mailbox failed with "
16144 "status x%x add_status x%x\n",
16145 shdr_status, shdr_add_status);
16146
16147 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16148}
16149
16150/**
16151 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16152 * @phba: pointer to lpfc hba data structure.
16153 * @fcf_record: pointer to the initialized fcf record to add.
16154 *
16155 * This routine is invoked to manually add a single FCF record. The caller
16156 * must pass a completely initialized FCF_Record. This routine takes
16157 * care of the nonembedded mailbox operations.
16158 **/
16159int
16160lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16161{
16162 int rc = 0;
16163 LPFC_MBOXQ_t *mboxq;
16164 uint8_t *bytep;
16165 void *virt_addr;
6fb120a7
JS
16166 struct lpfc_mbx_sge sge;
16167 uint32_t alloc_len, req_len;
16168 uint32_t fcfindex;
16169
16170 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16171 if (!mboxq) {
16172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16173 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16174 return -ENOMEM;
16175 }
16176
16177 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16178 sizeof(uint32_t);
16179
16180 /* Allocate DMA memory and set up the non-embedded mailbox command */
16181 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16182 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16183 req_len, LPFC_SLI4_MBX_NEMBED);
16184 if (alloc_len < req_len) {
16185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16186 "2523 Allocated DMA memory size (x%x) is "
16187 "less than the requested DMA memory "
16188 "size (x%x)\n", alloc_len, req_len);
16189 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16190 return -ENOMEM;
16191 }
16192
16193 /*
16194 * Get the first SGE entry from the non-embedded DMA memory. This
16195 * routine only uses a single SGE.
16196 */
16197 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
6fb120a7
JS
16198 virt_addr = mboxq->sge_array->addr[0];
16199 /*
16200 * Configure the FCF record for FCFI 0. This is the driver's
16201 * hardcoded default and gets used in nonFIP mode.
16202 */
16203 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16204 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16205 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16206
16207 /*
16208 * Copy the fcf_index and the FCF Record Data. The data starts after
16209 * the FCoE header plus word10. The data copy needs to be endian
16210 * correct.
16211 */
16212 bytep += sizeof(uint32_t);
16213 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16214 mboxq->vport = phba->pport;
16215 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16216 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16217 if (rc == MBX_NOT_FINISHED) {
16218 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16219 "2515 ADD_FCF_RECORD mailbox failed with "
16220 "status 0x%x\n", rc);
16221 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16222 rc = -EIO;
16223 } else
16224 rc = 0;
16225
16226 return rc;
16227}
16228
16229/**
16230 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16231 * @phba: pointer to lpfc hba data structure.
16232 * @fcf_record: pointer to the fcf record to write the default data.
16233 * @fcf_index: FCF table entry index.
16234 *
16235 * This routine is invoked to build the driver's default FCF record. The
16236 * values used are hardcoded. This routine handles memory initialization.
16237 *
16238 **/
16239void
16240lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16241 struct fcf_record *fcf_record,
16242 uint16_t fcf_index)
16243{
16244 memset(fcf_record, 0, sizeof(struct fcf_record));
16245 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16246 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16247 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16248 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16249 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16250 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16251 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16252 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16253 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16254 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16255 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16256 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16257 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 16258 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
16259 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16260 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16261 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16262 /* Set the VLAN bit map */
16263 if (phba->valid_vlan) {
16264 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16265 = 1 << (phba->vlan_id % 8);
16266 }
16267}
16268
16269/**
0c9ab6f5 16270 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
16271 * @phba: pointer to lpfc hba data structure.
16272 * @fcf_index: FCF table entry offset.
16273 *
0c9ab6f5
JS
16274 * This routine is invoked to scan the entire FCF table by reading FCF
16275 * record and processing it one at a time starting from the @fcf_index
16276 * for initial FCF discovery or fast FCF failover rediscovery.
16277 *
25985edc 16278 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 16279 * otherwise.
6fb120a7
JS
16280 **/
16281int
0c9ab6f5 16282lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
16283{
16284 int rc = 0, error;
16285 LPFC_MBOXQ_t *mboxq;
6fb120a7 16286
32b9793f 16287 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
80c17849 16288 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
6fb120a7
JS
16289 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16290 if (!mboxq) {
16291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16292 "2000 Failed to allocate mbox for "
16293 "READ_FCF cmd\n");
4d9ab994 16294 error = -ENOMEM;
0c9ab6f5 16295 goto fail_fcf_scan;
6fb120a7 16296 }
ecfd03c6 16297 /* Construct the read FCF record mailbox command */
0c9ab6f5 16298 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
16299 if (rc) {
16300 error = -EINVAL;
0c9ab6f5 16301 goto fail_fcf_scan;
6fb120a7 16302 }
ecfd03c6 16303 /* Issue the mailbox command asynchronously */
6fb120a7 16304 mboxq->vport = phba->pport;
0c9ab6f5 16305 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
16306
16307 spin_lock_irq(&phba->hbalock);
16308 phba->hba_flag |= FCF_TS_INPROG;
16309 spin_unlock_irq(&phba->hbalock);
16310
6fb120a7 16311 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 16312 if (rc == MBX_NOT_FINISHED)
6fb120a7 16313 error = -EIO;
ecfd03c6 16314 else {
38b92ef8
JS
16315 /* Reset eligible FCF count for new scan */
16316 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 16317 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 16318 error = 0;
32b9793f 16319 }
0c9ab6f5 16320fail_fcf_scan:
4d9ab994
JS
16321 if (error) {
16322 if (mboxq)
16323 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 16324 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 16325 spin_lock_irq(&phba->hbalock);
a93ff37a 16326 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
16327 spin_unlock_irq(&phba->hbalock);
16328 }
6fb120a7
JS
16329 return error;
16330}
a0c87cbd 16331
0c9ab6f5 16332/**
a93ff37a 16333 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
16334 * @phba: pointer to lpfc hba data structure.
16335 * @fcf_index: FCF table entry offset.
16336 *
16337 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 16338 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 16339 *
25985edc 16340 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
16341 * otherwise.
16342 **/
16343int
16344lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16345{
16346 int rc = 0, error;
16347 LPFC_MBOXQ_t *mboxq;
16348
16349 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16350 if (!mboxq) {
16351 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16352 "2763 Failed to allocate mbox for "
16353 "READ_FCF cmd\n");
16354 error = -ENOMEM;
16355 goto fail_fcf_read;
16356 }
16357 /* Construct the read FCF record mailbox command */
16358 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16359 if (rc) {
16360 error = -EINVAL;
16361 goto fail_fcf_read;
16362 }
16363 /* Issue the mailbox command asynchronously */
16364 mboxq->vport = phba->pport;
16365 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16366 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16367 if (rc == MBX_NOT_FINISHED)
16368 error = -EIO;
16369 else
16370 error = 0;
16371
16372fail_fcf_read:
16373 if (error && mboxq)
16374 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16375 return error;
16376}
16377
16378/**
16379 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16380 * @phba: pointer to lpfc hba data structure.
16381 * @fcf_index: FCF table entry offset.
16382 *
16383 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 16384 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 16385 *
25985edc 16386 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
16387 * otherwise.
16388 **/
16389int
16390lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16391{
16392 int rc = 0, error;
16393 LPFC_MBOXQ_t *mboxq;
16394
16395 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16396 if (!mboxq) {
16397 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16398 "2758 Failed to allocate mbox for "
16399 "READ_FCF cmd\n");
16400 error = -ENOMEM;
16401 goto fail_fcf_read;
16402 }
16403 /* Construct the read FCF record mailbox command */
16404 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16405 if (rc) {
16406 error = -EINVAL;
16407 goto fail_fcf_read;
16408 }
16409 /* Issue the mailbox command asynchronously */
16410 mboxq->vport = phba->pport;
16411 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16412 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16413 if (rc == MBX_NOT_FINISHED)
16414 error = -EIO;
16415 else
16416 error = 0;
16417
16418fail_fcf_read:
16419 if (error && mboxq)
16420 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16421 return error;
16422}
16423
7d791df7 16424/**
f5cb5304 16425 * lpfc_check_next_fcf_pri_level
7d791df7
JS
16426 * phba pointer to the lpfc_hba struct for this port.
16427 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16428 * routine when the rr_bmask is empty. The FCF indecies are put into the
16429 * rr_bmask based on their priority level. Starting from the highest priority
16430 * to the lowest. The most likely FCF candidate will be in the highest
16431 * priority group. When this routine is called it searches the fcf_pri list for
16432 * next lowest priority group and repopulates the rr_bmask with only those
16433 * fcf_indexes.
16434 * returns:
16435 * 1=success 0=failure
16436 **/
5d8b8167 16437static int
7d791df7
JS
16438lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16439{
16440 uint16_t next_fcf_pri;
16441 uint16_t last_index;
16442 struct lpfc_fcf_pri *fcf_pri;
16443 int rc;
16444 int ret = 0;
16445
16446 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16447 LPFC_SLI4_FCF_TBL_INDX_MAX);
16448 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16449 "3060 Last IDX %d\n", last_index);
2562669c
JS
16450
16451 /* Verify the priority list has 2 or more entries */
16452 spin_lock_irq(&phba->hbalock);
16453 if (list_empty(&phba->fcf.fcf_pri_list) ||
16454 list_is_singular(&phba->fcf.fcf_pri_list)) {
16455 spin_unlock_irq(&phba->hbalock);
7d791df7
JS
16456 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16457 "3061 Last IDX %d\n", last_index);
16458 return 0; /* Empty rr list */
16459 }
2562669c
JS
16460 spin_unlock_irq(&phba->hbalock);
16461
7d791df7
JS
16462 next_fcf_pri = 0;
16463 /*
16464 * Clear the rr_bmask and set all of the bits that are at this
16465 * priority.
16466 */
16467 memset(phba->fcf.fcf_rr_bmask, 0,
16468 sizeof(*phba->fcf.fcf_rr_bmask));
16469 spin_lock_irq(&phba->hbalock);
16470 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16471 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16472 continue;
16473 /*
16474 * the 1st priority that has not FLOGI failed
16475 * will be the highest.
16476 */
16477 if (!next_fcf_pri)
16478 next_fcf_pri = fcf_pri->fcf_rec.priority;
16479 spin_unlock_irq(&phba->hbalock);
16480 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16481 rc = lpfc_sli4_fcf_rr_index_set(phba,
16482 fcf_pri->fcf_rec.fcf_index);
16483 if (rc)
16484 return 0;
16485 }
16486 spin_lock_irq(&phba->hbalock);
16487 }
16488 /*
16489 * if next_fcf_pri was not set above and the list is not empty then
16490 * we have failed flogis on all of them. So reset flogi failed
4907cb7b 16491 * and start at the beginning.
7d791df7
JS
16492 */
16493 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16494 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16495 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16496 /*
16497 * the 1st priority that has not FLOGI failed
16498 * will be the highest.
16499 */
16500 if (!next_fcf_pri)
16501 next_fcf_pri = fcf_pri->fcf_rec.priority;
16502 spin_unlock_irq(&phba->hbalock);
16503 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16504 rc = lpfc_sli4_fcf_rr_index_set(phba,
16505 fcf_pri->fcf_rec.fcf_index);
16506 if (rc)
16507 return 0;
16508 }
16509 spin_lock_irq(&phba->hbalock);
16510 }
16511 } else
16512 ret = 1;
16513 spin_unlock_irq(&phba->hbalock);
16514
16515 return ret;
16516}
0c9ab6f5
JS
16517/**
16518 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16519 * @phba: pointer to lpfc hba data structure.
16520 *
16521 * This routine is to get the next eligible FCF record index in a round
16522 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 16523 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
16524 * shall be returned, otherwise, the next eligible FCF record's index
16525 * shall be returned.
16526 **/
16527uint16_t
16528lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16529{
16530 uint16_t next_fcf_index;
16531
421c6622 16532initial_priority:
3804dc84 16533 /* Search start from next bit of currently registered FCF index */
421c6622
JS
16534 next_fcf_index = phba->fcf.current_rec.fcf_indx;
16535
7d791df7 16536next_priority:
421c6622
JS
16537 /* Determine the next fcf index to check */
16538 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
16539 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16540 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
16541 next_fcf_index);
16542
0c9ab6f5 16543 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
16544 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16545 /*
16546 * If we have wrapped then we need to clear the bits that
16547 * have been tested so that we can detect when we should
16548 * change the priority level.
16549 */
0c9ab6f5
JS
16550 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16551 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
16552 }
16553
3804dc84
JS
16554
16555 /* Check roundrobin failover list empty condition */
7d791df7
JS
16556 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16557 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16558 /*
16559 * If next fcf index is not found check if there are lower
16560 * Priority level fcf's in the fcf_priority list.
16561 * Set up the rr_bmask with all of the avaiable fcf bits
16562 * at that level and continue the selection process.
16563 */
16564 if (lpfc_check_next_fcf_pri_level(phba))
421c6622 16565 goto initial_priority;
3804dc84
JS
16566 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16567 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
16568 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
16569 return LPFC_FCOE_FCF_NEXT_NONE;
16570 else {
16571 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16572 "3063 Only FCF available idx %d, flag %x\n",
16573 next_fcf_index,
16574 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
16575 return next_fcf_index;
16576 }
3804dc84
JS
16577 }
16578
7d791df7
JS
16579 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16580 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
f5cb5304
JS
16581 LPFC_FCF_FLOGI_FAILED) {
16582 if (list_is_singular(&phba->fcf.fcf_pri_list))
16583 return LPFC_FCOE_FCF_NEXT_NONE;
16584
7d791df7 16585 goto next_priority;
f5cb5304 16586 }
7d791df7 16587
3804dc84 16588 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
16589 "2845 Get next roundrobin failover FCF (x%x)\n",
16590 next_fcf_index);
16591
0c9ab6f5
JS
16592 return next_fcf_index;
16593}
16594
16595/**
16596 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16597 * @phba: pointer to lpfc hba data structure.
16598 *
16599 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 16600 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
16601 * does not go beyond the range of the driver allocated bmask dimension
16602 * before setting the bit.
16603 *
16604 * Returns 0 if the index bit successfully set, otherwise, it returns
16605 * -EINVAL.
16606 **/
16607int
16608lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16609{
16610 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16611 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
16612 "2610 FCF (x%x) reached driver's book "
16613 "keeping dimension:x%x\n",
0c9ab6f5
JS
16614 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16615 return -EINVAL;
16616 }
16617 /* Set the eligible FCF record index bmask */
16618 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16619
3804dc84 16620 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 16621 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
16622 "bmask\n", fcf_index);
16623
0c9ab6f5
JS
16624 return 0;
16625}
16626
16627/**
3804dc84 16628 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
16629 * @phba: pointer to lpfc hba data structure.
16630 *
16631 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 16632 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
16633 * does not go beyond the range of the driver allocated bmask dimension
16634 * before clearing the bit.
16635 **/
16636void
16637lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16638{
9a803a74 16639 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
0c9ab6f5
JS
16640 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16641 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
16642 "2762 FCF (x%x) reached driver's book "
16643 "keeping dimension:x%x\n",
0c9ab6f5
JS
16644 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16645 return;
16646 }
16647 /* Clear the eligible FCF record index bmask */
7d791df7 16648 spin_lock_irq(&phba->hbalock);
9a803a74
JS
16649 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16650 list) {
7d791df7
JS
16651 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16652 list_del_init(&fcf_pri->list);
16653 break;
16654 }
16655 }
16656 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 16657 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
16658
16659 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 16660 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 16661 "bmask\n", fcf_index);
0c9ab6f5
JS
16662}
16663
ecfd03c6
JS
16664/**
16665 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16666 * @phba: pointer to lpfc hba data structure.
16667 *
16668 * This routine is the completion routine for the rediscover FCF table mailbox
16669 * command. If the mailbox command returned failure, it will try to stop the
16670 * FCF rediscover wait timer.
16671 **/
5d8b8167 16672static void
ecfd03c6
JS
16673lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16674{
16675 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16676 uint32_t shdr_status, shdr_add_status;
16677
16678 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16679
16680 shdr_status = bf_get(lpfc_mbox_hdr_status,
16681 &redisc_fcf->header.cfg_shdr.response);
16682 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16683 &redisc_fcf->header.cfg_shdr.response);
16684 if (shdr_status || shdr_add_status) {
0c9ab6f5 16685 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
16686 "2746 Requesting for FCF rediscovery failed "
16687 "status x%x add_status x%x\n",
16688 shdr_status, shdr_add_status);
0c9ab6f5 16689 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 16690 spin_lock_irq(&phba->hbalock);
0c9ab6f5 16691 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
16692 spin_unlock_irq(&phba->hbalock);
16693 /*
16694 * CVL event triggered FCF rediscover request failed,
16695 * last resort to re-try current registered FCF entry.
16696 */
16697 lpfc_retry_pport_discovery(phba);
16698 } else {
16699 spin_lock_irq(&phba->hbalock);
0c9ab6f5 16700 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
16701 spin_unlock_irq(&phba->hbalock);
16702 /*
16703 * DEAD FCF event triggered FCF rediscover request
16704 * failed, last resort to fail over as a link down
16705 * to FCF registration.
16706 */
16707 lpfc_sli4_fcf_dead_failthrough(phba);
16708 }
0c9ab6f5
JS
16709 } else {
16710 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 16711 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
16712 /*
16713 * Start FCF rediscovery wait timer for pending FCF
16714 * before rescan FCF record table.
16715 */
16716 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 16717 }
ecfd03c6
JS
16718
16719 mempool_free(mbox, phba->mbox_mem_pool);
16720}
16721
16722/**
3804dc84 16723 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
16724 * @phba: pointer to lpfc hba data structure.
16725 *
16726 * This routine is invoked to request for rediscovery of the entire FCF table
16727 * by the port.
16728 **/
16729int
16730lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16731{
16732 LPFC_MBOXQ_t *mbox;
16733 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16734 int rc, length;
16735
0c9ab6f5
JS
16736 /* Cancel retry delay timers to all vports before FCF rediscover */
16737 lpfc_cancel_all_vport_retry_delay_timer(phba);
16738
ecfd03c6
JS
16739 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16740 if (!mbox) {
16741 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16742 "2745 Failed to allocate mbox for "
16743 "requesting FCF rediscover.\n");
16744 return -ENOMEM;
16745 }
16746
16747 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16748 sizeof(struct lpfc_sli4_cfg_mhdr));
16749 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16750 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16751 length, LPFC_SLI4_MBX_EMBED);
16752
16753 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16754 /* Set count to 0 for invalidating the entire FCF database */
16755 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16756
16757 /* Issue the mailbox command asynchronously */
16758 mbox->vport = phba->pport;
16759 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16760 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16761
16762 if (rc == MBX_NOT_FINISHED) {
16763 mempool_free(mbox, phba->mbox_mem_pool);
16764 return -EIO;
16765 }
16766 return 0;
16767}
16768
fc2b989b
JS
16769/**
16770 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16771 * @phba: pointer to lpfc hba data structure.
16772 *
16773 * This function is the failover routine as a last resort to the FCF DEAD
16774 * event when driver failed to perform fast FCF failover.
16775 **/
16776void
16777lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16778{
16779 uint32_t link_state;
16780
16781 /*
16782 * Last resort as FCF DEAD event failover will treat this as
16783 * a link down, but save the link state because we don't want
16784 * it to be changed to Link Down unless it is already down.
16785 */
16786 link_state = phba->link_state;
16787 lpfc_linkdown(phba);
16788 phba->link_state = link_state;
16789
16790 /* Unregister FCF if no devices connected to it */
16791 lpfc_unregister_unused_fcf(phba);
16792}
16793
a0c87cbd 16794/**
026abb87 16795 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 16796 * @phba: pointer to lpfc hba data structure.
026abb87 16797 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 16798 *
026abb87
JS
16799 * This function gets SLI3 port configure region 23 data through memory dump
16800 * mailbox command. When it successfully retrieves data, the size of the data
16801 * will be returned, otherwise, 0 will be returned.
a0c87cbd 16802 **/
026abb87
JS
16803static uint32_t
16804lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
16805{
16806 LPFC_MBOXQ_t *pmb = NULL;
16807 MAILBOX_t *mb;
026abb87 16808 uint32_t offset = 0;
a0c87cbd
JS
16809 int rc;
16810
026abb87
JS
16811 if (!rgn23_data)
16812 return 0;
16813
a0c87cbd
JS
16814 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16815 if (!pmb) {
16816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
16817 "2600 failed to allocate mailbox memory\n");
16818 return 0;
a0c87cbd
JS
16819 }
16820 mb = &pmb->u.mb;
16821
a0c87cbd
JS
16822 do {
16823 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16824 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16825
16826 if (rc != MBX_SUCCESS) {
16827 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
16828 "2601 failed to read config "
16829 "region 23, rc 0x%x Status 0x%x\n",
16830 rc, mb->mbxStatus);
a0c87cbd
JS
16831 mb->un.varDmp.word_cnt = 0;
16832 }
16833 /*
16834 * dump mem may return a zero when finished or we got a
16835 * mailbox error, either way we are done.
16836 */
16837 if (mb->un.varDmp.word_cnt == 0)
16838 break;
16839 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16840 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16841
16842 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
16843 rgn23_data + offset,
16844 mb->un.varDmp.word_cnt);
a0c87cbd
JS
16845 offset += mb->un.varDmp.word_cnt;
16846 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16847
026abb87
JS
16848 mempool_free(pmb, phba->mbox_mem_pool);
16849 return offset;
16850}
16851
16852/**
16853 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16854 * @phba: pointer to lpfc hba data structure.
16855 * @rgn23_data: pointer to configure region 23 data.
16856 *
16857 * This function gets SLI4 port configure region 23 data through memory dump
16858 * mailbox command. When it successfully retrieves data, the size of the data
16859 * will be returned, otherwise, 0 will be returned.
16860 **/
16861static uint32_t
16862lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16863{
16864 LPFC_MBOXQ_t *mboxq = NULL;
16865 struct lpfc_dmabuf *mp = NULL;
16866 struct lpfc_mqe *mqe;
16867 uint32_t data_length = 0;
16868 int rc;
16869
16870 if (!rgn23_data)
16871 return 0;
16872
16873 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16874 if (!mboxq) {
16875 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16876 "3105 failed to allocate mailbox memory\n");
16877 return 0;
16878 }
16879
16880 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16881 goto out;
16882 mqe = &mboxq->u.mqe;
16883 mp = (struct lpfc_dmabuf *) mboxq->context1;
16884 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16885 if (rc)
16886 goto out;
16887 data_length = mqe->un.mb_words[5];
16888 if (data_length == 0)
16889 goto out;
16890 if (data_length > DMP_RGN23_SIZE) {
16891 data_length = 0;
16892 goto out;
16893 }
16894 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16895out:
16896 mempool_free(mboxq, phba->mbox_mem_pool);
16897 if (mp) {
16898 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16899 kfree(mp);
16900 }
16901 return data_length;
16902}
16903
16904/**
16905 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16906 * @phba: pointer to lpfc hba data structure.
16907 *
16908 * This function read region 23 and parse TLV for port status to
16909 * decide if the user disaled the port. If the TLV indicates the
16910 * port is disabled, the hba_flag is set accordingly.
16911 **/
16912void
16913lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16914{
16915 uint8_t *rgn23_data = NULL;
16916 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16917 uint32_t offset = 0;
16918
16919 /* Get adapter Region 23 data */
16920 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16921 if (!rgn23_data)
16922 goto out;
16923
16924 if (phba->sli_rev < LPFC_SLI_REV4)
16925 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16926 else {
16927 if_type = bf_get(lpfc_sli_intf_if_type,
16928 &phba->sli4_hba.sli_intf);
16929 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16930 goto out;
16931 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16932 }
a0c87cbd
JS
16933
16934 if (!data_size)
16935 goto out;
16936
16937 /* Check the region signature first */
16938 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16940 "2619 Config region 23 has bad signature\n");
16941 goto out;
16942 }
16943 offset += 4;
16944
16945 /* Check the data structure version */
16946 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16947 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16948 "2620 Config region 23 has bad version\n");
16949 goto out;
16950 }
16951 offset += 4;
16952
16953 /* Parse TLV entries in the region */
16954 while (offset < data_size) {
16955 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16956 break;
16957 /*
16958 * If the TLV is not driver specific TLV or driver id is
16959 * not linux driver id, skip the record.
16960 */
16961 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16962 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16963 (rgn23_data[offset + 3] != 0)) {
16964 offset += rgn23_data[offset + 1] * 4 + 4;
16965 continue;
16966 }
16967
16968 /* Driver found a driver specific TLV in the config region */
16969 sub_tlv_len = rgn23_data[offset + 1] * 4;
16970 offset += 4;
16971 tlv_offset = 0;
16972
16973 /*
16974 * Search for configured port state sub-TLV.
16975 */
16976 while ((offset < data_size) &&
16977 (tlv_offset < sub_tlv_len)) {
16978 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16979 offset += 4;
16980 tlv_offset += 4;
16981 break;
16982 }
16983 if (rgn23_data[offset] != PORT_STE_TYPE) {
16984 offset += rgn23_data[offset + 1] * 4 + 4;
16985 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16986 continue;
16987 }
16988
16989 /* This HBA contains PORT_STE configured */
16990 if (!rgn23_data[offset + 2])
16991 phba->hba_flag |= LINK_DISABLED;
16992
16993 goto out;
16994 }
16995 }
026abb87 16996
a0c87cbd 16997out:
a0c87cbd
JS
16998 kfree(rgn23_data);
16999 return;
17000}
695a814e 17001
52d52440
JS
17002/**
17003 * lpfc_wr_object - write an object to the firmware
17004 * @phba: HBA structure that indicates port to create a queue on.
17005 * @dmabuf_list: list of dmabufs to write to the port.
17006 * @size: the total byte value of the objects to write to the port.
17007 * @offset: the current offset to be used to start the transfer.
17008 *
17009 * This routine will create a wr_object mailbox command to send to the port.
17010 * the mailbox command will be constructed using the dma buffers described in
17011 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
17012 * BDEs that the imbedded mailbox can support. The @offset variable will be
17013 * used to indicate the starting offset of the transfer and will also return
17014 * the offset after the write object mailbox has completed. @size is used to
17015 * determine the end of the object and whether the eof bit should be set.
17016 *
17017 * Return 0 is successful and offset will contain the the new offset to use
17018 * for the next write.
17019 * Return negative value for error cases.
17020 **/
17021int
17022lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
17023 uint32_t size, uint32_t *offset)
17024{
17025 struct lpfc_mbx_wr_object *wr_object;
17026 LPFC_MBOXQ_t *mbox;
17027 int rc = 0, i = 0;
17028 uint32_t shdr_status, shdr_add_status;
17029 uint32_t mbox_tmo;
17030 union lpfc_sli4_cfg_shdr *shdr;
17031 struct lpfc_dmabuf *dmabuf;
17032 uint32_t written = 0;
17033
17034 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17035 if (!mbox)
17036 return -ENOMEM;
17037
17038 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17039 LPFC_MBOX_OPCODE_WRITE_OBJECT,
17040 sizeof(struct lpfc_mbx_wr_object) -
17041 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17042
17043 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
17044 wr_object->u.request.write_offset = *offset;
17045 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
17046 wr_object->u.request.object_name[0] =
17047 cpu_to_le32(wr_object->u.request.object_name[0]);
17048 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
17049 list_for_each_entry(dmabuf, dmabuf_list, list) {
17050 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
17051 break;
17052 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
17053 wr_object->u.request.bde[i].addrHigh =
17054 putPaddrHigh(dmabuf->phys);
17055 if (written + SLI4_PAGE_SIZE >= size) {
17056 wr_object->u.request.bde[i].tus.f.bdeSize =
17057 (size - written);
17058 written += (size - written);
17059 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
17060 } else {
17061 wr_object->u.request.bde[i].tus.f.bdeSize =
17062 SLI4_PAGE_SIZE;
17063 written += SLI4_PAGE_SIZE;
17064 }
17065 i++;
17066 }
17067 wr_object->u.request.bde_count = i;
17068 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
17069 if (!phba->sli4_hba.intr_enable)
17070 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17071 else {
a183a15f 17072 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
17073 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17074 }
17075 /* The IOCTL status is embedded in the mailbox subheader. */
17076 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
17077 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17078 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17079 if (rc != MBX_TIMEOUT)
17080 mempool_free(mbox, phba->mbox_mem_pool);
17081 if (shdr_status || shdr_add_status || rc) {
17082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17083 "3025 Write Object mailbox failed with "
17084 "status x%x add_status x%x, mbx status x%x\n",
17085 shdr_status, shdr_add_status, rc);
17086 rc = -ENXIO;
17087 } else
17088 *offset += wr_object->u.response.actual_write_length;
17089 return rc;
17090}
17091
695a814e
JS
17092/**
17093 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
17094 * @vport: pointer to vport data structure.
17095 *
17096 * This function iterate through the mailboxq and clean up all REG_LOGIN
17097 * and REG_VPI mailbox commands associated with the vport. This function
17098 * is called when driver want to restart discovery of the vport due to
17099 * a Clear Virtual Link event.
17100 **/
17101void
17102lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17103{
17104 struct lpfc_hba *phba = vport->phba;
17105 LPFC_MBOXQ_t *mb, *nextmb;
17106 struct lpfc_dmabuf *mp;
78730cfe 17107 struct lpfc_nodelist *ndlp;
d439d286 17108 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 17109 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 17110 LIST_HEAD(mbox_cmd_list);
63e801ce 17111 uint8_t restart_loop;
695a814e 17112
d439d286 17113 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
17114 spin_lock_irq(&phba->hbalock);
17115 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17116 if (mb->vport != vport)
17117 continue;
17118
17119 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17120 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17121 continue;
17122
d439d286
JS
17123 list_del(&mb->list);
17124 list_add_tail(&mb->list, &mbox_cmd_list);
17125 }
17126 /* Clean up active mailbox command with the vport */
17127 mb = phba->sli.mbox_active;
17128 if (mb && (mb->vport == vport)) {
17129 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17130 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17131 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17132 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17133 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17134 /* Put reference count for delayed processing */
17135 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17136 /* Unregister the RPI when mailbox complete */
17137 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17138 }
17139 }
63e801ce
JS
17140 /* Cleanup any mailbox completions which are not yet processed */
17141 do {
17142 restart_loop = 0;
17143 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17144 /*
17145 * If this mailox is already processed or it is
17146 * for another vport ignore it.
17147 */
17148 if ((mb->vport != vport) ||
17149 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17150 continue;
17151
17152 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17153 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17154 continue;
17155
17156 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17157 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17158 ndlp = (struct lpfc_nodelist *)mb->context2;
17159 /* Unregister the RPI when mailbox complete */
17160 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17161 restart_loop = 1;
17162 spin_unlock_irq(&phba->hbalock);
17163 spin_lock(shost->host_lock);
17164 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17165 spin_unlock(shost->host_lock);
17166 spin_lock_irq(&phba->hbalock);
17167 break;
17168 }
17169 }
17170 } while (restart_loop);
17171
d439d286
JS
17172 spin_unlock_irq(&phba->hbalock);
17173
17174 /* Release the cleaned-up mailbox commands */
17175 while (!list_empty(&mbox_cmd_list)) {
17176 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
17177 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17178 mp = (struct lpfc_dmabuf *) (mb->context1);
17179 if (mp) {
17180 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17181 kfree(mp);
17182 }
78730cfe 17183 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 17184 mb->context2 = NULL;
78730cfe 17185 if (ndlp) {
ec21b3b0 17186 spin_lock(shost->host_lock);
589a52d6 17187 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 17188 spin_unlock(shost->host_lock);
78730cfe 17189 lpfc_nlp_put(ndlp);
78730cfe 17190 }
695a814e 17191 }
695a814e
JS
17192 mempool_free(mb, phba->mbox_mem_pool);
17193 }
d439d286
JS
17194
17195 /* Release the ndlp with the cleaned-up active mailbox command */
17196 if (act_mbx_ndlp) {
17197 spin_lock(shost->host_lock);
17198 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17199 spin_unlock(shost->host_lock);
17200 lpfc_nlp_put(act_mbx_ndlp);
695a814e 17201 }
695a814e
JS
17202}
17203
2a9bf3d0
JS
17204/**
17205 * lpfc_drain_txq - Drain the txq
17206 * @phba: Pointer to HBA context object.
17207 *
17208 * This function attempt to submit IOCBs on the txq
17209 * to the adapter. For SLI4 adapters, the txq contains
17210 * ELS IOCBs that have been deferred because the there
17211 * are no SGLs. This congestion can occur with large
17212 * vport counts during node discovery.
17213 **/
17214
17215uint32_t
17216lpfc_drain_txq(struct lpfc_hba *phba)
17217{
17218 LIST_HEAD(completions);
17219 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
2e706377 17220 struct lpfc_iocbq *piocbq = NULL;
2a9bf3d0
JS
17221 unsigned long iflags = 0;
17222 char *fail_msg = NULL;
17223 struct lpfc_sglq *sglq;
2f07784f
JS
17224 union lpfc_wqe128 wqe128;
17225 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
a2fc4aef 17226 uint32_t txq_cnt = 0;
2a9bf3d0 17227
398d81c9 17228 spin_lock_irqsave(&pring->ring_lock, iflags);
0e9bb8d7
JS
17229 list_for_each_entry(piocbq, &pring->txq, list) {
17230 txq_cnt++;
17231 }
17232
17233 if (txq_cnt > pring->txq_max)
17234 pring->txq_max = txq_cnt;
2a9bf3d0 17235
398d81c9 17236 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 17237
0e9bb8d7 17238 while (!list_empty(&pring->txq)) {
398d81c9 17239 spin_lock_irqsave(&pring->ring_lock, iflags);
2a9bf3d0 17240
19ca7609 17241 piocbq = lpfc_sli_ringtx_get(phba, pring);
a629852a 17242 if (!piocbq) {
398d81c9 17243 spin_unlock_irqrestore(&pring->ring_lock, iflags);
a629852a
JS
17244 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17245 "2823 txq empty and txq_cnt is %d\n ",
0e9bb8d7 17246 txq_cnt);
a629852a
JS
17247 break;
17248 }
19ca7609 17249 sglq = __lpfc_sli_get_sglq(phba, piocbq);
2a9bf3d0 17250 if (!sglq) {
19ca7609 17251 __lpfc_sli_ringtx_put(phba, pring, piocbq);
398d81c9 17252 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 17253 break;
2a9bf3d0 17254 }
0e9bb8d7 17255 txq_cnt--;
2a9bf3d0
JS
17256
17257 /* The xri and iocb resources secured,
17258 * attempt to issue request
17259 */
6d368e53 17260 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
17261 piocbq->sli4_xritag = sglq->sli4_xritag;
17262 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17263 fail_msg = "to convert bpl to sgl";
2f07784f 17264 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
2a9bf3d0 17265 fail_msg = "to convert iocb to wqe";
2f07784f 17266 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
2a9bf3d0
JS
17267 fail_msg = " - Wq is full";
17268 else
17269 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17270
17271 if (fail_msg) {
17272 /* Failed means we can't issue and need to cancel */
17273 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17274 "2822 IOCB failed %s iotag 0x%x "
17275 "xri 0x%x\n",
17276 fail_msg,
17277 piocbq->iotag, piocbq->sli4_xritag);
17278 list_add_tail(&piocbq->list, &completions);
17279 }
398d81c9 17280 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0
JS
17281 }
17282
2a9bf3d0
JS
17283 /* Cancel all the IOCBs that cannot be issued */
17284 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17285 IOERR_SLI_ABORTED);
17286
0e9bb8d7 17287 return txq_cnt;
2a9bf3d0 17288}