]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
[SCSI] scsilpfc 8.3.38: Fixed bsg timeout handling issues that would result in crashes
[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. *
bdcd2b92 4 * Copyright (C) 2004-2012 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>
dea3101e 27
91886523 28#include <scsi/scsi.h>
dea3101e
JB
29#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
f888ba3c 32#include <scsi/scsi_transport_fc.h>
da0436e9 33#include <scsi/fc/fc_fs.h>
0d878419 34#include <linux/aer.h>
dea3101e 35
da0436e9 36#include "lpfc_hw4.h"
dea3101e
JB
37#include "lpfc_hw.h"
38#include "lpfc_sli.h"
da0436e9 39#include "lpfc_sli4.h"
ea2151b4 40#include "lpfc_nl.h"
dea3101e
JB
41#include "lpfc_disc.h"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#include "lpfc_crtn.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_compat.h"
858c9f6c 47#include "lpfc_debugfs.h"
04c68496 48#include "lpfc_vport.h"
dea3101e
JB
49
50/* There are only four IOCB completion types. */
51typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56} lpfc_iocb_type;
57
4f774513
JS
58
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
45ed1190
JS
63 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
6669f9bb
JS
66static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
0558056c
JS
68static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
8a9d2e80
JS
70static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
71 int);
ba20c853
JS
72static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
73 uint32_t);
0558056c 74
4f774513
JS
75static IOCB_t *
76lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
77{
78 return &iocbq->iocb;
79}
80
81/**
82 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
83 * @q: The Work Queue to operate on.
84 * @wqe: The work Queue Entry to put on the Work queue.
85 *
86 * This routine will copy the contents of @wqe to the next available entry on
87 * the @q. This function will then ring the Work Queue Doorbell to signal the
88 * HBA to start processing the Work Queue Entry. This function returns 0 if
89 * successful. If no entries are available on @q then this function will return
90 * -ENOMEM.
91 * The caller is expected to hold the hbalock when calling this routine.
92 **/
93static uint32_t
94lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
95{
2e90f4b5 96 union lpfc_wqe *temp_wqe;
4f774513
JS
97 struct lpfc_register doorbell;
98 uint32_t host_index;
027140ea 99 uint32_t idx;
4f774513 100
2e90f4b5
JS
101 /* sanity check on queue memory */
102 if (unlikely(!q))
103 return -ENOMEM;
104 temp_wqe = q->qe[q->host_index].wqe;
105
4f774513 106 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
107 idx = ((q->host_index + 1) % q->entry_count);
108 if (idx == q->hba_index) {
b84daac9 109 q->WQ_overflow++;
4f774513 110 return -ENOMEM;
b84daac9
JS
111 }
112 q->WQ_posted++;
4f774513 113 /* set consumption flag every once in a while */
ff78d8f9 114 if (!((q->host_index + 1) % q->entry_repost))
f0d9bccc 115 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
fedd3b7b
JS
116 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
117 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
4f774513
JS
118 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
119
120 /* Update the host index before invoking device */
121 host_index = q->host_index;
027140ea
JS
122
123 q->host_index = idx;
4f774513
JS
124
125 /* Ring Doorbell */
126 doorbell.word0 = 0;
962bc51b
JS
127 if (q->db_format == LPFC_DB_LIST_FORMAT) {
128 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
129 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
130 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
131 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
132 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
133 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
134 } else {
135 return -EINVAL;
136 }
137 writel(doorbell.word0, q->db_regaddr);
4f774513
JS
138
139 return 0;
140}
141
142/**
143 * lpfc_sli4_wq_release - Updates internal hba index for WQ
144 * @q: The Work Queue to operate on.
145 * @index: The index to advance the hba index to.
146 *
147 * This routine will update the HBA index of a queue to reflect consumption of
148 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
149 * an entry the host calls this function to update the queue's internal
150 * pointers. This routine returns the number of entries that were consumed by
151 * the HBA.
152 **/
153static uint32_t
154lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
155{
156 uint32_t released = 0;
157
2e90f4b5
JS
158 /* sanity check on queue memory */
159 if (unlikely(!q))
160 return 0;
161
4f774513
JS
162 if (q->hba_index == index)
163 return 0;
164 do {
165 q->hba_index = ((q->hba_index + 1) % q->entry_count);
166 released++;
167 } while (q->hba_index != index);
168 return released;
169}
170
171/**
172 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
173 * @q: The Mailbox Queue to operate on.
174 * @wqe: The Mailbox Queue Entry to put on the Work queue.
175 *
176 * This routine will copy the contents of @mqe to the next available entry on
177 * the @q. This function will then ring the Work Queue Doorbell to signal the
178 * HBA to start processing the Work Queue Entry. This function returns 0 if
179 * successful. If no entries are available on @q then this function will return
180 * -ENOMEM.
181 * The caller is expected to hold the hbalock when calling this routine.
182 **/
183static uint32_t
184lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
185{
2e90f4b5 186 struct lpfc_mqe *temp_mqe;
4f774513
JS
187 struct lpfc_register doorbell;
188 uint32_t host_index;
189
2e90f4b5
JS
190 /* sanity check on queue memory */
191 if (unlikely(!q))
192 return -ENOMEM;
193 temp_mqe = q->qe[q->host_index].mqe;
194
4f774513
JS
195 /* If the host has not yet processed the next entry then we are done */
196 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
197 return -ENOMEM;
198 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
199 /* Save off the mailbox pointer for completion */
200 q->phba->mbox = (MAILBOX_t *)temp_mqe;
201
202 /* Update the host index before invoking device */
203 host_index = q->host_index;
204 q->host_index = ((q->host_index + 1) % q->entry_count);
205
206 /* Ring Doorbell */
207 doorbell.word0 = 0;
208 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
209 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
210 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
4f774513
JS
211 return 0;
212}
213
214/**
215 * lpfc_sli4_mq_release - Updates internal hba index for MQ
216 * @q: The Mailbox Queue to operate on.
217 *
218 * This routine will update the HBA index of a queue to reflect consumption of
219 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
220 * an entry the host calls this function to update the queue's internal
221 * pointers. This routine returns the number of entries that were consumed by
222 * the HBA.
223 **/
224static uint32_t
225lpfc_sli4_mq_release(struct lpfc_queue *q)
226{
2e90f4b5
JS
227 /* sanity check on queue memory */
228 if (unlikely(!q))
229 return 0;
230
4f774513
JS
231 /* Clear the mailbox pointer for completion */
232 q->phba->mbox = NULL;
233 q->hba_index = ((q->hba_index + 1) % q->entry_count);
234 return 1;
235}
236
237/**
238 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
239 * @q: The Event Queue to get the first valid EQE from
240 *
241 * This routine will get the first valid Event Queue Entry from @q, update
242 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
243 * the Queue (no more work to do), or the Queue is full of EQEs that have been
244 * processed, but not popped back to the HBA then this routine will return NULL.
245 **/
246static struct lpfc_eqe *
247lpfc_sli4_eq_get(struct lpfc_queue *q)
248{
2e90f4b5 249 struct lpfc_eqe *eqe;
027140ea 250 uint32_t idx;
2e90f4b5
JS
251
252 /* sanity check on queue memory */
253 if (unlikely(!q))
254 return NULL;
255 eqe = q->qe[q->hba_index].eqe;
4f774513
JS
256
257 /* If the next EQE is not valid then we are done */
cb5172ea 258 if (!bf_get_le32(lpfc_eqe_valid, eqe))
4f774513
JS
259 return NULL;
260 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
261 idx = ((q->hba_index + 1) % q->entry_count);
262 if (idx == q->host_index)
4f774513
JS
263 return NULL;
264
027140ea 265 q->hba_index = idx;
4f774513
JS
266 return eqe;
267}
268
ba20c853
JS
269/**
270 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
271 * @q: The Event Queue to disable interrupts
272 *
273 **/
274static inline void
275lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
276{
277 struct lpfc_register doorbell;
278
279 doorbell.word0 = 0;
280 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
281 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
282 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
283 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
284 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
285 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
286}
287
4f774513
JS
288/**
289 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
290 * @q: The Event Queue that the host has completed processing for.
291 * @arm: Indicates whether the host wants to arms this CQ.
292 *
293 * This routine will mark all Event Queue Entries on @q, from the last
294 * known completed entry to the last entry that was processed, as completed
295 * by clearing the valid bit for each completion queue entry. Then it will
296 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
297 * The internal host index in the @q will be updated by this routine to indicate
298 * that the host has finished processing the entries. The @arm parameter
299 * indicates that the queue should be rearmed when ringing the doorbell.
300 *
301 * This function will return the number of EQEs that were popped.
302 **/
303uint32_t
304lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
305{
306 uint32_t released = 0;
307 struct lpfc_eqe *temp_eqe;
308 struct lpfc_register doorbell;
309
2e90f4b5
JS
310 /* sanity check on queue memory */
311 if (unlikely(!q))
312 return 0;
313
4f774513
JS
314 /* while there are valid entries */
315 while (q->hba_index != q->host_index) {
316 temp_eqe = q->qe[q->host_index].eqe;
cb5172ea 317 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
4f774513
JS
318 released++;
319 q->host_index = ((q->host_index + 1) % q->entry_count);
320 }
321 if (unlikely(released == 0 && !arm))
322 return 0;
323
324 /* ring doorbell for number popped */
325 doorbell.word0 = 0;
326 if (arm) {
327 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
328 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
329 }
330 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
331 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
6b5151fd
JS
332 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
333 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
334 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
4f774513 335 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
a747c9ce
JS
336 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
337 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
338 readl(q->phba->sli4_hba.EQCQDBregaddr);
4f774513
JS
339 return released;
340}
341
342/**
343 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
344 * @q: The Completion Queue to get the first valid CQE from
345 *
346 * This routine will get the first valid Completion Queue Entry from @q, update
347 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
348 * the Queue (no more work to do), or the Queue is full of CQEs that have been
349 * processed, but not popped back to the HBA then this routine will return NULL.
350 **/
351static struct lpfc_cqe *
352lpfc_sli4_cq_get(struct lpfc_queue *q)
353{
354 struct lpfc_cqe *cqe;
027140ea 355 uint32_t idx;
4f774513 356
2e90f4b5
JS
357 /* sanity check on queue memory */
358 if (unlikely(!q))
359 return NULL;
360
4f774513 361 /* If the next CQE is not valid then we are done */
cb5172ea 362 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
4f774513
JS
363 return NULL;
364 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
365 idx = ((q->hba_index + 1) % q->entry_count);
366 if (idx == q->host_index)
4f774513
JS
367 return NULL;
368
369 cqe = q->qe[q->hba_index].cqe;
027140ea 370 q->hba_index = idx;
4f774513
JS
371 return cqe;
372}
373
374/**
375 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
376 * @q: The Completion Queue that the host has completed processing for.
377 * @arm: Indicates whether the host wants to arms this CQ.
378 *
379 * This routine will mark all Completion queue entries on @q, from the last
380 * known completed entry to the last entry that was processed, as completed
381 * by clearing the valid bit for each completion queue entry. Then it will
382 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
383 * The internal host index in the @q will be updated by this routine to indicate
384 * that the host has finished processing the entries. The @arm parameter
385 * indicates that the queue should be rearmed when ringing the doorbell.
386 *
387 * This function will return the number of CQEs that were released.
388 **/
389uint32_t
390lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
391{
392 uint32_t released = 0;
393 struct lpfc_cqe *temp_qe;
394 struct lpfc_register doorbell;
395
2e90f4b5
JS
396 /* sanity check on queue memory */
397 if (unlikely(!q))
398 return 0;
4f774513
JS
399 /* while there are valid entries */
400 while (q->hba_index != q->host_index) {
401 temp_qe = q->qe[q->host_index].cqe;
cb5172ea 402 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
4f774513
JS
403 released++;
404 q->host_index = ((q->host_index + 1) % q->entry_count);
405 }
406 if (unlikely(released == 0 && !arm))
407 return 0;
408
409 /* ring doorbell for number popped */
410 doorbell.word0 = 0;
411 if (arm)
412 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
413 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
414 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
6b5151fd
JS
415 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
416 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
417 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
4f774513
JS
418 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
419 return released;
420}
421
422/**
423 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
424 * @q: The Header Receive Queue to operate on.
425 * @wqe: The Receive Queue Entry to put on the Receive queue.
426 *
427 * This routine will copy the contents of @wqe to the next available entry on
428 * the @q. This function will then ring the Receive Queue Doorbell to signal the
429 * HBA to start processing the Receive Queue Entry. This function returns the
430 * index that the rqe was copied to if successful. If no entries are available
431 * on @q then this function will return -ENOMEM.
432 * The caller is expected to hold the hbalock when calling this routine.
433 **/
434static int
435lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
436 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
437{
2e90f4b5
JS
438 struct lpfc_rqe *temp_hrqe;
439 struct lpfc_rqe *temp_drqe;
4f774513
JS
440 struct lpfc_register doorbell;
441 int put_index = hq->host_index;
442
2e90f4b5
JS
443 /* sanity check on queue memory */
444 if (unlikely(!hq) || unlikely(!dq))
445 return -ENOMEM;
446 temp_hrqe = hq->qe[hq->host_index].rqe;
447 temp_drqe = dq->qe[dq->host_index].rqe;
448
4f774513
JS
449 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
450 return -EINVAL;
451 if (hq->host_index != dq->host_index)
452 return -EINVAL;
453 /* If the host has not yet processed the next entry then we are done */
454 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
455 return -EBUSY;
456 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
457 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
458
459 /* Update the host index to point to the next slot */
460 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
461 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
462
463 /* Ring The Header Receive Queue Doorbell */
73d91e50 464 if (!(hq->host_index % hq->entry_repost)) {
4f774513 465 doorbell.word0 = 0;
962bc51b
JS
466 if (hq->db_format == LPFC_DB_RING_FORMAT) {
467 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
468 hq->entry_repost);
469 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
470 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
471 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
472 hq->entry_repost);
473 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
474 hq->host_index);
475 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
476 } else {
477 return -EINVAL;
478 }
479 writel(doorbell.word0, hq->db_regaddr);
4f774513
JS
480 }
481 return put_index;
482}
483
484/**
485 * lpfc_sli4_rq_release - Updates internal hba index for RQ
486 * @q: The Header Receive Queue to operate on.
487 *
488 * This routine will update the HBA index of a queue to reflect consumption of
489 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
490 * consumed an entry the host calls this function to update the queue's
491 * internal pointers. This routine returns the number of entries that were
492 * consumed by the HBA.
493 **/
494static uint32_t
495lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
496{
2e90f4b5
JS
497 /* sanity check on queue memory */
498 if (unlikely(!hq) || unlikely(!dq))
499 return 0;
500
4f774513
JS
501 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
502 return 0;
503 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
504 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
505 return 1;
506}
507
e59058c4 508/**
3621a710 509 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
510 * @phba: Pointer to HBA context object.
511 * @pring: Pointer to driver SLI ring object.
512 *
513 * This function returns pointer to next command iocb entry
514 * in the command ring. The caller must hold hbalock to prevent
515 * other threads consume the next command iocb.
516 * SLI-2/SLI-3 provide different sized iocbs.
517 **/
ed957684
JS
518static inline IOCB_t *
519lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
520{
7e56aa25
JS
521 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
522 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
ed957684
JS
523}
524
e59058c4 525/**
3621a710 526 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
527 * @phba: Pointer to HBA context object.
528 * @pring: Pointer to driver SLI ring object.
529 *
530 * This function returns pointer to next response iocb entry
531 * in the response ring. The caller must hold hbalock to make sure
532 * that no other thread consume the next response iocb.
533 * SLI-2/SLI-3 provide different sized iocbs.
534 **/
ed957684
JS
535static inline IOCB_t *
536lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
537{
7e56aa25
JS
538 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
539 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
ed957684
JS
540}
541
e59058c4 542/**
3621a710 543 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
544 * @phba: Pointer to HBA context object.
545 *
546 * This function is called with hbalock held. This function
547 * allocates a new driver iocb object from the iocb pool. If the
548 * allocation is successful, it returns pointer to the newly
549 * allocated iocb object else it returns NULL.
550 **/
4f2e66c6 551struct lpfc_iocbq *
2e0fef85 552__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
553{
554 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
555 struct lpfc_iocbq * iocbq = NULL;
556
557 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
558 if (iocbq)
559 phba->iocb_cnt++;
560 if (phba->iocb_cnt > phba->iocb_max)
561 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
562 return iocbq;
563}
564
da0436e9
JS
565/**
566 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
567 * @phba: Pointer to HBA context object.
568 * @xritag: XRI value.
569 *
570 * This function clears the sglq pointer from the array of acive
571 * sglq's. The xritag that is passed in is used to index into the
572 * array. Before the xritag can be used it needs to be adjusted
573 * by subtracting the xribase.
574 *
575 * Returns sglq ponter = success, NULL = Failure.
576 **/
577static struct lpfc_sglq *
578__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
579{
da0436e9 580 struct lpfc_sglq *sglq;
6d368e53
JS
581
582 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
583 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
da0436e9
JS
584 return sglq;
585}
586
587/**
588 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
589 * @phba: Pointer to HBA context object.
590 * @xritag: XRI value.
591 *
592 * This function returns the sglq pointer from the array of acive
593 * sglq's. The xritag that is passed in is used to index into the
594 * array. Before the xritag can be used it needs to be adjusted
595 * by subtracting the xribase.
596 *
597 * Returns sglq ponter = success, NULL = Failure.
598 **/
0f65ff68 599struct lpfc_sglq *
da0436e9
JS
600__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
601{
da0436e9 602 struct lpfc_sglq *sglq;
6d368e53
JS
603
604 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
da0436e9
JS
605 return sglq;
606}
607
19ca7609 608/**
1151e3ec 609 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
19ca7609
JS
610 * @phba: Pointer to HBA context object.
611 * @xritag: xri used in this exchange.
612 * @rrq: The RRQ to be cleared.
613 *
19ca7609 614 **/
1151e3ec
JS
615void
616lpfc_clr_rrq_active(struct lpfc_hba *phba,
617 uint16_t xritag,
618 struct lpfc_node_rrq *rrq)
19ca7609 619{
1151e3ec 620 struct lpfc_nodelist *ndlp = NULL;
19ca7609 621
1151e3ec
JS
622 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
623 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
19ca7609
JS
624
625 /* The target DID could have been swapped (cable swap)
626 * we should use the ndlp from the findnode if it is
627 * available.
628 */
1151e3ec 629 if ((!ndlp) && rrq->ndlp)
19ca7609
JS
630 ndlp = rrq->ndlp;
631
1151e3ec
JS
632 if (!ndlp)
633 goto out;
634
6d368e53 635 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
19ca7609
JS
636 rrq->send_rrq = 0;
637 rrq->xritag = 0;
638 rrq->rrq_stop_time = 0;
639 }
1151e3ec 640out:
19ca7609
JS
641 mempool_free(rrq, phba->rrq_pool);
642}
643
644/**
645 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
646 * @phba: Pointer to HBA context object.
647 *
648 * This function is called with hbalock held. This function
649 * Checks if stop_time (ratov from setting rrq active) has
650 * been reached, if it has and the send_rrq flag is set then
651 * it will call lpfc_send_rrq. If the send_rrq flag is not set
652 * then it will just call the routine to clear the rrq and
653 * free the rrq resource.
654 * The timer is set to the next rrq that is going to expire before
655 * leaving the routine.
656 *
657 **/
658void
659lpfc_handle_rrq_active(struct lpfc_hba *phba)
660{
661 struct lpfc_node_rrq *rrq;
662 struct lpfc_node_rrq *nextrrq;
663 unsigned long next_time;
664 unsigned long iflags;
1151e3ec 665 LIST_HEAD(send_rrq);
19ca7609
JS
666
667 spin_lock_irqsave(&phba->hbalock, iflags);
668 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
669 next_time = jiffies + HZ * (phba->fc_ratov + 1);
670 list_for_each_entry_safe(rrq, nextrrq,
1151e3ec
JS
671 &phba->active_rrq_list, list) {
672 if (time_after(jiffies, rrq->rrq_stop_time))
673 list_move(&rrq->list, &send_rrq);
674 else if (time_before(rrq->rrq_stop_time, next_time))
19ca7609
JS
675 next_time = rrq->rrq_stop_time;
676 }
677 spin_unlock_irqrestore(&phba->hbalock, iflags);
678 if (!list_empty(&phba->active_rrq_list))
679 mod_timer(&phba->rrq_tmr, next_time);
1151e3ec
JS
680 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
681 list_del(&rrq->list);
682 if (!rrq->send_rrq)
683 /* this call will free the rrq */
684 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
685 else if (lpfc_send_rrq(phba, rrq)) {
686 /* if we send the rrq then the completion handler
687 * will clear the bit in the xribitmap.
688 */
689 lpfc_clr_rrq_active(phba, rrq->xritag,
690 rrq);
691 }
692 }
19ca7609
JS
693}
694
695/**
696 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
697 * @vport: Pointer to vport context object.
698 * @xri: The xri used in the exchange.
699 * @did: The targets DID for this exchange.
700 *
701 * returns NULL = rrq not found in the phba->active_rrq_list.
702 * rrq = rrq for this xri and target.
703 **/
704struct lpfc_node_rrq *
705lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
706{
707 struct lpfc_hba *phba = vport->phba;
708 struct lpfc_node_rrq *rrq;
709 struct lpfc_node_rrq *nextrrq;
710 unsigned long iflags;
711
712 if (phba->sli_rev != LPFC_SLI_REV4)
713 return NULL;
714 spin_lock_irqsave(&phba->hbalock, iflags);
715 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
716 if (rrq->vport == vport && rrq->xritag == xri &&
717 rrq->nlp_DID == did){
718 list_del(&rrq->list);
719 spin_unlock_irqrestore(&phba->hbalock, iflags);
720 return rrq;
721 }
722 }
723 spin_unlock_irqrestore(&phba->hbalock, iflags);
724 return NULL;
725}
726
727/**
728 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
729 * @vport: Pointer to vport context object.
1151e3ec
JS
730 * @ndlp: Pointer to the lpfc_node_list structure.
731 * If ndlp is NULL Remove all active RRQs for this vport from the
732 * phba->active_rrq_list and clear the rrq.
733 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
19ca7609
JS
734 **/
735void
1151e3ec 736lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
19ca7609
JS
737
738{
739 struct lpfc_hba *phba = vport->phba;
740 struct lpfc_node_rrq *rrq;
741 struct lpfc_node_rrq *nextrrq;
742 unsigned long iflags;
1151e3ec 743 LIST_HEAD(rrq_list);
19ca7609
JS
744
745 if (phba->sli_rev != LPFC_SLI_REV4)
746 return;
1151e3ec
JS
747 if (!ndlp) {
748 lpfc_sli4_vport_delete_els_xri_aborted(vport);
749 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
19ca7609 750 }
1151e3ec
JS
751 spin_lock_irqsave(&phba->hbalock, iflags);
752 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
753 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
754 list_move(&rrq->list, &rrq_list);
19ca7609 755 spin_unlock_irqrestore(&phba->hbalock, iflags);
1151e3ec
JS
756
757 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
758 list_del(&rrq->list);
759 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
760 }
19ca7609
JS
761}
762
763/**
764 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
765 * @phba: Pointer to HBA context object.
766 *
767 * Remove all rrqs from the phba->active_rrq_list and free them by
768 * calling __lpfc_clr_active_rrq
769 *
770 **/
771void
772lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
773{
774 struct lpfc_node_rrq *rrq;
775 struct lpfc_node_rrq *nextrrq;
776 unsigned long next_time;
777 unsigned long iflags;
1151e3ec 778 LIST_HEAD(rrq_list);
19ca7609
JS
779
780 if (phba->sli_rev != LPFC_SLI_REV4)
781 return;
782 spin_lock_irqsave(&phba->hbalock, iflags);
783 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
784 next_time = jiffies + HZ * (phba->fc_ratov * 2);
1151e3ec
JS
785 list_splice_init(&phba->active_rrq_list, &rrq_list);
786 spin_unlock_irqrestore(&phba->hbalock, iflags);
787
788 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
19ca7609 789 list_del(&rrq->list);
1151e3ec 790 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
19ca7609 791 }
19ca7609
JS
792 if (!list_empty(&phba->active_rrq_list))
793 mod_timer(&phba->rrq_tmr, next_time);
794}
795
796
797/**
1151e3ec 798 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
19ca7609
JS
799 * @phba: Pointer to HBA context object.
800 * @ndlp: Targets nodelist pointer for this exchange.
801 * @xritag the xri in the bitmap to test.
802 *
803 * This function is called with hbalock held. This function
804 * returns 0 = rrq not active for this xri
805 * 1 = rrq is valid for this xri.
806 **/
1151e3ec
JS
807int
808lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19ca7609
JS
809 uint16_t xritag)
810{
19ca7609
JS
811 if (!ndlp)
812 return 0;
6d368e53 813 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
19ca7609
JS
814 return 1;
815 else
816 return 0;
817}
818
819/**
820 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
821 * @phba: Pointer to HBA context object.
822 * @ndlp: nodelist pointer for this target.
823 * @xritag: xri used in this exchange.
824 * @rxid: Remote Exchange ID.
825 * @send_rrq: Flag used to determine if we should send rrq els cmd.
826 *
827 * This function takes the hbalock.
828 * The active bit is always set in the active rrq xri_bitmap even
829 * if there is no slot avaiable for the other rrq information.
830 *
831 * returns 0 rrq actived for this xri
832 * < 0 No memory or invalid ndlp.
833 **/
834int
835lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
b42c07c8 836 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
19ca7609 837{
19ca7609 838 unsigned long iflags;
b42c07c8
JS
839 struct lpfc_node_rrq *rrq;
840 int empty;
841
842 if (!ndlp)
843 return -EINVAL;
844
845 if (!phba->cfg_enable_rrq)
846 return -EINVAL;
19ca7609
JS
847
848 spin_lock_irqsave(&phba->hbalock, iflags);
b42c07c8
JS
849 if (phba->pport->load_flag & FC_UNLOADING) {
850 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
851 goto out;
852 }
853
854 /*
855 * set the active bit even if there is no mem available.
856 */
857 if (NLP_CHK_FREE_REQ(ndlp))
858 goto out;
859
860 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
861 goto out;
862
863 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
864 goto out;
865
19ca7609 866 spin_unlock_irqrestore(&phba->hbalock, iflags);
b42c07c8
JS
867 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
868 if (!rrq) {
869 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
870 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
871 " DID:0x%x Send:%d\n",
872 xritag, rxid, ndlp->nlp_DID, send_rrq);
873 return -EINVAL;
874 }
875 rrq->send_rrq = send_rrq;
876 rrq->xritag = xritag;
877 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
878 rrq->ndlp = ndlp;
879 rrq->nlp_DID = ndlp->nlp_DID;
880 rrq->vport = ndlp->vport;
881 rrq->rxid = rxid;
882 rrq->send_rrq = send_rrq;
883 spin_lock_irqsave(&phba->hbalock, iflags);
884 empty = list_empty(&phba->active_rrq_list);
885 list_add_tail(&rrq->list, &phba->active_rrq_list);
886 phba->hba_flag |= HBA_RRQ_ACTIVE;
887 if (empty)
888 lpfc_worker_wake_up(phba);
889 spin_unlock_irqrestore(&phba->hbalock, iflags);
890 return 0;
891out:
892 spin_unlock_irqrestore(&phba->hbalock, iflags);
893 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
894 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
895 " DID:0x%x Send:%d\n",
896 xritag, rxid, ndlp->nlp_DID, send_rrq);
897 return -EINVAL;
19ca7609
JS
898}
899
da0436e9
JS
900/**
901 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
902 * @phba: Pointer to HBA context object.
19ca7609 903 * @piocb: Pointer to the iocbq.
da0436e9
JS
904 *
905 * This function is called with hbalock held. This function
6d368e53 906 * gets a new driver sglq object from the sglq list. If the
da0436e9
JS
907 * list is not empty then it is successful, it returns pointer to the newly
908 * allocated sglq object else it returns NULL.
909 **/
910static struct lpfc_sglq *
19ca7609 911__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
da0436e9
JS
912{
913 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
914 struct lpfc_sglq *sglq = NULL;
19ca7609 915 struct lpfc_sglq *start_sglq = NULL;
19ca7609
JS
916 struct lpfc_scsi_buf *lpfc_cmd;
917 struct lpfc_nodelist *ndlp;
918 int found = 0;
919
920 if (piocbq->iocb_flag & LPFC_IO_FCP) {
921 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
922 ndlp = lpfc_cmd->rdata->pnode;
be858b65
JS
923 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
924 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
19ca7609 925 ndlp = piocbq->context_un.ndlp;
93d1379e
JS
926 else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
927 (piocbq->iocb_flag & LPFC_IO_LIBDFC))
928 ndlp = piocbq->context_un.ndlp;
19ca7609
JS
929 else
930 ndlp = piocbq->context1;
931
da0436e9 932 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
19ca7609
JS
933 start_sglq = sglq;
934 while (!found) {
935 if (!sglq)
936 return NULL;
ee0f4fe1 937 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
19ca7609
JS
938 /* This xri has an rrq outstanding for this DID.
939 * put it back in the list and get another xri.
940 */
941 list_add_tail(&sglq->list, lpfc_sgl_list);
942 sglq = NULL;
943 list_remove_head(lpfc_sgl_list, sglq,
944 struct lpfc_sglq, list);
945 if (sglq == start_sglq) {
946 sglq = NULL;
947 break;
948 } else
949 continue;
950 }
951 sglq->ndlp = ndlp;
952 found = 1;
6d368e53 953 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
19ca7609
JS
954 sglq->state = SGL_ALLOCATED;
955 }
da0436e9
JS
956 return sglq;
957}
958
e59058c4 959/**
3621a710 960 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
961 * @phba: Pointer to HBA context object.
962 *
963 * This function is called with no lock held. This function
964 * allocates a new driver iocb object from the iocb pool. If the
965 * allocation is successful, it returns pointer to the newly
966 * allocated iocb object else it returns NULL.
967 **/
2e0fef85
JS
968struct lpfc_iocbq *
969lpfc_sli_get_iocbq(struct lpfc_hba *phba)
970{
971 struct lpfc_iocbq * iocbq = NULL;
972 unsigned long iflags;
973
974 spin_lock_irqsave(&phba->hbalock, iflags);
975 iocbq = __lpfc_sli_get_iocbq(phba);
976 spin_unlock_irqrestore(&phba->hbalock, iflags);
977 return iocbq;
978}
979
4f774513
JS
980/**
981 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
982 * @phba: Pointer to HBA context object.
983 * @iocbq: Pointer to driver iocb object.
984 *
985 * This function is called with hbalock held to release driver
986 * iocb object to the iocb pool. The iotag in the iocb object
987 * does not change for each use of the iocb object. This function
988 * clears all other fields of the iocb object when it is freed.
989 * The sqlq structure that holds the xritag and phys and virtual
990 * mappings for the scatter gather list is retrieved from the
991 * active array of sglq. The get of the sglq pointer also clears
992 * the entry in the array. If the status of the IO indiactes that
993 * this IO was aborted then the sglq entry it put on the
994 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
995 * IO has good status or fails for any other reason then the sglq
996 * entry is added to the free list (lpfc_sgl_list).
997 **/
998static void
999__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1000{
1001 struct lpfc_sglq *sglq;
1002 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0
JS
1003 unsigned long iflag = 0;
1004 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4f774513
JS
1005
1006 if (iocbq->sli4_xritag == NO_XRI)
1007 sglq = NULL;
1008 else
6d368e53
JS
1009 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1010
0e9bb8d7
JS
1011 /*
1012 ** This should have been removed from the txcmplq before calling
1013 ** iocbq_release. The normal completion
1014 ** path should have already done the list_del_init.
1015 */
1016 if (unlikely(!list_empty(&iocbq->list))) {
1017 if (iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)
1018 iocbq->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
1019 list_del_init(&iocbq->list);
1020 }
1021
1022
4f774513 1023 if (sglq) {
0f65ff68
JS
1024 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1025 (sglq->state != SGL_XRI_ABORTED)) {
4f774513
JS
1026 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1027 iflag);
1028 list_add(&sglq->list,
1029 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1030 spin_unlock_irqrestore(
1031 &phba->sli4_hba.abts_sgl_list_lock, iflag);
0f65ff68
JS
1032 } else {
1033 sglq->state = SGL_FREED;
19ca7609 1034 sglq->ndlp = NULL;
fedd3b7b
JS
1035 list_add_tail(&sglq->list,
1036 &phba->sli4_hba.lpfc_sgl_list);
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
0e9bb8d7
JS
1070 /*
1071 ** This should have been removed from the txcmplq before calling
1072 ** iocbq_release. The normal completion
1073 ** path should have already done the list_del_init.
1074 */
1075 if (unlikely(!list_empty(&iocbq->list)))
1076 list_del_init(&iocbq->list);
1077
604a3e30
JB
1078 /*
1079 * Clean all volatile data fields, preserve iotag and node struct.
1080 */
1081 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 1082 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
1083 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1084}
1085
3772a991
JS
1086/**
1087 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1088 * @phba: Pointer to HBA context object.
1089 * @iocbq: Pointer to driver iocb object.
1090 *
1091 * This function is called with hbalock held to release driver
1092 * iocb object to the iocb pool. The iotag in the iocb object
1093 * does not change for each use of the iocb object. This function
1094 * clears all other fields of the iocb object when it is freed.
1095 **/
1096static void
1097__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1098{
1099 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 1100 phba->iocb_cnt--;
3772a991
JS
1101}
1102
e59058c4 1103/**
3621a710 1104 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
1105 * @phba: Pointer to HBA context object.
1106 * @iocbq: Pointer to driver iocb object.
1107 *
1108 * This function is called with no lock held to release the iocb to
1109 * iocb pool.
1110 **/
2e0fef85
JS
1111void
1112lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1113{
1114 unsigned long iflags;
1115
1116 /*
1117 * Clean all volatile data fields, preserve iotag and node struct.
1118 */
1119 spin_lock_irqsave(&phba->hbalock, iflags);
1120 __lpfc_sli_release_iocbq(phba, iocbq);
1121 spin_unlock_irqrestore(&phba->hbalock, iflags);
1122}
1123
a257bf90
JS
1124/**
1125 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1126 * @phba: Pointer to HBA context object.
1127 * @iocblist: List of IOCBs.
1128 * @ulpstatus: ULP status in IOCB command field.
1129 * @ulpWord4: ULP word-4 in IOCB command field.
1130 *
1131 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1132 * on the list by invoking the complete callback function associated with the
1133 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1134 * fields.
1135 **/
1136void
1137lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1138 uint32_t ulpstatus, uint32_t ulpWord4)
1139{
1140 struct lpfc_iocbq *piocb;
1141
1142 while (!list_empty(iocblist)) {
1143 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
a257bf90
JS
1144 if (!piocb->iocb_cmpl)
1145 lpfc_sli_release_iocbq(phba, piocb);
1146 else {
1147 piocb->iocb.ulpStatus = ulpstatus;
1148 piocb->iocb.un.ulpWord[4] = ulpWord4;
1149 (piocb->iocb_cmpl) (phba, piocb, piocb);
1150 }
1151 }
1152 return;
1153}
1154
e59058c4 1155/**
3621a710
JS
1156 * lpfc_sli_iocb_cmd_type - Get the iocb type
1157 * @iocb_cmnd: iocb command code.
e59058c4
JS
1158 *
1159 * This function is called by ring event handler function to get the iocb type.
1160 * This function translates the iocb command to an iocb command type used to
1161 * decide the final disposition of each completed IOCB.
1162 * The function returns
1163 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1164 * LPFC_SOL_IOCB if it is a solicited iocb completion
1165 * LPFC_ABORT_IOCB if it is an abort iocb
1166 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1167 *
1168 * The caller is not required to hold any lock.
1169 **/
dea3101e
JB
1170static lpfc_iocb_type
1171lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1172{
1173 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1174
1175 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1176 return 0;
1177
1178 switch (iocb_cmnd) {
1179 case CMD_XMIT_SEQUENCE_CR:
1180 case CMD_XMIT_SEQUENCE_CX:
1181 case CMD_XMIT_BCAST_CN:
1182 case CMD_XMIT_BCAST_CX:
1183 case CMD_ELS_REQUEST_CR:
1184 case CMD_ELS_REQUEST_CX:
1185 case CMD_CREATE_XRI_CR:
1186 case CMD_CREATE_XRI_CX:
1187 case CMD_GET_RPI_CN:
1188 case CMD_XMIT_ELS_RSP_CX:
1189 case CMD_GET_RPI_CR:
1190 case CMD_FCP_IWRITE_CR:
1191 case CMD_FCP_IWRITE_CX:
1192 case CMD_FCP_IREAD_CR:
1193 case CMD_FCP_IREAD_CX:
1194 case CMD_FCP_ICMND_CR:
1195 case CMD_FCP_ICMND_CX:
f5603511
JS
1196 case CMD_FCP_TSEND_CX:
1197 case CMD_FCP_TRSP_CX:
1198 case CMD_FCP_TRECEIVE_CX:
1199 case CMD_FCP_AUTO_TRSP_CX:
dea3101e
JB
1200 case CMD_ADAPTER_MSG:
1201 case CMD_ADAPTER_DUMP:
1202 case CMD_XMIT_SEQUENCE64_CR:
1203 case CMD_XMIT_SEQUENCE64_CX:
1204 case CMD_XMIT_BCAST64_CN:
1205 case CMD_XMIT_BCAST64_CX:
1206 case CMD_ELS_REQUEST64_CR:
1207 case CMD_ELS_REQUEST64_CX:
1208 case CMD_FCP_IWRITE64_CR:
1209 case CMD_FCP_IWRITE64_CX:
1210 case CMD_FCP_IREAD64_CR:
1211 case CMD_FCP_IREAD64_CX:
1212 case CMD_FCP_ICMND64_CR:
1213 case CMD_FCP_ICMND64_CX:
f5603511
JS
1214 case CMD_FCP_TSEND64_CX:
1215 case CMD_FCP_TRSP64_CX:
1216 case CMD_FCP_TRECEIVE64_CX:
dea3101e
JB
1217 case CMD_GEN_REQUEST64_CR:
1218 case CMD_GEN_REQUEST64_CX:
1219 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
1220 case DSSCMD_IWRITE64_CR:
1221 case DSSCMD_IWRITE64_CX:
1222 case DSSCMD_IREAD64_CR:
1223 case DSSCMD_IREAD64_CX:
dea3101e
JB
1224 type = LPFC_SOL_IOCB;
1225 break;
1226 case CMD_ABORT_XRI_CN:
1227 case CMD_ABORT_XRI_CX:
1228 case CMD_CLOSE_XRI_CN:
1229 case CMD_CLOSE_XRI_CX:
1230 case CMD_XRI_ABORTED_CX:
1231 case CMD_ABORT_MXRI64_CN:
6669f9bb 1232 case CMD_XMIT_BLS_RSP64_CX:
dea3101e
JB
1233 type = LPFC_ABORT_IOCB;
1234 break;
1235 case CMD_RCV_SEQUENCE_CX:
1236 case CMD_RCV_ELS_REQ_CX:
1237 case CMD_RCV_SEQUENCE64_CX:
1238 case CMD_RCV_ELS_REQ64_CX:
57127f15 1239 case CMD_ASYNC_STATUS:
ed957684
JS
1240 case CMD_IOCB_RCV_SEQ64_CX:
1241 case CMD_IOCB_RCV_ELS64_CX:
1242 case CMD_IOCB_RCV_CONT64_CX:
3163f725 1243 case CMD_IOCB_RET_XRI64_CX:
dea3101e
JB
1244 type = LPFC_UNSOL_IOCB;
1245 break;
3163f725
JS
1246 case CMD_IOCB_XMIT_MSEQ64_CR:
1247 case CMD_IOCB_XMIT_MSEQ64_CX:
1248 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1249 case CMD_IOCB_RCV_ELS_LIST64_CX:
1250 case CMD_IOCB_CLOSE_EXTENDED_CN:
1251 case CMD_IOCB_ABORT_EXTENDED_CN:
1252 case CMD_IOCB_RET_HBQE64_CN:
1253 case CMD_IOCB_FCP_IBIDIR64_CR:
1254 case CMD_IOCB_FCP_IBIDIR64_CX:
1255 case CMD_IOCB_FCP_ITASKMGT64_CX:
1256 case CMD_IOCB_LOGENTRY_CN:
1257 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1258 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 1259 __func__, iocb_cmnd);
3163f725
JS
1260 type = LPFC_UNKNOWN_IOCB;
1261 break;
dea3101e
JB
1262 default:
1263 type = LPFC_UNKNOWN_IOCB;
1264 break;
1265 }
1266
1267 return type;
1268}
1269
e59058c4 1270/**
3621a710 1271 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
1272 * @phba: Pointer to HBA context object.
1273 *
1274 * This function is called from SLI initialization code
1275 * to configure every ring of the HBA's SLI interface. The
1276 * caller is not required to hold any lock. This function issues
1277 * a config_ring mailbox command for each ring.
1278 * This function returns zero if successful else returns a negative
1279 * error code.
1280 **/
dea3101e 1281static int
ed957684 1282lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e
JB
1283{
1284 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
1285 LPFC_MBOXQ_t *pmb;
1286 MAILBOX_t *pmbox;
1287 int i, rc, ret = 0;
dea3101e 1288
ed957684
JS
1289 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1290 if (!pmb)
1291 return -ENOMEM;
04c68496 1292 pmbox = &pmb->u.mb;
ed957684 1293 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 1294 for (i = 0; i < psli->num_rings; i++) {
dea3101e
JB
1295 lpfc_config_ring(phba, i, pmb);
1296 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1297 if (rc != MBX_SUCCESS) {
92d7f7b0 1298 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1299 "0446 Adapter failed to init (%d), "
dea3101e
JB
1300 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1301 "ring %d\n",
e8b62011
JS
1302 rc, pmbox->mbxCommand,
1303 pmbox->mbxStatus, i);
2e0fef85 1304 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
1305 ret = -ENXIO;
1306 break;
dea3101e
JB
1307 }
1308 }
ed957684
JS
1309 mempool_free(pmb, phba->mbox_mem_pool);
1310 return ret;
dea3101e
JB
1311}
1312
e59058c4 1313/**
3621a710 1314 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
1315 * @phba: Pointer to HBA context object.
1316 * @pring: Pointer to driver SLI ring object.
1317 * @piocb: Pointer to the driver iocb object.
1318 *
1319 * This function is called with hbalock held. The function adds the
1320 * new iocb to txcmplq of the given ring. This function always returns
1321 * 0. If this function is called for ELS ring, this function checks if
1322 * there is a vport associated with the ELS command. This function also
1323 * starts els_tmofunc timer if this is an ELS command.
1324 **/
dea3101e 1325static int
2e0fef85
JS
1326lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1327 struct lpfc_iocbq *piocb)
dea3101e 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) &&
1334 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1335 if (!piocb->vport)
1336 BUG();
1337 else
1338 mod_timer(&piocb->vport->els_tmofunc,
1339 jiffies + HZ * (phba->fc_ratov << 1));
1340 }
1341
dea3101e 1342
2e0fef85 1343 return 0;
dea3101e
JB
1344}
1345
e59058c4 1346/**
3621a710 1347 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
1348 * @phba: Pointer to HBA context object.
1349 * @pring: Pointer to driver SLI ring object.
1350 *
1351 * This function is called with hbalock held to get next
1352 * iocb in txq of the given ring. If there is any iocb in
1353 * the txq, the function returns first iocb in the list after
1354 * removing the iocb from the list, else it returns NULL.
1355 **/
2a9bf3d0 1356struct lpfc_iocbq *
2e0fef85 1357lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1358{
dea3101e
JB
1359 struct lpfc_iocbq *cmd_iocb;
1360
858c9f6c 1361 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
2e0fef85 1362 return cmd_iocb;
dea3101e
JB
1363}
1364
e59058c4 1365/**
3621a710 1366 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
1367 * @phba: Pointer to HBA context object.
1368 * @pring: Pointer to driver SLI ring object.
1369 *
1370 * This function is called with hbalock held and the caller must post the
1371 * iocb without releasing the lock. If the caller releases the lock,
1372 * iocb slot returned by the function is not guaranteed to be available.
1373 * The function returns pointer to the next available iocb slot if there
1374 * is available slot in the ring, else it returns NULL.
1375 * If the get index of the ring is ahead of the put index, the function
1376 * will post an error attention event to the worker thread to take the
1377 * HBA to offline state.
1378 **/
dea3101e
JB
1379static IOCB_t *
1380lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1381{
34b02dcd 1382 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
7e56aa25
JS
1383 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1384 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1385 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1386 pring->sli.sli3.next_cmdidx = 0;
dea3101e 1387
7e56aa25
JS
1388 if (unlikely(pring->sli.sli3.local_getidx ==
1389 pring->sli.sli3.next_cmdidx)) {
dea3101e 1390
7e56aa25 1391 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e 1392
7e56aa25 1393 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea3101e 1394 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 1395 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 1396 "is bigger than cmd ring %d\n",
e8b62011 1397 pring->ringno,
7e56aa25
JS
1398 pring->sli.sli3.local_getidx,
1399 max_cmd_idx);
dea3101e 1400
2e0fef85 1401 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
1402 /*
1403 * All error attention handlers are posted to
1404 * worker thread
1405 */
1406 phba->work_ha |= HA_ERATT;
1407 phba->work_hs = HS_FFER3;
92d7f7b0 1408
5e9d9b82 1409 lpfc_worker_wake_up(phba);
dea3101e
JB
1410
1411 return NULL;
1412 }
1413
7e56aa25 1414 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea3101e
JB
1415 return NULL;
1416 }
1417
ed957684 1418 return lpfc_cmd_iocb(phba, pring);
dea3101e
JB
1419}
1420
e59058c4 1421/**
3621a710 1422 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
1423 * @phba: Pointer to HBA context object.
1424 * @iocbq: Pointer to driver iocb object.
1425 *
1426 * This function gets an iotag for the iocb. If there is no unused iotag and
1427 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1428 * array and assigns a new iotag.
1429 * The function returns the allocated iotag if successful, else returns zero.
1430 * Zero is not a valid iotag.
1431 * The caller is not required to hold any lock.
1432 **/
604a3e30 1433uint16_t
2e0fef85 1434lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 1435{
2e0fef85
JS
1436 struct lpfc_iocbq **new_arr;
1437 struct lpfc_iocbq **old_arr;
604a3e30
JB
1438 size_t new_len;
1439 struct lpfc_sli *psli = &phba->sli;
1440 uint16_t iotag;
dea3101e 1441
2e0fef85 1442 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1443 iotag = psli->last_iotag;
1444 if(++iotag < psli->iocbq_lookup_len) {
1445 psli->last_iotag = iotag;
1446 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1447 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1448 iocbq->iotag = iotag;
1449 return iotag;
2e0fef85 1450 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1451 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1452 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
1453 spin_unlock_irq(&phba->hbalock);
1454 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
1455 GFP_KERNEL);
1456 if (new_arr) {
2e0fef85 1457 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1458 old_arr = psli->iocbq_lookup;
1459 if (new_len <= psli->iocbq_lookup_len) {
1460 /* highly unprobable case */
1461 kfree(new_arr);
1462 iotag = psli->last_iotag;
1463 if(++iotag < psli->iocbq_lookup_len) {
1464 psli->last_iotag = iotag;
1465 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1466 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1467 iocbq->iotag = iotag;
1468 return iotag;
1469 }
2e0fef85 1470 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1471 return 0;
1472 }
1473 if (psli->iocbq_lookup)
1474 memcpy(new_arr, old_arr,
1475 ((psli->last_iotag + 1) *
311464ec 1476 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1477 psli->iocbq_lookup = new_arr;
1478 psli->iocbq_lookup_len = new_len;
1479 psli->last_iotag = iotag;
1480 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1481 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1482 iocbq->iotag = iotag;
1483 kfree(old_arr);
1484 return iotag;
1485 }
8f6d98d2 1486 } else
2e0fef85 1487 spin_unlock_irq(&phba->hbalock);
dea3101e 1488
bc73905a 1489 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1490 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1491 psli->last_iotag);
dea3101e 1492
604a3e30 1493 return 0;
dea3101e
JB
1494}
1495
e59058c4 1496/**
3621a710 1497 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1498 * @phba: Pointer to HBA context object.
1499 * @pring: Pointer to driver SLI ring object.
1500 * @iocb: Pointer to iocb slot in the ring.
1501 * @nextiocb: Pointer to driver iocb object which need to be
1502 * posted to firmware.
1503 *
1504 * This function is called with hbalock held to post a new iocb to
1505 * the firmware. This function copies the new iocb to ring iocb slot and
1506 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1507 * a completion call back for this iocb else the function will free the
1508 * iocb object.
1509 **/
dea3101e
JB
1510static void
1511lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1512 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1513{
1514 /*
604a3e30 1515 * Set up an iotag
dea3101e 1516 */
604a3e30 1517 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1518
e2a0a9d6 1519
a58cbd52
JS
1520 if (pring->ringno == LPFC_ELS_RING) {
1521 lpfc_debugfs_slow_ring_trc(phba,
1522 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1523 *(((uint32_t *) &nextiocb->iocb) + 4),
1524 *(((uint32_t *) &nextiocb->iocb) + 6),
1525 *(((uint32_t *) &nextiocb->iocb) + 7));
1526 }
1527
dea3101e
JB
1528 /*
1529 * Issue iocb command to adapter
1530 */
92d7f7b0 1531 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e
JB
1532 wmb();
1533 pring->stats.iocb_cmd++;
1534
1535 /*
1536 * If there is no completion routine to call, we can release the
1537 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1538 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1539 */
1540 if (nextiocb->iocb_cmpl)
1541 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1542 else
2e0fef85 1543 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e
JB
1544
1545 /*
1546 * Let the HBA know what IOCB slot will be the next one the
1547 * driver will put a command into.
1548 */
7e56aa25
JS
1549 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1550 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e
JB
1551}
1552
e59058c4 1553/**
3621a710 1554 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1555 * @phba: Pointer to HBA context object.
1556 * @pring: Pointer to driver SLI ring object.
1557 *
1558 * The caller is not required to hold any lock for calling this function.
1559 * This function updates the chip attention bits for the ring to inform firmware
1560 * that there are pending work to be done for this ring and requests an
1561 * interrupt when there is space available in the ring. This function is
1562 * called when the driver is unable to post more iocbs to the ring due
1563 * to unavailability of space in the ring.
1564 **/
dea3101e 1565static void
2e0fef85 1566lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1567{
1568 int ringno = pring->ringno;
1569
1570 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1571
1572 wmb();
1573
1574 /*
1575 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1576 * The HBA will tell us when an IOCB entry is available.
1577 */
1578 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1579 readl(phba->CAregaddr); /* flush */
1580
1581 pring->stats.iocb_cmd_full++;
1582}
1583
e59058c4 1584/**
3621a710 1585 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1586 * @phba: Pointer to HBA context object.
1587 * @pring: Pointer to driver SLI ring object.
1588 *
1589 * This function updates the chip attention register bit for the
1590 * given ring to inform HBA that there is more work to be done
1591 * in this ring. The caller is not required to hold any lock.
1592 **/
dea3101e 1593static void
2e0fef85 1594lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1595{
1596 int ringno = pring->ringno;
1597
1598 /*
1599 * Tell the HBA that there is work to do in this ring.
1600 */
34b02dcd
JS
1601 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1602 wmb();
1603 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1604 readl(phba->CAregaddr); /* flush */
1605 }
dea3101e
JB
1606}
1607
e59058c4 1608/**
3621a710 1609 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1610 * @phba: Pointer to HBA context object.
1611 * @pring: Pointer to driver SLI ring object.
1612 *
1613 * This function is called with hbalock held to post pending iocbs
1614 * in the txq to the firmware. This function is called when driver
1615 * detects space available in the ring.
1616 **/
dea3101e 1617static void
2e0fef85 1618lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1619{
1620 IOCB_t *iocb;
1621 struct lpfc_iocbq *nextiocb;
1622
1623 /*
1624 * Check to see if:
1625 * (a) there is anything on the txq to send
1626 * (b) link is up
1627 * (c) link attention events can be processed (fcp ring only)
1628 * (d) IOCB processing is not blocked by the outstanding mbox command.
1629 */
0e9bb8d7
JS
1630
1631 if (lpfc_is_link_up(phba) &&
1632 (!list_empty(&pring->txq)) &&
dea3101e 1633 (pring->ringno != phba->sli.fcp_ring ||
0b727fea 1634 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e
JB
1635
1636 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1637 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1638 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1639
1640 if (iocb)
1641 lpfc_sli_update_ring(phba, pring);
1642 else
1643 lpfc_sli_update_full_ring(phba, pring);
1644 }
1645
1646 return;
1647}
1648
e59058c4 1649/**
3621a710 1650 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1651 * @phba: Pointer to HBA context object.
1652 * @hbqno: HBQ number.
1653 *
1654 * This function is called with hbalock held to get the next
1655 * available slot for the given HBQ. If there is free slot
1656 * available for the HBQ it will return pointer to the next available
1657 * HBQ entry else it will return NULL.
1658 **/
a6ababd2 1659static struct lpfc_hbq_entry *
ed957684
JS
1660lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1661{
1662 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1663
1664 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1665 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1666 hbqp->next_hbqPutIdx = 0;
1667
1668 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1669 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1670 uint32_t getidx = le32_to_cpu(raw_index);
1671
1672 hbqp->local_hbqGetIdx = getidx;
1673
1674 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1675 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1676 LOG_SLI | LOG_VPORT,
e8b62011 1677 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1678 "%u is > than hbqp->entry_count %u\n",
e8b62011 1679 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1680 hbqp->entry_count);
1681
1682 phba->link_state = LPFC_HBA_ERROR;
1683 return NULL;
1684 }
1685
1686 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1687 return NULL;
1688 }
1689
51ef4c26
JS
1690 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1691 hbqp->hbqPutIdx;
ed957684
JS
1692}
1693
e59058c4 1694/**
3621a710 1695 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1696 * @phba: Pointer to HBA context object.
1697 *
1698 * This function is called with no lock held to free all the
1699 * hbq buffers while uninitializing the SLI interface. It also
1700 * frees the HBQ buffers returned by the firmware but not yet
1701 * processed by the upper layers.
1702 **/
ed957684
JS
1703void
1704lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1705{
92d7f7b0
JS
1706 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1707 struct hbq_dmabuf *hbq_buf;
3163f725 1708 unsigned long flags;
51ef4c26 1709 int i, hbq_count;
3163f725 1710 uint32_t hbqno;
ed957684 1711
51ef4c26 1712 hbq_count = lpfc_sli_hbq_count();
ed957684 1713 /* Return all memory used by all HBQs */
3163f725 1714 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1715 for (i = 0; i < hbq_count; ++i) {
1716 list_for_each_entry_safe(dmabuf, next_dmabuf,
1717 &phba->hbqs[i].hbq_buffer_list, list) {
1718 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1719 list_del(&hbq_buf->dbuf.list);
1720 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1721 }
a8adb832 1722 phba->hbqs[i].buffer_count = 0;
ed957684 1723 }
3163f725 1724 /* Return all HBQ buffer that are in-fly */
3772a991
JS
1725 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1726 list) {
3163f725
JS
1727 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1728 list_del(&hbq_buf->dbuf.list);
1729 if (hbq_buf->tag == -1) {
1730 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1731 (phba, hbq_buf);
1732 } else {
1733 hbqno = hbq_buf->tag >> 16;
1734 if (hbqno >= LPFC_MAX_HBQS)
1735 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1736 (phba, hbq_buf);
1737 else
1738 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1739 hbq_buf);
1740 }
1741 }
1742
1743 /* Mark the HBQs not in use */
1744 phba->hbq_in_use = 0;
1745 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1746}
1747
e59058c4 1748/**
3621a710 1749 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1750 * @phba: Pointer to HBA context object.
1751 * @hbqno: HBQ number.
1752 * @hbq_buf: Pointer to HBQ buffer.
1753 *
1754 * This function is called with the hbalock held to post a
1755 * hbq buffer to the firmware. If the function finds an empty
1756 * slot in the HBQ, it will post the buffer. The function will return
1757 * pointer to the hbq entry if it successfully post the buffer
1758 * else it will return NULL.
1759 **/
3772a991 1760static int
ed957684 1761lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1762 struct hbq_dmabuf *hbq_buf)
3772a991
JS
1763{
1764 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1765}
1766
1767/**
1768 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1769 * @phba: Pointer to HBA context object.
1770 * @hbqno: HBQ number.
1771 * @hbq_buf: Pointer to HBQ buffer.
1772 *
1773 * This function is called with the hbalock held to post a hbq buffer to the
1774 * firmware. If the function finds an empty slot in the HBQ, it will post the
1775 * buffer and place it on the hbq_buffer_list. The function will return zero if
1776 * it successfully post the buffer else it will return an error.
1777 **/
1778static int
1779lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1780 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1781{
1782 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1783 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684
JS
1784
1785 /* Get next HBQ entry slot to use */
1786 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1787 if (hbqe) {
1788 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1789
92d7f7b0
JS
1790 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1791 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
51ef4c26 1792 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
ed957684 1793 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1794 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1795 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1796 /* Sync SLIM */
ed957684
JS
1797 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1798 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1799 /* flush */
ed957684 1800 readl(phba->hbq_put + hbqno);
51ef4c26 1801 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1802 return 0;
1803 } else
1804 return -ENOMEM;
ed957684
JS
1805}
1806
4f774513
JS
1807/**
1808 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1809 * @phba: Pointer to HBA context object.
1810 * @hbqno: HBQ number.
1811 * @hbq_buf: Pointer to HBQ buffer.
1812 *
1813 * This function is called with the hbalock held to post an RQE to the SLI4
1814 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1815 * the hbq_buffer_list and return zero, otherwise it will return an error.
1816 **/
1817static int
1818lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1819 struct hbq_dmabuf *hbq_buf)
1820{
1821 int rc;
1822 struct lpfc_rqe hrqe;
1823 struct lpfc_rqe drqe;
1824
1825 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1826 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1827 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1828 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1829 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1830 &hrqe, &drqe);
1831 if (rc < 0)
1832 return rc;
1833 hbq_buf->tag = rc;
1834 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1835 return 0;
1836}
1837
e59058c4 1838/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1839static struct lpfc_hbq_init lpfc_els_hbq = {
1840 .rn = 1,
def9c7a9 1841 .entry_count = 256,
92d7f7b0
JS
1842 .mask_count = 0,
1843 .profile = 0,
51ef4c26 1844 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1845 .buffer_count = 0,
a257bf90
JS
1846 .init_count = 40,
1847 .add_count = 40,
92d7f7b0 1848};
ed957684 1849
e59058c4 1850/* HBQ for the extra ring if needed */
51ef4c26
JS
1851static struct lpfc_hbq_init lpfc_extra_hbq = {
1852 .rn = 1,
1853 .entry_count = 200,
1854 .mask_count = 0,
1855 .profile = 0,
1856 .ring_mask = (1 << LPFC_EXTRA_RING),
1857 .buffer_count = 0,
1858 .init_count = 0,
1859 .add_count = 5,
1860};
1861
e59058c4 1862/* Array of HBQs */
78b2d852 1863struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0 1864 &lpfc_els_hbq,
51ef4c26 1865 &lpfc_extra_hbq,
92d7f7b0 1866};
ed957684 1867
e59058c4 1868/**
3621a710 1869 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1870 * @phba: Pointer to HBA context object.
1871 * @hbqno: HBQ number.
1872 * @count: Number of HBQ buffers to be posted.
1873 *
d7c255b2
JS
1874 * This function is called with no lock held to post more hbq buffers to the
1875 * given HBQ. The function returns the number of HBQ buffers successfully
1876 * posted.
e59058c4 1877 **/
311464ec 1878static int
92d7f7b0 1879lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1880{
d7c255b2 1881 uint32_t i, posted = 0;
3163f725 1882 unsigned long flags;
92d7f7b0 1883 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1884 LIST_HEAD(hbq_buf_list);
eafe1df9 1885 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1886 return 0;
51ef4c26 1887
d7c255b2
JS
1888 if ((phba->hbqs[hbqno].buffer_count + count) >
1889 lpfc_hbq_defs[hbqno]->entry_count)
1890 count = lpfc_hbq_defs[hbqno]->entry_count -
1891 phba->hbqs[hbqno].buffer_count;
1892 if (!count)
1893 return 0;
1894 /* Allocate HBQ entries */
1895 for (i = 0; i < count; i++) {
1896 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1897 if (!hbq_buffer)
1898 break;
1899 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1900 }
3163f725
JS
1901 /* Check whether HBQ is still in use */
1902 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1903 if (!phba->hbq_in_use)
d7c255b2
JS
1904 goto err;
1905 while (!list_empty(&hbq_buf_list)) {
1906 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1907 dbuf.list);
1908 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1909 (hbqno << 16));
3772a991 1910 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1911 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1912 posted++;
1913 } else
51ef4c26 1914 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1915 }
3163f725 1916 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1917 return posted;
1918err:
eafe1df9 1919 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1920 while (!list_empty(&hbq_buf_list)) {
1921 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1922 dbuf.list);
1923 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1924 }
1925 return 0;
ed957684
JS
1926}
1927
e59058c4 1928/**
3621a710 1929 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1930 * @phba: Pointer to HBA context object.
1931 * @qno: HBQ number.
1932 *
1933 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1934 * is called with no lock held. The function returns the number of HBQ entries
1935 * successfully allocated.
e59058c4 1936 **/
92d7f7b0
JS
1937int
1938lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1939{
def9c7a9
JS
1940 if (phba->sli_rev == LPFC_SLI_REV4)
1941 return 0;
1942 else
1943 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1944 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 1945}
ed957684 1946
e59058c4 1947/**
3621a710 1948 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
1949 * @phba: Pointer to HBA context object.
1950 * @qno: HBQ queue number.
1951 *
1952 * This function is called from SLI initialization code path with
1953 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 1954 * function returns the number of HBQ entries successfully allocated.
e59058c4 1955 **/
a6ababd2 1956static int
92d7f7b0
JS
1957lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1958{
def9c7a9
JS
1959 if (phba->sli_rev == LPFC_SLI_REV4)
1960 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
73d91e50 1961 lpfc_hbq_defs[qno]->entry_count);
def9c7a9
JS
1962 else
1963 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1964 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
1965}
1966
3772a991
JS
1967/**
1968 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1969 * @phba: Pointer to HBA context object.
1970 * @hbqno: HBQ number.
1971 *
1972 * This function removes the first hbq buffer on an hbq list and returns a
1973 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1974 **/
1975static struct hbq_dmabuf *
1976lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1977{
1978 struct lpfc_dmabuf *d_buf;
1979
1980 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1981 if (!d_buf)
1982 return NULL;
1983 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1984}
1985
e59058c4 1986/**
3621a710 1987 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
1988 * @phba: Pointer to HBA context object.
1989 * @tag: Tag of the hbq buffer.
1990 *
1991 * This function is called with hbalock held. This function searches
1992 * for the hbq buffer associated with the given tag in the hbq buffer
1993 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1994 * it returns NULL.
1995 **/
a6ababd2 1996static struct hbq_dmabuf *
92d7f7b0 1997lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 1998{
92d7f7b0
JS
1999 struct lpfc_dmabuf *d_buf;
2000 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
2001 uint32_t hbqno;
2002
2003 hbqno = tag >> 16;
a0a74e45 2004 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 2005 return NULL;
ed957684 2006
3772a991 2007 spin_lock_irq(&phba->hbalock);
51ef4c26 2008 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 2009 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 2010 if (hbq_buf->tag == tag) {
3772a991 2011 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2012 return hbq_buf;
ed957684
JS
2013 }
2014 }
3772a991 2015 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2016 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 2017 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 2018 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 2019 return NULL;
ed957684
JS
2020}
2021
e59058c4 2022/**
3621a710 2023 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
2024 * @phba: Pointer to HBA context object.
2025 * @hbq_buffer: Pointer to HBQ buffer.
2026 *
2027 * This function is called with hbalock. This function gives back
2028 * the hbq buffer to firmware. If the HBQ does not have space to
2029 * post the buffer, it will free the buffer.
2030 **/
ed957684 2031void
51ef4c26 2032lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
2033{
2034 uint32_t hbqno;
2035
51ef4c26
JS
2036 if (hbq_buffer) {
2037 hbqno = hbq_buffer->tag >> 16;
3772a991 2038 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 2039 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
2040 }
2041}
2042
e59058c4 2043/**
3621a710 2044 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
2045 * @mbxCommand: mailbox command code.
2046 *
2047 * This function is called by the mailbox event handler function to verify
2048 * that the completed mailbox command is a legitimate mailbox command. If the
2049 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2050 * and the mailbox event handler will take the HBA offline.
2051 **/
dea3101e
JB
2052static int
2053lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2054{
2055 uint8_t ret;
2056
2057 switch (mbxCommand) {
2058 case MBX_LOAD_SM:
2059 case MBX_READ_NV:
2060 case MBX_WRITE_NV:
a8adb832 2061 case MBX_WRITE_VPARMS:
dea3101e
JB
2062 case MBX_RUN_BIU_DIAG:
2063 case MBX_INIT_LINK:
2064 case MBX_DOWN_LINK:
2065 case MBX_CONFIG_LINK:
2066 case MBX_CONFIG_RING:
2067 case MBX_RESET_RING:
2068 case MBX_READ_CONFIG:
2069 case MBX_READ_RCONFIG:
2070 case MBX_READ_SPARM:
2071 case MBX_READ_STATUS:
2072 case MBX_READ_RPI:
2073 case MBX_READ_XRI:
2074 case MBX_READ_REV:
2075 case MBX_READ_LNK_STAT:
2076 case MBX_REG_LOGIN:
2077 case MBX_UNREG_LOGIN:
dea3101e
JB
2078 case MBX_CLEAR_LA:
2079 case MBX_DUMP_MEMORY:
2080 case MBX_DUMP_CONTEXT:
2081 case MBX_RUN_DIAGS:
2082 case MBX_RESTART:
2083 case MBX_UPDATE_CFG:
2084 case MBX_DOWN_LOAD:
2085 case MBX_DEL_LD_ENTRY:
2086 case MBX_RUN_PROGRAM:
2087 case MBX_SET_MASK:
09372820 2088 case MBX_SET_VARIABLE:
dea3101e 2089 case MBX_UNREG_D_ID:
41415862 2090 case MBX_KILL_BOARD:
dea3101e 2091 case MBX_CONFIG_FARP:
41415862 2092 case MBX_BEACON:
dea3101e
JB
2093 case MBX_LOAD_AREA:
2094 case MBX_RUN_BIU_DIAG64:
2095 case MBX_CONFIG_PORT:
2096 case MBX_READ_SPARM64:
2097 case MBX_READ_RPI64:
2098 case MBX_REG_LOGIN64:
76a95d75 2099 case MBX_READ_TOPOLOGY:
09372820 2100 case MBX_WRITE_WWN:
dea3101e
JB
2101 case MBX_SET_DEBUG:
2102 case MBX_LOAD_EXP_ROM:
57127f15 2103 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
2104 case MBX_REG_VPI:
2105 case MBX_UNREG_VPI:
858c9f6c 2106 case MBX_HEARTBEAT:
84774a4d
JS
2107 case MBX_PORT_CAPABILITIES:
2108 case MBX_PORT_IOV_CONTROL:
04c68496
JS
2109 case MBX_SLI4_CONFIG:
2110 case MBX_SLI4_REQ_FTRS:
2111 case MBX_REG_FCFI:
2112 case MBX_UNREG_FCFI:
2113 case MBX_REG_VFI:
2114 case MBX_UNREG_VFI:
2115 case MBX_INIT_VPI:
2116 case MBX_INIT_VFI:
2117 case MBX_RESUME_RPI:
c7495937
JS
2118 case MBX_READ_EVENT_LOG_STATUS:
2119 case MBX_READ_EVENT_LOG:
dcf2a4e0
JS
2120 case MBX_SECURITY_MGMT:
2121 case MBX_AUTH_PORT:
940eb687 2122 case MBX_ACCESS_VDATA:
dea3101e
JB
2123 ret = mbxCommand;
2124 break;
2125 default:
2126 ret = MBX_SHUTDOWN;
2127 break;
2128 }
2e0fef85 2129 return ret;
dea3101e 2130}
e59058c4
JS
2131
2132/**
3621a710 2133 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
2134 * @phba: Pointer to HBA context object.
2135 * @pmboxq: Pointer to mailbox command.
2136 *
2137 * This is completion handler function for mailbox commands issued from
2138 * lpfc_sli_issue_mbox_wait function. This function is called by the
2139 * mailbox event handler function with no lock held. This function
2140 * will wake up thread waiting on the wait queue pointed by context1
2141 * of the mailbox.
2142 **/
04c68496 2143void
2e0fef85 2144lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e
JB
2145{
2146 wait_queue_head_t *pdone_q;
858c9f6c 2147 unsigned long drvr_flag;
dea3101e
JB
2148
2149 /*
2150 * If pdone_q is empty, the driver thread gave up waiting and
2151 * continued running.
2152 */
7054a606 2153 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2154 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e
JB
2155 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2156 if (pdone_q)
2157 wake_up_interruptible(pdone_q);
858c9f6c 2158 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
2159 return;
2160}
2161
e59058c4
JS
2162
2163/**
3621a710 2164 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
2165 * @phba: Pointer to HBA context object.
2166 * @pmb: Pointer to mailbox object.
2167 *
2168 * This function is the default mailbox completion handler. It
2169 * frees the memory resources associated with the completed mailbox
2170 * command. If the completed command is a REG_LOGIN mailbox command,
2171 * this function will issue a UREG_LOGIN to re-claim the RPI.
2172 **/
dea3101e 2173void
2e0fef85 2174lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2175{
d439d286 2176 struct lpfc_vport *vport = pmb->vport;
dea3101e 2177 struct lpfc_dmabuf *mp;
d439d286 2178 struct lpfc_nodelist *ndlp;
5af5eee7 2179 struct Scsi_Host *shost;
04c68496 2180 uint16_t rpi, vpi;
7054a606
JS
2181 int rc;
2182
dea3101e 2183 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 2184
dea3101e
JB
2185 if (mp) {
2186 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2187 kfree(mp);
2188 }
7054a606
JS
2189
2190 /*
2191 * If a REG_LOGIN succeeded after node is destroyed or node
2192 * is in re-discovery driver need to cleanup the RPI.
2193 */
2e0fef85 2194 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
2195 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2196 !pmb->u.mb.mbxStatus) {
2197 rpi = pmb->u.mb.un.varWords[0];
6d368e53 2198 vpi = pmb->u.mb.un.varRegLogin.vpi;
04c68496 2199 lpfc_unreg_login(phba, vpi, rpi, pmb);
92d7f7b0 2200 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
2201 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2202 if (rc != MBX_NOT_FINISHED)
2203 return;
2204 }
2205
695a814e
JS
2206 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2207 !(phba->pport->load_flag & FC_UNLOADING) &&
2208 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
2209 shost = lpfc_shost_from_vport(vport);
2210 spin_lock_irq(shost->host_lock);
2211 vport->vpi_state |= LPFC_VPI_REGISTERED;
2212 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2213 spin_unlock_irq(shost->host_lock);
695a814e
JS
2214 }
2215
d439d286
JS
2216 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2217 ndlp = (struct lpfc_nodelist *)pmb->context2;
2218 lpfc_nlp_put(ndlp);
2219 pmb->context2 = NULL;
2220 }
2221
dcf2a4e0
JS
2222 /* Check security permission status on INIT_LINK mailbox command */
2223 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2224 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2225 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2226 "2860 SLI authentication is required "
2227 "for INIT_LINK but has not done yet\n");
2228
04c68496
JS
2229 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2230 lpfc_sli4_mbox_cmd_free(phba, pmb);
2231 else
2232 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e
JB
2233}
2234
e59058c4 2235/**
3621a710 2236 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
2237 * @phba: Pointer to HBA context object.
2238 *
2239 * This function is called with no lock held. This function processes all
2240 * the completed mailbox commands and gives it to upper layers. The interrupt
2241 * service routine processes mailbox completion interrupt and adds completed
2242 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2243 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2244 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2245 * function returns the mailbox commands to the upper layer by calling the
2246 * completion handler function of each mailbox.
2247 **/
dea3101e 2248int
2e0fef85 2249lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 2250{
92d7f7b0 2251 MAILBOX_t *pmbox;
dea3101e 2252 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
2253 int rc;
2254 LIST_HEAD(cmplq);
dea3101e
JB
2255
2256 phba->sli.slistat.mbox_event++;
2257
92d7f7b0
JS
2258 /* Get all completed mailboxe buffers into the cmplq */
2259 spin_lock_irq(&phba->hbalock);
2260 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2261 spin_unlock_irq(&phba->hbalock);
dea3101e 2262
92d7f7b0
JS
2263 /* Get a Mailbox buffer to setup mailbox commands for callback */
2264 do {
2265 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2266 if (pmb == NULL)
2267 break;
2e0fef85 2268
04c68496 2269 pmbox = &pmb->u.mb;
dea3101e 2270
858c9f6c
JS
2271 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2272 if (pmb->vport) {
2273 lpfc_debugfs_disc_trc(pmb->vport,
2274 LPFC_DISC_TRC_MBOX_VPORT,
2275 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2276 (uint32_t)pmbox->mbxCommand,
2277 pmbox->un.varWords[0],
2278 pmbox->un.varWords[1]);
2279 }
2280 else {
2281 lpfc_debugfs_disc_trc(phba->pport,
2282 LPFC_DISC_TRC_MBOX,
2283 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2284 (uint32_t)pmbox->mbxCommand,
2285 pmbox->un.varWords[0],
2286 pmbox->un.varWords[1]);
2287 }
2288 }
2289
dea3101e
JB
2290 /*
2291 * It is a fatal error if unknown mbox command completion.
2292 */
2293 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2294 MBX_SHUTDOWN) {
af901ca1 2295 /* Unknown mailbox command compl */
92d7f7b0 2296 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 2297 "(%d):0323 Unknown Mailbox command "
a183a15f 2298 "x%x (x%x/x%x) Cmpl\n",
92d7f7b0 2299 pmb->vport ? pmb->vport->vpi : 0,
04c68496 2300 pmbox->mbxCommand,
a183a15f
JS
2301 lpfc_sli_config_mbox_subsys_get(phba,
2302 pmb),
2303 lpfc_sli_config_mbox_opcode_get(phba,
2304 pmb));
2e0fef85 2305 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
2306 phba->work_hs = HS_FFER3;
2307 lpfc_handle_eratt(phba);
92d7f7b0 2308 continue;
dea3101e
JB
2309 }
2310
dea3101e
JB
2311 if (pmbox->mbxStatus) {
2312 phba->sli.slistat.mbox_stat_err++;
2313 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2314 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0 2315 lpfc_printf_log(phba, KERN_INFO,
a183a15f
JS
2316 LOG_MBOX | LOG_SLI,
2317 "(%d):0305 Mbox cmd cmpl "
2318 "error - RETRYing Data: x%x "
2319 "(x%x/x%x) x%x x%x x%x\n",
2320 pmb->vport ? pmb->vport->vpi : 0,
2321 pmbox->mbxCommand,
2322 lpfc_sli_config_mbox_subsys_get(phba,
2323 pmb),
2324 lpfc_sli_config_mbox_opcode_get(phba,
2325 pmb),
2326 pmbox->mbxStatus,
2327 pmbox->un.varWords[0],
2328 pmb->vport->port_state);
dea3101e
JB
2329 pmbox->mbxStatus = 0;
2330 pmbox->mbxOwner = OWN_HOST;
dea3101e 2331 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 2332 if (rc != MBX_NOT_FINISHED)
92d7f7b0 2333 continue;
dea3101e
JB
2334 }
2335 }
2336
2337 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 2338 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 2339 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea3101e 2340 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
92d7f7b0 2341 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 2342 pmbox->mbxCommand,
a183a15f
JS
2343 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2344 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea3101e
JB
2345 pmb->mbox_cmpl,
2346 *((uint32_t *) pmbox),
2347 pmbox->un.varWords[0],
2348 pmbox->un.varWords[1],
2349 pmbox->un.varWords[2],
2350 pmbox->un.varWords[3],
2351 pmbox->un.varWords[4],
2352 pmbox->un.varWords[5],
2353 pmbox->un.varWords[6],
2354 pmbox->un.varWords[7]);
2355
92d7f7b0 2356 if (pmb->mbox_cmpl)
dea3101e 2357 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
2358 } while (1);
2359 return 0;
2360}
dea3101e 2361
e59058c4 2362/**
3621a710 2363 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
2364 * @phba: Pointer to HBA context object.
2365 * @pring: Pointer to driver SLI ring object.
2366 * @tag: buffer tag.
2367 *
2368 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2369 * is set in the tag the buffer is posted for a particular exchange,
2370 * the function will return the buffer without replacing the buffer.
2371 * If the buffer is for unsolicited ELS or CT traffic, this function
2372 * returns the buffer and also posts another buffer to the firmware.
2373 **/
76bb24ef
JS
2374static struct lpfc_dmabuf *
2375lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
2376 struct lpfc_sli_ring *pring,
2377 uint32_t tag)
76bb24ef 2378{
9f1e1b50
JS
2379 struct hbq_dmabuf *hbq_entry;
2380
76bb24ef
JS
2381 if (tag & QUE_BUFTAG_BIT)
2382 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
2383 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2384 if (!hbq_entry)
2385 return NULL;
2386 return &hbq_entry->dbuf;
76bb24ef 2387}
57127f15 2388
3772a991
JS
2389/**
2390 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2391 * @phba: Pointer to HBA context object.
2392 * @pring: Pointer to driver SLI ring object.
2393 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2394 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2395 * @fch_type: the type for the first frame of the sequence.
2396 *
2397 * This function is called with no lock held. This function uses the r_ctl and
2398 * type of the received sequence to find the correct callback function to call
2399 * to process the sequence.
2400 **/
2401static int
2402lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2403 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2404 uint32_t fch_type)
2405{
2406 int i;
2407
2408 /* unSolicited Responses */
2409 if (pring->prt[0].profile) {
2410 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2411 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2412 saveq);
2413 return 1;
2414 }
2415 /* We must search, based on rctl / type
2416 for the right routine */
2417 for (i = 0; i < pring->num_mask; i++) {
2418 if ((pring->prt[i].rctl == fch_r_ctl) &&
2419 (pring->prt[i].type == fch_type)) {
2420 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2421 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2422 (phba, pring, saveq);
2423 return 1;
2424 }
2425 }
2426 return 0;
2427}
e59058c4
JS
2428
2429/**
3621a710 2430 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
2431 * @phba: Pointer to HBA context object.
2432 * @pring: Pointer to driver SLI ring object.
2433 * @saveq: Pointer to the unsolicited iocb.
2434 *
2435 * This function is called with no lock held by the ring event handler
2436 * when there is an unsolicited iocb posted to the response ring by the
2437 * firmware. This function gets the buffer associated with the iocbs
2438 * and calls the event handler for the ring. This function handles both
2439 * qring buffers and hbq buffers.
2440 * When the function returns 1 the caller can free the iocb object otherwise
2441 * upper layer functions will free the iocb objects.
2442 **/
dea3101e
JB
2443static int
2444lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2445 struct lpfc_iocbq *saveq)
2446{
2447 IOCB_t * irsp;
2448 WORD5 * w5p;
2449 uint32_t Rctl, Type;
3772a991 2450 uint32_t match;
76bb24ef 2451 struct lpfc_iocbq *iocbq;
3163f725 2452 struct lpfc_dmabuf *dmzbuf;
dea3101e
JB
2453
2454 match = 0;
2455 irsp = &(saveq->iocb);
57127f15
JS
2456
2457 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2458 if (pring->lpfc_sli_rcv_async_status)
2459 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2460 else
2461 lpfc_printf_log(phba,
2462 KERN_WARNING,
2463 LOG_SLI,
2464 "0316 Ring %d handler: unexpected "
2465 "ASYNC_STATUS iocb received evt_code "
2466 "0x%x\n",
2467 pring->ringno,
2468 irsp->un.asyncstat.evt_code);
2469 return 1;
2470 }
2471
3163f725
JS
2472 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2473 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2474 if (irsp->ulpBdeCount > 0) {
2475 dmzbuf = lpfc_sli_get_buff(phba, pring,
2476 irsp->un.ulpWord[3]);
2477 lpfc_in_buf_free(phba, dmzbuf);
2478 }
2479
2480 if (irsp->ulpBdeCount > 1) {
2481 dmzbuf = lpfc_sli_get_buff(phba, pring,
2482 irsp->unsli3.sli3Words[3]);
2483 lpfc_in_buf_free(phba, dmzbuf);
2484 }
2485
2486 if (irsp->ulpBdeCount > 2) {
2487 dmzbuf = lpfc_sli_get_buff(phba, pring,
2488 irsp->unsli3.sli3Words[7]);
2489 lpfc_in_buf_free(phba, dmzbuf);
2490 }
2491
2492 return 1;
2493 }
2494
92d7f7b0 2495 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2496 if (irsp->ulpBdeCount != 0) {
2497 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2498 irsp->un.ulpWord[3]);
2499 if (!saveq->context2)
2500 lpfc_printf_log(phba,
2501 KERN_ERR,
2502 LOG_SLI,
2503 "0341 Ring %d Cannot find buffer for "
2504 "an unsolicited iocb. tag 0x%x\n",
2505 pring->ringno,
2506 irsp->un.ulpWord[3]);
76bb24ef
JS
2507 }
2508 if (irsp->ulpBdeCount == 2) {
2509 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2510 irsp->unsli3.sli3Words[7]);
2511 if (!saveq->context3)
2512 lpfc_printf_log(phba,
2513 KERN_ERR,
2514 LOG_SLI,
2515 "0342 Ring %d Cannot find buffer for an"
2516 " unsolicited iocb. tag 0x%x\n",
2517 pring->ringno,
2518 irsp->unsli3.sli3Words[7]);
2519 }
2520 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2521 irsp = &(iocbq->iocb);
76bb24ef
JS
2522 if (irsp->ulpBdeCount != 0) {
2523 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2524 irsp->un.ulpWord[3]);
9c2face6 2525 if (!iocbq->context2)
76bb24ef
JS
2526 lpfc_printf_log(phba,
2527 KERN_ERR,
2528 LOG_SLI,
2529 "0343 Ring %d Cannot find "
2530 "buffer for an unsolicited iocb"
2531 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2532 irsp->un.ulpWord[3]);
76bb24ef
JS
2533 }
2534 if (irsp->ulpBdeCount == 2) {
2535 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2536 irsp->unsli3.sli3Words[7]);
9c2face6 2537 if (!iocbq->context3)
76bb24ef
JS
2538 lpfc_printf_log(phba,
2539 KERN_ERR,
2540 LOG_SLI,
2541 "0344 Ring %d Cannot find "
2542 "buffer for an unsolicited "
2543 "iocb. tag 0x%x\n",
2544 pring->ringno,
2545 irsp->unsli3.sli3Words[7]);
2546 }
2547 }
92d7f7b0 2548 }
9c2face6
JS
2549 if (irsp->ulpBdeCount != 0 &&
2550 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2551 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2552 int found = 0;
2553
2554 /* search continue save q for same XRI */
2555 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
7851fe2c
JS
2556 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2557 saveq->iocb.unsli3.rcvsli3.ox_id) {
9c2face6
JS
2558 list_add_tail(&saveq->list, &iocbq->list);
2559 found = 1;
2560 break;
2561 }
2562 }
2563 if (!found)
2564 list_add_tail(&saveq->clist,
2565 &pring->iocb_continue_saveq);
2566 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2567 list_del_init(&iocbq->clist);
2568 saveq = iocbq;
2569 irsp = &(saveq->iocb);
2570 } else
2571 return 0;
2572 }
2573 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2574 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2575 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2576 Rctl = FC_RCTL_ELS_REQ;
2577 Type = FC_TYPE_ELS;
9c2face6
JS
2578 } else {
2579 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2580 Rctl = w5p->hcsw.Rctl;
2581 Type = w5p->hcsw.Type;
2582
2583 /* Firmware Workaround */
2584 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2585 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2586 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2587 Rctl = FC_RCTL_ELS_REQ;
2588 Type = FC_TYPE_ELS;
9c2face6
JS
2589 w5p->hcsw.Rctl = Rctl;
2590 w5p->hcsw.Type = Type;
2591 }
2592 }
92d7f7b0 2593
3772a991 2594 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2595 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2596 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2597 "Type x%x received\n",
e8b62011 2598 pring->ringno, Rctl, Type);
3772a991 2599
92d7f7b0 2600 return 1;
dea3101e
JB
2601}
2602
e59058c4 2603/**
3621a710 2604 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2605 * @phba: Pointer to HBA context object.
2606 * @pring: Pointer to driver SLI ring object.
2607 * @prspiocb: Pointer to response iocb object.
2608 *
2609 * This function looks up the iocb_lookup table to get the command iocb
2610 * corresponding to the given response iocb using the iotag of the
2611 * response iocb. This function is called with the hbalock held.
2612 * This function returns the command iocb object if it finds the command
2613 * iocb else returns NULL.
2614 **/
dea3101e 2615static struct lpfc_iocbq *
2e0fef85
JS
2616lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2617 struct lpfc_sli_ring *pring,
2618 struct lpfc_iocbq *prspiocb)
dea3101e 2619{
dea3101e
JB
2620 struct lpfc_iocbq *cmd_iocb = NULL;
2621 uint16_t iotag;
2622
604a3e30
JB
2623 iotag = prspiocb->iocb.ulpIoTag;
2624
2625 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2626 cmd_iocb = phba->sli.iocbq_lookup[iotag];
92d7f7b0 2627 list_del_init(&cmd_iocb->list);
4f2e66c6 2628 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
4f2e66c6 2629 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2a9bf3d0 2630 }
604a3e30 2631 return cmd_iocb;
dea3101e
JB
2632 }
2633
dea3101e 2634 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2635 "0317 iotag x%x is out off "
604a3e30 2636 "range: max iotag x%x wd0 x%x\n",
e8b62011 2637 iotag, phba->sli.last_iotag,
604a3e30 2638 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e
JB
2639 return NULL;
2640}
2641
3772a991
JS
2642/**
2643 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2644 * @phba: Pointer to HBA context object.
2645 * @pring: Pointer to driver SLI ring object.
2646 * @iotag: IOCB tag.
2647 *
2648 * This function looks up the iocb_lookup table to get the command iocb
2649 * corresponding to the given iotag. This function is called with the
2650 * hbalock held.
2651 * This function returns the command iocb object if it finds the command
2652 * iocb else returns NULL.
2653 **/
2654static struct lpfc_iocbq *
2655lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2656 struct lpfc_sli_ring *pring, uint16_t iotag)
2657{
2658 struct lpfc_iocbq *cmd_iocb;
2659
2660 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2661 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6
JS
2662 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2663 /* remove from txcmpl queue list */
2664 list_del_init(&cmd_iocb->list);
2665 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4f2e66c6 2666 return cmd_iocb;
2a9bf3d0 2667 }
3772a991 2668 }
3772a991
JS
2669 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2670 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2671 iotag, phba->sli.last_iotag);
2672 return NULL;
2673}
2674
e59058c4 2675/**
3621a710 2676 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2677 * @phba: Pointer to HBA context object.
2678 * @pring: Pointer to driver SLI ring object.
2679 * @saveq: Pointer to the response iocb to be processed.
2680 *
2681 * This function is called by the ring event handler for non-fcp
2682 * rings when there is a new response iocb in the response ring.
2683 * The caller is not required to hold any locks. This function
2684 * gets the command iocb associated with the response iocb and
2685 * calls the completion handler for the command iocb. If there
2686 * is no completion handler, the function will free the resources
2687 * associated with command iocb. If the response iocb is for
2688 * an already aborted command iocb, the status of the completion
2689 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2690 * This function always returns 1.
2691 **/
dea3101e 2692static int
2e0fef85 2693lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e
JB
2694 struct lpfc_iocbq *saveq)
2695{
2e0fef85 2696 struct lpfc_iocbq *cmdiocbp;
dea3101e
JB
2697 int rc = 1;
2698 unsigned long iflag;
2699
2700 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2e0fef85 2701 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2702 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2e0fef85
JS
2703 spin_unlock_irqrestore(&phba->hbalock, iflag);
2704
dea3101e
JB
2705 if (cmdiocbp) {
2706 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2707 /*
2708 * If an ELS command failed send an event to mgmt
2709 * application.
2710 */
2711 if (saveq->iocb.ulpStatus &&
2712 (pring->ringno == LPFC_ELS_RING) &&
2713 (cmdiocbp->iocb.ulpCommand ==
2714 CMD_ELS_REQUEST64_CR))
2715 lpfc_send_els_failure_event(phba,
2716 cmdiocbp, saveq);
2717
dea3101e
JB
2718 /*
2719 * Post all ELS completions to the worker thread.
2720 * All other are passed to the completion callback.
2721 */
2722 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2723 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2724 (cmdiocbp->iocb_flag &
2725 LPFC_DRIVER_ABORTED)) {
2726 spin_lock_irqsave(&phba->hbalock,
2727 iflag);
07951076
JS
2728 cmdiocbp->iocb_flag &=
2729 ~LPFC_DRIVER_ABORTED;
341af102
JS
2730 spin_unlock_irqrestore(&phba->hbalock,
2731 iflag);
07951076
JS
2732 saveq->iocb.ulpStatus =
2733 IOSTAT_LOCAL_REJECT;
2734 saveq->iocb.un.ulpWord[4] =
2735 IOERR_SLI_ABORTED;
0ff10d46
JS
2736
2737 /* Firmware could still be in progress
2738 * of DMAing payload, so don't free data
2739 * buffer till after a hbeat.
2740 */
341af102
JS
2741 spin_lock_irqsave(&phba->hbalock,
2742 iflag);
0ff10d46 2743 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2744 spin_unlock_irqrestore(&phba->hbalock,
2745 iflag);
2746 }
0f65ff68
JS
2747 if (phba->sli_rev == LPFC_SLI_REV4) {
2748 if (saveq->iocb_flag &
2749 LPFC_EXCHANGE_BUSY) {
2750 /* Set cmdiocb flag for the
2751 * exchange busy so sgl (xri)
2752 * will not be released until
2753 * the abort xri is received
2754 * from hba.
2755 */
2756 spin_lock_irqsave(
2757 &phba->hbalock, iflag);
2758 cmdiocbp->iocb_flag |=
2759 LPFC_EXCHANGE_BUSY;
2760 spin_unlock_irqrestore(
2761 &phba->hbalock, iflag);
2762 }
2763 if (cmdiocbp->iocb_flag &
2764 LPFC_DRIVER_ABORTED) {
2765 /*
2766 * Clear LPFC_DRIVER_ABORTED
2767 * bit in case it was driver
2768 * initiated abort.
2769 */
2770 spin_lock_irqsave(
2771 &phba->hbalock, iflag);
2772 cmdiocbp->iocb_flag &=
2773 ~LPFC_DRIVER_ABORTED;
2774 spin_unlock_irqrestore(
2775 &phba->hbalock, iflag);
2776 cmdiocbp->iocb.ulpStatus =
2777 IOSTAT_LOCAL_REJECT;
2778 cmdiocbp->iocb.un.ulpWord[4] =
2779 IOERR_ABORT_REQUESTED;
2780 /*
2781 * For SLI4, irsiocb contains
2782 * NO_XRI in sli_xritag, it
2783 * shall not affect releasing
2784 * sgl (xri) process.
2785 */
2786 saveq->iocb.ulpStatus =
2787 IOSTAT_LOCAL_REJECT;
2788 saveq->iocb.un.ulpWord[4] =
2789 IOERR_SLI_ABORTED;
2790 spin_lock_irqsave(
2791 &phba->hbalock, iflag);
2792 saveq->iocb_flag |=
2793 LPFC_DELAY_MEM_FREE;
2794 spin_unlock_irqrestore(
2795 &phba->hbalock, iflag);
2796 }
07951076 2797 }
dea3101e 2798 }
2e0fef85 2799 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2800 } else
2801 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e
JB
2802 } else {
2803 /*
2804 * Unknown initiating command based on the response iotag.
2805 * This could be the case on the ELS ring because of
2806 * lpfc_els_abort().
2807 */
2808 if (pring->ringno != LPFC_ELS_RING) {
2809 /*
2810 * Ring <ringno> handler: unexpected completion IoTag
2811 * <IoTag>
2812 */
a257bf90 2813 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2814 "0322 Ring %d handler: "
2815 "unexpected completion IoTag x%x "
2816 "Data: x%x x%x x%x x%x\n",
2817 pring->ringno,
2818 saveq->iocb.ulpIoTag,
2819 saveq->iocb.ulpStatus,
2820 saveq->iocb.un.ulpWord[4],
2821 saveq->iocb.ulpCommand,
2822 saveq->iocb.ulpContext);
dea3101e
JB
2823 }
2824 }
68876920 2825
dea3101e
JB
2826 return rc;
2827}
2828
e59058c4 2829/**
3621a710 2830 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2831 * @phba: Pointer to HBA context object.
2832 * @pring: Pointer to driver SLI ring object.
2833 *
2834 * This function is called from the iocb ring event handlers when
2835 * put pointer is ahead of the get pointer for a ring. This function signal
2836 * an error attention condition to the worker thread and the worker
2837 * thread will transition the HBA to offline state.
2838 **/
2e0fef85
JS
2839static void
2840lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2841{
34b02dcd 2842 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2843 /*
025dfdaf 2844 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2845 * rsp ring <portRspMax>
2846 */
2847 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2848 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2849 "is bigger than rsp ring %d\n",
e8b62011 2850 pring->ringno, le32_to_cpu(pgp->rspPutInx),
7e56aa25 2851 pring->sli.sli3.numRiocb);
875fbdfe 2852
2e0fef85 2853 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2854
2855 /*
2856 * All error attention handlers are posted to
2857 * worker thread
2858 */
2859 phba->work_ha |= HA_ERATT;
2860 phba->work_hs = HS_FFER3;
92d7f7b0 2861
5e9d9b82 2862 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
2863
2864 return;
2865}
2866
9399627f 2867/**
3621a710 2868 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
2869 * @ptr: Pointer to address of HBA context object.
2870 *
2871 * This function is invoked by the Error Attention polling timer when the
2872 * timer times out. It will check the SLI Error Attention register for
2873 * possible attention events. If so, it will post an Error Attention event
2874 * and wake up worker thread to process it. Otherwise, it will set up the
2875 * Error Attention polling timer for the next poll.
2876 **/
2877void lpfc_poll_eratt(unsigned long ptr)
2878{
2879 struct lpfc_hba *phba;
aa6fbb75
JS
2880 uint32_t eratt = 0, rem;
2881 uint64_t sli_intr, cnt;
9399627f
JS
2882
2883 phba = (struct lpfc_hba *)ptr;
2884
aa6fbb75
JS
2885 /* Here we will also keep track of interrupts per sec of the hba */
2886 sli_intr = phba->sli.slistat.sli_intr;
2887
2888 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2889 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2890 sli_intr);
2891 else
2892 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2893
2894 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2895 rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2896 phba->sli.slistat.sli_ips = cnt;
2897
2898 phba->sli.slistat.sli_prev_intr = sli_intr;
2899
9399627f
JS
2900 /* Check chip HA register for error event */
2901 eratt = lpfc_sli_check_eratt(phba);
2902
2903 if (eratt)
2904 /* Tell the worker thread there is work to do */
2905 lpfc_worker_wake_up(phba);
2906 else
2907 /* Restart the timer for next eratt poll */
2908 mod_timer(&phba->eratt_poll, jiffies +
2909 HZ * LPFC_ERATT_POLL_INTERVAL);
2910 return;
2911}
2912
875fbdfe 2913
e59058c4 2914/**
3621a710 2915 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
2916 * @phba: Pointer to HBA context object.
2917 * @pring: Pointer to driver SLI ring object.
2918 * @mask: Host attention register mask for this ring.
2919 *
2920 * This function is called from the interrupt context when there is a ring
2921 * event for the fcp ring. The caller does not hold any lock.
2922 * The function processes each response iocb in the response ring until it
25985edc 2923 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
e59058c4
JS
2924 * LE bit set. The function will call the completion handler of the command iocb
2925 * if the response iocb indicates a completion for a command iocb or it is
2926 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2927 * function if this is an unsolicited iocb.
dea3101e 2928 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
2929 * to check it explicitly.
2930 */
2931int
2e0fef85
JS
2932lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2933 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2934{
34b02dcd 2935 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 2936 IOCB_t *irsp = NULL;
87f6eaff 2937 IOCB_t *entry = NULL;
dea3101e
JB
2938 struct lpfc_iocbq *cmdiocbq = NULL;
2939 struct lpfc_iocbq rspiocbq;
dea3101e
JB
2940 uint32_t status;
2941 uint32_t portRspPut, portRspMax;
2942 int rc = 1;
2943 lpfc_iocb_type type;
2944 unsigned long iflag;
2945 uint32_t rsp_cmpl = 0;
dea3101e 2946
2e0fef85 2947 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
2948 pring->stats.iocb_event++;
2949
dea3101e
JB
2950 /*
2951 * The next available response entry should never exceed the maximum
2952 * entries. If it does, treat it as an adapter hardware error.
2953 */
7e56aa25 2954 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
2955 portRspPut = le32_to_cpu(pgp->rspPutInx);
2956 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 2957 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 2958 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
2959 return 1;
2960 }
45ed1190
JS
2961 if (phba->fcp_ring_in_use) {
2962 spin_unlock_irqrestore(&phba->hbalock, iflag);
2963 return 1;
2964 } else
2965 phba->fcp_ring_in_use = 1;
dea3101e
JB
2966
2967 rmb();
7e56aa25 2968 while (pring->sli.sli3.rspidx != portRspPut) {
87f6eaff
JSEC
2969 /*
2970 * Fetch an entry off the ring and copy it into a local data
2971 * structure. The copy involves a byte-swap since the
2972 * network byte order and pci byte orders are different.
2973 */
ed957684 2974 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 2975 phba->last_completion_time = jiffies;
875fbdfe 2976
7e56aa25
JS
2977 if (++pring->sli.sli3.rspidx >= portRspMax)
2978 pring->sli.sli3.rspidx = 0;
875fbdfe 2979
87f6eaff
JSEC
2980 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2981 (uint32_t *) &rspiocbq.iocb,
ed957684 2982 phba->iocb_rsp_size);
a4bc3379 2983 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
2984 irsp = &rspiocbq.iocb;
2985
dea3101e
JB
2986 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2987 pring->stats.iocb_rsp++;
2988 rsp_cmpl++;
2989
2990 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
2991 /*
2992 * If resource errors reported from HBA, reduce
2993 * queuedepths of the SCSI device.
2994 */
2995 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
2996 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
2997 IOERR_NO_RESOURCES)) {
92d7f7b0 2998 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2999 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
3000 spin_lock_irqsave(&phba->hbalock, iflag);
3001 }
3002
dea3101e
JB
3003 /* Rsp ring <ringno> error: IOCB */
3004 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 3005 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 3006 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 3007 pring->ringno,
92d7f7b0
JS
3008 irsp->un.ulpWord[0],
3009 irsp->un.ulpWord[1],
3010 irsp->un.ulpWord[2],
3011 irsp->un.ulpWord[3],
3012 irsp->un.ulpWord[4],
3013 irsp->un.ulpWord[5],
d7c255b2
JS
3014 *(uint32_t *)&irsp->un1,
3015 *((uint32_t *)&irsp->un1 + 1));
dea3101e
JB
3016 }
3017
3018 switch (type) {
3019 case LPFC_ABORT_IOCB:
3020 case LPFC_SOL_IOCB:
3021 /*
3022 * Idle exchange closed via ABTS from port. No iocb
3023 * resources need to be recovered.
3024 */
3025 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 3026 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3027 "0333 IOCB cmd 0x%x"
dca9479b 3028 " processed. Skipping"
92d7f7b0 3029 " completion\n",
dca9479b 3030 irsp->ulpCommand);
dea3101e
JB
3031 break;
3032 }
3033
604a3e30
JB
3034 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3035 &rspiocbq);
0f65ff68
JS
3036 if (unlikely(!cmdiocbq))
3037 break;
3038 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3039 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3040 if (cmdiocbq->iocb_cmpl) {
3041 spin_unlock_irqrestore(&phba->hbalock, iflag);
3042 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3043 &rspiocbq);
3044 spin_lock_irqsave(&phba->hbalock, iflag);
3045 }
dea3101e 3046 break;
a4bc3379 3047 case LPFC_UNSOL_IOCB:
2e0fef85 3048 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 3049 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 3050 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 3051 break;
dea3101e
JB
3052 default:
3053 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3054 char adaptermsg[LPFC_MAX_ADPTMSG];
3055 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3056 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3057 MAX_MSG_DATA);
898eb71c
JP
3058 dev_warn(&((phba->pcidev)->dev),
3059 "lpfc%d: %s\n",
dea3101e
JB
3060 phba->brd_no, adaptermsg);
3061 } else {
3062 /* Unknown IOCB command */
3063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3064 "0334 Unknown IOCB command "
92d7f7b0 3065 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 3066 type, irsp->ulpCommand,
92d7f7b0
JS
3067 irsp->ulpStatus,
3068 irsp->ulpIoTag,
3069 irsp->ulpContext);
dea3101e
JB
3070 }
3071 break;
3072 }
3073
3074 /*
3075 * The response IOCB has been processed. Update the ring
3076 * pointer in SLIM. If the port response put pointer has not
3077 * been updated, sync the pgp->rspPutInx and fetch the new port
3078 * response put pointer.
3079 */
7e56aa25
JS
3080 writel(pring->sli.sli3.rspidx,
3081 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3082
7e56aa25 3083 if (pring->sli.sli3.rspidx == portRspPut)
dea3101e
JB
3084 portRspPut = le32_to_cpu(pgp->rspPutInx);
3085 }
3086
3087 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3088 pring->stats.iocb_rsp_full++;
3089 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3090 writel(status, phba->CAregaddr);
3091 readl(phba->CAregaddr);
3092 }
3093 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3094 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3095 pring->stats.iocb_cmd_empty++;
3096
3097 /* Force update of the local copy of cmdGetInx */
7e56aa25 3098 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3099 lpfc_sli_resume_iocb(phba, pring);
3100
3101 if ((pring->lpfc_sli_cmd_available))
3102 (pring->lpfc_sli_cmd_available) (phba, pring);
3103
3104 }
3105
45ed1190 3106 phba->fcp_ring_in_use = 0;
2e0fef85 3107 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3108 return rc;
3109}
3110
e59058c4 3111/**
3772a991
JS
3112 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3113 * @phba: Pointer to HBA context object.
3114 * @pring: Pointer to driver SLI ring object.
3115 * @rspiocbp: Pointer to driver response IOCB object.
3116 *
3117 * This function is called from the worker thread when there is a slow-path
3118 * response IOCB to process. This function chains all the response iocbs until
3119 * seeing the iocb with the LE bit set. The function will call
3120 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3121 * completion of a command iocb. The function will call the
3122 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3123 * The function frees the resources or calls the completion handler if this
3124 * iocb is an abort completion. The function returns NULL when the response
3125 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3126 * this function shall chain the iocb on to the iocb_continueq and return the
3127 * response iocb passed in.
3128 **/
3129static struct lpfc_iocbq *
3130lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3131 struct lpfc_iocbq *rspiocbp)
3132{
3133 struct lpfc_iocbq *saveq;
3134 struct lpfc_iocbq *cmdiocbp;
3135 struct lpfc_iocbq *next_iocb;
3136 IOCB_t *irsp = NULL;
3137 uint32_t free_saveq;
3138 uint8_t iocb_cmd_type;
3139 lpfc_iocb_type type;
3140 unsigned long iflag;
3141 int rc;
3142
3143 spin_lock_irqsave(&phba->hbalock, iflag);
3144 /* First add the response iocb to the countinueq list */
3145 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3146 pring->iocb_continueq_cnt++;
3147
70f23fd6 3148 /* Now, determine whether the list is completed for processing */
3772a991
JS
3149 irsp = &rspiocbp->iocb;
3150 if (irsp->ulpLe) {
3151 /*
3152 * By default, the driver expects to free all resources
3153 * associated with this iocb completion.
3154 */
3155 free_saveq = 1;
3156 saveq = list_get_first(&pring->iocb_continueq,
3157 struct lpfc_iocbq, list);
3158 irsp = &(saveq->iocb);
3159 list_del_init(&pring->iocb_continueq);
3160 pring->iocb_continueq_cnt = 0;
3161
3162 pring->stats.iocb_rsp++;
3163
3164 /*
3165 * If resource errors reported from HBA, reduce
3166 * queuedepths of the SCSI device.
3167 */
3168 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3169 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3170 IOERR_NO_RESOURCES)) {
3772a991
JS
3171 spin_unlock_irqrestore(&phba->hbalock, iflag);
3172 phba->lpfc_rampdown_queue_depth(phba);
3173 spin_lock_irqsave(&phba->hbalock, iflag);
3174 }
3175
3176 if (irsp->ulpStatus) {
3177 /* Rsp ring <ringno> error: IOCB */
3178 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3179 "0328 Rsp Ring %d error: "
3180 "IOCB Data: "
3181 "x%x x%x x%x x%x "
3182 "x%x x%x x%x x%x "
3183 "x%x x%x x%x x%x "
3184 "x%x x%x x%x x%x\n",
3185 pring->ringno,
3186 irsp->un.ulpWord[0],
3187 irsp->un.ulpWord[1],
3188 irsp->un.ulpWord[2],
3189 irsp->un.ulpWord[3],
3190 irsp->un.ulpWord[4],
3191 irsp->un.ulpWord[5],
3192 *(((uint32_t *) irsp) + 6),
3193 *(((uint32_t *) irsp) + 7),
3194 *(((uint32_t *) irsp) + 8),
3195 *(((uint32_t *) irsp) + 9),
3196 *(((uint32_t *) irsp) + 10),
3197 *(((uint32_t *) irsp) + 11),
3198 *(((uint32_t *) irsp) + 12),
3199 *(((uint32_t *) irsp) + 13),
3200 *(((uint32_t *) irsp) + 14),
3201 *(((uint32_t *) irsp) + 15));
3202 }
3203
3204 /*
3205 * Fetch the IOCB command type and call the correct completion
3206 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3207 * get freed back to the lpfc_iocb_list by the discovery
3208 * kernel thread.
3209 */
3210 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3211 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3212 switch (type) {
3213 case LPFC_SOL_IOCB:
3214 spin_unlock_irqrestore(&phba->hbalock, iflag);
3215 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3216 spin_lock_irqsave(&phba->hbalock, iflag);
3217 break;
3218
3219 case LPFC_UNSOL_IOCB:
3220 spin_unlock_irqrestore(&phba->hbalock, iflag);
3221 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3222 spin_lock_irqsave(&phba->hbalock, iflag);
3223 if (!rc)
3224 free_saveq = 0;
3225 break;
3226
3227 case LPFC_ABORT_IOCB:
3228 cmdiocbp = NULL;
3229 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3230 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3231 saveq);
3232 if (cmdiocbp) {
3233 /* Call the specified completion routine */
3234 if (cmdiocbp->iocb_cmpl) {
3235 spin_unlock_irqrestore(&phba->hbalock,
3236 iflag);
3237 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3238 saveq);
3239 spin_lock_irqsave(&phba->hbalock,
3240 iflag);
3241 } else
3242 __lpfc_sli_release_iocbq(phba,
3243 cmdiocbp);
3244 }
3245 break;
3246
3247 case LPFC_UNKNOWN_IOCB:
3248 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3249 char adaptermsg[LPFC_MAX_ADPTMSG];
3250 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3251 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3252 MAX_MSG_DATA);
3253 dev_warn(&((phba->pcidev)->dev),
3254 "lpfc%d: %s\n",
3255 phba->brd_no, adaptermsg);
3256 } else {
3257 /* Unknown IOCB command */
3258 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3259 "0335 Unknown IOCB "
3260 "command Data: x%x "
3261 "x%x x%x x%x\n",
3262 irsp->ulpCommand,
3263 irsp->ulpStatus,
3264 irsp->ulpIoTag,
3265 irsp->ulpContext);
3266 }
3267 break;
3268 }
3269
3270 if (free_saveq) {
3271 list_for_each_entry_safe(rspiocbp, next_iocb,
3272 &saveq->list, list) {
3273 list_del(&rspiocbp->list);
3274 __lpfc_sli_release_iocbq(phba, rspiocbp);
3275 }
3276 __lpfc_sli_release_iocbq(phba, saveq);
3277 }
3278 rspiocbp = NULL;
3279 }
3280 spin_unlock_irqrestore(&phba->hbalock, iflag);
3281 return rspiocbp;
3282}
3283
3284/**
3285 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
3286 * @phba: Pointer to HBA context object.
3287 * @pring: Pointer to driver SLI ring object.
3288 * @mask: Host attention register mask for this ring.
3289 *
3772a991
JS
3290 * This routine wraps the actual slow_ring event process routine from the
3291 * API jump table function pointer from the lpfc_hba struct.
e59058c4 3292 **/
3772a991 3293void
2e0fef85
JS
3294lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3295 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
3296{
3297 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3298}
3299
3300/**
3301 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3302 * @phba: Pointer to HBA context object.
3303 * @pring: Pointer to driver SLI ring object.
3304 * @mask: Host attention register mask for this ring.
3305 *
3306 * This function is called from the worker thread when there is a ring event
3307 * for non-fcp rings. The caller does not hold any lock. The function will
3308 * remove each response iocb in the response ring and calls the handle
3309 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3310 **/
3311static void
3312lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3313 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3314{
34b02dcd 3315 struct lpfc_pgp *pgp;
dea3101e
JB
3316 IOCB_t *entry;
3317 IOCB_t *irsp = NULL;
3318 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 3319 uint32_t portRspPut, portRspMax;
dea3101e 3320 unsigned long iflag;
3772a991 3321 uint32_t status;
dea3101e 3322
34b02dcd 3323 pgp = &phba->port_gp[pring->ringno];
2e0fef85 3324 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3325 pring->stats.iocb_event++;
3326
dea3101e
JB
3327 /*
3328 * The next available response entry should never exceed the maximum
3329 * entries. If it does, treat it as an adapter hardware error.
3330 */
7e56aa25 3331 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3332 portRspPut = le32_to_cpu(pgp->rspPutInx);
3333 if (portRspPut >= portRspMax) {
3334 /*
025dfdaf 3335 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e
JB
3336 * rsp ring <portRspMax>
3337 */
ed957684 3338 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3339 "0303 Ring %d handler: portRspPut %d "
025dfdaf 3340 "is bigger than rsp ring %d\n",
e8b62011 3341 pring->ringno, portRspPut, portRspMax);
dea3101e 3342
2e0fef85
JS
3343 phba->link_state = LPFC_HBA_ERROR;
3344 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3345
3346 phba->work_hs = HS_FFER3;
3347 lpfc_handle_eratt(phba);
3348
3772a991 3349 return;
dea3101e
JB
3350 }
3351
3352 rmb();
7e56aa25 3353 while (pring->sli.sli3.rspidx != portRspPut) {
dea3101e
JB
3354 /*
3355 * Build a completion list and call the appropriate handler.
3356 * The process is to get the next available response iocb, get
3357 * a free iocb from the list, copy the response data into the
3358 * free iocb, insert to the continuation list, and update the
3359 * next response index to slim. This process makes response
3360 * iocb's in the ring available to DMA as fast as possible but
3361 * pays a penalty for a copy operation. Since the iocb is
3362 * only 32 bytes, this penalty is considered small relative to
3363 * the PCI reads for register values and a slim write. When
3364 * the ulpLe field is set, the entire Command has been
3365 * received.
3366 */
ed957684
JS
3367 entry = lpfc_resp_iocb(phba, pring);
3368
858c9f6c 3369 phba->last_completion_time = jiffies;
2e0fef85 3370 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
3371 if (rspiocbp == NULL) {
3372 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 3373 "completion.\n", __func__);
dea3101e
JB
3374 break;
3375 }
3376
ed957684
JS
3377 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3378 phba->iocb_rsp_size);
dea3101e
JB
3379 irsp = &rspiocbp->iocb;
3380
7e56aa25
JS
3381 if (++pring->sli.sli3.rspidx >= portRspMax)
3382 pring->sli.sli3.rspidx = 0;
dea3101e 3383
a58cbd52
JS
3384 if (pring->ringno == LPFC_ELS_RING) {
3385 lpfc_debugfs_slow_ring_trc(phba,
3386 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3387 *(((uint32_t *) irsp) + 4),
3388 *(((uint32_t *) irsp) + 6),
3389 *(((uint32_t *) irsp) + 7));
3390 }
3391
7e56aa25
JS
3392 writel(pring->sli.sli3.rspidx,
3393 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3394
3772a991
JS
3395 spin_unlock_irqrestore(&phba->hbalock, iflag);
3396 /* Handle the response IOCB */
3397 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3398 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3399
3400 /*
3401 * If the port response put pointer has not been updated, sync
3402 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3403 * response put pointer.
3404 */
7e56aa25 3405 if (pring->sli.sli3.rspidx == portRspPut) {
dea3101e
JB
3406 portRspPut = le32_to_cpu(pgp->rspPutInx);
3407 }
7e56aa25 3408 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea3101e 3409
92d7f7b0 3410 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e
JB
3411 /* At least one response entry has been freed */
3412 pring->stats.iocb_rsp_full++;
3413 /* SET RxRE_RSP in Chip Att register */
3414 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3415 writel(status, phba->CAregaddr);
3416 readl(phba->CAregaddr); /* flush */
3417 }
3418 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3419 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3420 pring->stats.iocb_cmd_empty++;
3421
3422 /* Force update of the local copy of cmdGetInx */
7e56aa25 3423 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3424 lpfc_sli_resume_iocb(phba, pring);
3425
3426 if ((pring->lpfc_sli_cmd_available))
3427 (pring->lpfc_sli_cmd_available) (phba, pring);
3428
3429 }
3430
2e0fef85 3431 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3432 return;
dea3101e
JB
3433}
3434
4f774513
JS
3435/**
3436 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3437 * @phba: Pointer to HBA context object.
3438 * @pring: Pointer to driver SLI ring object.
3439 * @mask: Host attention register mask for this ring.
3440 *
3441 * This function is called from the worker thread when there is a pending
3442 * ELS response iocb on the driver internal slow-path response iocb worker
3443 * queue. The caller does not hold any lock. The function will remove each
3444 * response iocb from the response worker queue and calls the handle
3445 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3446 **/
3447static void
3448lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3449 struct lpfc_sli_ring *pring, uint32_t mask)
3450{
3451 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3452 struct hbq_dmabuf *dmabuf;
3453 struct lpfc_cq_event *cq_event;
4f774513
JS
3454 unsigned long iflag;
3455
45ed1190
JS
3456 spin_lock_irqsave(&phba->hbalock, iflag);
3457 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3458 spin_unlock_irqrestore(&phba->hbalock, iflag);
3459 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
3460 /* Get the response iocb from the head of work queue */
3461 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3462 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3463 cq_event, struct lpfc_cq_event, list);
4f774513 3464 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3465
3466 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3467 case CQE_CODE_COMPL_WQE:
3468 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3469 cq_event);
45ed1190
JS
3470 /* Translate ELS WCQE to response IOCBQ */
3471 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3472 irspiocbq);
3473 if (irspiocbq)
3474 lpfc_sli_sp_handle_rspiocb(phba, pring,
3475 irspiocbq);
4d9ab994
JS
3476 break;
3477 case CQE_CODE_RECEIVE:
7851fe2c 3478 case CQE_CODE_RECEIVE_V1:
4d9ab994
JS
3479 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3480 cq_event);
3481 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3482 break;
3483 default:
3484 break;
3485 }
4f774513
JS
3486 }
3487}
3488
e59058c4 3489/**
3621a710 3490 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3491 * @phba: Pointer to HBA context object.
3492 * @pring: Pointer to driver SLI ring object.
3493 *
3494 * This function aborts all iocbs in the given ring and frees all the iocb
3495 * objects in txq. This function issues an abort iocb for all the iocb commands
3496 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3497 * the return of this function. The caller is not required to hold any locks.
3498 **/
2e0fef85 3499void
dea3101e
JB
3500lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3501{
2534ba75 3502 LIST_HEAD(completions);
dea3101e 3503 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3504
92d7f7b0
JS
3505 if (pring->ringno == LPFC_ELS_RING) {
3506 lpfc_fabric_abort_hba(phba);
3507 }
3508
dea3101e
JB
3509 /* Error everything on txq and txcmplq
3510 * First do the txq.
3511 */
2e0fef85 3512 spin_lock_irq(&phba->hbalock);
2534ba75 3513 list_splice_init(&pring->txq, &completions);
dea3101e
JB
3514
3515 /* Next issue ABTS for everything on the txcmplq */
2534ba75
JS
3516 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3517 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea3101e 3518
2e0fef85 3519 spin_unlock_irq(&phba->hbalock);
dea3101e 3520
a257bf90
JS
3521 /* Cancel all the IOCBs from the completions list */
3522 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3523 IOERR_SLI_ABORTED);
dea3101e
JB
3524}
3525
a8e497d5 3526/**
3621a710 3527 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3528 * @phba: Pointer to HBA context object.
3529 *
3530 * This function flushes all iocbs in the fcp ring and frees all the iocb
3531 * objects in txq and txcmplq. This function will not issue abort iocbs
3532 * for all the iocb commands in txcmplq, they will just be returned with
3533 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3534 * slot has been permanently disabled.
3535 **/
3536void
3537lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3538{
3539 LIST_HEAD(txq);
3540 LIST_HEAD(txcmplq);
a8e497d5
JS
3541 struct lpfc_sli *psli = &phba->sli;
3542 struct lpfc_sli_ring *pring;
3543
3544 /* Currently, only one fcp ring */
3545 pring = &psli->ring[psli->fcp_ring];
3546
3547 spin_lock_irq(&phba->hbalock);
3548 /* Retrieve everything on txq */
3549 list_splice_init(&pring->txq, &txq);
a8e497d5
JS
3550
3551 /* Retrieve everything on the txcmplq */
3552 list_splice_init(&pring->txcmplq, &txcmplq);
4f2e66c6
JS
3553
3554 /* Indicate the I/O queues are flushed */
3555 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
a8e497d5
JS
3556 spin_unlock_irq(&phba->hbalock);
3557
3558 /* Flush the txq */
a257bf90
JS
3559 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3560 IOERR_SLI_DOWN);
a8e497d5
JS
3561
3562 /* Flush the txcmpq */
a257bf90
JS
3563 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3564 IOERR_SLI_DOWN);
a8e497d5
JS
3565}
3566
e59058c4 3567/**
3772a991 3568 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3569 * @phba: Pointer to HBA context object.
3570 * @mask: Bit mask to be checked.
3571 *
3572 * This function reads the host status register and compares
3573 * with the provided bit mask to check if HBA completed
3574 * the restart. This function will wait in a loop for the
3575 * HBA to complete restart. If the HBA does not restart within
3576 * 15 iterations, the function will reset the HBA again. The
3577 * function returns 1 when HBA fail to restart otherwise returns
3578 * zero.
3579 **/
3772a991
JS
3580static int
3581lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3582{
41415862
JW
3583 uint32_t status;
3584 int i = 0;
3585 int retval = 0;
dea3101e 3586
41415862 3587 /* Read the HBA Host Status Register */
9940b97b
JS
3588 if (lpfc_readl(phba->HSregaddr, &status))
3589 return 1;
dea3101e 3590
41415862
JW
3591 /*
3592 * Check status register every 100ms for 5 retries, then every
3593 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3594 * every 2.5 sec for 4.
3595 * Break our of the loop if errors occurred during init.
3596 */
3597 while (((status & mask) != mask) &&
3598 !(status & HS_FFERM) &&
3599 i++ < 20) {
dea3101e 3600
41415862
JW
3601 if (i <= 5)
3602 msleep(10);
3603 else if (i <= 10)
3604 msleep(500);
3605 else
3606 msleep(2500);
dea3101e 3607
41415862 3608 if (i == 15) {
2e0fef85 3609 /* Do post */
92d7f7b0 3610 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3611 lpfc_sli_brdrestart(phba);
3612 }
3613 /* Read the HBA Host Status Register */
9940b97b
JS
3614 if (lpfc_readl(phba->HSregaddr, &status)) {
3615 retval = 1;
3616 break;
3617 }
41415862 3618 }
dea3101e 3619
41415862
JW
3620 /* Check to see if any errors occurred during init */
3621 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
3622 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3623 "2751 Adapter failed to restart, "
3624 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3625 status,
3626 readl(phba->MBslimaddr + 0xa8),
3627 readl(phba->MBslimaddr + 0xac));
2e0fef85 3628 phba->link_state = LPFC_HBA_ERROR;
41415862 3629 retval = 1;
dea3101e 3630 }
dea3101e 3631
41415862
JW
3632 return retval;
3633}
dea3101e 3634
da0436e9
JS
3635/**
3636 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3637 * @phba: Pointer to HBA context object.
3638 * @mask: Bit mask to be checked.
3639 *
3640 * This function checks the host status register to check if HBA is
3641 * ready. This function will wait in a loop for the HBA to be ready
3642 * If the HBA is not ready , the function will will reset the HBA PCI
3643 * function again. The function returns 1 when HBA fail to be ready
3644 * otherwise returns zero.
3645 **/
3646static int
3647lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3648{
3649 uint32_t status;
3650 int retval = 0;
3651
3652 /* Read the HBA Host Status Register */
3653 status = lpfc_sli4_post_status_check(phba);
3654
3655 if (status) {
3656 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3657 lpfc_sli_brdrestart(phba);
3658 status = lpfc_sli4_post_status_check(phba);
3659 }
3660
3661 /* Check to see if any errors occurred during init */
3662 if (status) {
3663 phba->link_state = LPFC_HBA_ERROR;
3664 retval = 1;
3665 } else
3666 phba->sli4_hba.intr_enable = 0;
3667
3668 return retval;
3669}
3670
3671/**
3672 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3673 * @phba: Pointer to HBA context object.
3674 * @mask: Bit mask to be checked.
3675 *
3676 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3677 * from the API jump table function pointer from the lpfc_hba struct.
3678 **/
3679int
3680lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3681{
3682 return phba->lpfc_sli_brdready(phba, mask);
3683}
3684
9290831f
JS
3685#define BARRIER_TEST_PATTERN (0xdeadbeef)
3686
e59058c4 3687/**
3621a710 3688 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3689 * @phba: Pointer to HBA context object.
3690 *
1b51197d
JS
3691 * This function is called before resetting an HBA. This function is called
3692 * with hbalock held and requests HBA to quiesce DMAs before a reset.
e59058c4 3693 **/
2e0fef85 3694void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 3695{
65a29c16
JS
3696 uint32_t __iomem *resp_buf;
3697 uint32_t __iomem *mbox_buf;
9290831f 3698 volatile uint32_t mbox;
9940b97b 3699 uint32_t hc_copy, ha_copy, resp_data;
9290831f
JS
3700 int i;
3701 uint8_t hdrtype;
3702
3703 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3704 if (hdrtype != 0x80 ||
3705 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3706 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3707 return;
3708
3709 /*
3710 * Tell the other part of the chip to suspend temporarily all
3711 * its DMA activity.
3712 */
65a29c16 3713 resp_buf = phba->MBslimaddr;
9290831f
JS
3714
3715 /* Disable the error attention */
9940b97b
JS
3716 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3717 return;
9290831f
JS
3718 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3719 readl(phba->HCregaddr); /* flush */
2e0fef85 3720 phba->link_flag |= LS_IGNORE_ERATT;
9290831f 3721
9940b97b
JS
3722 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3723 return;
3724 if (ha_copy & HA_ERATT) {
9290831f
JS
3725 /* Clear Chip error bit */
3726 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3727 phba->pport->stopped = 1;
9290831f
JS
3728 }
3729
3730 mbox = 0;
3731 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3732 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3733
3734 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 3735 mbox_buf = phba->MBslimaddr;
9290831f
JS
3736 writel(mbox, mbox_buf);
3737
9940b97b
JS
3738 for (i = 0; i < 50; i++) {
3739 if (lpfc_readl((resp_buf + 1), &resp_data))
3740 return;
3741 if (resp_data != ~(BARRIER_TEST_PATTERN))
3742 mdelay(1);
3743 else
3744 break;
3745 }
3746 resp_data = 0;
3747 if (lpfc_readl((resp_buf + 1), &resp_data))
3748 return;
3749 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 3750 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 3751 phba->pport->stopped)
9290831f
JS
3752 goto restore_hc;
3753 else
3754 goto clear_errat;
3755 }
3756
3757 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
9940b97b
JS
3758 resp_data = 0;
3759 for (i = 0; i < 500; i++) {
3760 if (lpfc_readl(resp_buf, &resp_data))
3761 return;
3762 if (resp_data != mbox)
3763 mdelay(1);
3764 else
3765 break;
3766 }
9290831f
JS
3767
3768clear_errat:
3769
9940b97b
JS
3770 while (++i < 500) {
3771 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3772 return;
3773 if (!(ha_copy & HA_ERATT))
3774 mdelay(1);
3775 else
3776 break;
3777 }
9290831f
JS
3778
3779 if (readl(phba->HAregaddr) & HA_ERATT) {
3780 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3781 phba->pport->stopped = 1;
9290831f
JS
3782 }
3783
3784restore_hc:
2e0fef85 3785 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
3786 writel(hc_copy, phba->HCregaddr);
3787 readl(phba->HCregaddr); /* flush */
3788}
3789
e59058c4 3790/**
3621a710 3791 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
3792 * @phba: Pointer to HBA context object.
3793 *
3794 * This function issues a kill_board mailbox command and waits for
3795 * the error attention interrupt. This function is called for stopping
3796 * the firmware processing. The caller is not required to hold any
3797 * locks. This function calls lpfc_hba_down_post function to free
3798 * any pending commands after the kill. The function will return 1 when it
3799 * fails to kill the board else will return 0.
3800 **/
41415862 3801int
2e0fef85 3802lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
3803{
3804 struct lpfc_sli *psli;
3805 LPFC_MBOXQ_t *pmb;
3806 uint32_t status;
3807 uint32_t ha_copy;
3808 int retval;
3809 int i = 0;
dea3101e 3810
41415862 3811 psli = &phba->sli;
dea3101e 3812
41415862 3813 /* Kill HBA */
ed957684 3814 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
3815 "0329 Kill HBA Data: x%x x%x\n",
3816 phba->pport->port_state, psli->sli_flag);
41415862 3817
98c9ea5c
JS
3818 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3819 if (!pmb)
41415862 3820 return 1;
41415862
JW
3821
3822 /* Disable the error attention */
2e0fef85 3823 spin_lock_irq(&phba->hbalock);
9940b97b
JS
3824 if (lpfc_readl(phba->HCregaddr, &status)) {
3825 spin_unlock_irq(&phba->hbalock);
3826 mempool_free(pmb, phba->mbox_mem_pool);
3827 return 1;
3828 }
41415862
JW
3829 status &= ~HC_ERINT_ENA;
3830 writel(status, phba->HCregaddr);
3831 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
3832 phba->link_flag |= LS_IGNORE_ERATT;
3833 spin_unlock_irq(&phba->hbalock);
41415862
JW
3834
3835 lpfc_kill_board(phba, pmb);
3836 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3837 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3838
3839 if (retval != MBX_SUCCESS) {
3840 if (retval != MBX_BUSY)
3841 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
3842 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3843 "2752 KILL_BOARD command failed retval %d\n",
3844 retval);
2e0fef85
JS
3845 spin_lock_irq(&phba->hbalock);
3846 phba->link_flag &= ~LS_IGNORE_ERATT;
3847 spin_unlock_irq(&phba->hbalock);
41415862
JW
3848 return 1;
3849 }
3850
f4b4c68f
JS
3851 spin_lock_irq(&phba->hbalock);
3852 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3853 spin_unlock_irq(&phba->hbalock);
9290831f 3854
41415862
JW
3855 mempool_free(pmb, phba->mbox_mem_pool);
3856
3857 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3858 * attention every 100ms for 3 seconds. If we don't get ERATT after
3859 * 3 seconds we still set HBA_ERROR state because the status of the
3860 * board is now undefined.
3861 */
9940b97b
JS
3862 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3863 return 1;
41415862
JW
3864 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3865 mdelay(100);
9940b97b
JS
3866 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3867 return 1;
41415862
JW
3868 }
3869
3870 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
3871 if (ha_copy & HA_ERATT) {
3872 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3873 phba->pport->stopped = 1;
9290831f 3874 }
2e0fef85 3875 spin_lock_irq(&phba->hbalock);
41415862 3876 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 3877 psli->mbox_active = NULL;
2e0fef85
JS
3878 phba->link_flag &= ~LS_IGNORE_ERATT;
3879 spin_unlock_irq(&phba->hbalock);
41415862 3880
41415862 3881 lpfc_hba_down_post(phba);
2e0fef85 3882 phba->link_state = LPFC_HBA_ERROR;
41415862 3883
2e0fef85 3884 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e
JB
3885}
3886
e59058c4 3887/**
3772a991 3888 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
3889 * @phba: Pointer to HBA context object.
3890 *
3891 * This function resets the HBA by writing HC_INITFF to the control
3892 * register. After the HBA resets, this function resets all the iocb ring
3893 * indices. This function disables PCI layer parity checking during
3894 * the reset.
3895 * This function returns 0 always.
3896 * The caller is not required to hold any locks.
3897 **/
41415862 3898int
2e0fef85 3899lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 3900{
41415862 3901 struct lpfc_sli *psli;
dea3101e 3902 struct lpfc_sli_ring *pring;
41415862 3903 uint16_t cfg_value;
dea3101e 3904 int i;
dea3101e 3905
41415862 3906 psli = &phba->sli;
dea3101e 3907
41415862
JW
3908 /* Reset HBA */
3909 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3910 "0325 Reset HBA Data: x%x x%x\n",
2e0fef85 3911 phba->pport->port_state, psli->sli_flag);
dea3101e
JB
3912
3913 /* perform board reset */
3914 phba->fc_eventTag = 0;
4d9ab994 3915 phba->link_events = 0;
2e0fef85
JS
3916 phba->pport->fc_myDID = 0;
3917 phba->pport->fc_prevDID = 0;
dea3101e 3918
41415862
JW
3919 /* Turn off parity checking and serr during the physical reset */
3920 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3921 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3922 (cfg_value &
3923 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3924
3772a991
JS
3925 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3926
41415862
JW
3927 /* Now toggle INITFF bit in the Host Control Register */
3928 writel(HC_INITFF, phba->HCregaddr);
3929 mdelay(1);
3930 readl(phba->HCregaddr); /* flush */
3931 writel(0, phba->HCregaddr);
3932 readl(phba->HCregaddr); /* flush */
3933
3934 /* Restore PCI cmd register */
3935 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e
JB
3936
3937 /* Initialize relevant SLI info */
41415862
JW
3938 for (i = 0; i < psli->num_rings; i++) {
3939 pring = &psli->ring[i];
dea3101e 3940 pring->flag = 0;
7e56aa25
JS
3941 pring->sli.sli3.rspidx = 0;
3942 pring->sli.sli3.next_cmdidx = 0;
3943 pring->sli.sli3.local_getidx = 0;
3944 pring->sli.sli3.cmdidx = 0;
dea3101e
JB
3945 pring->missbufcnt = 0;
3946 }
dea3101e 3947
2e0fef85 3948 phba->link_state = LPFC_WARM_START;
41415862
JW
3949 return 0;
3950}
3951
e59058c4 3952/**
da0436e9
JS
3953 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3954 * @phba: Pointer to HBA context object.
3955 *
3956 * This function resets a SLI4 HBA. This function disables PCI layer parity
3957 * checking during resets the device. The caller is not required to hold
3958 * any locks.
3959 *
3960 * This function returns 0 always.
3961 **/
3962int
3963lpfc_sli4_brdreset(struct lpfc_hba *phba)
3964{
3965 struct lpfc_sli *psli = &phba->sli;
3966 uint16_t cfg_value;
27b01b82 3967 int rc;
da0436e9
JS
3968
3969 /* Reset HBA */
3970 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3971 "0295 Reset HBA Data: x%x x%x\n",
3972 phba->pport->port_state, psli->sli_flag);
3973
3974 /* perform board reset */
3975 phba->fc_eventTag = 0;
4d9ab994 3976 phba->link_events = 0;
da0436e9
JS
3977 phba->pport->fc_myDID = 0;
3978 phba->pport->fc_prevDID = 0;
3979
da0436e9
JS
3980 spin_lock_irq(&phba->hbalock);
3981 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3982 phba->fcf.fcf_flag = 0;
da0436e9
JS
3983 spin_unlock_irq(&phba->hbalock);
3984
3985 /* Now physically reset the device */
3986 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3987 "0389 Performing PCI function reset!\n");
be858b65
JS
3988
3989 /* Turn off parity checking and serr during the physical reset */
3990 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3991 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3992 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3993
88318816 3994 /* Perform FCoE PCI function reset before freeing queue memory */
27b01b82 3995 rc = lpfc_pci_function_reset(phba);
88318816 3996 lpfc_sli4_queue_destroy(phba);
da0436e9 3997
be858b65
JS
3998 /* Restore PCI cmd register */
3999 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4000
27b01b82 4001 return rc;
da0436e9
JS
4002}
4003
4004/**
4005 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
4006 * @phba: Pointer to HBA context object.
4007 *
4008 * This function is called in the SLI initialization code path to
4009 * restart the HBA. The caller is not required to hold any lock.
4010 * This function writes MBX_RESTART mailbox command to the SLIM and
4011 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4012 * function to free any pending commands. The function enables
4013 * POST only during the first initialization. The function returns zero.
4014 * The function does not guarantee completion of MBX_RESTART mailbox
4015 * command before the return of this function.
4016 **/
da0436e9
JS
4017static int
4018lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
4019{
4020 MAILBOX_t *mb;
4021 struct lpfc_sli *psli;
41415862
JW
4022 volatile uint32_t word0;
4023 void __iomem *to_slim;
0d878419 4024 uint32_t hba_aer_enabled;
41415862 4025
2e0fef85 4026 spin_lock_irq(&phba->hbalock);
41415862 4027
0d878419
JS
4028 /* Take PCIe device Advanced Error Reporting (AER) state */
4029 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4030
41415862
JW
4031 psli = &phba->sli;
4032
4033 /* Restart HBA */
4034 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4035 "0337 Restart HBA Data: x%x x%x\n",
2e0fef85 4036 phba->pport->port_state, psli->sli_flag);
41415862
JW
4037
4038 word0 = 0;
4039 mb = (MAILBOX_t *) &word0;
4040 mb->mbxCommand = MBX_RESTART;
4041 mb->mbxHc = 1;
4042
9290831f
JS
4043 lpfc_reset_barrier(phba);
4044
41415862
JW
4045 to_slim = phba->MBslimaddr;
4046 writel(*(uint32_t *) mb, to_slim);
4047 readl(to_slim); /* flush */
4048
4049 /* Only skip post after fc_ffinit is completed */
eaf15d5b 4050 if (phba->pport->port_state)
41415862 4051 word0 = 1; /* This is really setting up word1 */
eaf15d5b 4052 else
41415862 4053 word0 = 0; /* This is really setting up word1 */
65a29c16 4054 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
4055 writel(*(uint32_t *) mb, to_slim);
4056 readl(to_slim); /* flush */
dea3101e 4057
41415862 4058 lpfc_sli_brdreset(phba);
2e0fef85
JS
4059 phba->pport->stopped = 0;
4060 phba->link_state = LPFC_INIT_START;
da0436e9 4061 phba->hba_flag = 0;
2e0fef85 4062 spin_unlock_irq(&phba->hbalock);
41415862 4063
64ba8818
JS
4064 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4065 psli->stats_start = get_seconds();
4066
eaf15d5b
JS
4067 /* Give the INITFF and Post time to settle. */
4068 mdelay(100);
41415862 4069
0d878419
JS
4070 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4071 if (hba_aer_enabled)
4072 pci_disable_pcie_error_reporting(phba->pcidev);
4073
41415862 4074 lpfc_hba_down_post(phba);
dea3101e
JB
4075
4076 return 0;
4077}
4078
da0436e9
JS
4079/**
4080 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4081 * @phba: Pointer to HBA context object.
4082 *
4083 * This function is called in the SLI initialization code path to restart
4084 * a SLI4 HBA. The caller is not required to hold any lock.
4085 * At the end of the function, it calls lpfc_hba_down_post function to
4086 * free any pending commands.
4087 **/
4088static int
4089lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4090{
4091 struct lpfc_sli *psli = &phba->sli;
75baf696 4092 uint32_t hba_aer_enabled;
27b01b82 4093 int rc;
da0436e9
JS
4094
4095 /* Restart HBA */
4096 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4097 "0296 Restart HBA Data: x%x x%x\n",
4098 phba->pport->port_state, psli->sli_flag);
4099
75baf696
JS
4100 /* Take PCIe device Advanced Error Reporting (AER) state */
4101 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4102
27b01b82 4103 rc = lpfc_sli4_brdreset(phba);
da0436e9
JS
4104
4105 spin_lock_irq(&phba->hbalock);
4106 phba->pport->stopped = 0;
4107 phba->link_state = LPFC_INIT_START;
4108 phba->hba_flag = 0;
4109 spin_unlock_irq(&phba->hbalock);
4110
4111 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4112 psli->stats_start = get_seconds();
4113
75baf696
JS
4114 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4115 if (hba_aer_enabled)
4116 pci_disable_pcie_error_reporting(phba->pcidev);
4117
da0436e9
JS
4118 lpfc_hba_down_post(phba);
4119
27b01b82 4120 return rc;
da0436e9
JS
4121}
4122
4123/**
4124 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4125 * @phba: Pointer to HBA context object.
4126 *
4127 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4128 * API jump table function pointer from the lpfc_hba struct.
4129**/
4130int
4131lpfc_sli_brdrestart(struct lpfc_hba *phba)
4132{
4133 return phba->lpfc_sli_brdrestart(phba);
4134}
4135
e59058c4 4136/**
3621a710 4137 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
4138 * @phba: Pointer to HBA context object.
4139 *
4140 * This function is called after a HBA restart to wait for successful
4141 * restart of the HBA. Successful restart of the HBA is indicated by
4142 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4143 * iteration, the function will restart the HBA again. The function returns
4144 * zero if HBA successfully restarted else returns negative error code.
4145 **/
dea3101e
JB
4146static int
4147lpfc_sli_chipset_init(struct lpfc_hba *phba)
4148{
4149 uint32_t status, i = 0;
4150
4151 /* Read the HBA Host Status Register */
9940b97b
JS
4152 if (lpfc_readl(phba->HSregaddr, &status))
4153 return -EIO;
dea3101e
JB
4154
4155 /* Check status register to see what current state is */
4156 i = 0;
4157 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4158
dcf2a4e0
JS
4159 /* Check every 10ms for 10 retries, then every 100ms for 90
4160 * retries, then every 1 sec for 50 retires for a total of
4161 * ~60 seconds before reset the board again and check every
4162 * 1 sec for 50 retries. The up to 60 seconds before the
4163 * board ready is required by the Falcon FIPS zeroization
4164 * complete, and any reset the board in between shall cause
4165 * restart of zeroization, further delay the board ready.
dea3101e 4166 */
dcf2a4e0 4167 if (i++ >= 200) {
dea3101e
JB
4168 /* Adapter failed to init, timeout, status reg
4169 <status> */
ed957684 4170 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4171 "0436 Adapter failed to init, "
09372820
JS
4172 "timeout, status reg x%x, "
4173 "FW Data: A8 x%x AC x%x\n", status,
4174 readl(phba->MBslimaddr + 0xa8),
4175 readl(phba->MBslimaddr + 0xac));
2e0fef85 4176 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4177 return -ETIMEDOUT;
4178 }
4179
4180 /* Check to see if any errors occurred during init */
4181 if (status & HS_FFERM) {
4182 /* ERROR: During chipset initialization */
4183 /* Adapter failed to init, chipset, status reg
4184 <status> */
ed957684 4185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4186 "0437 Adapter failed to init, "
09372820
JS
4187 "chipset, status reg x%x, "
4188 "FW Data: A8 x%x AC x%x\n", status,
4189 readl(phba->MBslimaddr + 0xa8),
4190 readl(phba->MBslimaddr + 0xac));
2e0fef85 4191 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4192 return -EIO;
4193 }
4194
dcf2a4e0 4195 if (i <= 10)
dea3101e 4196 msleep(10);
dcf2a4e0
JS
4197 else if (i <= 100)
4198 msleep(100);
4199 else
4200 msleep(1000);
dea3101e 4201
dcf2a4e0
JS
4202 if (i == 150) {
4203 /* Do post */
92d7f7b0 4204 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4205 lpfc_sli_brdrestart(phba);
dea3101e
JB
4206 }
4207 /* Read the HBA Host Status Register */
9940b97b
JS
4208 if (lpfc_readl(phba->HSregaddr, &status))
4209 return -EIO;
dea3101e
JB
4210 }
4211
4212 /* Check to see if any errors occurred during init */
4213 if (status & HS_FFERM) {
4214 /* ERROR: During chipset initialization */
4215 /* Adapter failed to init, chipset, status reg <status> */
ed957684 4216 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4217 "0438 Adapter failed to init, chipset, "
09372820
JS
4218 "status reg x%x, "
4219 "FW Data: A8 x%x AC x%x\n", status,
4220 readl(phba->MBslimaddr + 0xa8),
4221 readl(phba->MBslimaddr + 0xac));
2e0fef85 4222 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4223 return -EIO;
4224 }
4225
4226 /* Clear all interrupt enable conditions */
4227 writel(0, phba->HCregaddr);
4228 readl(phba->HCregaddr); /* flush */
4229
4230 /* setup host attn register */
4231 writel(0xffffffff, phba->HAregaddr);
4232 readl(phba->HAregaddr); /* flush */
4233 return 0;
4234}
4235
e59058c4 4236/**
3621a710 4237 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
4238 *
4239 * This function calculates and returns the number of HBQs required to be
4240 * configured.
4241 **/
78b2d852 4242int
ed957684
JS
4243lpfc_sli_hbq_count(void)
4244{
92d7f7b0 4245 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
4246}
4247
e59058c4 4248/**
3621a710 4249 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
4250 *
4251 * This function adds the number of hbq entries in every HBQ to get
4252 * the total number of hbq entries required for the HBA and returns
4253 * the total count.
4254 **/
ed957684
JS
4255static int
4256lpfc_sli_hbq_entry_count(void)
4257{
4258 int hbq_count = lpfc_sli_hbq_count();
4259 int count = 0;
4260 int i;
4261
4262 for (i = 0; i < hbq_count; ++i)
92d7f7b0 4263 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
4264 return count;
4265}
4266
e59058c4 4267/**
3621a710 4268 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
4269 *
4270 * This function calculates amount of memory required for all hbq entries
4271 * to be configured and returns the total memory required.
4272 **/
dea3101e 4273int
ed957684
JS
4274lpfc_sli_hbq_size(void)
4275{
4276 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4277}
4278
e59058c4 4279/**
3621a710 4280 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
4281 * @phba: Pointer to HBA context object.
4282 *
4283 * This function is called during the SLI initialization to configure
4284 * all the HBQs and post buffers to the HBQ. The caller is not
4285 * required to hold any locks. This function will return zero if successful
4286 * else it will return negative error code.
4287 **/
ed957684
JS
4288static int
4289lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4290{
4291 int hbq_count = lpfc_sli_hbq_count();
4292 LPFC_MBOXQ_t *pmb;
4293 MAILBOX_t *pmbox;
4294 uint32_t hbqno;
4295 uint32_t hbq_entry_index;
ed957684 4296
92d7f7b0
JS
4297 /* Get a Mailbox buffer to setup mailbox
4298 * commands for HBA initialization
4299 */
ed957684
JS
4300 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4301
4302 if (!pmb)
4303 return -ENOMEM;
4304
04c68496 4305 pmbox = &pmb->u.mb;
ed957684
JS
4306
4307 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4308 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 4309 phba->hbq_in_use = 1;
ed957684
JS
4310
4311 hbq_entry_index = 0;
4312 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4313 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4314 phba->hbqs[hbqno].hbqPutIdx = 0;
4315 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4316 phba->hbqs[hbqno].entry_count =
92d7f7b0 4317 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
4318 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4319 hbq_entry_index, pmb);
ed957684
JS
4320 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4321
4322 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4323 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4324 mbxStatus <status>, ring <num> */
4325
4326 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4327 LOG_SLI | LOG_VPORT,
e8b62011 4328 "1805 Adapter failed to init. "
ed957684 4329 "Data: x%x x%x x%x\n",
e8b62011 4330 pmbox->mbxCommand,
ed957684
JS
4331 pmbox->mbxStatus, hbqno);
4332
4333 phba->link_state = LPFC_HBA_ERROR;
4334 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 4335 return -ENXIO;
ed957684
JS
4336 }
4337 }
4338 phba->hbq_count = hbq_count;
4339
ed957684
JS
4340 mempool_free(pmb, phba->mbox_mem_pool);
4341
92d7f7b0 4342 /* Initially populate or replenish the HBQs */
d7c255b2
JS
4343 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4344 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
4345 return 0;
4346}
4347
4f774513
JS
4348/**
4349 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4350 * @phba: Pointer to HBA context object.
4351 *
4352 * This function is called during the SLI initialization to configure
4353 * all the HBQs and post buffers to the HBQ. The caller is not
4354 * required to hold any locks. This function will return zero if successful
4355 * else it will return negative error code.
4356 **/
4357static int
4358lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4359{
4360 phba->hbq_in_use = 1;
4361 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4362 phba->hbq_count = 1;
4363 /* Initially populate or replenish the HBQs */
4364 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4365 return 0;
4366}
4367
e59058c4 4368/**
3621a710 4369 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
4370 * @phba: Pointer to HBA context object.
4371 * @sli_mode: sli mode - 2/3
4372 *
4373 * This function is called by the sli intialization code path
4374 * to issue config_port mailbox command. This function restarts the
4375 * HBA firmware and issues a config_port mailbox command to configure
4376 * the SLI interface in the sli mode specified by sli_mode
4377 * variable. The caller is not required to hold any locks.
4378 * The function returns 0 if successful, else returns negative error
4379 * code.
4380 **/
9399627f
JS
4381int
4382lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e
JB
4383{
4384 LPFC_MBOXQ_t *pmb;
4385 uint32_t resetcount = 0, rc = 0, done = 0;
4386
4387 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4388 if (!pmb) {
2e0fef85 4389 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4390 return -ENOMEM;
4391 }
4392
ed957684 4393 phba->sli_rev = sli_mode;
dea3101e 4394 while (resetcount < 2 && !done) {
2e0fef85 4395 spin_lock_irq(&phba->hbalock);
1c067a42 4396 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4397 spin_unlock_irq(&phba->hbalock);
92d7f7b0 4398 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4399 lpfc_sli_brdrestart(phba);
dea3101e
JB
4400 rc = lpfc_sli_chipset_init(phba);
4401 if (rc)
4402 break;
4403
2e0fef85 4404 spin_lock_irq(&phba->hbalock);
1c067a42 4405 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4406 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4407 resetcount++;
4408
ed957684
JS
4409 /* Call pre CONFIG_PORT mailbox command initialization. A
4410 * value of 0 means the call was successful. Any other
4411 * nonzero value is a failure, but if ERESTART is returned,
4412 * the driver may reset the HBA and try again.
4413 */
dea3101e
JB
4414 rc = lpfc_config_port_prep(phba);
4415 if (rc == -ERESTART) {
ed957684 4416 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 4417 continue;
34b02dcd 4418 } else if (rc)
dea3101e 4419 break;
6d368e53 4420
2e0fef85 4421 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
4422 lpfc_config_port(phba, pmb);
4423 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
4424 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4425 LPFC_SLI3_HBQ_ENABLED |
4426 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
4427 LPFC_SLI3_BG_ENABLED |
4428 LPFC_SLI3_DSS_ENABLED);
ed957684 4429 if (rc != MBX_SUCCESS) {
dea3101e 4430 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4431 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 4432 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 4433 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 4434 spin_lock_irq(&phba->hbalock);
04c68496 4435 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
4436 spin_unlock_irq(&phba->hbalock);
4437 rc = -ENXIO;
04c68496
JS
4438 } else {
4439 /* Allow asynchronous mailbox command to go through */
4440 spin_lock_irq(&phba->hbalock);
4441 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4442 spin_unlock_irq(&phba->hbalock);
ed957684 4443 done = 1;
cb69f7de
JS
4444
4445 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4446 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4447 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4448 "3110 Port did not grant ASABT\n");
04c68496 4449 }
dea3101e 4450 }
ed957684
JS
4451 if (!done) {
4452 rc = -EINVAL;
4453 goto do_prep_failed;
4454 }
04c68496
JS
4455 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4456 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
4457 rc = -ENXIO;
4458 goto do_prep_failed;
4459 }
04c68496 4460 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 4461 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
4462 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4463 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4464 phba->max_vpi : phba->max_vports;
4465
34b02dcd
JS
4466 } else
4467 phba->max_vpi = 0;
bc73905a
JS
4468 phba->fips_level = 0;
4469 phba->fips_spec_rev = 0;
4470 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 4471 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
4472 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4473 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4474 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4475 "2850 Security Crypto Active. FIPS x%d "
4476 "(Spec Rev: x%d)",
4477 phba->fips_level, phba->fips_spec_rev);
4478 }
4479 if (pmb->u.mb.un.varCfgPort.sec_err) {
4480 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4481 "2856 Config Port Security Crypto "
4482 "Error: x%x ",
4483 pmb->u.mb.un.varCfgPort.sec_err);
4484 }
04c68496 4485 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 4486 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 4487 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 4488 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
4489
4490 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4491 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6
JS
4492
4493 if (phba->cfg_enable_bg) {
04c68496 4494 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
4495 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4496 else
4497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4498 "0443 Adapter did not grant "
4499 "BlockGuard\n");
4500 }
34b02dcd 4501 } else {
8f34f4ce 4502 phba->hbq_get = NULL;
34b02dcd 4503 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 4504 phba->max_vpi = 0;
ed957684 4505 }
92d7f7b0 4506do_prep_failed:
ed957684
JS
4507 mempool_free(pmb, phba->mbox_mem_pool);
4508 return rc;
4509}
4510
e59058c4
JS
4511
4512/**
3621a710 4513 * lpfc_sli_hba_setup - SLI intialization function
e59058c4
JS
4514 * @phba: Pointer to HBA context object.
4515 *
4516 * This function is the main SLI intialization function. This function
4517 * is called by the HBA intialization code, HBA reset code and HBA
4518 * error attention handler code. Caller is not required to hold any
4519 * locks. This function issues config_port mailbox command to configure
4520 * the SLI, setup iocb rings and HBQ rings. In the end the function
4521 * calls the config_port_post function to issue init_link mailbox
4522 * command and to start the discovery. The function will return zero
4523 * if successful, else it will return negative error code.
4524 **/
ed957684
JS
4525int
4526lpfc_sli_hba_setup(struct lpfc_hba *phba)
4527{
4528 uint32_t rc;
6d368e53
JS
4529 int mode = 3, i;
4530 int longs;
ed957684
JS
4531
4532 switch (lpfc_sli_mode) {
4533 case 2:
78b2d852 4534 if (phba->cfg_enable_npiv) {
92d7f7b0 4535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011 4536 "1824 NPIV enabled: Override lpfc_sli_mode "
92d7f7b0 4537 "parameter (%d) to auto (0).\n",
e8b62011 4538 lpfc_sli_mode);
92d7f7b0
JS
4539 break;
4540 }
ed957684
JS
4541 mode = 2;
4542 break;
4543 case 0:
4544 case 3:
4545 break;
4546 default:
92d7f7b0 4547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4548 "1819 Unrecognized lpfc_sli_mode "
4549 "parameter: %d.\n", lpfc_sli_mode);
ed957684
JS
4550
4551 break;
4552 }
4553
9399627f
JS
4554 rc = lpfc_sli_config_port(phba, mode);
4555
ed957684 4556 if (rc && lpfc_sli_mode == 3)
92d7f7b0 4557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4558 "1820 Unable to select SLI-3. "
4559 "Not supported by adapter.\n");
ed957684 4560 if (rc && mode != 2)
9399627f 4561 rc = lpfc_sli_config_port(phba, 2);
ed957684 4562 if (rc)
dea3101e
JB
4563 goto lpfc_sli_hba_setup_error;
4564
0d878419
JS
4565 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4566 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4567 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4568 if (!rc) {
4569 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4570 "2709 This device supports "
4571 "Advanced Error Reporting (AER)\n");
4572 spin_lock_irq(&phba->hbalock);
4573 phba->hba_flag |= HBA_AER_ENABLED;
4574 spin_unlock_irq(&phba->hbalock);
4575 } else {
4576 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4577 "2708 This device does not support "
4578 "Advanced Error Reporting (AER)\n");
4579 phba->cfg_aer_support = 0;
4580 }
4581 }
4582
ed957684
JS
4583 if (phba->sli_rev == 3) {
4584 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4585 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4586 } else {
4587 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4588 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4589 phba->sli3_options = 0;
ed957684
JS
4590 }
4591
4592 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4593 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4594 phba->sli_rev, phba->max_vpi);
ed957684 4595 rc = lpfc_sli_ring_map(phba);
dea3101e
JB
4596
4597 if (rc)
4598 goto lpfc_sli_hba_setup_error;
4599
6d368e53
JS
4600 /* Initialize VPIs. */
4601 if (phba->sli_rev == LPFC_SLI_REV3) {
4602 /*
4603 * The VPI bitmask and physical ID array are allocated
4604 * and initialized once only - at driver load. A port
4605 * reset doesn't need to reinitialize this memory.
4606 */
4607 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4608 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4609 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4610 GFP_KERNEL);
4611 if (!phba->vpi_bmask) {
4612 rc = -ENOMEM;
4613 goto lpfc_sli_hba_setup_error;
4614 }
4615
4616 phba->vpi_ids = kzalloc(
4617 (phba->max_vpi+1) * sizeof(uint16_t),
4618 GFP_KERNEL);
4619 if (!phba->vpi_ids) {
4620 kfree(phba->vpi_bmask);
4621 rc = -ENOMEM;
4622 goto lpfc_sli_hba_setup_error;
4623 }
4624 for (i = 0; i < phba->max_vpi; i++)
4625 phba->vpi_ids[i] = i;
4626 }
4627 }
4628
9399627f 4629 /* Init HBQs */
ed957684
JS
4630 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4631 rc = lpfc_sli_hbq_setup(phba);
4632 if (rc)
4633 goto lpfc_sli_hba_setup_error;
4634 }
04c68496 4635 spin_lock_irq(&phba->hbalock);
dea3101e 4636 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4637 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4638
4639 rc = lpfc_config_port_post(phba);
4640 if (rc)
4641 goto lpfc_sli_hba_setup_error;
4642
ed957684
JS
4643 return rc;
4644
92d7f7b0 4645lpfc_sli_hba_setup_error:
2e0fef85 4646 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 4647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4648 "0445 Firmware initialization failed\n");
dea3101e
JB
4649 return rc;
4650}
4651
e59058c4 4652/**
da0436e9
JS
4653 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4654 * @phba: Pointer to HBA context object.
4655 * @mboxq: mailbox pointer.
4656 * This function issue a dump mailbox command to read config region
4657 * 23 and parse the records in the region and populate driver
4658 * data structure.
e59058c4 4659 **/
da0436e9 4660static int
ff78d8f9 4661lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
dea3101e 4662{
ff78d8f9 4663 LPFC_MBOXQ_t *mboxq;
da0436e9
JS
4664 struct lpfc_dmabuf *mp;
4665 struct lpfc_mqe *mqe;
4666 uint32_t data_length;
4667 int rc;
dea3101e 4668
da0436e9
JS
4669 /* Program the default value of vlan_id and fc_map */
4670 phba->valid_vlan = 0;
4671 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4672 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4673 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4674
ff78d8f9
JS
4675 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4676 if (!mboxq)
da0436e9
JS
4677 return -ENOMEM;
4678
ff78d8f9
JS
4679 mqe = &mboxq->u.mqe;
4680 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4681 rc = -ENOMEM;
4682 goto out_free_mboxq;
4683 }
4684
da0436e9
JS
4685 mp = (struct lpfc_dmabuf *) mboxq->context1;
4686 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4687
4688 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4689 "(%d):2571 Mailbox cmd x%x Status x%x "
4690 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4691 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4692 "CQ: x%x x%x x%x x%x\n",
4693 mboxq->vport ? mboxq->vport->vpi : 0,
4694 bf_get(lpfc_mqe_command, mqe),
4695 bf_get(lpfc_mqe_status, mqe),
4696 mqe->un.mb_words[0], mqe->un.mb_words[1],
4697 mqe->un.mb_words[2], mqe->un.mb_words[3],
4698 mqe->un.mb_words[4], mqe->un.mb_words[5],
4699 mqe->un.mb_words[6], mqe->un.mb_words[7],
4700 mqe->un.mb_words[8], mqe->un.mb_words[9],
4701 mqe->un.mb_words[10], mqe->un.mb_words[11],
4702 mqe->un.mb_words[12], mqe->un.mb_words[13],
4703 mqe->un.mb_words[14], mqe->un.mb_words[15],
4704 mqe->un.mb_words[16], mqe->un.mb_words[50],
4705 mboxq->mcqe.word0,
4706 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4707 mboxq->mcqe.trailer);
4708
4709 if (rc) {
4710 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4711 kfree(mp);
ff78d8f9
JS
4712 rc = -EIO;
4713 goto out_free_mboxq;
da0436e9
JS
4714 }
4715 data_length = mqe->un.mb_words[5];
a0c87cbd 4716 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
4717 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4718 kfree(mp);
ff78d8f9
JS
4719 rc = -EIO;
4720 goto out_free_mboxq;
d11e31dd 4721 }
dea3101e 4722
da0436e9
JS
4723 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4724 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4725 kfree(mp);
ff78d8f9
JS
4726 rc = 0;
4727
4728out_free_mboxq:
4729 mempool_free(mboxq, phba->mbox_mem_pool);
4730 return rc;
da0436e9 4731}
e59058c4
JS
4732
4733/**
da0436e9
JS
4734 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4735 * @phba: pointer to lpfc hba data structure.
4736 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4737 * @vpd: pointer to the memory to hold resulting port vpd data.
4738 * @vpd_size: On input, the number of bytes allocated to @vpd.
4739 * On output, the number of data bytes in @vpd.
e59058c4 4740 *
da0436e9
JS
4741 * This routine executes a READ_REV SLI4 mailbox command. In
4742 * addition, this routine gets the port vpd data.
4743 *
4744 * Return codes
af901ca1 4745 * 0 - successful
d439d286 4746 * -ENOMEM - could not allocated memory.
e59058c4 4747 **/
da0436e9
JS
4748static int
4749lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4750 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 4751{
da0436e9
JS
4752 int rc = 0;
4753 uint32_t dma_size;
4754 struct lpfc_dmabuf *dmabuf;
4755 struct lpfc_mqe *mqe;
dea3101e 4756
da0436e9
JS
4757 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4758 if (!dmabuf)
4759 return -ENOMEM;
4760
4761 /*
4762 * Get a DMA buffer for the vpd data resulting from the READ_REV
4763 * mailbox command.
a257bf90 4764 */
da0436e9
JS
4765 dma_size = *vpd_size;
4766 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4767 dma_size,
4768 &dmabuf->phys,
4769 GFP_KERNEL);
4770 if (!dmabuf->virt) {
4771 kfree(dmabuf);
4772 return -ENOMEM;
a257bf90 4773 }
da0436e9 4774 memset(dmabuf->virt, 0, dma_size);
a257bf90 4775
da0436e9
JS
4776 /*
4777 * The SLI4 implementation of READ_REV conflicts at word1,
4778 * bits 31:16 and SLI4 adds vpd functionality not present
4779 * in SLI3. This code corrects the conflicts.
1dcb58e5 4780 */
da0436e9
JS
4781 lpfc_read_rev(phba, mboxq);
4782 mqe = &mboxq->u.mqe;
4783 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4784 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4785 mqe->un.read_rev.word1 &= 0x0000FFFF;
4786 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4787 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4788
4789 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4790 if (rc) {
4791 dma_free_coherent(&phba->pcidev->dev, dma_size,
4792 dmabuf->virt, dmabuf->phys);
def9c7a9 4793 kfree(dmabuf);
da0436e9
JS
4794 return -EIO;
4795 }
1dcb58e5 4796
da0436e9
JS
4797 /*
4798 * The available vpd length cannot be bigger than the
4799 * DMA buffer passed to the port. Catch the less than
4800 * case and update the caller's size.
4801 */
4802 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4803 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 4804
d7c47992
JS
4805 memcpy(vpd, dmabuf->virt, *vpd_size);
4806
da0436e9
JS
4807 dma_free_coherent(&phba->pcidev->dev, dma_size,
4808 dmabuf->virt, dmabuf->phys);
4809 kfree(dmabuf);
4810 return 0;
dea3101e
JB
4811}
4812
cd1c8301
JS
4813/**
4814 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4815 * @phba: pointer to lpfc hba data structure.
4816 *
4817 * This routine retrieves SLI4 device physical port name this PCI function
4818 * is attached to.
4819 *
4820 * Return codes
4907cb7b 4821 * 0 - successful
cd1c8301
JS
4822 * otherwise - failed to retrieve physical port name
4823 **/
4824static int
4825lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4826{
4827 LPFC_MBOXQ_t *mboxq;
cd1c8301
JS
4828 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4829 struct lpfc_controller_attribute *cntl_attr;
4830 struct lpfc_mbx_get_port_name *get_port_name;
4831 void *virtaddr = NULL;
4832 uint32_t alloclen, reqlen;
4833 uint32_t shdr_status, shdr_add_status;
4834 union lpfc_sli4_cfg_shdr *shdr;
4835 char cport_name = 0;
4836 int rc;
4837
4838 /* We assume nothing at this point */
4839 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4840 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4841
4842 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4843 if (!mboxq)
4844 return -ENOMEM;
cd1c8301 4845 /* obtain link type and link number via READ_CONFIG */
ff78d8f9
JS
4846 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4847 lpfc_sli4_read_config(phba);
4848 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4849 goto retrieve_ppname;
cd1c8301
JS
4850
4851 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4852 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4853 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4854 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4855 LPFC_SLI4_MBX_NEMBED);
4856 if (alloclen < reqlen) {
4857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4858 "3084 Allocated DMA memory size (%d) is "
4859 "less than the requested DMA memory size "
4860 "(%d)\n", alloclen, reqlen);
4861 rc = -ENOMEM;
4862 goto out_free_mboxq;
4863 }
4864 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4865 virtaddr = mboxq->sge_array->addr[0];
4866 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4867 shdr = &mbx_cntl_attr->cfg_shdr;
4868 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4869 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4870 if (shdr_status || shdr_add_status || rc) {
4871 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4872 "3085 Mailbox x%x (x%x/x%x) failed, "
4873 "rc:x%x, status:x%x, add_status:x%x\n",
4874 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4875 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4876 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4877 rc, shdr_status, shdr_add_status);
4878 rc = -ENXIO;
4879 goto out_free_mboxq;
4880 }
4881 cntl_attr = &mbx_cntl_attr->cntl_attr;
4882 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4883 phba->sli4_hba.lnk_info.lnk_tp =
4884 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4885 phba->sli4_hba.lnk_info.lnk_no =
4886 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4887 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4888 "3086 lnk_type:%d, lnk_numb:%d\n",
4889 phba->sli4_hba.lnk_info.lnk_tp,
4890 phba->sli4_hba.lnk_info.lnk_no);
4891
4892retrieve_ppname:
4893 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4894 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4895 sizeof(struct lpfc_mbx_get_port_name) -
4896 sizeof(struct lpfc_sli4_cfg_mhdr),
4897 LPFC_SLI4_MBX_EMBED);
4898 get_port_name = &mboxq->u.mqe.un.get_port_name;
4899 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4900 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4901 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4902 phba->sli4_hba.lnk_info.lnk_tp);
4903 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4904 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4905 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4906 if (shdr_status || shdr_add_status || rc) {
4907 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4908 "3087 Mailbox x%x (x%x/x%x) failed: "
4909 "rc:x%x, status:x%x, add_status:x%x\n",
4910 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4911 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4912 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4913 rc, shdr_status, shdr_add_status);
4914 rc = -ENXIO;
4915 goto out_free_mboxq;
4916 }
4917 switch (phba->sli4_hba.lnk_info.lnk_no) {
4918 case LPFC_LINK_NUMBER_0:
4919 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4920 &get_port_name->u.response);
4921 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4922 break;
4923 case LPFC_LINK_NUMBER_1:
4924 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4925 &get_port_name->u.response);
4926 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4927 break;
4928 case LPFC_LINK_NUMBER_2:
4929 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4930 &get_port_name->u.response);
4931 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4932 break;
4933 case LPFC_LINK_NUMBER_3:
4934 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4935 &get_port_name->u.response);
4936 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4937 break;
4938 default:
4939 break;
4940 }
4941
4942 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4943 phba->Port[0] = cport_name;
4944 phba->Port[1] = '\0';
4945 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4946 "3091 SLI get port name: %s\n", phba->Port);
4947 }
4948
4949out_free_mboxq:
4950 if (rc != MBX_TIMEOUT) {
4951 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4952 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4953 else
4954 mempool_free(mboxq, phba->mbox_mem_pool);
4955 }
4956 return rc;
4957}
4958
e59058c4 4959/**
da0436e9
JS
4960 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4961 * @phba: pointer to lpfc hba data structure.
e59058c4 4962 *
da0436e9
JS
4963 * This routine is called to explicitly arm the SLI4 device's completion and
4964 * event queues
4965 **/
4966static void
4967lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4968{
962bc51b 4969 int fcp_eqidx;
da0436e9
JS
4970
4971 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4972 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
0558056c 4973 fcp_eqidx = 0;
2e90f4b5 4974 if (phba->sli4_hba.fcp_cq) {
67d12733 4975 do {
2e90f4b5
JS
4976 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4977 LPFC_QUEUE_REARM);
67d12733 4978 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
2e90f4b5 4979 }
67d12733
JS
4980 if (phba->sli4_hba.hba_eq) {
4981 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
2e90f4b5 4982 fcp_eqidx++)
67d12733 4983 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
2e90f4b5
JS
4984 LPFC_QUEUE_REARM);
4985 }
da0436e9
JS
4986}
4987
6d368e53
JS
4988/**
4989 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4990 * @phba: Pointer to HBA context object.
4991 * @type: The resource extent type.
b76f2dc9
JS
4992 * @extnt_count: buffer to hold port available extent count.
4993 * @extnt_size: buffer to hold element count per extent.
6d368e53 4994 *
b76f2dc9
JS
4995 * This function calls the port and retrievs the number of available
4996 * extents and their size for a particular extent type.
4997 *
4998 * Returns: 0 if successful. Nonzero otherwise.
6d368e53 4999 **/
b76f2dc9 5000int
6d368e53
JS
5001lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5002 uint16_t *extnt_count, uint16_t *extnt_size)
5003{
5004 int rc = 0;
5005 uint32_t length;
5006 uint32_t mbox_tmo;
5007 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5008 LPFC_MBOXQ_t *mbox;
5009
5010 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5011 if (!mbox)
5012 return -ENOMEM;
5013
5014 /* Find out how many extents are available for this resource type */
5015 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5016 sizeof(struct lpfc_sli4_cfg_mhdr));
5017 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5018 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5019 length, LPFC_SLI4_MBX_EMBED);
5020
5021 /* Send an extents count of 0 - the GET doesn't use it. */
5022 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5023 LPFC_SLI4_MBX_EMBED);
5024 if (unlikely(rc)) {
5025 rc = -EIO;
5026 goto err_exit;
5027 }
5028
5029 if (!phba->sli4_hba.intr_enable)
5030 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5031 else {
a183a15f 5032 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5033 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5034 }
5035 if (unlikely(rc)) {
5036 rc = -EIO;
5037 goto err_exit;
5038 }
5039
5040 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5041 if (bf_get(lpfc_mbox_hdr_status,
5042 &rsrc_info->header.cfg_shdr.response)) {
5043 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5044 "2930 Failed to get resource extents "
5045 "Status 0x%x Add'l Status 0x%x\n",
5046 bf_get(lpfc_mbox_hdr_status,
5047 &rsrc_info->header.cfg_shdr.response),
5048 bf_get(lpfc_mbox_hdr_add_status,
5049 &rsrc_info->header.cfg_shdr.response));
5050 rc = -EIO;
5051 goto err_exit;
5052 }
5053
5054 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5055 &rsrc_info->u.rsp);
5056 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5057 &rsrc_info->u.rsp);
8a9d2e80
JS
5058
5059 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5060 "3162 Retrieved extents type-%d from port: count:%d, "
5061 "size:%d\n", type, *extnt_count, *extnt_size);
5062
5063err_exit:
6d368e53
JS
5064 mempool_free(mbox, phba->mbox_mem_pool);
5065 return rc;
5066}
5067
5068/**
5069 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5070 * @phba: Pointer to HBA context object.
5071 * @type: The extent type to check.
5072 *
5073 * This function reads the current available extents from the port and checks
5074 * if the extent count or extent size has changed since the last access.
5075 * Callers use this routine post port reset to understand if there is a
5076 * extent reprovisioning requirement.
5077 *
5078 * Returns:
5079 * -Error: error indicates problem.
5080 * 1: Extent count or size has changed.
5081 * 0: No changes.
5082 **/
5083static int
5084lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5085{
5086 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5087 uint16_t size_diff, rsrc_ext_size;
5088 int rc = 0;
5089 struct lpfc_rsrc_blks *rsrc_entry;
5090 struct list_head *rsrc_blk_list = NULL;
5091
5092 size_diff = 0;
5093 curr_ext_cnt = 0;
5094 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5095 &rsrc_ext_cnt,
5096 &rsrc_ext_size);
5097 if (unlikely(rc))
5098 return -EIO;
5099
5100 switch (type) {
5101 case LPFC_RSC_TYPE_FCOE_RPI:
5102 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5103 break;
5104 case LPFC_RSC_TYPE_FCOE_VPI:
5105 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5106 break;
5107 case LPFC_RSC_TYPE_FCOE_XRI:
5108 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5109 break;
5110 case LPFC_RSC_TYPE_FCOE_VFI:
5111 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5112 break;
5113 default:
5114 break;
5115 }
5116
5117 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5118 curr_ext_cnt++;
5119 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5120 size_diff++;
5121 }
5122
5123 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5124 rc = 1;
5125
5126 return rc;
5127}
5128
5129/**
5130 * lpfc_sli4_cfg_post_extnts -
5131 * @phba: Pointer to HBA context object.
5132 * @extnt_cnt - number of available extents.
5133 * @type - the extent type (rpi, xri, vfi, vpi).
5134 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5135 * @mbox - pointer to the caller's allocated mailbox structure.
5136 *
5137 * This function executes the extents allocation request. It also
5138 * takes care of the amount of memory needed to allocate or get the
5139 * allocated extents. It is the caller's responsibility to evaluate
5140 * the response.
5141 *
5142 * Returns:
5143 * -Error: Error value describes the condition found.
5144 * 0: if successful
5145 **/
5146static int
8a9d2e80 5147lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6d368e53
JS
5148 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5149{
5150 int rc = 0;
5151 uint32_t req_len;
5152 uint32_t emb_len;
5153 uint32_t alloc_len, mbox_tmo;
5154
5155 /* Calculate the total requested length of the dma memory */
8a9d2e80 5156 req_len = extnt_cnt * sizeof(uint16_t);
6d368e53
JS
5157
5158 /*
5159 * Calculate the size of an embedded mailbox. The uint32_t
5160 * accounts for extents-specific word.
5161 */
5162 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5163 sizeof(uint32_t);
5164
5165 /*
5166 * Presume the allocation and response will fit into an embedded
5167 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5168 */
5169 *emb = LPFC_SLI4_MBX_EMBED;
5170 if (req_len > emb_len) {
8a9d2e80 5171 req_len = extnt_cnt * sizeof(uint16_t) +
6d368e53
JS
5172 sizeof(union lpfc_sli4_cfg_shdr) +
5173 sizeof(uint32_t);
5174 *emb = LPFC_SLI4_MBX_NEMBED;
5175 }
5176
5177 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5178 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5179 req_len, *emb);
5180 if (alloc_len < req_len) {
5181 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
b76f2dc9 5182 "2982 Allocated DMA memory size (x%x) is "
6d368e53
JS
5183 "less than the requested DMA memory "
5184 "size (x%x)\n", alloc_len, req_len);
5185 return -ENOMEM;
5186 }
8a9d2e80 5187 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6d368e53
JS
5188 if (unlikely(rc))
5189 return -EIO;
5190
5191 if (!phba->sli4_hba.intr_enable)
5192 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5193 else {
a183a15f 5194 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5195 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5196 }
5197
5198 if (unlikely(rc))
5199 rc = -EIO;
5200 return rc;
5201}
5202
5203/**
5204 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5205 * @phba: Pointer to HBA context object.
5206 * @type: The resource extent type to allocate.
5207 *
5208 * This function allocates the number of elements for the specified
5209 * resource type.
5210 **/
5211static int
5212lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5213{
5214 bool emb = false;
5215 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5216 uint16_t rsrc_id, rsrc_start, j, k;
5217 uint16_t *ids;
5218 int i, rc;
5219 unsigned long longs;
5220 unsigned long *bmask;
5221 struct lpfc_rsrc_blks *rsrc_blks;
5222 LPFC_MBOXQ_t *mbox;
5223 uint32_t length;
5224 struct lpfc_id_range *id_array = NULL;
5225 void *virtaddr = NULL;
5226 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5227 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5228 struct list_head *ext_blk_list;
5229
5230 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5231 &rsrc_cnt,
5232 &rsrc_size);
5233 if (unlikely(rc))
5234 return -EIO;
5235
5236 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5237 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5238 "3009 No available Resource Extents "
5239 "for resource type 0x%x: Count: 0x%x, "
5240 "Size 0x%x\n", type, rsrc_cnt,
5241 rsrc_size);
5242 return -ENOMEM;
5243 }
5244
8a9d2e80
JS
5245 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5246 "2903 Post resource extents type-0x%x: "
5247 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6d368e53
JS
5248
5249 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5250 if (!mbox)
5251 return -ENOMEM;
5252
8a9d2e80 5253 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6d368e53
JS
5254 if (unlikely(rc)) {
5255 rc = -EIO;
5256 goto err_exit;
5257 }
5258
5259 /*
5260 * Figure out where the response is located. Then get local pointers
5261 * to the response data. The port does not guarantee to respond to
5262 * all extents counts request so update the local variable with the
5263 * allocated count from the port.
5264 */
5265 if (emb == LPFC_SLI4_MBX_EMBED) {
5266 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5267 id_array = &rsrc_ext->u.rsp.id[0];
5268 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5269 } else {
5270 virtaddr = mbox->sge_array->addr[0];
5271 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5272 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5273 id_array = &n_rsrc->id;
5274 }
5275
5276 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5277 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5278
5279 /*
5280 * Based on the resource size and count, correct the base and max
5281 * resource values.
5282 */
5283 length = sizeof(struct lpfc_rsrc_blks);
5284 switch (type) {
5285 case LPFC_RSC_TYPE_FCOE_RPI:
5286 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5287 sizeof(unsigned long),
5288 GFP_KERNEL);
5289 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5290 rc = -ENOMEM;
5291 goto err_exit;
5292 }
5293 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5294 sizeof(uint16_t),
5295 GFP_KERNEL);
5296 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5297 kfree(phba->sli4_hba.rpi_bmask);
5298 rc = -ENOMEM;
5299 goto err_exit;
5300 }
5301
5302 /*
5303 * The next_rpi was initialized with the maximum available
5304 * count but the port may allocate a smaller number. Catch
5305 * that case and update the next_rpi.
5306 */
5307 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5308
5309 /* Initialize local ptrs for common extent processing later. */
5310 bmask = phba->sli4_hba.rpi_bmask;
5311 ids = phba->sli4_hba.rpi_ids;
5312 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5313 break;
5314 case LPFC_RSC_TYPE_FCOE_VPI:
5315 phba->vpi_bmask = kzalloc(longs *
5316 sizeof(unsigned long),
5317 GFP_KERNEL);
5318 if (unlikely(!phba->vpi_bmask)) {
5319 rc = -ENOMEM;
5320 goto err_exit;
5321 }
5322 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5323 sizeof(uint16_t),
5324 GFP_KERNEL);
5325 if (unlikely(!phba->vpi_ids)) {
5326 kfree(phba->vpi_bmask);
5327 rc = -ENOMEM;
5328 goto err_exit;
5329 }
5330
5331 /* Initialize local ptrs for common extent processing later. */
5332 bmask = phba->vpi_bmask;
5333 ids = phba->vpi_ids;
5334 ext_blk_list = &phba->lpfc_vpi_blk_list;
5335 break;
5336 case LPFC_RSC_TYPE_FCOE_XRI:
5337 phba->sli4_hba.xri_bmask = kzalloc(longs *
5338 sizeof(unsigned long),
5339 GFP_KERNEL);
5340 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5341 rc = -ENOMEM;
5342 goto err_exit;
5343 }
8a9d2e80 5344 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
5345 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5346 sizeof(uint16_t),
5347 GFP_KERNEL);
5348 if (unlikely(!phba->sli4_hba.xri_ids)) {
5349 kfree(phba->sli4_hba.xri_bmask);
5350 rc = -ENOMEM;
5351 goto err_exit;
5352 }
5353
5354 /* Initialize local ptrs for common extent processing later. */
5355 bmask = phba->sli4_hba.xri_bmask;
5356 ids = phba->sli4_hba.xri_ids;
5357 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5358 break;
5359 case LPFC_RSC_TYPE_FCOE_VFI:
5360 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5361 sizeof(unsigned long),
5362 GFP_KERNEL);
5363 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5364 rc = -ENOMEM;
5365 goto err_exit;
5366 }
5367 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5368 sizeof(uint16_t),
5369 GFP_KERNEL);
5370 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5371 kfree(phba->sli4_hba.vfi_bmask);
5372 rc = -ENOMEM;
5373 goto err_exit;
5374 }
5375
5376 /* Initialize local ptrs for common extent processing later. */
5377 bmask = phba->sli4_hba.vfi_bmask;
5378 ids = phba->sli4_hba.vfi_ids;
5379 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5380 break;
5381 default:
5382 /* Unsupported Opcode. Fail call. */
5383 id_array = NULL;
5384 bmask = NULL;
5385 ids = NULL;
5386 ext_blk_list = NULL;
5387 goto err_exit;
5388 }
5389
5390 /*
5391 * Complete initializing the extent configuration with the
5392 * allocated ids assigned to this function. The bitmask serves
5393 * as an index into the array and manages the available ids. The
5394 * array just stores the ids communicated to the port via the wqes.
5395 */
5396 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5397 if ((i % 2) == 0)
5398 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5399 &id_array[k]);
5400 else
5401 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5402 &id_array[k]);
5403
5404 rsrc_blks = kzalloc(length, GFP_KERNEL);
5405 if (unlikely(!rsrc_blks)) {
5406 rc = -ENOMEM;
5407 kfree(bmask);
5408 kfree(ids);
5409 goto err_exit;
5410 }
5411 rsrc_blks->rsrc_start = rsrc_id;
5412 rsrc_blks->rsrc_size = rsrc_size;
5413 list_add_tail(&rsrc_blks->list, ext_blk_list);
5414 rsrc_start = rsrc_id;
5415 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5416 phba->sli4_hba.scsi_xri_start = rsrc_start +
5417 lpfc_sli4_get_els_iocb_cnt(phba);
5418
5419 while (rsrc_id < (rsrc_start + rsrc_size)) {
5420 ids[j] = rsrc_id;
5421 rsrc_id++;
5422 j++;
5423 }
5424 /* Entire word processed. Get next word.*/
5425 if ((i % 2) == 1)
5426 k++;
5427 }
5428 err_exit:
5429 lpfc_sli4_mbox_cmd_free(phba, mbox);
5430 return rc;
5431}
5432
5433/**
5434 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5435 * @phba: Pointer to HBA context object.
5436 * @type: the extent's type.
5437 *
5438 * This function deallocates all extents of a particular resource type.
5439 * SLI4 does not allow for deallocating a particular extent range. It
5440 * is the caller's responsibility to release all kernel memory resources.
5441 **/
5442static int
5443lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5444{
5445 int rc;
5446 uint32_t length, mbox_tmo = 0;
5447 LPFC_MBOXQ_t *mbox;
5448 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5449 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5450
5451 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5452 if (!mbox)
5453 return -ENOMEM;
5454
5455 /*
5456 * This function sends an embedded mailbox because it only sends the
5457 * the resource type. All extents of this type are released by the
5458 * port.
5459 */
5460 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5461 sizeof(struct lpfc_sli4_cfg_mhdr));
5462 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5463 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5464 length, LPFC_SLI4_MBX_EMBED);
5465
5466 /* Send an extents count of 0 - the dealloc doesn't use it. */
5467 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5468 LPFC_SLI4_MBX_EMBED);
5469 if (unlikely(rc)) {
5470 rc = -EIO;
5471 goto out_free_mbox;
5472 }
5473 if (!phba->sli4_hba.intr_enable)
5474 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5475 else {
a183a15f 5476 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5477 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5478 }
5479 if (unlikely(rc)) {
5480 rc = -EIO;
5481 goto out_free_mbox;
5482 }
5483
5484 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5485 if (bf_get(lpfc_mbox_hdr_status,
5486 &dealloc_rsrc->header.cfg_shdr.response)) {
5487 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5488 "2919 Failed to release resource extents "
5489 "for type %d - Status 0x%x Add'l Status 0x%x. "
5490 "Resource memory not released.\n",
5491 type,
5492 bf_get(lpfc_mbox_hdr_status,
5493 &dealloc_rsrc->header.cfg_shdr.response),
5494 bf_get(lpfc_mbox_hdr_add_status,
5495 &dealloc_rsrc->header.cfg_shdr.response));
5496 rc = -EIO;
5497 goto out_free_mbox;
5498 }
5499
5500 /* Release kernel memory resources for the specific type. */
5501 switch (type) {
5502 case LPFC_RSC_TYPE_FCOE_VPI:
5503 kfree(phba->vpi_bmask);
5504 kfree(phba->vpi_ids);
5505 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5506 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5507 &phba->lpfc_vpi_blk_list, list) {
5508 list_del_init(&rsrc_blk->list);
5509 kfree(rsrc_blk);
5510 }
5511 break;
5512 case LPFC_RSC_TYPE_FCOE_XRI:
5513 kfree(phba->sli4_hba.xri_bmask);
5514 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
5515 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5516 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5517 list_del_init(&rsrc_blk->list);
5518 kfree(rsrc_blk);
5519 }
5520 break;
5521 case LPFC_RSC_TYPE_FCOE_VFI:
5522 kfree(phba->sli4_hba.vfi_bmask);
5523 kfree(phba->sli4_hba.vfi_ids);
5524 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5525 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5526 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5527 list_del_init(&rsrc_blk->list);
5528 kfree(rsrc_blk);
5529 }
5530 break;
5531 case LPFC_RSC_TYPE_FCOE_RPI:
5532 /* RPI bitmask and physical id array are cleaned up earlier. */
5533 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5534 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5535 list_del_init(&rsrc_blk->list);
5536 kfree(rsrc_blk);
5537 }
5538 break;
5539 default:
5540 break;
5541 }
5542
5543 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5544
5545 out_free_mbox:
5546 mempool_free(mbox, phba->mbox_mem_pool);
5547 return rc;
5548}
5549
5550/**
5551 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5552 * @phba: Pointer to HBA context object.
5553 *
5554 * This function allocates all SLI4 resource identifiers.
5555 **/
5556int
5557lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5558{
5559 int i, rc, error = 0;
5560 uint16_t count, base;
5561 unsigned long longs;
5562
ff78d8f9
JS
5563 if (!phba->sli4_hba.rpi_hdrs_in_use)
5564 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6d368e53
JS
5565 if (phba->sli4_hba.extents_in_use) {
5566 /*
5567 * The port supports resource extents. The XRI, VPI, VFI, RPI
5568 * resource extent count must be read and allocated before
5569 * provisioning the resource id arrays.
5570 */
5571 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5572 LPFC_IDX_RSRC_RDY) {
5573 /*
5574 * Extent-based resources are set - the driver could
5575 * be in a port reset. Figure out if any corrective
5576 * actions need to be taken.
5577 */
5578 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5579 LPFC_RSC_TYPE_FCOE_VFI);
5580 if (rc != 0)
5581 error++;
5582 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5583 LPFC_RSC_TYPE_FCOE_VPI);
5584 if (rc != 0)
5585 error++;
5586 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5587 LPFC_RSC_TYPE_FCOE_XRI);
5588 if (rc != 0)
5589 error++;
5590 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5591 LPFC_RSC_TYPE_FCOE_RPI);
5592 if (rc != 0)
5593 error++;
5594
5595 /*
5596 * It's possible that the number of resources
5597 * provided to this port instance changed between
5598 * resets. Detect this condition and reallocate
5599 * resources. Otherwise, there is no action.
5600 */
5601 if (error) {
5602 lpfc_printf_log(phba, KERN_INFO,
5603 LOG_MBOX | LOG_INIT,
5604 "2931 Detected extent resource "
5605 "change. Reallocating all "
5606 "extents.\n");
5607 rc = lpfc_sli4_dealloc_extent(phba,
5608 LPFC_RSC_TYPE_FCOE_VFI);
5609 rc = lpfc_sli4_dealloc_extent(phba,
5610 LPFC_RSC_TYPE_FCOE_VPI);
5611 rc = lpfc_sli4_dealloc_extent(phba,
5612 LPFC_RSC_TYPE_FCOE_XRI);
5613 rc = lpfc_sli4_dealloc_extent(phba,
5614 LPFC_RSC_TYPE_FCOE_RPI);
5615 } else
5616 return 0;
5617 }
5618
5619 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5620 if (unlikely(rc))
5621 goto err_exit;
5622
5623 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5624 if (unlikely(rc))
5625 goto err_exit;
5626
5627 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5628 if (unlikely(rc))
5629 goto err_exit;
5630
5631 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5632 if (unlikely(rc))
5633 goto err_exit;
5634 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5635 LPFC_IDX_RSRC_RDY);
5636 return rc;
5637 } else {
5638 /*
5639 * The port does not support resource extents. The XRI, VPI,
5640 * VFI, RPI resource ids were determined from READ_CONFIG.
5641 * Just allocate the bitmasks and provision the resource id
5642 * arrays. If a port reset is active, the resources don't
5643 * need any action - just exit.
5644 */
5645 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
ff78d8f9
JS
5646 LPFC_IDX_RSRC_RDY) {
5647 lpfc_sli4_dealloc_resource_identifiers(phba);
5648 lpfc_sli4_remove_rpis(phba);
5649 }
6d368e53
JS
5650 /* RPIs. */
5651 count = phba->sli4_hba.max_cfg_param.max_rpi;
0a630c27
JS
5652 if (count <= 0) {
5653 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5654 "3279 Invalid provisioning of "
5655 "rpi:%d\n", count);
5656 rc = -EINVAL;
5657 goto err_exit;
5658 }
6d368e53
JS
5659 base = phba->sli4_hba.max_cfg_param.rpi_base;
5660 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5661 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5662 sizeof(unsigned long),
5663 GFP_KERNEL);
5664 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5665 rc = -ENOMEM;
5666 goto err_exit;
5667 }
5668 phba->sli4_hba.rpi_ids = kzalloc(count *
5669 sizeof(uint16_t),
5670 GFP_KERNEL);
5671 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5672 rc = -ENOMEM;
5673 goto free_rpi_bmask;
5674 }
5675
5676 for (i = 0; i < count; i++)
5677 phba->sli4_hba.rpi_ids[i] = base + i;
5678
5679 /* VPIs. */
5680 count = phba->sli4_hba.max_cfg_param.max_vpi;
0a630c27
JS
5681 if (count <= 0) {
5682 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5683 "3280 Invalid provisioning of "
5684 "vpi:%d\n", count);
5685 rc = -EINVAL;
5686 goto free_rpi_ids;
5687 }
6d368e53
JS
5688 base = phba->sli4_hba.max_cfg_param.vpi_base;
5689 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5690 phba->vpi_bmask = kzalloc(longs *
5691 sizeof(unsigned long),
5692 GFP_KERNEL);
5693 if (unlikely(!phba->vpi_bmask)) {
5694 rc = -ENOMEM;
5695 goto free_rpi_ids;
5696 }
5697 phba->vpi_ids = kzalloc(count *
5698 sizeof(uint16_t),
5699 GFP_KERNEL);
5700 if (unlikely(!phba->vpi_ids)) {
5701 rc = -ENOMEM;
5702 goto free_vpi_bmask;
5703 }
5704
5705 for (i = 0; i < count; i++)
5706 phba->vpi_ids[i] = base + i;
5707
5708 /* XRIs. */
5709 count = phba->sli4_hba.max_cfg_param.max_xri;
0a630c27
JS
5710 if (count <= 0) {
5711 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5712 "3281 Invalid provisioning of "
5713 "xri:%d\n", count);
5714 rc = -EINVAL;
5715 goto free_vpi_ids;
5716 }
6d368e53
JS
5717 base = phba->sli4_hba.max_cfg_param.xri_base;
5718 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5719 phba->sli4_hba.xri_bmask = kzalloc(longs *
5720 sizeof(unsigned long),
5721 GFP_KERNEL);
5722 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5723 rc = -ENOMEM;
5724 goto free_vpi_ids;
5725 }
41899be7 5726 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
5727 phba->sli4_hba.xri_ids = kzalloc(count *
5728 sizeof(uint16_t),
5729 GFP_KERNEL);
5730 if (unlikely(!phba->sli4_hba.xri_ids)) {
5731 rc = -ENOMEM;
5732 goto free_xri_bmask;
5733 }
5734
5735 for (i = 0; i < count; i++)
5736 phba->sli4_hba.xri_ids[i] = base + i;
5737
5738 /* VFIs. */
5739 count = phba->sli4_hba.max_cfg_param.max_vfi;
0a630c27
JS
5740 if (count <= 0) {
5741 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5742 "3282 Invalid provisioning of "
5743 "vfi:%d\n", count);
5744 rc = -EINVAL;
5745 goto free_xri_ids;
5746 }
6d368e53
JS
5747 base = phba->sli4_hba.max_cfg_param.vfi_base;
5748 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5749 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5750 sizeof(unsigned long),
5751 GFP_KERNEL);
5752 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5753 rc = -ENOMEM;
5754 goto free_xri_ids;
5755 }
5756 phba->sli4_hba.vfi_ids = kzalloc(count *
5757 sizeof(uint16_t),
5758 GFP_KERNEL);
5759 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5760 rc = -ENOMEM;
5761 goto free_vfi_bmask;
5762 }
5763
5764 for (i = 0; i < count; i++)
5765 phba->sli4_hba.vfi_ids[i] = base + i;
5766
5767 /*
5768 * Mark all resources ready. An HBA reset doesn't need
5769 * to reset the initialization.
5770 */
5771 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5772 LPFC_IDX_RSRC_RDY);
5773 return 0;
5774 }
5775
5776 free_vfi_bmask:
5777 kfree(phba->sli4_hba.vfi_bmask);
5778 free_xri_ids:
5779 kfree(phba->sli4_hba.xri_ids);
5780 free_xri_bmask:
5781 kfree(phba->sli4_hba.xri_bmask);
5782 free_vpi_ids:
5783 kfree(phba->vpi_ids);
5784 free_vpi_bmask:
5785 kfree(phba->vpi_bmask);
5786 free_rpi_ids:
5787 kfree(phba->sli4_hba.rpi_ids);
5788 free_rpi_bmask:
5789 kfree(phba->sli4_hba.rpi_bmask);
5790 err_exit:
5791 return rc;
5792}
5793
5794/**
5795 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5796 * @phba: Pointer to HBA context object.
5797 *
5798 * This function allocates the number of elements for the specified
5799 * resource type.
5800 **/
5801int
5802lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5803{
5804 if (phba->sli4_hba.extents_in_use) {
5805 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5806 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5807 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5808 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5809 } else {
5810 kfree(phba->vpi_bmask);
5811 kfree(phba->vpi_ids);
5812 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5813 kfree(phba->sli4_hba.xri_bmask);
5814 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
5815 kfree(phba->sli4_hba.vfi_bmask);
5816 kfree(phba->sli4_hba.vfi_ids);
5817 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5818 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5819 }
5820
5821 return 0;
5822}
5823
b76f2dc9
JS
5824/**
5825 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5826 * @phba: Pointer to HBA context object.
5827 * @type: The resource extent type.
5828 * @extnt_count: buffer to hold port extent count response
5829 * @extnt_size: buffer to hold port extent size response.
5830 *
5831 * This function calls the port to read the host allocated extents
5832 * for a particular type.
5833 **/
5834int
5835lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5836 uint16_t *extnt_cnt, uint16_t *extnt_size)
5837{
5838 bool emb;
5839 int rc = 0;
5840 uint16_t curr_blks = 0;
5841 uint32_t req_len, emb_len;
5842 uint32_t alloc_len, mbox_tmo;
5843 struct list_head *blk_list_head;
5844 struct lpfc_rsrc_blks *rsrc_blk;
5845 LPFC_MBOXQ_t *mbox;
5846 void *virtaddr = NULL;
5847 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5848 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5849 union lpfc_sli4_cfg_shdr *shdr;
5850
5851 switch (type) {
5852 case LPFC_RSC_TYPE_FCOE_VPI:
5853 blk_list_head = &phba->lpfc_vpi_blk_list;
5854 break;
5855 case LPFC_RSC_TYPE_FCOE_XRI:
5856 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5857 break;
5858 case LPFC_RSC_TYPE_FCOE_VFI:
5859 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5860 break;
5861 case LPFC_RSC_TYPE_FCOE_RPI:
5862 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5863 break;
5864 default:
5865 return -EIO;
5866 }
5867
5868 /* Count the number of extents currently allocatd for this type. */
5869 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5870 if (curr_blks == 0) {
5871 /*
5872 * The GET_ALLOCATED mailbox does not return the size,
5873 * just the count. The size should be just the size
5874 * stored in the current allocated block and all sizes
5875 * for an extent type are the same so set the return
5876 * value now.
5877 */
5878 *extnt_size = rsrc_blk->rsrc_size;
5879 }
5880 curr_blks++;
5881 }
5882
5883 /* Calculate the total requested length of the dma memory. */
5884 req_len = curr_blks * sizeof(uint16_t);
5885
5886 /*
5887 * Calculate the size of an embedded mailbox. The uint32_t
5888 * accounts for extents-specific word.
5889 */
5890 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5891 sizeof(uint32_t);
5892
5893 /*
5894 * Presume the allocation and response will fit into an embedded
5895 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5896 */
5897 emb = LPFC_SLI4_MBX_EMBED;
5898 req_len = emb_len;
5899 if (req_len > emb_len) {
5900 req_len = curr_blks * sizeof(uint16_t) +
5901 sizeof(union lpfc_sli4_cfg_shdr) +
5902 sizeof(uint32_t);
5903 emb = LPFC_SLI4_MBX_NEMBED;
5904 }
5905
5906 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5907 if (!mbox)
5908 return -ENOMEM;
5909 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5910
5911 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5912 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5913 req_len, emb);
5914 if (alloc_len < req_len) {
5915 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5916 "2983 Allocated DMA memory size (x%x) is "
5917 "less than the requested DMA memory "
5918 "size (x%x)\n", alloc_len, req_len);
5919 rc = -ENOMEM;
5920 goto err_exit;
5921 }
5922 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5923 if (unlikely(rc)) {
5924 rc = -EIO;
5925 goto err_exit;
5926 }
5927
5928 if (!phba->sli4_hba.intr_enable)
5929 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5930 else {
a183a15f 5931 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
b76f2dc9
JS
5932 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5933 }
5934
5935 if (unlikely(rc)) {
5936 rc = -EIO;
5937 goto err_exit;
5938 }
5939
5940 /*
5941 * Figure out where the response is located. Then get local pointers
5942 * to the response data. The port does not guarantee to respond to
5943 * all extents counts request so update the local variable with the
5944 * allocated count from the port.
5945 */
5946 if (emb == LPFC_SLI4_MBX_EMBED) {
5947 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5948 shdr = &rsrc_ext->header.cfg_shdr;
5949 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5950 } else {
5951 virtaddr = mbox->sge_array->addr[0];
5952 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5953 shdr = &n_rsrc->cfg_shdr;
5954 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5955 }
5956
5957 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5958 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5959 "2984 Failed to read allocated resources "
5960 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5961 type,
5962 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5963 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5964 rc = -EIO;
5965 goto err_exit;
5966 }
5967 err_exit:
5968 lpfc_sli4_mbox_cmd_free(phba, mbox);
5969 return rc;
5970}
5971
8a9d2e80
JS
5972/**
5973 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5974 * @phba: pointer to lpfc hba data structure.
5975 *
5976 * This routine walks the list of els buffers that have been allocated and
5977 * repost them to the port by using SGL block post. This is needed after a
5978 * pci_function_reset/warm_start or start. It attempts to construct blocks
5979 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5980 * SGL block post mailbox commands to post them to the port. For single els
5981 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5982 * mailbox command for posting.
5983 *
5984 * Returns: 0 = success, non-zero failure.
5985 **/
5986static int
5987lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5988{
5989 struct lpfc_sglq *sglq_entry = NULL;
5990 struct lpfc_sglq *sglq_entry_next = NULL;
5991 struct lpfc_sglq *sglq_entry_first = NULL;
5992 int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
5993 int last_xritag = NO_XRI;
5994 LIST_HEAD(prep_sgl_list);
5995 LIST_HEAD(blck_sgl_list);
5996 LIST_HEAD(allc_sgl_list);
5997 LIST_HEAD(post_sgl_list);
5998 LIST_HEAD(free_sgl_list);
5999
6000 spin_lock(&phba->hbalock);
6001 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
6002 spin_unlock(&phba->hbalock);
6003
6004 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6005 &allc_sgl_list, list) {
6006 list_del_init(&sglq_entry->list);
6007 block_cnt++;
6008 if ((last_xritag != NO_XRI) &&
6009 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6010 /* a hole in xri block, form a sgl posting block */
6011 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6012 post_cnt = block_cnt - 1;
6013 /* prepare list for next posting block */
6014 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6015 block_cnt = 1;
6016 } else {
6017 /* prepare list for next posting block */
6018 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6019 /* enough sgls for non-embed sgl mbox command */
6020 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6021 list_splice_init(&prep_sgl_list,
6022 &blck_sgl_list);
6023 post_cnt = block_cnt;
6024 block_cnt = 0;
6025 }
6026 }
6027 num_posted++;
6028
6029 /* keep track of last sgl's xritag */
6030 last_xritag = sglq_entry->sli4_xritag;
6031
6032 /* end of repost sgl list condition for els buffers */
6033 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6034 if (post_cnt == 0) {
6035 list_splice_init(&prep_sgl_list,
6036 &blck_sgl_list);
6037 post_cnt = block_cnt;
6038 } else if (block_cnt == 1) {
6039 status = lpfc_sli4_post_sgl(phba,
6040 sglq_entry->phys, 0,
6041 sglq_entry->sli4_xritag);
6042 if (!status) {
6043 /* successful, put sgl to posted list */
6044 list_add_tail(&sglq_entry->list,
6045 &post_sgl_list);
6046 } else {
6047 /* Failure, put sgl to free list */
6048 lpfc_printf_log(phba, KERN_WARNING,
6049 LOG_SLI,
6050 "3159 Failed to post els "
6051 "sgl, xritag:x%x\n",
6052 sglq_entry->sli4_xritag);
6053 list_add_tail(&sglq_entry->list,
6054 &free_sgl_list);
6055 spin_lock_irq(&phba->hbalock);
6056 phba->sli4_hba.els_xri_cnt--;
6057 spin_unlock_irq(&phba->hbalock);
6058 }
6059 }
6060 }
6061
6062 /* continue until a nembed page worth of sgls */
6063 if (post_cnt == 0)
6064 continue;
6065
6066 /* post the els buffer list sgls as a block */
6067 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6068 post_cnt);
6069
6070 if (!status) {
6071 /* success, put sgl list to posted sgl list */
6072 list_splice_init(&blck_sgl_list, &post_sgl_list);
6073 } else {
6074 /* Failure, put sgl list to free sgl list */
6075 sglq_entry_first = list_first_entry(&blck_sgl_list,
6076 struct lpfc_sglq,
6077 list);
6078 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6079 "3160 Failed to post els sgl-list, "
6080 "xritag:x%x-x%x\n",
6081 sglq_entry_first->sli4_xritag,
6082 (sglq_entry_first->sli4_xritag +
6083 post_cnt - 1));
6084 list_splice_init(&blck_sgl_list, &free_sgl_list);
6085 spin_lock_irq(&phba->hbalock);
6086 phba->sli4_hba.els_xri_cnt -= post_cnt;
6087 spin_unlock_irq(&phba->hbalock);
6088 }
6089
6090 /* don't reset xirtag due to hole in xri block */
6091 if (block_cnt == 0)
6092 last_xritag = NO_XRI;
6093
6094 /* reset els sgl post count for next round of posting */
6095 post_cnt = 0;
6096 }
6097
6098 /* free the els sgls failed to post */
6099 lpfc_free_sgl_list(phba, &free_sgl_list);
6100
6101 /* push els sgls posted to the availble list */
6102 if (!list_empty(&post_sgl_list)) {
6103 spin_lock(&phba->hbalock);
6104 list_splice_init(&post_sgl_list,
6105 &phba->sli4_hba.lpfc_sgl_list);
6106 spin_unlock(&phba->hbalock);
6107 } else {
6108 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6109 "3161 Failure to post els sgl to port.\n");
6110 return -EIO;
6111 }
6112 return 0;
6113}
6114
da0436e9
JS
6115/**
6116 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6117 * @phba: Pointer to HBA context object.
6118 *
6119 * This function is the main SLI4 device intialization PCI function. This
6120 * function is called by the HBA intialization code, HBA reset code and
6121 * HBA error attention handler code. Caller is not required to hold any
6122 * locks.
6123 **/
6124int
6125lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6126{
6127 int rc;
6128 LPFC_MBOXQ_t *mboxq;
6129 struct lpfc_mqe *mqe;
6130 uint8_t *vpd;
6131 uint32_t vpd_size;
6132 uint32_t ftr_rsp = 0;
6133 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6134 struct lpfc_vport *vport = phba->pport;
6135 struct lpfc_dmabuf *mp;
6136
6137 /* Perform a PCI function reset to start from clean */
6138 rc = lpfc_pci_function_reset(phba);
6139 if (unlikely(rc))
6140 return -ENODEV;
6141
6142 /* Check the HBA Host Status Register for readyness */
6143 rc = lpfc_sli4_post_status_check(phba);
6144 if (unlikely(rc))
6145 return -ENODEV;
6146 else {
6147 spin_lock_irq(&phba->hbalock);
6148 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6149 spin_unlock_irq(&phba->hbalock);
6150 }
6151
6152 /*
6153 * Allocate a single mailbox container for initializing the
6154 * port.
6155 */
6156 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6157 if (!mboxq)
6158 return -ENOMEM;
6159
da0436e9 6160 /* Issue READ_REV to collect vpd and FW information. */
49198b37 6161 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
6162 vpd = kzalloc(vpd_size, GFP_KERNEL);
6163 if (!vpd) {
6164 rc = -ENOMEM;
6165 goto out_free_mbox;
6166 }
6167
6168 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
76a95d75
JS
6169 if (unlikely(rc)) {
6170 kfree(vpd);
6171 goto out_free_mbox;
6172 }
da0436e9 6173 mqe = &mboxq->u.mqe;
f1126688
JS
6174 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6175 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
76a95d75
JS
6176 phba->hba_flag |= HBA_FCOE_MODE;
6177 else
6178 phba->hba_flag &= ~HBA_FCOE_MODE;
45ed1190
JS
6179
6180 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6181 LPFC_DCBX_CEE_MODE)
6182 phba->hba_flag |= HBA_FIP_SUPPORT;
6183 else
6184 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6185
4f2e66c6
JS
6186 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6187
c31098ce 6188 if (phba->sli_rev != LPFC_SLI_REV4) {
da0436e9
JS
6189 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6190 "0376 READ_REV Error. SLI Level %d "
6191 "FCoE enabled %d\n",
76a95d75 6192 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
da0436e9 6193 rc = -EIO;
76a95d75
JS
6194 kfree(vpd);
6195 goto out_free_mbox;
da0436e9 6196 }
cd1c8301 6197
ff78d8f9
JS
6198 /*
6199 * Continue initialization with default values even if driver failed
6200 * to read FCoE param config regions, only read parameters if the
6201 * board is FCoE
6202 */
6203 if (phba->hba_flag & HBA_FCOE_MODE &&
6204 lpfc_sli4_read_fcoe_params(phba))
6205 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6206 "2570 Failed to read FCoE parameters\n");
6207
cd1c8301
JS
6208 /*
6209 * Retrieve sli4 device physical port name, failure of doing it
6210 * is considered as non-fatal.
6211 */
6212 rc = lpfc_sli4_retrieve_pport_name(phba);
6213 if (!rc)
6214 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6215 "3080 Successful retrieving SLI4 device "
6216 "physical port name: %s.\n", phba->Port);
6217
da0436e9
JS
6218 /*
6219 * Evaluate the read rev and vpd data. Populate the driver
6220 * state with the results. If this routine fails, the failure
6221 * is not fatal as the driver will use generic values.
6222 */
6223 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6224 if (unlikely(!rc)) {
6225 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6226 "0377 Error %d parsing vpd. "
6227 "Using defaults.\n", rc);
6228 rc = 0;
6229 }
76a95d75 6230 kfree(vpd);
da0436e9 6231
f1126688
JS
6232 /* Save information as VPD data */
6233 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6234 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6235 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6236 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6237 &mqe->un.read_rev);
6238 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6239 &mqe->un.read_rev);
6240 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6241 &mqe->un.read_rev);
6242 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6243 &mqe->un.read_rev);
6244 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6245 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6246 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6247 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6248 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6249 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6250 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6251 "(%d):0380 READ_REV Status x%x "
6252 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6253 mboxq->vport ? mboxq->vport->vpi : 0,
6254 bf_get(lpfc_mqe_status, mqe),
6255 phba->vpd.rev.opFwName,
6256 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6257 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9
JS
6258
6259 /*
6260 * Discover the port's supported feature set and match it against the
6261 * hosts requests.
6262 */
6263 lpfc_request_features(phba, mboxq);
6264 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6265 if (unlikely(rc)) {
6266 rc = -EIO;
76a95d75 6267 goto out_free_mbox;
da0436e9
JS
6268 }
6269
6270 /*
6271 * The port must support FCP initiator mode as this is the
6272 * only mode running in the host.
6273 */
6274 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6275 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6276 "0378 No support for fcpi mode.\n");
6277 ftr_rsp++;
6278 }
fedd3b7b
JS
6279 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6280 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6281 else
6282 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
da0436e9
JS
6283 /*
6284 * If the port cannot support the host's requested features
6285 * then turn off the global config parameters to disable the
6286 * feature in the driver. This is not a fatal error.
6287 */
bf08611b
JS
6288 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6289 if (phba->cfg_enable_bg) {
6290 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6291 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6292 else
6293 ftr_rsp++;
6294 }
da0436e9
JS
6295
6296 if (phba->max_vpi && phba->cfg_enable_npiv &&
6297 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6298 ftr_rsp++;
6299
6300 if (ftr_rsp) {
6301 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6302 "0379 Feature Mismatch Data: x%08x %08x "
6303 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6304 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6305 phba->cfg_enable_npiv, phba->max_vpi);
6306 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6307 phba->cfg_enable_bg = 0;
6308 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6309 phba->cfg_enable_npiv = 0;
6310 }
6311
6312 /* These SLI3 features are assumed in SLI4 */
6313 spin_lock_irq(&phba->hbalock);
6314 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6315 spin_unlock_irq(&phba->hbalock);
6316
6d368e53
JS
6317 /*
6318 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6319 * calls depends on these resources to complete port setup.
6320 */
6321 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6322 if (rc) {
6323 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6324 "2920 Failed to alloc Resource IDs "
6325 "rc = x%x\n", rc);
6326 goto out_free_mbox;
6327 }
6328
da0436e9 6329 /* Read the port's service parameters. */
9f1177a3
JS
6330 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6331 if (rc) {
6332 phba->link_state = LPFC_HBA_ERROR;
6333 rc = -ENOMEM;
76a95d75 6334 goto out_free_mbox;
9f1177a3
JS
6335 }
6336
da0436e9
JS
6337 mboxq->vport = vport;
6338 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6339 mp = (struct lpfc_dmabuf *) mboxq->context1;
6340 if (rc == MBX_SUCCESS) {
6341 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6342 rc = 0;
6343 }
6344
6345 /*
6346 * This memory was allocated by the lpfc_read_sparam routine. Release
6347 * it to the mbuf pool.
6348 */
6349 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6350 kfree(mp);
6351 mboxq->context1 = NULL;
6352 if (unlikely(rc)) {
6353 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6354 "0382 READ_SPARAM command failed "
6355 "status %d, mbxStatus x%x\n",
6356 rc, bf_get(lpfc_mqe_status, mqe));
6357 phba->link_state = LPFC_HBA_ERROR;
6358 rc = -EIO;
76a95d75 6359 goto out_free_mbox;
da0436e9
JS
6360 }
6361
0558056c 6362 lpfc_update_vport_wwn(vport);
da0436e9
JS
6363
6364 /* Update the fc_host data structures with new wwn. */
6365 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6366 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6367
8a9d2e80
JS
6368 /* update host els and scsi xri-sgl sizes and mappings */
6369 rc = lpfc_sli4_xri_sgl_update(phba);
6370 if (unlikely(rc)) {
6371 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6372 "1400 Failed to update xri-sgl size and "
6373 "mapping: %d\n", rc);
6374 goto out_free_mbox;
da0436e9
JS
6375 }
6376
8a9d2e80
JS
6377 /* register the els sgl pool to the port */
6378 rc = lpfc_sli4_repost_els_sgl_list(phba);
6379 if (unlikely(rc)) {
6380 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6381 "0582 Error %d during els sgl post "
6382 "operation\n", rc);
6383 rc = -ENODEV;
6384 goto out_free_mbox;
6385 }
6386
6387 /* register the allocated scsi sgl pool to the port */
da0436e9
JS
6388 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6389 if (unlikely(rc)) {
6d368e53 6390 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
6391 "0383 Error %d during scsi sgl post "
6392 "operation\n", rc);
da0436e9
JS
6393 /* Some Scsi buffers were moved to the abort scsi list */
6394 /* A pci function reset will repost them */
6395 rc = -ENODEV;
76a95d75 6396 goto out_free_mbox;
da0436e9
JS
6397 }
6398
6399 /* Post the rpi header region to the device. */
6400 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6401 if (unlikely(rc)) {
6402 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6403 "0393 Error %d during rpi post operation\n",
6404 rc);
6405 rc = -ENODEV;
76a95d75 6406 goto out_free_mbox;
da0436e9 6407 }
97f2ecf1 6408 lpfc_sli4_node_prep(phba);
da0436e9 6409
5350d872
JS
6410 /* Create all the SLI4 queues */
6411 rc = lpfc_sli4_queue_create(phba);
6412 if (rc) {
6413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6414 "3089 Failed to allocate queues\n");
6415 rc = -ENODEV;
6416 goto out_stop_timers;
6417 }
da0436e9
JS
6418 /* Set up all the queues to the device */
6419 rc = lpfc_sli4_queue_setup(phba);
6420 if (unlikely(rc)) {
6421 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6422 "0381 Error %d during queue setup.\n ", rc);
5350d872 6423 goto out_destroy_queue;
da0436e9
JS
6424 }
6425
6426 /* Arm the CQs and then EQs on device */
6427 lpfc_sli4_arm_cqeq_intr(phba);
6428
6429 /* Indicate device interrupt mode */
6430 phba->sli4_hba.intr_enable = 1;
6431
6432 /* Allow asynchronous mailbox command to go through */
6433 spin_lock_irq(&phba->hbalock);
6434 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6435 spin_unlock_irq(&phba->hbalock);
6436
6437 /* Post receive buffers to the device */
6438 lpfc_sli4_rb_setup(phba);
6439
fc2b989b
JS
6440 /* Reset HBA FCF states after HBA reset */
6441 phba->fcf.fcf_flag = 0;
6442 phba->fcf.current_rec.flag = 0;
6443
da0436e9 6444 /* Start the ELS watchdog timer */
8fa38513
JS
6445 mod_timer(&vport->els_tmofunc,
6446 jiffies + HZ * (phba->fc_ratov * 2));
da0436e9
JS
6447
6448 /* Start heart beat timer */
6449 mod_timer(&phba->hb_tmofunc,
6450 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6451 phba->hb_outstanding = 0;
6452 phba->last_completion_time = jiffies;
6453
6454 /* Start error attention (ERATT) polling timer */
6455 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6456
75baf696
JS
6457 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6458 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6459 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6460 if (!rc) {
6461 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6462 "2829 This device supports "
6463 "Advanced Error Reporting (AER)\n");
6464 spin_lock_irq(&phba->hbalock);
6465 phba->hba_flag |= HBA_AER_ENABLED;
6466 spin_unlock_irq(&phba->hbalock);
6467 } else {
6468 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6469 "2830 This device does not support "
6470 "Advanced Error Reporting (AER)\n");
6471 phba->cfg_aer_support = 0;
6472 }
0a96e975 6473 rc = 0;
75baf696
JS
6474 }
6475
76a95d75
JS
6476 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6477 /*
6478 * The FC Port needs to register FCFI (index 0)
6479 */
6480 lpfc_reg_fcfi(phba, mboxq);
6481 mboxq->vport = phba->pport;
6482 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9589b062 6483 if (rc != MBX_SUCCESS)
76a95d75 6484 goto out_unset_queue;
9589b062
JS
6485 rc = 0;
6486 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6487 &mboxq->u.mqe.un.reg_fcfi);
026abb87
JS
6488
6489 /* Check if the port is configured to be disabled */
6490 lpfc_sli_read_link_ste(phba);
76a95d75 6491 }
026abb87 6492
da0436e9
JS
6493 /*
6494 * The port is ready, set the host's link state to LINK_DOWN
6495 * in preparation for link interrupts.
6496 */
da0436e9
JS
6497 spin_lock_irq(&phba->hbalock);
6498 phba->link_state = LPFC_LINK_DOWN;
6499 spin_unlock_irq(&phba->hbalock);
026abb87
JS
6500 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6501 (phba->hba_flag & LINK_DISABLED)) {
6502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6503 "3103 Adapter Link is disabled.\n");
6504 lpfc_down_link(phba, mboxq);
6505 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6506 if (rc != MBX_SUCCESS) {
6507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6508 "3104 Adapter failed to issue "
6509 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6510 goto out_unset_queue;
6511 }
6512 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
1b51197d
JS
6513 /* don't perform init_link on SLI4 FC port loopback test */
6514 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6515 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6516 if (rc)
6517 goto out_unset_queue;
6518 }
5350d872
JS
6519 }
6520 mempool_free(mboxq, phba->mbox_mem_pool);
6521 return rc;
76a95d75 6522out_unset_queue:
da0436e9 6523 /* Unset all the queues set up in this routine when error out */
5350d872
JS
6524 lpfc_sli4_queue_unset(phba);
6525out_destroy_queue:
6526 lpfc_sli4_queue_destroy(phba);
da0436e9 6527out_stop_timers:
5350d872 6528 lpfc_stop_hba_timers(phba);
da0436e9
JS
6529out_free_mbox:
6530 mempool_free(mboxq, phba->mbox_mem_pool);
6531 return rc;
6532}
6533
6534/**
6535 * lpfc_mbox_timeout - Timeout call back function for mbox timer
6536 * @ptr: context object - pointer to hba structure.
6537 *
6538 * This is the callback function for mailbox timer. The mailbox
6539 * timer is armed when a new mailbox command is issued and the timer
6540 * is deleted when the mailbox complete. The function is called by
6541 * the kernel timer code when a mailbox does not complete within
6542 * expected time. This function wakes up the worker thread to
6543 * process the mailbox timeout and returns. All the processing is
6544 * done by the worker thread function lpfc_mbox_timeout_handler.
6545 **/
6546void
6547lpfc_mbox_timeout(unsigned long ptr)
6548{
6549 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
6550 unsigned long iflag;
6551 uint32_t tmo_posted;
6552
6553 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6554 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6555 if (!tmo_posted)
6556 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6557 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6558
6559 if (!tmo_posted)
6560 lpfc_worker_wake_up(phba);
6561 return;
6562}
6563
6564
6565/**
6566 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6567 * @phba: Pointer to HBA context object.
6568 *
6569 * This function is called from worker thread when a mailbox command times out.
6570 * The caller is not required to hold any locks. This function will reset the
6571 * HBA and recover all the pending commands.
6572 **/
6573void
6574lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6575{
6576 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
04c68496 6577 MAILBOX_t *mb = &pmbox->u.mb;
da0436e9
JS
6578 struct lpfc_sli *psli = &phba->sli;
6579 struct lpfc_sli_ring *pring;
6580
6581 /* Check the pmbox pointer first. There is a race condition
6582 * between the mbox timeout handler getting executed in the
6583 * worklist and the mailbox actually completing. When this
6584 * race condition occurs, the mbox_active will be NULL.
6585 */
6586 spin_lock_irq(&phba->hbalock);
6587 if (pmbox == NULL) {
6588 lpfc_printf_log(phba, KERN_WARNING,
6589 LOG_MBOX | LOG_SLI,
6590 "0353 Active Mailbox cleared - mailbox timeout "
6591 "exiting\n");
6592 spin_unlock_irq(&phba->hbalock);
6593 return;
6594 }
6595
6596 /* Mbox cmd <mbxCommand> timeout */
6597 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6598 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6599 mb->mbxCommand,
6600 phba->pport->port_state,
6601 phba->sli.sli_flag,
6602 phba->sli.mbox_active);
6603 spin_unlock_irq(&phba->hbalock);
6604
6605 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6606 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
25985edc 6607 * it to fail all outstanding SCSI IO.
da0436e9
JS
6608 */
6609 spin_lock_irq(&phba->pport->work_port_lock);
6610 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6611 spin_unlock_irq(&phba->pport->work_port_lock);
6612 spin_lock_irq(&phba->hbalock);
6613 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 6614 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
6615 spin_unlock_irq(&phba->hbalock);
6616
6617 pring = &psli->ring[psli->fcp_ring];
6618 lpfc_sli_abort_iocb_ring(phba, pring);
6619
6620 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6621 "0345 Resetting board due to mailbox timeout\n");
6622
6623 /* Reset the HBA device */
6624 lpfc_reset_hba(phba);
6625}
6626
6627/**
6628 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6629 * @phba: Pointer to HBA context object.
6630 * @pmbox: Pointer to mailbox object.
6631 * @flag: Flag indicating how the mailbox need to be processed.
6632 *
6633 * This function is called by discovery code and HBA management code
6634 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6635 * function gets the hbalock to protect the data structures.
6636 * The mailbox command can be submitted in polling mode, in which case
6637 * this function will wait in a polling loop for the completion of the
6638 * mailbox.
6639 * If the mailbox is submitted in no_wait mode (not polling) the
6640 * function will submit the command and returns immediately without waiting
6641 * for the mailbox completion. The no_wait is supported only when HBA
6642 * is in SLI2/SLI3 mode - interrupts are enabled.
6643 * The SLI interface allows only one mailbox pending at a time. If the
6644 * mailbox is issued in polling mode and there is already a mailbox
6645 * pending, then the function will return an error. If the mailbox is issued
6646 * in NO_WAIT mode and there is a mailbox pending already, the function
6647 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6648 * The sli layer owns the mailbox object until the completion of mailbox
6649 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6650 * return codes the caller owns the mailbox command after the return of
6651 * the function.
e59058c4 6652 **/
3772a991
JS
6653static int
6654lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6655 uint32_t flag)
dea3101e 6656{
bf07bdea 6657 MAILBOX_t *mbx;
2e0fef85 6658 struct lpfc_sli *psli = &phba->sli;
dea3101e 6659 uint32_t status, evtctr;
9940b97b 6660 uint32_t ha_copy, hc_copy;
dea3101e 6661 int i;
09372820 6662 unsigned long timeout;
dea3101e 6663 unsigned long drvr_flag = 0;
34b02dcd 6664 uint32_t word0, ldata;
dea3101e 6665 void __iomem *to_slim;
58da1ffb
JS
6666 int processing_queue = 0;
6667
6668 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6669 if (!pmbox) {
8568a4d2 6670 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 6671 /* processing mbox queue from intr_handler */
3772a991
JS
6672 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6673 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6674 return MBX_SUCCESS;
6675 }
58da1ffb 6676 processing_queue = 1;
58da1ffb
JS
6677 pmbox = lpfc_mbox_get(phba);
6678 if (!pmbox) {
6679 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6680 return MBX_SUCCESS;
6681 }
6682 }
dea3101e 6683
ed957684 6684 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 6685 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 6686 if(!pmbox->vport) {
58da1ffb 6687 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 6688 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 6689 LOG_MBOX | LOG_VPORT,
e8b62011 6690 "1806 Mbox x%x failed. No vport\n",
3772a991 6691 pmbox->u.mb.mbxCommand);
ed957684 6692 dump_stack();
58da1ffb 6693 goto out_not_finished;
ed957684
JS
6694 }
6695 }
6696
8d63f375 6697 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
6698 if (unlikely(pci_channel_offline(phba->pcidev))) {
6699 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6700 goto out_not_finished;
6701 }
8d63f375 6702
a257bf90
JS
6703 /* If HBA has a deferred error attention, fail the iocb. */
6704 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6705 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6706 goto out_not_finished;
6707 }
6708
dea3101e 6709 psli = &phba->sli;
92d7f7b0 6710
bf07bdea 6711 mbx = &pmbox->u.mb;
dea3101e
JB
6712 status = MBX_SUCCESS;
6713
2e0fef85
JS
6714 if (phba->link_state == LPFC_HBA_ERROR) {
6715 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
6716
6717 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6718 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6719 "(%d):0311 Mailbox command x%x cannot "
6720 "issue Data: x%x x%x\n",
6721 pmbox->vport ? pmbox->vport->vpi : 0,
6722 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 6723 goto out_not_finished;
41415862
JW
6724 }
6725
bf07bdea 6726 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9940b97b
JS
6727 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6728 !(hc_copy & HC_MBINT_ENA)) {
6729 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6730 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3772a991
JS
6731 "(%d):2528 Mailbox command x%x cannot "
6732 "issue Data: x%x x%x\n",
6733 pmbox->vport ? pmbox->vport->vpi : 0,
6734 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9940b97b
JS
6735 goto out_not_finished;
6736 }
9290831f
JS
6737 }
6738
dea3101e
JB
6739 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6740 /* Polling for a mbox command when another one is already active
6741 * is not allowed in SLI. Also, the driver must have established
6742 * SLI2 mode to queue and process multiple mbox commands.
6743 */
6744
6745 if (flag & MBX_POLL) {
2e0fef85 6746 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
6747
6748 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6749 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6750 "(%d):2529 Mailbox command x%x "
6751 "cannot issue Data: x%x x%x\n",
6752 pmbox->vport ? pmbox->vport->vpi : 0,
6753 pmbox->u.mb.mbxCommand,
6754 psli->sli_flag, flag);
58da1ffb 6755 goto out_not_finished;
dea3101e
JB
6756 }
6757
3772a991 6758 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 6759 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6760 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6761 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6762 "(%d):2530 Mailbox command x%x "
6763 "cannot issue Data: x%x x%x\n",
6764 pmbox->vport ? pmbox->vport->vpi : 0,
6765 pmbox->u.mb.mbxCommand,
6766 psli->sli_flag, flag);
58da1ffb 6767 goto out_not_finished;
dea3101e
JB
6768 }
6769
dea3101e
JB
6770 /* Another mailbox command is still being processed, queue this
6771 * command to be processed later.
6772 */
6773 lpfc_mbox_put(phba, pmbox);
6774
6775 /* Mbox cmd issue - BUSY */
ed957684 6776 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 6777 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 6778 "x%x x%x x%x x%x\n",
92d7f7b0 6779 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
bf07bdea 6780 mbx->mbxCommand, phba->pport->port_state,
92d7f7b0 6781 psli->sli_flag, flag);
dea3101e
JB
6782
6783 psli->slistat.mbox_busy++;
2e0fef85 6784 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6785
858c9f6c
JS
6786 if (pmbox->vport) {
6787 lpfc_debugfs_disc_trc(pmbox->vport,
6788 LPFC_DISC_TRC_MBOX_VPORT,
6789 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
6790 (uint32_t)mbx->mbxCommand,
6791 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
6792 }
6793 else {
6794 lpfc_debugfs_disc_trc(phba->pport,
6795 LPFC_DISC_TRC_MBOX,
6796 "MBOX Bsy: cmd:x%x mb:x%x x%x",
bf07bdea
RD
6797 (uint32_t)mbx->mbxCommand,
6798 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
6799 }
6800
2e0fef85 6801 return MBX_BUSY;
dea3101e
JB
6802 }
6803
dea3101e
JB
6804 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6805
6806 /* If we are not polling, we MUST be in SLI2 mode */
6807 if (flag != MBX_POLL) {
3772a991 6808 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
bf07bdea 6809 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 6810 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 6811 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6812 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6813 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6814 "(%d):2531 Mailbox command x%x "
6815 "cannot issue Data: x%x x%x\n",
6816 pmbox->vport ? pmbox->vport->vpi : 0,
6817 pmbox->u.mb.mbxCommand,
6818 psli->sli_flag, flag);
58da1ffb 6819 goto out_not_finished;
dea3101e
JB
6820 }
6821 /* timeout active mbox command */
a309a6b6 6822 mod_timer(&psli->mbox_tmo, (jiffies +
a183a15f 6823 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea3101e
JB
6824 }
6825
6826 /* Mailbox cmd <cmd> issue */
ed957684 6827 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 6828 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 6829 "x%x\n",
e8b62011 6830 pmbox->vport ? pmbox->vport->vpi : 0,
bf07bdea 6831 mbx->mbxCommand, phba->pport->port_state,
92d7f7b0 6832 psli->sli_flag, flag);
dea3101e 6833
bf07bdea 6834 if (mbx->mbxCommand != MBX_HEARTBEAT) {
858c9f6c
JS
6835 if (pmbox->vport) {
6836 lpfc_debugfs_disc_trc(pmbox->vport,
6837 LPFC_DISC_TRC_MBOX_VPORT,
6838 "MBOX Send vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
6839 (uint32_t)mbx->mbxCommand,
6840 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
6841 }
6842 else {
6843 lpfc_debugfs_disc_trc(phba->pport,
6844 LPFC_DISC_TRC_MBOX,
6845 "MBOX Send: cmd:x%x mb:x%x x%x",
bf07bdea
RD
6846 (uint32_t)mbx->mbxCommand,
6847 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
6848 }
6849 }
6850
dea3101e
JB
6851 psli->slistat.mbox_cmd++;
6852 evtctr = psli->slistat.mbox_event;
6853
6854 /* next set own bit for the adapter and copy over command word */
bf07bdea 6855 mbx->mbxOwner = OWN_CHIP;
dea3101e 6856
3772a991 6857 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
6858 /* Populate mbox extension offset word. */
6859 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
bf07bdea 6860 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
6861 = (uint8_t *)phba->mbox_ext
6862 - (uint8_t *)phba->mbox;
6863 }
6864
6865 /* Copy the mailbox extension data */
6866 if (pmbox->in_ext_byte_len && pmbox->context2) {
6867 lpfc_sli_pcimem_bcopy(pmbox->context2,
6868 (uint8_t *)phba->mbox_ext,
6869 pmbox->in_ext_byte_len);
6870 }
6871 /* Copy command data to host SLIM area */
bf07bdea 6872 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 6873 } else {
7a470277
JS
6874 /* Populate mbox extension offset word. */
6875 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
bf07bdea 6876 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
6877 = MAILBOX_HBA_EXT_OFFSET;
6878
6879 /* Copy the mailbox extension data */
6880 if (pmbox->in_ext_byte_len && pmbox->context2) {
6881 lpfc_memcpy_to_slim(phba->MBslimaddr +
6882 MAILBOX_HBA_EXT_OFFSET,
6883 pmbox->context2, pmbox->in_ext_byte_len);
6884
6885 }
bf07bdea 6886 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 6887 /* copy command data into host mbox for cmpl */
bf07bdea 6888 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e
JB
6889 }
6890
6891 /* First copy mbox command data to HBA SLIM, skip past first
6892 word */
6893 to_slim = phba->MBslimaddr + sizeof (uint32_t);
bf07bdea 6894 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea3101e
JB
6895 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6896
6897 /* Next copy over first word, with mbxOwner set */
bf07bdea 6898 ldata = *((uint32_t *)mbx);
dea3101e
JB
6899 to_slim = phba->MBslimaddr;
6900 writel(ldata, to_slim);
6901 readl(to_slim); /* flush */
6902
bf07bdea 6903 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 6904 /* switch over to host mailbox */
3772a991 6905 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e
JB
6906 }
6907 }
6908
6909 wmb();
dea3101e
JB
6910
6911 switch (flag) {
6912 case MBX_NOWAIT:
09372820 6913 /* Set up reference to mailbox command */
dea3101e 6914 psli->mbox_active = pmbox;
09372820
JS
6915 /* Interrupt board to do it */
6916 writel(CA_MBATT, phba->CAregaddr);
6917 readl(phba->CAregaddr); /* flush */
6918 /* Don't wait for it to finish, just return */
dea3101e
JB
6919 break;
6920
6921 case MBX_POLL:
09372820 6922 /* Set up null reference to mailbox command */
dea3101e 6923 psli->mbox_active = NULL;
09372820
JS
6924 /* Interrupt board to do it */
6925 writel(CA_MBATT, phba->CAregaddr);
6926 readl(phba->CAregaddr); /* flush */
6927
3772a991 6928 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 6929 /* First read mbox status word */
34b02dcd 6930 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
6931 word0 = le32_to_cpu(word0);
6932 } else {
6933 /* First read mbox status word */
9940b97b
JS
6934 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6935 spin_unlock_irqrestore(&phba->hbalock,
6936 drvr_flag);
6937 goto out_not_finished;
6938 }
dea3101e
JB
6939 }
6940
6941 /* Read the HBA Host Attention Register */
9940b97b
JS
6942 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6943 spin_unlock_irqrestore(&phba->hbalock,
6944 drvr_flag);
6945 goto out_not_finished;
6946 }
a183a15f
JS
6947 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6948 1000) + jiffies;
09372820 6949 i = 0;
dea3101e 6950 /* Wait for command to complete */
41415862
JW
6951 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6952 (!(ha_copy & HA_MBATT) &&
2e0fef85 6953 (phba->link_state > LPFC_WARM_START))) {
09372820 6954 if (time_after(jiffies, timeout)) {
dea3101e 6955 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 6956 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 6957 drvr_flag);
58da1ffb 6958 goto out_not_finished;
dea3101e
JB
6959 }
6960
6961 /* Check if we took a mbox interrupt while we were
6962 polling */
6963 if (((word0 & OWN_CHIP) != OWN_CHIP)
6964 && (evtctr != psli->slistat.mbox_event))
6965 break;
6966
09372820
JS
6967 if (i++ > 10) {
6968 spin_unlock_irqrestore(&phba->hbalock,
6969 drvr_flag);
6970 msleep(1);
6971 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6972 }
dea3101e 6973
3772a991 6974 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 6975 /* First copy command data */
34b02dcd 6976 word0 = *((uint32_t *)phba->mbox);
dea3101e 6977 word0 = le32_to_cpu(word0);
bf07bdea 6978 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 6979 MAILBOX_t *slimmb;
34b02dcd 6980 uint32_t slimword0;
dea3101e
JB
6981 /* Check real SLIM for any errors */
6982 slimword0 = readl(phba->MBslimaddr);
6983 slimmb = (MAILBOX_t *) & slimword0;
6984 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6985 && slimmb->mbxStatus) {
6986 psli->sli_flag &=
3772a991 6987 ~LPFC_SLI_ACTIVE;
dea3101e
JB
6988 word0 = slimword0;
6989 }
6990 }
6991 } else {
6992 /* First copy command data */
6993 word0 = readl(phba->MBslimaddr);
6994 }
6995 /* Read the HBA Host Attention Register */
9940b97b
JS
6996 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6997 spin_unlock_irqrestore(&phba->hbalock,
6998 drvr_flag);
6999 goto out_not_finished;
7000 }
dea3101e
JB
7001 }
7002
3772a991 7003 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7004 /* copy results back to user */
bf07bdea 7005 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
7a470277
JS
7006 /* Copy the mailbox extension data */
7007 if (pmbox->out_ext_byte_len && pmbox->context2) {
7008 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7009 pmbox->context2,
7010 pmbox->out_ext_byte_len);
7011 }
dea3101e
JB
7012 } else {
7013 /* First copy command data */
bf07bdea 7014 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea3101e 7015 MAILBOX_CMD_SIZE);
7a470277
JS
7016 /* Copy the mailbox extension data */
7017 if (pmbox->out_ext_byte_len && pmbox->context2) {
7018 lpfc_memcpy_from_slim(pmbox->context2,
7019 phba->MBslimaddr +
7020 MAILBOX_HBA_EXT_OFFSET,
7021 pmbox->out_ext_byte_len);
dea3101e
JB
7022 }
7023 }
7024
7025 writel(HA_MBATT, phba->HAregaddr);
7026 readl(phba->HAregaddr); /* flush */
7027
7028 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
bf07bdea 7029 status = mbx->mbxStatus;
dea3101e
JB
7030 }
7031
2e0fef85
JS
7032 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7033 return status;
58da1ffb
JS
7034
7035out_not_finished:
7036 if (processing_queue) {
da0436e9 7037 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
7038 lpfc_mbox_cmpl_put(phba, pmbox);
7039 }
7040 return MBX_NOT_FINISHED;
dea3101e
JB
7041}
7042
f1126688
JS
7043/**
7044 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7045 * @phba: Pointer to HBA context object.
7046 *
7047 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7048 * the driver internal pending mailbox queue. It will then try to wait out the
7049 * possible outstanding mailbox command before return.
7050 *
7051 * Returns:
7052 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7053 * the outstanding mailbox command timed out.
7054 **/
7055static int
7056lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7057{
7058 struct lpfc_sli *psli = &phba->sli;
f1126688 7059 int rc = 0;
a183a15f 7060 unsigned long timeout = 0;
f1126688
JS
7061
7062 /* Mark the asynchronous mailbox command posting as blocked */
7063 spin_lock_irq(&phba->hbalock);
7064 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
f1126688
JS
7065 /* Determine how long we might wait for the active mailbox
7066 * command to be gracefully completed by firmware.
7067 */
a183a15f
JS
7068 if (phba->sli.mbox_active)
7069 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7070 phba->sli.mbox_active) *
7071 1000) + jiffies;
7072 spin_unlock_irq(&phba->hbalock);
7073
f1126688
JS
7074 /* Wait for the outstnading mailbox command to complete */
7075 while (phba->sli.mbox_active) {
7076 /* Check active mailbox complete status every 2ms */
7077 msleep(2);
7078 if (time_after(jiffies, timeout)) {
7079 /* Timeout, marked the outstanding cmd not complete */
7080 rc = 1;
7081 break;
7082 }
7083 }
7084
7085 /* Can not cleanly block async mailbox command, fails it */
7086 if (rc) {
7087 spin_lock_irq(&phba->hbalock);
7088 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7089 spin_unlock_irq(&phba->hbalock);
7090 }
7091 return rc;
7092}
7093
7094/**
7095 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7096 * @phba: Pointer to HBA context object.
7097 *
7098 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7099 * commands from the driver internal pending mailbox queue. It makes sure
7100 * that there is no outstanding mailbox command before resuming posting
7101 * asynchronous mailbox commands. If, for any reason, there is outstanding
7102 * mailbox command, it will try to wait it out before resuming asynchronous
7103 * mailbox command posting.
7104 **/
7105static void
7106lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7107{
7108 struct lpfc_sli *psli = &phba->sli;
7109
7110 spin_lock_irq(&phba->hbalock);
7111 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7112 /* Asynchronous mailbox posting is not blocked, do nothing */
7113 spin_unlock_irq(&phba->hbalock);
7114 return;
7115 }
7116
7117 /* Outstanding synchronous mailbox command is guaranteed to be done,
7118 * successful or timeout, after timing-out the outstanding mailbox
7119 * command shall always be removed, so just unblock posting async
7120 * mailbox command and resume
7121 */
7122 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7123 spin_unlock_irq(&phba->hbalock);
7124
7125 /* wake up worker thread to post asynchronlous mailbox command */
7126 lpfc_worker_wake_up(phba);
7127}
7128
2d843edc
JS
7129/**
7130 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7131 * @phba: Pointer to HBA context object.
7132 * @mboxq: Pointer to mailbox object.
7133 *
7134 * The function waits for the bootstrap mailbox register ready bit from
7135 * port for twice the regular mailbox command timeout value.
7136 *
7137 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7138 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7139 **/
7140static int
7141lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7142{
7143 uint32_t db_ready;
7144 unsigned long timeout;
7145 struct lpfc_register bmbx_reg;
7146
7147 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7148 * 1000) + jiffies;
7149
7150 do {
7151 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7152 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7153 if (!db_ready)
7154 msleep(2);
7155
7156 if (time_after(jiffies, timeout))
7157 return MBXERR_ERROR;
7158 } while (!db_ready);
7159
7160 return 0;
7161}
7162
da0436e9
JS
7163/**
7164 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7165 * @phba: Pointer to HBA context object.
7166 * @mboxq: Pointer to mailbox object.
7167 *
7168 * The function posts a mailbox to the port. The mailbox is expected
7169 * to be comletely filled in and ready for the port to operate on it.
7170 * This routine executes a synchronous completion operation on the
7171 * mailbox by polling for its completion.
7172 *
7173 * The caller must not be holding any locks when calling this routine.
7174 *
7175 * Returns:
7176 * MBX_SUCCESS - mailbox posted successfully
7177 * Any of the MBX error values.
7178 **/
7179static int
7180lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7181{
7182 int rc = MBX_SUCCESS;
7183 unsigned long iflag;
da0436e9
JS
7184 uint32_t mcqe_status;
7185 uint32_t mbx_cmnd;
da0436e9
JS
7186 struct lpfc_sli *psli = &phba->sli;
7187 struct lpfc_mqe *mb = &mboxq->u.mqe;
7188 struct lpfc_bmbx_create *mbox_rgn;
7189 struct dma_address *dma_address;
da0436e9
JS
7190
7191 /*
7192 * Only one mailbox can be active to the bootstrap mailbox region
7193 * at a time and there is no queueing provided.
7194 */
7195 spin_lock_irqsave(&phba->hbalock, iflag);
7196 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7197 spin_unlock_irqrestore(&phba->hbalock, iflag);
7198 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7199 "(%d):2532 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7200 "cannot issue Data: x%x x%x\n",
7201 mboxq->vport ? mboxq->vport->vpi : 0,
7202 mboxq->u.mb.mbxCommand,
a183a15f
JS
7203 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7204 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7205 psli->sli_flag, MBX_POLL);
7206 return MBXERR_ERROR;
7207 }
7208 /* The server grabs the token and owns it until release */
7209 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7210 phba->sli.mbox_active = mboxq;
7211 spin_unlock_irqrestore(&phba->hbalock, iflag);
7212
2d843edc
JS
7213 /* wait for bootstrap mbox register for readyness */
7214 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7215 if (rc)
7216 goto exit;
7217
da0436e9
JS
7218 /*
7219 * Initialize the bootstrap memory region to avoid stale data areas
7220 * in the mailbox post. Then copy the caller's mailbox contents to
7221 * the bmbx mailbox region.
7222 */
7223 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7224 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7225 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7226 sizeof(struct lpfc_mqe));
7227
7228 /* Post the high mailbox dma address to the port and wait for ready. */
7229 dma_address = &phba->sli4_hba.bmbx.dma_address;
7230 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7231
2d843edc
JS
7232 /* wait for bootstrap mbox register for hi-address write done */
7233 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7234 if (rc)
7235 goto exit;
da0436e9
JS
7236
7237 /* Post the low mailbox dma address to the port. */
7238 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
da0436e9 7239
2d843edc
JS
7240 /* wait for bootstrap mbox register for low address write done */
7241 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7242 if (rc)
7243 goto exit;
da0436e9
JS
7244
7245 /*
7246 * Read the CQ to ensure the mailbox has completed.
7247 * If so, update the mailbox status so that the upper layers
7248 * can complete the request normally.
7249 */
7250 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7251 sizeof(struct lpfc_mqe));
7252 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7253 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7254 sizeof(struct lpfc_mcqe));
7255 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
0558056c
JS
7256 /*
7257 * When the CQE status indicates a failure and the mailbox status
7258 * indicates success then copy the CQE status into the mailbox status
7259 * (and prefix it with x4000).
7260 */
da0436e9 7261 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
0558056c
JS
7262 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7263 bf_set(lpfc_mqe_status, mb,
7264 (LPFC_MBX_ERROR_RANGE | mcqe_status));
da0436e9 7265 rc = MBXERR_ERROR;
d7c47992
JS
7266 } else
7267 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
7268
7269 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 7270 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
da0436e9
JS
7271 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7272 " x%x x%x CQ: x%x x%x x%x x%x\n",
a183a15f
JS
7273 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7274 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7275 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7276 bf_get(lpfc_mqe_status, mb),
7277 mb->un.mb_words[0], mb->un.mb_words[1],
7278 mb->un.mb_words[2], mb->un.mb_words[3],
7279 mb->un.mb_words[4], mb->un.mb_words[5],
7280 mb->un.mb_words[6], mb->un.mb_words[7],
7281 mb->un.mb_words[8], mb->un.mb_words[9],
7282 mb->un.mb_words[10], mb->un.mb_words[11],
7283 mb->un.mb_words[12], mboxq->mcqe.word0,
7284 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7285 mboxq->mcqe.trailer);
7286exit:
7287 /* We are holding the token, no needed for lock when release */
7288 spin_lock_irqsave(&phba->hbalock, iflag);
7289 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7290 phba->sli.mbox_active = NULL;
7291 spin_unlock_irqrestore(&phba->hbalock, iflag);
7292 return rc;
7293}
7294
7295/**
7296 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7297 * @phba: Pointer to HBA context object.
7298 * @pmbox: Pointer to mailbox object.
7299 * @flag: Flag indicating how the mailbox need to be processed.
7300 *
7301 * This function is called by discovery code and HBA management code to submit
7302 * a mailbox command to firmware with SLI-4 interface spec.
7303 *
7304 * Return codes the caller owns the mailbox command after the return of the
7305 * function.
7306 **/
7307static int
7308lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7309 uint32_t flag)
7310{
7311 struct lpfc_sli *psli = &phba->sli;
7312 unsigned long iflags;
7313 int rc;
7314
b76f2dc9
JS
7315 /* dump from issue mailbox command if setup */
7316 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7317
8fa38513
JS
7318 rc = lpfc_mbox_dev_check(phba);
7319 if (unlikely(rc)) {
7320 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7321 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8fa38513
JS
7322 "cannot issue Data: x%x x%x\n",
7323 mboxq->vport ? mboxq->vport->vpi : 0,
7324 mboxq->u.mb.mbxCommand,
a183a15f
JS
7325 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7326 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8fa38513
JS
7327 psli->sli_flag, flag);
7328 goto out_not_finished;
7329 }
7330
da0436e9
JS
7331 /* Detect polling mode and jump to a handler */
7332 if (!phba->sli4_hba.intr_enable) {
7333 if (flag == MBX_POLL)
7334 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7335 else
7336 rc = -EIO;
7337 if (rc != MBX_SUCCESS)
0558056c 7338 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
da0436e9 7339 "(%d):2541 Mailbox command x%x "
cc459f19
JS
7340 "(x%x/x%x) failure: "
7341 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7342 "Data: x%x x%x\n,",
da0436e9
JS
7343 mboxq->vport ? mboxq->vport->vpi : 0,
7344 mboxq->u.mb.mbxCommand,
a183a15f
JS
7345 lpfc_sli_config_mbox_subsys_get(phba,
7346 mboxq),
7347 lpfc_sli_config_mbox_opcode_get(phba,
7348 mboxq),
cc459f19
JS
7349 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7350 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7351 bf_get(lpfc_mcqe_ext_status,
7352 &mboxq->mcqe),
da0436e9
JS
7353 psli->sli_flag, flag);
7354 return rc;
7355 } else if (flag == MBX_POLL) {
f1126688
JS
7356 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7357 "(%d):2542 Try to issue mailbox command "
a183a15f 7358 "x%x (x%x/x%x) synchronously ahead of async"
f1126688 7359 "mailbox command queue: x%x x%x\n",
da0436e9
JS
7360 mboxq->vport ? mboxq->vport->vpi : 0,
7361 mboxq->u.mb.mbxCommand,
a183a15f
JS
7362 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7363 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9 7364 psli->sli_flag, flag);
f1126688
JS
7365 /* Try to block the asynchronous mailbox posting */
7366 rc = lpfc_sli4_async_mbox_block(phba);
7367 if (!rc) {
7368 /* Successfully blocked, now issue sync mbox cmd */
7369 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7370 if (rc != MBX_SUCCESS)
cc459f19 7371 lpfc_printf_log(phba, KERN_WARNING,
a183a15f 7372 LOG_MBOX | LOG_SLI,
cc459f19
JS
7373 "(%d):2597 Sync Mailbox command "
7374 "x%x (x%x/x%x) failure: "
7375 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7376 "Data: x%x x%x\n,",
7377 mboxq->vport ? mboxq->vport->vpi : 0,
a183a15f
JS
7378 mboxq->u.mb.mbxCommand,
7379 lpfc_sli_config_mbox_subsys_get(phba,
7380 mboxq),
7381 lpfc_sli_config_mbox_opcode_get(phba,
7382 mboxq),
cc459f19
JS
7383 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7384 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7385 bf_get(lpfc_mcqe_ext_status,
7386 &mboxq->mcqe),
a183a15f 7387 psli->sli_flag, flag);
f1126688
JS
7388 /* Unblock the async mailbox posting afterward */
7389 lpfc_sli4_async_mbox_unblock(phba);
7390 }
7391 return rc;
da0436e9
JS
7392 }
7393
7394 /* Now, interrupt mode asynchrous mailbox command */
7395 rc = lpfc_mbox_cmd_check(phba, mboxq);
7396 if (rc) {
7397 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7398 "(%d):2543 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7399 "cannot issue Data: x%x x%x\n",
7400 mboxq->vport ? mboxq->vport->vpi : 0,
7401 mboxq->u.mb.mbxCommand,
a183a15f
JS
7402 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7403 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7404 psli->sli_flag, flag);
7405 goto out_not_finished;
7406 }
da0436e9
JS
7407
7408 /* Put the mailbox command to the driver internal FIFO */
7409 psli->slistat.mbox_busy++;
7410 spin_lock_irqsave(&phba->hbalock, iflags);
7411 lpfc_mbox_put(phba, mboxq);
7412 spin_unlock_irqrestore(&phba->hbalock, iflags);
7413 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7414 "(%d):0354 Mbox cmd issue - Enqueue Data: "
a183a15f 7415 "x%x (x%x/x%x) x%x x%x x%x\n",
da0436e9
JS
7416 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7417 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
a183a15f
JS
7418 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7419 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7420 phba->pport->port_state,
7421 psli->sli_flag, MBX_NOWAIT);
7422 /* Wake up worker thread to transport mailbox command from head */
7423 lpfc_worker_wake_up(phba);
7424
7425 return MBX_BUSY;
7426
7427out_not_finished:
7428 return MBX_NOT_FINISHED;
7429}
7430
7431/**
7432 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7433 * @phba: Pointer to HBA context object.
7434 *
7435 * This function is called by worker thread to send a mailbox command to
7436 * SLI4 HBA firmware.
7437 *
7438 **/
7439int
7440lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7441{
7442 struct lpfc_sli *psli = &phba->sli;
7443 LPFC_MBOXQ_t *mboxq;
7444 int rc = MBX_SUCCESS;
7445 unsigned long iflags;
7446 struct lpfc_mqe *mqe;
7447 uint32_t mbx_cmnd;
7448
7449 /* Check interrupt mode before post async mailbox command */
7450 if (unlikely(!phba->sli4_hba.intr_enable))
7451 return MBX_NOT_FINISHED;
7452
7453 /* Check for mailbox command service token */
7454 spin_lock_irqsave(&phba->hbalock, iflags);
7455 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7456 spin_unlock_irqrestore(&phba->hbalock, iflags);
7457 return MBX_NOT_FINISHED;
7458 }
7459 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7460 spin_unlock_irqrestore(&phba->hbalock, iflags);
7461 return MBX_NOT_FINISHED;
7462 }
7463 if (unlikely(phba->sli.mbox_active)) {
7464 spin_unlock_irqrestore(&phba->hbalock, iflags);
7465 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7466 "0384 There is pending active mailbox cmd\n");
7467 return MBX_NOT_FINISHED;
7468 }
7469 /* Take the mailbox command service token */
7470 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7471
7472 /* Get the next mailbox command from head of queue */
7473 mboxq = lpfc_mbox_get(phba);
7474
7475 /* If no more mailbox command waiting for post, we're done */
7476 if (!mboxq) {
7477 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7478 spin_unlock_irqrestore(&phba->hbalock, iflags);
7479 return MBX_SUCCESS;
7480 }
7481 phba->sli.mbox_active = mboxq;
7482 spin_unlock_irqrestore(&phba->hbalock, iflags);
7483
7484 /* Check device readiness for posting mailbox command */
7485 rc = lpfc_mbox_dev_check(phba);
7486 if (unlikely(rc))
7487 /* Driver clean routine will clean up pending mailbox */
7488 goto out_not_finished;
7489
7490 /* Prepare the mbox command to be posted */
7491 mqe = &mboxq->u.mqe;
7492 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7493
7494 /* Start timer for the mbox_tmo and log some mailbox post messages */
7495 mod_timer(&psli->mbox_tmo, (jiffies +
a183a15f 7496 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
da0436e9
JS
7497
7498 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 7499 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
da0436e9
JS
7500 "x%x x%x\n",
7501 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
a183a15f
JS
7502 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7503 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7504 phba->pport->port_state, psli->sli_flag);
7505
7506 if (mbx_cmnd != MBX_HEARTBEAT) {
7507 if (mboxq->vport) {
7508 lpfc_debugfs_disc_trc(mboxq->vport,
7509 LPFC_DISC_TRC_MBOX_VPORT,
7510 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7511 mbx_cmnd, mqe->un.mb_words[0],
7512 mqe->un.mb_words[1]);
7513 } else {
7514 lpfc_debugfs_disc_trc(phba->pport,
7515 LPFC_DISC_TRC_MBOX,
7516 "MBOX Send: cmd:x%x mb:x%x x%x",
7517 mbx_cmnd, mqe->un.mb_words[0],
7518 mqe->un.mb_words[1]);
7519 }
7520 }
7521 psli->slistat.mbox_cmd++;
7522
7523 /* Post the mailbox command to the port */
7524 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7525 if (rc != MBX_SUCCESS) {
7526 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7527 "(%d):2533 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7528 "cannot issue Data: x%x x%x\n",
7529 mboxq->vport ? mboxq->vport->vpi : 0,
7530 mboxq->u.mb.mbxCommand,
a183a15f
JS
7531 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7532 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7533 psli->sli_flag, MBX_NOWAIT);
7534 goto out_not_finished;
7535 }
7536
7537 return rc;
7538
7539out_not_finished:
7540 spin_lock_irqsave(&phba->hbalock, iflags);
d7069f09
JS
7541 if (phba->sli.mbox_active) {
7542 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7543 __lpfc_mbox_cmpl_put(phba, mboxq);
7544 /* Release the token */
7545 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7546 phba->sli.mbox_active = NULL;
7547 }
da0436e9
JS
7548 spin_unlock_irqrestore(&phba->hbalock, iflags);
7549
7550 return MBX_NOT_FINISHED;
7551}
7552
7553/**
7554 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7555 * @phba: Pointer to HBA context object.
7556 * @pmbox: Pointer to mailbox object.
7557 * @flag: Flag indicating how the mailbox need to be processed.
7558 *
7559 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7560 * the API jump table function pointer from the lpfc_hba struct.
7561 *
7562 * Return codes the caller owns the mailbox command after the return of the
7563 * function.
7564 **/
7565int
7566lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7567{
7568 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7569}
7570
7571/**
25985edc 7572 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
da0436e9
JS
7573 * @phba: The hba struct for which this call is being executed.
7574 * @dev_grp: The HBA PCI-Device group number.
7575 *
7576 * This routine sets up the mbox interface API function jump table in @phba
7577 * struct.
7578 * Returns: 0 - success, -ENODEV - failure.
7579 **/
7580int
7581lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7582{
7583
7584 switch (dev_grp) {
7585 case LPFC_PCI_DEV_LP:
7586 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7587 phba->lpfc_sli_handle_slow_ring_event =
7588 lpfc_sli_handle_slow_ring_event_s3;
7589 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7590 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7591 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7592 break;
7593 case LPFC_PCI_DEV_OC:
7594 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7595 phba->lpfc_sli_handle_slow_ring_event =
7596 lpfc_sli_handle_slow_ring_event_s4;
7597 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7598 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7599 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7600 break;
7601 default:
7602 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7603 "1420 Invalid HBA PCI-device group: 0x%x\n",
7604 dev_grp);
7605 return -ENODEV;
7606 break;
7607 }
7608 return 0;
7609}
7610
e59058c4 7611/**
3621a710 7612 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
7613 * @phba: Pointer to HBA context object.
7614 * @pring: Pointer to driver SLI ring object.
7615 * @piocb: Pointer to address of newly added command iocb.
7616 *
7617 * This function is called with hbalock held to add a command
7618 * iocb to the txq when SLI layer cannot submit the command iocb
7619 * to the ring.
7620 **/
2a9bf3d0 7621void
92d7f7b0 7622__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 7623 struct lpfc_iocbq *piocb)
dea3101e
JB
7624{
7625 /* Insert the caller's iocb in the txq tail for later processing. */
7626 list_add_tail(&piocb->list, &pring->txq);
dea3101e
JB
7627}
7628
e59058c4 7629/**
3621a710 7630 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
7631 * @phba: Pointer to HBA context object.
7632 * @pring: Pointer to driver SLI ring object.
7633 * @piocb: Pointer to address of newly added command iocb.
7634 *
7635 * This function is called with hbalock held before a new
7636 * iocb is submitted to the firmware. This function checks
7637 * txq to flush the iocbs in txq to Firmware before
7638 * submitting new iocbs to the Firmware.
7639 * If there are iocbs in the txq which need to be submitted
7640 * to firmware, lpfc_sli_next_iocb returns the first element
7641 * of the txq after dequeuing it from txq.
7642 * If there is no iocb in the txq then the function will return
7643 * *piocb and *piocb is set to NULL. Caller needs to check
7644 * *piocb to find if there are more commands in the txq.
7645 **/
dea3101e
JB
7646static struct lpfc_iocbq *
7647lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 7648 struct lpfc_iocbq **piocb)
dea3101e
JB
7649{
7650 struct lpfc_iocbq * nextiocb;
7651
7652 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7653 if (!nextiocb) {
7654 nextiocb = *piocb;
7655 *piocb = NULL;
7656 }
7657
7658 return nextiocb;
7659}
7660
e59058c4 7661/**
3772a991 7662 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 7663 * @phba: Pointer to HBA context object.
3772a991 7664 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
7665 * @piocb: Pointer to command iocb.
7666 * @flag: Flag indicating if this command can be put into txq.
7667 *
3772a991
JS
7668 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7669 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7670 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7671 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7672 * this function allows only iocbs for posting buffers. This function finds
7673 * next available slot in the command ring and posts the command to the
7674 * available slot and writes the port attention register to request HBA start
7675 * processing new iocb. If there is no slot available in the ring and
7676 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7677 * the function returns IOCB_BUSY.
e59058c4 7678 *
3772a991
JS
7679 * This function is called with hbalock held. The function will return success
7680 * after it successfully submit the iocb to firmware or after adding to the
7681 * txq.
e59058c4 7682 **/
98c9ea5c 7683static int
3772a991 7684__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e
JB
7685 struct lpfc_iocbq *piocb, uint32_t flag)
7686{
7687 struct lpfc_iocbq *nextiocb;
7688 IOCB_t *iocb;
3772a991 7689 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea3101e 7690
92d7f7b0
JS
7691 if (piocb->iocb_cmpl && (!piocb->vport) &&
7692 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7693 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7694 lpfc_printf_log(phba, KERN_ERR,
7695 LOG_SLI | LOG_VPORT,
e8b62011 7696 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
7697 piocb->iocb.ulpCommand);
7698 dump_stack();
7699 return IOCB_ERROR;
7700 }
7701
7702
8d63f375
LV
7703 /* If the PCI channel is in offline state, do not post iocbs. */
7704 if (unlikely(pci_channel_offline(phba->pcidev)))
7705 return IOCB_ERROR;
7706
a257bf90
JS
7707 /* If HBA has a deferred error attention, fail the iocb. */
7708 if (unlikely(phba->hba_flag & DEFER_ERATT))
7709 return IOCB_ERROR;
7710
dea3101e
JB
7711 /*
7712 * We should never get an IOCB if we are in a < LINK_DOWN state
7713 */
2e0fef85 7714 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e
JB
7715 return IOCB_ERROR;
7716
7717 /*
7718 * Check to see if we are blocking IOCB processing because of a
0b727fea 7719 * outstanding event.
dea3101e 7720 */
0b727fea 7721 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e
JB
7722 goto iocb_busy;
7723
2e0fef85 7724 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 7725 /*
2680eeaa 7726 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e
JB
7727 * can be issued if the link is not up.
7728 */
7729 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
7730 case CMD_GEN_REQUEST64_CR:
7731 case CMD_GEN_REQUEST64_CX:
7732 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7733 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 7734 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
7735 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7736 MENLO_TRANSPORT_TYPE))
7737
7738 goto iocb_busy;
7739 break;
dea3101e
JB
7740 case CMD_QUE_RING_BUF_CN:
7741 case CMD_QUE_RING_BUF64_CN:
dea3101e
JB
7742 /*
7743 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7744 * completion, iocb_cmpl MUST be 0.
7745 */
7746 if (piocb->iocb_cmpl)
7747 piocb->iocb_cmpl = NULL;
7748 /*FALLTHROUGH*/
7749 case CMD_CREATE_XRI_CR:
2680eeaa
JS
7750 case CMD_CLOSE_XRI_CN:
7751 case CMD_CLOSE_XRI_CX:
dea3101e
JB
7752 break;
7753 default:
7754 goto iocb_busy;
7755 }
7756
7757 /*
7758 * For FCP commands, we must be in a state where we can process link
7759 * attention events.
7760 */
7761 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
92d7f7b0 7762 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 7763 goto iocb_busy;
92d7f7b0 7764 }
dea3101e 7765
dea3101e
JB
7766 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7767 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7768 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7769
7770 if (iocb)
7771 lpfc_sli_update_ring(phba, pring);
7772 else
7773 lpfc_sli_update_full_ring(phba, pring);
7774
7775 if (!piocb)
7776 return IOCB_SUCCESS;
7777
7778 goto out_busy;
7779
7780 iocb_busy:
7781 pring->stats.iocb_cmd_delay++;
7782
7783 out_busy:
7784
7785 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 7786 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e
JB
7787 return IOCB_SUCCESS;
7788 }
7789
7790 return IOCB_BUSY;
7791}
7792
3772a991 7793/**
4f774513
JS
7794 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7795 * @phba: Pointer to HBA context object.
7796 * @piocb: Pointer to command iocb.
7797 * @sglq: Pointer to the scatter gather queue object.
7798 *
7799 * This routine converts the bpl or bde that is in the IOCB
7800 * to a sgl list for the sli4 hardware. The physical address
7801 * of the bpl/bde is converted back to a virtual address.
7802 * If the IOCB contains a BPL then the list of BDE's is
7803 * converted to sli4_sge's. If the IOCB contains a single
7804 * BDE then it is converted to a single sli_sge.
7805 * The IOCB is still in cpu endianess so the contents of
7806 * the bpl can be used without byte swapping.
7807 *
7808 * Returns valid XRI = Success, NO_XRI = Failure.
7809**/
7810static uint16_t
7811lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7812 struct lpfc_sglq *sglq)
3772a991 7813{
4f774513
JS
7814 uint16_t xritag = NO_XRI;
7815 struct ulp_bde64 *bpl = NULL;
7816 struct ulp_bde64 bde;
7817 struct sli4_sge *sgl = NULL;
1b51197d 7818 struct lpfc_dmabuf *dmabuf;
4f774513
JS
7819 IOCB_t *icmd;
7820 int numBdes = 0;
7821 int i = 0;
63e801ce
JS
7822 uint32_t offset = 0; /* accumulated offset in the sg request list */
7823 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 7824
4f774513
JS
7825 if (!piocbq || !sglq)
7826 return xritag;
7827
7828 sgl = (struct sli4_sge *)sglq->sgl;
7829 icmd = &piocbq->iocb;
6b5151fd
JS
7830 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7831 return sglq->sli4_xritag;
4f774513
JS
7832 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7833 numBdes = icmd->un.genreq64.bdl.bdeSize /
7834 sizeof(struct ulp_bde64);
7835 /* The addrHigh and addrLow fields within the IOCB
7836 * have not been byteswapped yet so there is no
7837 * need to swap them back.
7838 */
1b51197d
JS
7839 if (piocbq->context3)
7840 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7841 else
7842 return xritag;
4f774513 7843
1b51197d 7844 bpl = (struct ulp_bde64 *)dmabuf->virt;
4f774513
JS
7845 if (!bpl)
7846 return xritag;
7847
7848 for (i = 0; i < numBdes; i++) {
7849 /* Should already be byte swapped. */
28baac74
JS
7850 sgl->addr_hi = bpl->addrHigh;
7851 sgl->addr_lo = bpl->addrLow;
7852
0558056c 7853 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
7854 if ((i+1) == numBdes)
7855 bf_set(lpfc_sli4_sge_last, sgl, 1);
7856 else
7857 bf_set(lpfc_sli4_sge_last, sgl, 0);
28baac74
JS
7858 /* swap the size field back to the cpu so we
7859 * can assign it to the sgl.
7860 */
7861 bde.tus.w = le32_to_cpu(bpl->tus.w);
7862 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
7863 /* The offsets in the sgl need to be accumulated
7864 * separately for the request and reply lists.
7865 * The request is always first, the reply follows.
7866 */
7867 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7868 /* add up the reply sg entries */
7869 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7870 inbound++;
7871 /* first inbound? reset the offset */
7872 if (inbound == 1)
7873 offset = 0;
7874 bf_set(lpfc_sli4_sge_offset, sgl, offset);
f9bb2da1
JS
7875 bf_set(lpfc_sli4_sge_type, sgl,
7876 LPFC_SGE_TYPE_DATA);
63e801ce
JS
7877 offset += bde.tus.f.bdeSize;
7878 }
546fc854 7879 sgl->word2 = cpu_to_le32(sgl->word2);
4f774513
JS
7880 bpl++;
7881 sgl++;
7882 }
7883 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7884 /* The addrHigh and addrLow fields of the BDE have not
7885 * been byteswapped yet so they need to be swapped
7886 * before putting them in the sgl.
7887 */
7888 sgl->addr_hi =
7889 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7890 sgl->addr_lo =
7891 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
0558056c 7892 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
7893 bf_set(lpfc_sli4_sge_last, sgl, 1);
7894 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
7895 sgl->sge_len =
7896 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
7897 }
7898 return sglq->sli4_xritag;
3772a991 7899}
92d7f7b0 7900
e59058c4 7901/**
4f774513 7902 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
e59058c4 7903 * @phba: Pointer to HBA context object.
e59058c4 7904 *
a93ff37a 7905 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
8fa38513
JS
7906 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7907 * held.
4f774513
JS
7908 *
7909 * Return: index into SLI4 fast-path FCP queue index.
e59058c4 7910 **/
2a76a283 7911static inline uint32_t
8fa38513 7912lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
92d7f7b0 7913{
2a76a283 7914 int i;
92d7f7b0 7915
49aa143d
JS
7916 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU)
7917 i = smp_processor_id();
7918 else
7919 i = atomic_add_return(1, &phba->fcp_qidx);
92d7f7b0 7920
67d12733 7921 i = (i % phba->cfg_fcp_io_channel);
2a76a283 7922 return i;
92d7f7b0
JS
7923}
7924
e59058c4 7925/**
4f774513 7926 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 7927 * @phba: Pointer to HBA context object.
4f774513
JS
7928 * @piocb: Pointer to command iocb.
7929 * @wqe: Pointer to the work queue entry.
e59058c4 7930 *
4f774513
JS
7931 * This routine converts the iocb command to its Work Queue Entry
7932 * equivalent. The wqe pointer should not have any fields set when
7933 * this routine is called because it will memcpy over them.
7934 * This routine does not set the CQ_ID or the WQEC bits in the
7935 * wqe.
e59058c4 7936 *
4f774513 7937 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 7938 **/
cf5bf97e 7939static int
4f774513
JS
7940lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7941 union lpfc_wqe *wqe)
cf5bf97e 7942{
5ffc266e 7943 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
7944 uint8_t ct = 0;
7945 uint32_t fip;
7946 uint32_t abort_tag;
7947 uint8_t command_type = ELS_COMMAND_NON_FIP;
7948 uint8_t cmnd;
7949 uint16_t xritag;
dcf2a4e0
JS
7950 uint16_t abrt_iotag;
7951 struct lpfc_iocbq *abrtiocbq;
4f774513 7952 struct ulp_bde64 *bpl = NULL;
f0d9bccc 7953 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
7954 int numBdes, i;
7955 struct ulp_bde64 bde;
c31098ce 7956 struct lpfc_nodelist *ndlp;
ff78d8f9 7957 uint32_t *pcmd;
1b51197d 7958 uint32_t if_type;
4f774513 7959
45ed1190 7960 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 7961 /* The fcp commands will set command type */
0c287589 7962 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 7963 command_type = FCP_COMMAND;
c868595d 7964 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
7965 command_type = ELS_COMMAND_FIP;
7966 else
7967 command_type = ELS_COMMAND_NON_FIP;
7968
4f774513
JS
7969 /* Some of the fields are in the right position already */
7970 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7971 abort_tag = (uint32_t) iocbq->iotag;
7972 xritag = iocbq->sli4_xritag;
f0d9bccc 7973 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
4f774513
JS
7974 /* words0-2 bpl convert bde */
7975 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
7976 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7977 sizeof(struct ulp_bde64);
4f774513
JS
7978 bpl = (struct ulp_bde64 *)
7979 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7980 if (!bpl)
7981 return IOCB_ERROR;
cf5bf97e 7982
4f774513
JS
7983 /* Should already be byte swapped. */
7984 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7985 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7986 /* swap the size field back to the cpu so we
7987 * can assign it to the sgl.
7988 */
7989 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
7990 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7991 total_len = 0;
7992 for (i = 0; i < numBdes; i++) {
7993 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7994 total_len += bde.tus.f.bdeSize;
7995 }
4f774513 7996 } else
5ffc266e 7997 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 7998
4f774513
JS
7999 iocbq->iocb.ulpIoTag = iocbq->iotag;
8000 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 8001
4f774513
JS
8002 switch (iocbq->iocb.ulpCommand) {
8003 case CMD_ELS_REQUEST64_CR:
93d1379e
JS
8004 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8005 ndlp = iocbq->context_un.ndlp;
8006 else
8007 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513
JS
8008 if (!iocbq->iocb.ulpLe) {
8009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8010 "2007 Only Limited Edition cmd Format"
8011 " supported 0x%x\n",
8012 iocbq->iocb.ulpCommand);
8013 return IOCB_ERROR;
8014 }
ff78d8f9 8015
5ffc266e 8016 wqe->els_req.payload_len = xmit_len;
4f774513
JS
8017 /* Els_reguest64 has a TMO */
8018 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8019 iocbq->iocb.ulpTimeout);
8020 /* Need a VF for word 4 set the vf bit*/
8021 bf_set(els_req64_vf, &wqe->els_req, 0);
8022 /* And a VFID for word 12 */
8023 bf_set(els_req64_vfid, &wqe->els_req, 0);
4f774513 8024 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
8025 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8026 iocbq->iocb.ulpContext);
8027 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8028 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 8029 /* CCP CCPE PV PRI in word10 were set in the memcpy */
ff78d8f9 8030 if (command_type == ELS_COMMAND_FIP)
c868595d
JS
8031 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8032 >> LPFC_FIP_ELS_ID_SHIFT);
ff78d8f9
JS
8033 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8034 iocbq->context2)->virt);
1b51197d
JS
8035 if_type = bf_get(lpfc_sli_intf_if_type,
8036 &phba->sli4_hba.sli_intf);
8037 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
ff78d8f9 8038 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
cb69f7de 8039 *pcmd == ELS_CMD_SCR ||
6b5151fd 8040 *pcmd == ELS_CMD_FDISC ||
bdcd2b92 8041 *pcmd == ELS_CMD_LOGO ||
ff78d8f9
JS
8042 *pcmd == ELS_CMD_PLOGI)) {
8043 bf_set(els_req64_sp, &wqe->els_req, 1);
8044 bf_set(els_req64_sid, &wqe->els_req,
8045 iocbq->vport->fc_myDID);
939723a4
JS
8046 if ((*pcmd == ELS_CMD_FLOGI) &&
8047 !(phba->fc_topology ==
8048 LPFC_TOPOLOGY_LOOP))
8049 bf_set(els_req64_sid, &wqe->els_req, 0);
ff78d8f9
JS
8050 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8051 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
a7dd9c0f 8052 phba->vpi_ids[iocbq->vport->vpi]);
3ef6d24c 8053 } else if (pcmd && iocbq->context1) {
ff78d8f9
JS
8054 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8055 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8056 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8057 }
c868595d 8058 }
6d368e53
JS
8059 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8060 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
f0d9bccc
JS
8061 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8062 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8063 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8064 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8065 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8066 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
7851fe2c 8067 break;
5ffc266e 8068 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
8069 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8070 iocbq->iocb.un.ulpWord[3]);
8071 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7851fe2c 8072 iocbq->iocb.unsli3.rcvsli3.ox_id);
5ffc266e
JS
8073 /* The entire sequence is transmitted for this IOCB */
8074 xmit_len = total_len;
8075 cmnd = CMD_XMIT_SEQUENCE64_CR;
1b51197d
JS
8076 if (phba->link_flag & LS_LOOPBACK_MODE)
8077 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
4f774513 8078 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
8079 /* word3 iocb=io_tag32 wqe=reserved */
8080 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
8081 /* word4 relative_offset memcpy */
8082 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
8083 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8084 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8085 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8086 LPFC_WQE_IOD_WRITE);
8087 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8088 LPFC_WQE_LENLOC_WORD12);
8089 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
8090 wqe->xmit_sequence.xmit_len = xmit_len;
8091 command_type = OTHER_COMMAND;
7851fe2c 8092 break;
4f774513 8093 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
8094 /* word3 iocb=iotag32 wqe=seq_payload_len */
8095 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
8096 /* word4 iocb=rsvd wqe=rsvd */
8097 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8098 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 8099 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 8100 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
8101 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8102 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8103 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8104 LPFC_WQE_LENLOC_WORD3);
8105 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7851fe2c 8106 break;
4f774513
JS
8107 case CMD_FCP_IWRITE64_CR:
8108 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
8109 /* word3 iocb=iotag wqe=payload_offset_len */
8110 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8111 wqe->fcp_iwrite.payload_offset_len =
8112 xmit_len + sizeof(struct fcp_rsp);
8113 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8114 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8115 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8116 iocbq->iocb.ulpFCP2Rcvy);
8117 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8118 /* Always open the exchange */
8119 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
f0d9bccc
JS
8120 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8121 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8122 LPFC_WQE_LENLOC_WORD4);
8123 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8124 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8125 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7851fe2c 8126 break;
4f774513 8127 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
8128 /* word3 iocb=iotag wqe=payload_offset_len */
8129 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8130 wqe->fcp_iread.payload_offset_len =
5ffc266e 8131 xmit_len + sizeof(struct fcp_rsp);
f0d9bccc
JS
8132 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8133 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8134 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8135 iocbq->iocb.ulpFCP2Rcvy);
8136 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688
JS
8137 /* Always open the exchange */
8138 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
f0d9bccc
JS
8139 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8140 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8141 LPFC_WQE_LENLOC_WORD4);
8142 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8143 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8144 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7851fe2c 8145 break;
4f774513 8146 case CMD_FCP_ICMND64_CR:
f0d9bccc
JS
8147 /* word3 iocb=IO_TAG wqe=reserved */
8148 wqe->fcp_icmd.rsrvd3 = 0;
8149 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 8150 /* Always open the exchange */
f0d9bccc
JS
8151 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8152 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8153 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8154 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8155 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8156 LPFC_WQE_LENLOC_NONE);
8157 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
2a94aea4
JS
8158 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8159 iocbq->iocb.ulpFCP2Rcvy);
7851fe2c 8160 break;
4f774513 8161 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
8162 /* For this command calculate the xmit length of the
8163 * request bde.
8164 */
8165 xmit_len = 0;
8166 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8167 sizeof(struct ulp_bde64);
8168 for (i = 0; i < numBdes; i++) {
63e801ce 8169 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
546fc854
JS
8170 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8171 break;
63e801ce
JS
8172 xmit_len += bde.tus.f.bdeSize;
8173 }
f0d9bccc
JS
8174 /* word3 iocb=IO_TAG wqe=request_payload_len */
8175 wqe->gen_req.request_payload_len = xmit_len;
8176 /* word4 iocb=parameter wqe=relative_offset memcpy */
8177 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
8178 /* word6 context tag copied in memcpy */
8179 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8180 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8181 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8182 "2015 Invalid CT %x command 0x%x\n",
8183 ct, iocbq->iocb.ulpCommand);
8184 return IOCB_ERROR;
8185 }
f0d9bccc
JS
8186 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8187 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8188 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8189 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8190 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8191 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8192 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8193 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
4f774513 8194 command_type = OTHER_COMMAND;
7851fe2c 8195 break;
4f774513 8196 case CMD_XMIT_ELS_RSP64_CX:
c31098ce 8197 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513 8198 /* words0-2 BDE memcpy */
f0d9bccc
JS
8199 /* word3 iocb=iotag32 wqe=response_payload_len */
8200 wqe->xmit_els_rsp.response_payload_len = xmit_len;
939723a4
JS
8201 /* word4 */
8202 wqe->xmit_els_rsp.word4 = 0;
4f774513
JS
8203 /* word5 iocb=rsvd wge=did */
8204 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
939723a4
JS
8205 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8206
8207 if_type = bf_get(lpfc_sli_intf_if_type,
8208 &phba->sli4_hba.sli_intf);
8209 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8210 if (iocbq->vport->fc_flag & FC_PT2PT) {
8211 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8212 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8213 iocbq->vport->fc_myDID);
8214 if (iocbq->vport->fc_myDID == Fabric_DID) {
8215 bf_set(wqe_els_did,
8216 &wqe->xmit_els_rsp.wqe_dest, 0);
8217 }
8218 }
8219 }
f0d9bccc
JS
8220 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8221 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8222 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8223 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7851fe2c 8224 iocbq->iocb.unsli3.rcvsli3.ox_id);
4f774513 8225 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 8226 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6d368e53 8227 phba->vpi_ids[iocbq->vport->vpi]);
f0d9bccc
JS
8228 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8229 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8230 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8231 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8232 LPFC_WQE_LENLOC_WORD3);
8233 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6d368e53
JS
8234 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8235 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
ff78d8f9
JS
8236 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8237 iocbq->context2)->virt);
8238 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
939723a4
JS
8239 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8240 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
ff78d8f9 8241 iocbq->vport->fc_myDID);
939723a4
JS
8242 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8243 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
ff78d8f9
JS
8244 phba->vpi_ids[phba->pport->vpi]);
8245 }
4f774513 8246 command_type = OTHER_COMMAND;
7851fe2c 8247 break;
4f774513
JS
8248 case CMD_CLOSE_XRI_CN:
8249 case CMD_ABORT_XRI_CN:
8250 case CMD_ABORT_XRI_CX:
8251 /* words 0-2 memcpy should be 0 rserved */
8252 /* port will send abts */
dcf2a4e0
JS
8253 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8254 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8255 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8256 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8257 } else
8258 fip = 0;
8259
8260 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 8261 /*
dcf2a4e0
JS
8262 * The link is down, or the command was ELS_FIP
8263 * so the fw does not need to send abts
4f774513
JS
8264 * on the wire.
8265 */
8266 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8267 else
8268 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8269 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
8270 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8271 wqe->abort_cmd.rsrvd5 = 0;
8272 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
8273 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8274 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
8275 /*
8276 * The abort handler will send us CMD_ABORT_XRI_CN or
8277 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8278 */
f0d9bccc
JS
8279 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8280 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8281 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8282 LPFC_WQE_LENLOC_NONE);
4f774513
JS
8283 cmnd = CMD_ABORT_XRI_CX;
8284 command_type = OTHER_COMMAND;
8285 xritag = 0;
7851fe2c 8286 break;
6669f9bb 8287 case CMD_XMIT_BLS_RSP64_CX:
6b5151fd 8288 ndlp = (struct lpfc_nodelist *)iocbq->context1;
546fc854 8289 /* As BLS ABTS RSP WQE is very different from other WQEs,
6669f9bb
JS
8290 * we re-construct this WQE here based on information in
8291 * iocbq from scratch.
8292 */
8293 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 8294 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 8295 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
546fc854
JS
8296 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8297 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
5ffc266e
JS
8298 LPFC_ABTS_UNSOL_INT) {
8299 /* ABTS sent by initiator to CT exchange, the
8300 * RX_ID field will be filled with the newly
8301 * allocated responder XRI.
8302 */
8303 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8304 iocbq->sli4_xritag);
8305 } else {
8306 /* ABTS sent by responder to CT exchange, the
8307 * RX_ID field will be filled with the responder
8308 * RX_ID from ABTS.
8309 */
8310 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
546fc854 8311 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
5ffc266e 8312 }
6669f9bb
JS
8313 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8314 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6b5151fd
JS
8315
8316 /* Use CT=VPI */
8317 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8318 ndlp->nlp_DID);
8319 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8320 iocbq->iocb.ulpContext);
8321 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
6669f9bb 8322 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6b5151fd 8323 phba->vpi_ids[phba->pport->vpi]);
f0d9bccc
JS
8324 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8325 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8326 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
8327 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8328 command_type = OTHER_COMMAND;
546fc854
JS
8329 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8330 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8331 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8332 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8333 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8334 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8335 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8336 }
8337
7851fe2c 8338 break;
4f774513
JS
8339 case CMD_XRI_ABORTED_CX:
8340 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
8341 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8342 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8343 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8344 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8345 default:
8346 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8347 "2014 Invalid command 0x%x\n",
8348 iocbq->iocb.ulpCommand);
8349 return IOCB_ERROR;
7851fe2c 8350 break;
4f774513 8351 }
6d368e53 8352
8012cc38
JS
8353 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8354 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8355 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8356 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8357 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8358 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8359 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8360 LPFC_IO_DIF_INSERT);
f0d9bccc
JS
8361 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8362 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8363 wqe->generic.wqe_com.abort_tag = abort_tag;
8364 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8365 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8366 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8367 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
8368 return 0;
8369}
8370
8371/**
8372 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8373 * @phba: Pointer to HBA context object.
8374 * @ring_number: SLI ring number to issue iocb on.
8375 * @piocb: Pointer to command iocb.
8376 * @flag: Flag indicating if this command can be put into txq.
8377 *
8378 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8379 * an iocb command to an HBA with SLI-4 interface spec.
8380 *
8381 * This function is called with hbalock held. The function will return success
8382 * after it successfully submit the iocb to firmware or after adding to the
8383 * txq.
8384 **/
8385static int
8386__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8387 struct lpfc_iocbq *piocb, uint32_t flag)
8388{
8389 struct lpfc_sglq *sglq;
4f774513
JS
8390 union lpfc_wqe wqe;
8391 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
4f774513
JS
8392
8393 if (piocb->sli4_xritag == NO_XRI) {
8394 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6b5151fd 8395 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
8396 sglq = NULL;
8397 else {
0e9bb8d7 8398 if (!list_empty(&pring->txq)) {
2a9bf3d0
JS
8399 if (!(flag & SLI_IOCB_RET_IOCB)) {
8400 __lpfc_sli_ringtx_put(phba,
8401 pring, piocb);
8402 return IOCB_SUCCESS;
8403 } else {
8404 return IOCB_BUSY;
8405 }
8406 } else {
6d368e53 8407 sglq = __lpfc_sli_get_sglq(phba, piocb);
2a9bf3d0
JS
8408 if (!sglq) {
8409 if (!(flag & SLI_IOCB_RET_IOCB)) {
8410 __lpfc_sli_ringtx_put(phba,
8411 pring,
8412 piocb);
8413 return IOCB_SUCCESS;
8414 } else
8415 return IOCB_BUSY;
8416 }
8417 }
4f774513
JS
8418 }
8419 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6d368e53
JS
8420 /* These IO's already have an XRI and a mapped sgl. */
8421 sglq = NULL;
4f774513 8422 } else {
6d368e53
JS
8423 /*
8424 * This is a continuation of a commandi,(CX) so this
4f774513
JS
8425 * sglq is on the active list
8426 */
edccdc17 8427 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
4f774513
JS
8428 if (!sglq)
8429 return IOCB_ERROR;
8430 }
8431
8432 if (sglq) {
6d368e53 8433 piocb->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0 8434 piocb->sli4_xritag = sglq->sli4_xritag;
2a9bf3d0 8435 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
8436 return IOCB_ERROR;
8437 }
8438
8439 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8440 return IOCB_ERROR;
8441
341af102
JS
8442 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8443 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
5ffc266e
JS
8444 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8445 &wqe))
4f774513
JS
8446 return IOCB_ERROR;
8447 } else {
8448 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8449 return IOCB_ERROR;
8450 }
8451 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8452
8453 return 0;
8454}
8455
8456/**
8457 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8458 *
8459 * This routine wraps the actual lockless version for issusing IOCB function
8460 * pointer from the lpfc_hba struct.
8461 *
8462 * Return codes:
8463 * IOCB_ERROR - Error
8464 * IOCB_SUCCESS - Success
8465 * IOCB_BUSY - Busy
8466 **/
2a9bf3d0 8467int
4f774513
JS
8468__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8469 struct lpfc_iocbq *piocb, uint32_t flag)
8470{
8471 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8472}
8473
8474/**
25985edc 8475 * lpfc_sli_api_table_setup - Set up sli api function jump table
4f774513
JS
8476 * @phba: The hba struct for which this call is being executed.
8477 * @dev_grp: The HBA PCI-Device group number.
8478 *
8479 * This routine sets up the SLI interface API function jump table in @phba
8480 * struct.
8481 * Returns: 0 - success, -ENODEV - failure.
8482 **/
8483int
8484lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8485{
8486
8487 switch (dev_grp) {
8488 case LPFC_PCI_DEV_LP:
8489 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8490 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8491 break;
8492 case LPFC_PCI_DEV_OC:
8493 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8494 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8495 break;
8496 default:
8497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8498 "1419 Invalid HBA PCI-device group: 0x%x\n",
8499 dev_grp);
8500 return -ENODEV;
8501 break;
8502 }
8503 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8504 return 0;
8505}
8506
8507/**
8508 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
8509 * @phba: Pointer to HBA context object.
8510 * @pring: Pointer to driver SLI ring object.
8511 * @piocb: Pointer to command iocb.
8512 * @flag: Flag indicating if this command can be put into txq.
8513 *
8514 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8515 * function. This function gets the hbalock and calls
8516 * __lpfc_sli_issue_iocb function and will return the error returned
8517 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8518 * functions which do not hold hbalock.
8519 **/
8520int
8521lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8522 struct lpfc_iocbq *piocb, uint32_t flag)
8523{
ba20c853 8524 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
2a76a283 8525 struct lpfc_sli_ring *pring;
ba20c853
JS
8526 struct lpfc_queue *fpeq;
8527 struct lpfc_eqe *eqe;
4f774513 8528 unsigned long iflags;
2a76a283 8529 int rc, idx;
4f774513 8530
7e56aa25 8531 if (phba->sli_rev == LPFC_SLI_REV4) {
2a76a283
JS
8532 if (piocb->iocb_flag & LPFC_IO_FCP) {
8533 if (unlikely(!phba->sli4_hba.fcp_wq))
8534 return IOCB_ERROR;
8535 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8536 piocb->fcp_wqidx = idx;
8537 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
ba20c853
JS
8538
8539 pring = &phba->sli.ring[ring_number];
8540 spin_lock_irqsave(&pring->ring_lock, iflags);
8541 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8542 flag);
8543 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8544
8545 if (lpfc_fcp_look_ahead) {
8546 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
8547
8548 if (atomic_dec_and_test(&fcp_eq_hdl->
8549 fcp_eq_in_use)) {
4f774513 8550
ba20c853
JS
8551 /* Get associated EQ with this index */
8552 fpeq = phba->sli4_hba.hba_eq[idx];
8553
8554 /* Turn off interrupts from this EQ */
8555 lpfc_sli4_eq_clr_intr(fpeq);
8556
8557 /*
8558 * Process all the events on FCP EQ
8559 */
8560 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8561 lpfc_sli4_hba_handle_eqe(phba,
8562 eqe, idx);
8563 fpeq->EQ_processed++;
8564 }
8565
8566 /* Always clear and re-arm the EQ */
8567 lpfc_sli4_eq_release(fpeq,
8568 LPFC_QUEUE_REARM);
8569 }
8570 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
8571 }
8572 } else {
8573 pring = &phba->sli.ring[ring_number];
8574 spin_lock_irqsave(&pring->ring_lock, iflags);
8575 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8576 flag);
8577 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8578
2a76a283 8579 }
7e56aa25
JS
8580 } else {
8581 /* For now, SLI2/3 will still use hbalock */
8582 spin_lock_irqsave(&phba->hbalock, iflags);
8583 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8584 spin_unlock_irqrestore(&phba->hbalock, iflags);
8585 }
4f774513
JS
8586 return rc;
8587}
8588
8589/**
8590 * lpfc_extra_ring_setup - Extra ring setup function
8591 * @phba: Pointer to HBA context object.
8592 *
8593 * This function is called while driver attaches with the
8594 * HBA to setup the extra ring. The extra ring is used
8595 * only when driver needs to support target mode functionality
8596 * or IP over FC functionalities.
8597 *
8598 * This function is called with no lock held.
8599 **/
8600static int
8601lpfc_extra_ring_setup( struct lpfc_hba *phba)
8602{
8603 struct lpfc_sli *psli;
8604 struct lpfc_sli_ring *pring;
8605
8606 psli = &phba->sli;
8607
8608 /* Adjust cmd/rsp ring iocb entries more evenly */
8609
8610 /* Take some away from the FCP ring */
8611 pring = &psli->ring[psli->fcp_ring];
7e56aa25
JS
8612 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8613 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8614 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8615 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e 8616
a4bc3379
JS
8617 /* and give them to the extra ring */
8618 pring = &psli->ring[psli->extra_ring];
8619
7e56aa25
JS
8620 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8621 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8622 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8623 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e
JW
8624
8625 /* Setup default profile for this ring */
8626 pring->iotag_max = 4096;
8627 pring->num_mask = 1;
8628 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
8629 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8630 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
8631 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8632 return 0;
8633}
8634
cb69f7de
JS
8635/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8636 * @phba: Pointer to HBA context object.
8637 * @iocbq: Pointer to iocb object.
8638 *
8639 * The async_event handler calls this routine when it receives
8640 * an ASYNC_STATUS_CN event from the port. The port generates
8641 * this event when an Abort Sequence request to an rport fails
8642 * twice in succession. The abort could be originated by the
8643 * driver or by the port. The ABTS could have been for an ELS
8644 * or FCP IO. The port only generates this event when an ABTS
8645 * fails to complete after one retry.
8646 */
8647static void
8648lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8649 struct lpfc_iocbq *iocbq)
8650{
8651 struct lpfc_nodelist *ndlp = NULL;
8652 uint16_t rpi = 0, vpi = 0;
8653 struct lpfc_vport *vport = NULL;
8654
8655 /* The rpi in the ulpContext is vport-sensitive. */
8656 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8657 rpi = iocbq->iocb.ulpContext;
8658
8659 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8660 "3092 Port generated ABTS async event "
8661 "on vpi %d rpi %d status 0x%x\n",
8662 vpi, rpi, iocbq->iocb.ulpStatus);
8663
8664 vport = lpfc_find_vport_by_vpid(phba, vpi);
8665 if (!vport)
8666 goto err_exit;
8667 ndlp = lpfc_findnode_rpi(vport, rpi);
8668 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8669 goto err_exit;
8670
8671 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8672 lpfc_sli_abts_recover_port(vport, ndlp);
8673 return;
8674
8675 err_exit:
8676 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8677 "3095 Event Context not found, no "
8678 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8679 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8680 vpi, rpi);
8681}
8682
8683/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8684 * @phba: pointer to HBA context object.
8685 * @ndlp: nodelist pointer for the impacted rport.
8686 * @axri: pointer to the wcqe containing the failed exchange.
8687 *
8688 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8689 * port. The port generates this event when an abort exchange request to an
8690 * rport fails twice in succession with no reply. The abort could be originated
8691 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8692 */
8693void
8694lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8695 struct lpfc_nodelist *ndlp,
8696 struct sli4_wcqe_xri_aborted *axri)
8697{
8698 struct lpfc_vport *vport;
5c1db2ac 8699 uint32_t ext_status = 0;
cb69f7de 8700
6b5151fd 8701 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
cb69f7de
JS
8702 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8703 "3115 Node Context not found, driver "
8704 "ignoring abts err event\n");
6b5151fd
JS
8705 return;
8706 }
8707
cb69f7de
JS
8708 vport = ndlp->vport;
8709 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8710 "3116 Port generated FCP XRI ABORT event on "
5c1db2ac 8711 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
cb69f7de
JS
8712 ndlp->vport->vpi, ndlp->nlp_rpi,
8713 bf_get(lpfc_wcqe_xa_xri, axri),
5c1db2ac
JS
8714 bf_get(lpfc_wcqe_xa_status, axri),
8715 axri->parameter);
cb69f7de 8716
5c1db2ac
JS
8717 /*
8718 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8719 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8720 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8721 */
e3d2b802 8722 ext_status = axri->parameter & IOERR_PARAM_MASK;
5c1db2ac
JS
8723 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8724 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
cb69f7de
JS
8725 lpfc_sli_abts_recover_port(vport, ndlp);
8726}
8727
e59058c4 8728/**
3621a710 8729 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
8730 * @phba: Pointer to HBA context object.
8731 * @pring: Pointer to driver SLI ring object.
8732 * @iocbq: Pointer to iocb object.
8733 *
8734 * This function is called by the slow ring event handler
8735 * function when there is an ASYNC event iocb in the ring.
8736 * This function is called with no lock held.
8737 * Currently this function handles only temperature related
8738 * ASYNC events. The function decodes the temperature sensor
8739 * event message and posts events for the management applications.
8740 **/
98c9ea5c 8741static void
57127f15
JS
8742lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8743 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8744{
8745 IOCB_t *icmd;
8746 uint16_t evt_code;
57127f15
JS
8747 struct temp_event temp_event_data;
8748 struct Scsi_Host *shost;
a257bf90 8749 uint32_t *iocb_w;
57127f15
JS
8750
8751 icmd = &iocbq->iocb;
8752 evt_code = icmd->un.asyncstat.evt_code;
57127f15 8753
cb69f7de
JS
8754 switch (evt_code) {
8755 case ASYNC_TEMP_WARN:
8756 case ASYNC_TEMP_SAFE:
8757 temp_event_data.data = (uint32_t) icmd->ulpContext;
8758 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8759 if (evt_code == ASYNC_TEMP_WARN) {
8760 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8761 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8762 "0347 Adapter is very hot, please take "
8763 "corrective action. temperature : %d Celsius\n",
8764 (uint32_t) icmd->ulpContext);
8765 } else {
8766 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8767 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8768 "0340 Adapter temperature is OK now. "
8769 "temperature : %d Celsius\n",
8770 (uint32_t) icmd->ulpContext);
8771 }
8772
8773 /* Send temperature change event to applications */
8774 shost = lpfc_shost_from_vport(phba->pport);
8775 fc_host_post_vendor_event(shost, fc_get_event_number(),
8776 sizeof(temp_event_data), (char *) &temp_event_data,
8777 LPFC_NL_VENDOR_ID);
8778 break;
8779 case ASYNC_STATUS_CN:
8780 lpfc_sli_abts_err_handler(phba, iocbq);
8781 break;
8782 default:
a257bf90 8783 iocb_w = (uint32_t *) icmd;
cb69f7de 8784 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
76bb24ef 8785 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 8786 " evt_code 0x%x\n"
a257bf90
JS
8787 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8788 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8789 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8790 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
cb69f7de 8791 pring->ringno, icmd->un.asyncstat.evt_code,
a257bf90
JS
8792 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8793 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8794 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8795 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8796
cb69f7de 8797 break;
57127f15 8798 }
57127f15
JS
8799}
8800
8801
e59058c4 8802/**
3621a710 8803 * lpfc_sli_setup - SLI ring setup function
e59058c4
JS
8804 * @phba: Pointer to HBA context object.
8805 *
8806 * lpfc_sli_setup sets up rings of the SLI interface with
8807 * number of iocbs per ring and iotags. This function is
8808 * called while driver attach to the HBA and before the
8809 * interrupts are enabled. So there is no need for locking.
8810 *
8811 * This function always returns 0.
8812 **/
dea3101e
JB
8813int
8814lpfc_sli_setup(struct lpfc_hba *phba)
8815{
ed957684 8816 int i, totiocbsize = 0;
dea3101e
JB
8817 struct lpfc_sli *psli = &phba->sli;
8818 struct lpfc_sli_ring *pring;
8819
2a76a283
JS
8820 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
8821 if (phba->sli_rev == LPFC_SLI_REV4)
67d12733 8822 psli->num_rings += phba->cfg_fcp_io_channel;
dea3101e
JB
8823 psli->sli_flag = 0;
8824 psli->fcp_ring = LPFC_FCP_RING;
8825 psli->next_ring = LPFC_FCP_NEXT_RING;
a4bc3379 8826 psli->extra_ring = LPFC_EXTRA_RING;
dea3101e 8827
604a3e30
JB
8828 psli->iocbq_lookup = NULL;
8829 psli->iocbq_lookup_len = 0;
8830 psli->last_iotag = 0;
8831
dea3101e
JB
8832 for (i = 0; i < psli->num_rings; i++) {
8833 pring = &psli->ring[i];
8834 switch (i) {
8835 case LPFC_FCP_RING: /* ring 0 - FCP */
8836 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
8837 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8838 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8839 pring->sli.sli3.numCiocb +=
8840 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8841 pring->sli.sli3.numRiocb +=
8842 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8843 pring->sli.sli3.numCiocb +=
8844 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8845 pring->sli.sli3.numRiocb +=
8846 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8847 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8848 SLI3_IOCB_CMD_SIZE :
8849 SLI2_IOCB_CMD_SIZE;
7e56aa25 8850 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8851 SLI3_IOCB_RSP_SIZE :
8852 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
8853 pring->iotag_ctr = 0;
8854 pring->iotag_max =
92d7f7b0 8855 (phba->cfg_hba_queue_depth * 2);
dea3101e
JB
8856 pring->fast_iotag = pring->iotag_max;
8857 pring->num_mask = 0;
8858 break;
a4bc3379 8859 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 8860 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
8861 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8862 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8863 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8864 SLI3_IOCB_CMD_SIZE :
8865 SLI2_IOCB_CMD_SIZE;
7e56aa25 8866 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8867 SLI3_IOCB_RSP_SIZE :
8868 SLI2_IOCB_RSP_SIZE;
2e0fef85 8869 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e
JB
8870 pring->num_mask = 0;
8871 break;
8872 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8873 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
8874 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8875 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8876 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8877 SLI3_IOCB_CMD_SIZE :
8878 SLI2_IOCB_CMD_SIZE;
7e56aa25 8879 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8880 SLI3_IOCB_RSP_SIZE :
8881 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
8882 pring->fast_iotag = 0;
8883 pring->iotag_ctr = 0;
8884 pring->iotag_max = 4096;
57127f15
JS
8885 pring->lpfc_sli_rcv_async_status =
8886 lpfc_sli_async_event_handler;
6669f9bb 8887 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 8888 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
8889 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8890 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 8891 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 8892 lpfc_els_unsol_event;
dea3101e 8893 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
8894 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8895 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 8896 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 8897 lpfc_els_unsol_event;
dea3101e
JB
8898 pring->prt[2].profile = 0; /* Mask 2 */
8899 /* NameServer Inquiry */
6a9c52cf 8900 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 8901 /* NameServer */
6a9c52cf 8902 pring->prt[2].type = FC_TYPE_CT;
dea3101e 8903 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 8904 lpfc_ct_unsol_event;
dea3101e
JB
8905 pring->prt[3].profile = 0; /* Mask 3 */
8906 /* NameServer response */
6a9c52cf 8907 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 8908 /* NameServer */
6a9c52cf 8909 pring->prt[3].type = FC_TYPE_CT;
dea3101e 8910 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 8911 lpfc_ct_unsol_event;
dea3101e
JB
8912 break;
8913 }
7e56aa25
JS
8914 totiocbsize += (pring->sli.sli3.numCiocb *
8915 pring->sli.sli3.sizeCiocb) +
8916 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea3101e 8917 }
ed957684 8918 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 8919 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
8920 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8921 "SLI2 SLIM Data: x%x x%lx\n",
8922 phba->brd_no, totiocbsize,
8923 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 8924 }
cf5bf97e
JW
8925 if (phba->cfg_multi_ring_support == 2)
8926 lpfc_extra_ring_setup(phba);
dea3101e
JB
8927
8928 return 0;
8929}
8930
e59058c4 8931/**
3621a710 8932 * lpfc_sli_queue_setup - Queue initialization function
e59058c4
JS
8933 * @phba: Pointer to HBA context object.
8934 *
8935 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8936 * ring. This function also initializes ring indices of each ring.
8937 * This function is called during the initialization of the SLI
8938 * interface of an HBA.
8939 * This function is called with no lock held and always returns
8940 * 1.
8941 **/
dea3101e 8942int
2e0fef85 8943lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea3101e
JB
8944{
8945 struct lpfc_sli *psli;
8946 struct lpfc_sli_ring *pring;
604a3e30 8947 int i;
dea3101e
JB
8948
8949 psli = &phba->sli;
2e0fef85 8950 spin_lock_irq(&phba->hbalock);
dea3101e 8951 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 8952 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e
JB
8953 /* Initialize list headers for txq and txcmplq as double linked lists */
8954 for (i = 0; i < psli->num_rings; i++) {
8955 pring = &psli->ring[i];
8956 pring->ringno = i;
7e56aa25
JS
8957 pring->sli.sli3.next_cmdidx = 0;
8958 pring->sli.sli3.local_getidx = 0;
8959 pring->sli.sli3.cmdidx = 0;
dea3101e
JB
8960 INIT_LIST_HEAD(&pring->txq);
8961 INIT_LIST_HEAD(&pring->txcmplq);
8962 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 8963 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 8964 INIT_LIST_HEAD(&pring->postbufq);
7e56aa25 8965 spin_lock_init(&pring->ring_lock);
dea3101e 8966 }
2e0fef85
JS
8967 spin_unlock_irq(&phba->hbalock);
8968 return 1;
dea3101e
JB
8969}
8970
04c68496
JS
8971/**
8972 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8973 * @phba: Pointer to HBA context object.
8974 *
8975 * This routine flushes the mailbox command subsystem. It will unconditionally
8976 * flush all the mailbox commands in the three possible stages in the mailbox
8977 * command sub-system: pending mailbox command queue; the outstanding mailbox
8978 * command; and completed mailbox command queue. It is caller's responsibility
8979 * to make sure that the driver is in the proper state to flush the mailbox
8980 * command sub-system. Namely, the posting of mailbox commands into the
8981 * pending mailbox command queue from the various clients must be stopped;
8982 * either the HBA is in a state that it will never works on the outstanding
8983 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8984 * mailbox command has been completed.
8985 **/
8986static void
8987lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8988{
8989 LIST_HEAD(completions);
8990 struct lpfc_sli *psli = &phba->sli;
8991 LPFC_MBOXQ_t *pmb;
8992 unsigned long iflag;
8993
8994 /* Flush all the mailbox commands in the mbox system */
8995 spin_lock_irqsave(&phba->hbalock, iflag);
8996 /* The pending mailbox command queue */
8997 list_splice_init(&phba->sli.mboxq, &completions);
8998 /* The outstanding active mailbox command */
8999 if (psli->mbox_active) {
9000 list_add_tail(&psli->mbox_active->list, &completions);
9001 psli->mbox_active = NULL;
9002 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9003 }
9004 /* The completed mailbox command queue */
9005 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9006 spin_unlock_irqrestore(&phba->hbalock, iflag);
9007
9008 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9009 while (!list_empty(&completions)) {
9010 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9011 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9012 if (pmb->mbox_cmpl)
9013 pmb->mbox_cmpl(phba, pmb);
9014 }
9015}
9016
e59058c4 9017/**
3621a710 9018 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
9019 * @vport: Pointer to virtual port object.
9020 *
9021 * lpfc_sli_host_down is called to clean up the resources
9022 * associated with a vport before destroying virtual
9023 * port data structures.
9024 * This function does following operations:
9025 * - Free discovery resources associated with this virtual
9026 * port.
9027 * - Free iocbs associated with this virtual port in
9028 * the txq.
9029 * - Send abort for all iocb commands associated with this
9030 * vport in txcmplq.
9031 *
9032 * This function is called with no lock held and always returns 1.
9033 **/
92d7f7b0
JS
9034int
9035lpfc_sli_host_down(struct lpfc_vport *vport)
9036{
858c9f6c 9037 LIST_HEAD(completions);
92d7f7b0
JS
9038 struct lpfc_hba *phba = vport->phba;
9039 struct lpfc_sli *psli = &phba->sli;
9040 struct lpfc_sli_ring *pring;
9041 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
9042 int i;
9043 unsigned long flags = 0;
9044 uint16_t prev_pring_flag;
9045
9046 lpfc_cleanup_discovery_resources(vport);
9047
9048 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0
JS
9049 for (i = 0; i < psli->num_rings; i++) {
9050 pring = &psli->ring[i];
9051 prev_pring_flag = pring->flag;
5e9d9b82
JS
9052 /* Only slow rings */
9053 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 9054 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
9055 /* Set the lpfc data pending flag */
9056 set_bit(LPFC_DATA_READY, &phba->data_flags);
9057 }
92d7f7b0
JS
9058 /*
9059 * Error everything on the txq since these iocbs have not been
9060 * given to the FW yet.
9061 */
92d7f7b0
JS
9062 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9063 if (iocb->vport != vport)
9064 continue;
858c9f6c 9065 list_move_tail(&iocb->list, &completions);
92d7f7b0
JS
9066 }
9067
9068 /* Next issue ABTS for everything on the txcmplq */
9069 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9070 list) {
9071 if (iocb->vport != vport)
9072 continue;
9073 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9074 }
9075
9076 pring->flag = prev_pring_flag;
9077 }
9078
9079 spin_unlock_irqrestore(&phba->hbalock, flags);
9080
a257bf90
JS
9081 /* Cancel all the IOCBs from the completions list */
9082 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9083 IOERR_SLI_DOWN);
92d7f7b0
JS
9084 return 1;
9085}
9086
e59058c4 9087/**
3621a710 9088 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
9089 * @phba: Pointer to HBA context object.
9090 *
9091 * This function cleans up all iocb, buffers, mailbox commands
9092 * while shutting down the HBA. This function is called with no
9093 * lock held and always returns 1.
9094 * This function does the following to cleanup driver resources:
9095 * - Free discovery resources for each virtual port
9096 * - Cleanup any pending fabric iocbs
9097 * - Iterate through the iocb txq and free each entry
9098 * in the list.
9099 * - Free up any buffer posted to the HBA
9100 * - Free mailbox commands in the mailbox queue.
9101 **/
dea3101e 9102int
2e0fef85 9103lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 9104{
2534ba75 9105 LIST_HEAD(completions);
2e0fef85 9106 struct lpfc_sli *psli = &phba->sli;
dea3101e 9107 struct lpfc_sli_ring *pring;
0ff10d46 9108 struct lpfc_dmabuf *buf_ptr;
dea3101e 9109 unsigned long flags = 0;
04c68496
JS
9110 int i;
9111
9112 /* Shutdown the mailbox command sub-system */
618a5230 9113 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea3101e 9114
dea3101e
JB
9115 lpfc_hba_down_prep(phba);
9116
92d7f7b0
JS
9117 lpfc_fabric_abort_hba(phba);
9118
2e0fef85 9119 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e
JB
9120 for (i = 0; i < psli->num_rings; i++) {
9121 pring = &psli->ring[i];
5e9d9b82
JS
9122 /* Only slow rings */
9123 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 9124 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
9125 /* Set the lpfc data pending flag */
9126 set_bit(LPFC_DATA_READY, &phba->data_flags);
9127 }
dea3101e
JB
9128
9129 /*
9130 * Error everything on the txq since these iocbs have not been
9131 * given to the FW yet.
9132 */
2534ba75 9133 list_splice_init(&pring->txq, &completions);
2534ba75 9134 }
2e0fef85 9135 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 9136
a257bf90
JS
9137 /* Cancel all the IOCBs from the completions list */
9138 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9139 IOERR_SLI_DOWN);
dea3101e 9140
0ff10d46
JS
9141 spin_lock_irqsave(&phba->hbalock, flags);
9142 list_splice_init(&phba->elsbuf, &completions);
9143 phba->elsbuf_cnt = 0;
9144 phba->elsbuf_prev_cnt = 0;
9145 spin_unlock_irqrestore(&phba->hbalock, flags);
9146
9147 while (!list_empty(&completions)) {
9148 list_remove_head(&completions, buf_ptr,
9149 struct lpfc_dmabuf, list);
9150 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9151 kfree(buf_ptr);
9152 }
9153
dea3101e
JB
9154 /* Return any active mbox cmds */
9155 del_timer_sync(&psli->mbox_tmo);
2e0fef85 9156
da0436e9 9157 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 9158 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 9159 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 9160
da0436e9
JS
9161 return 1;
9162}
9163
e59058c4 9164/**
3621a710 9165 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
9166 * @srcp: Source memory pointer.
9167 * @destp: Destination memory pointer.
9168 * @cnt: Number of words required to be copied.
9169 *
9170 * This function is used for copying data between driver memory
9171 * and the SLI memory. This function also changes the endianness
9172 * of each word if native endianness is different from SLI
9173 * endianness. This function can be called with or without
9174 * lock.
9175 **/
dea3101e
JB
9176void
9177lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9178{
9179 uint32_t *src = srcp;
9180 uint32_t *dest = destp;
9181 uint32_t ldata;
9182 int i;
9183
9184 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9185 ldata = *src;
9186 ldata = le32_to_cpu(ldata);
9187 *dest = ldata;
9188 src++;
9189 dest++;
9190 }
9191}
9192
e59058c4 9193
a0c87cbd
JS
9194/**
9195 * lpfc_sli_bemem_bcopy - SLI memory copy function
9196 * @srcp: Source memory pointer.
9197 * @destp: Destination memory pointer.
9198 * @cnt: Number of words required to be copied.
9199 *
9200 * This function is used for copying data between a data structure
9201 * with big endian representation to local endianness.
9202 * This function can be called with or without lock.
9203 **/
9204void
9205lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9206{
9207 uint32_t *src = srcp;
9208 uint32_t *dest = destp;
9209 uint32_t ldata;
9210 int i;
9211
9212 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9213 ldata = *src;
9214 ldata = be32_to_cpu(ldata);
9215 *dest = ldata;
9216 src++;
9217 dest++;
9218 }
9219}
9220
e59058c4 9221/**
3621a710 9222 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
9223 * @phba: Pointer to HBA context object.
9224 * @pring: Pointer to driver SLI ring object.
9225 * @mp: Pointer to driver buffer object.
9226 *
9227 * This function is called with no lock held.
9228 * It always return zero after adding the buffer to the postbufq
9229 * buffer list.
9230 **/
dea3101e 9231int
2e0fef85
JS
9232lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9233 struct lpfc_dmabuf *mp)
dea3101e
JB
9234{
9235 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9236 later */
2e0fef85 9237 spin_lock_irq(&phba->hbalock);
dea3101e 9238 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 9239 pring->postbufq_cnt++;
2e0fef85 9240 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
9241 return 0;
9242}
9243
e59058c4 9244/**
3621a710 9245 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
9246 * @phba: Pointer to HBA context object.
9247 *
9248 * When HBQ is enabled, buffers are searched based on tags. This function
9249 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9250 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9251 * does not conflict with tags of buffer posted for unsolicited events.
9252 * The function returns the allocated tag. The function is called with
9253 * no locks held.
9254 **/
76bb24ef
JS
9255uint32_t
9256lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9257{
9258 spin_lock_irq(&phba->hbalock);
9259 phba->buffer_tag_count++;
9260 /*
9261 * Always set the QUE_BUFTAG_BIT to distiguish between
9262 * a tag assigned by HBQ.
9263 */
9264 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9265 spin_unlock_irq(&phba->hbalock);
9266 return phba->buffer_tag_count;
9267}
9268
e59058c4 9269/**
3621a710 9270 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
9271 * @phba: Pointer to HBA context object.
9272 * @pring: Pointer to driver SLI ring object.
9273 * @tag: Buffer tag.
9274 *
9275 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9276 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9277 * iocb is posted to the response ring with the tag of the buffer.
9278 * This function searches the pring->postbufq list using the tag
9279 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9280 * iocb. If the buffer is found then lpfc_dmabuf object of the
9281 * buffer is returned to the caller else NULL is returned.
9282 * This function is called with no lock held.
9283 **/
76bb24ef
JS
9284struct lpfc_dmabuf *
9285lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9286 uint32_t tag)
9287{
9288 struct lpfc_dmabuf *mp, *next_mp;
9289 struct list_head *slp = &pring->postbufq;
9290
25985edc 9291 /* Search postbufq, from the beginning, looking for a match on tag */
76bb24ef
JS
9292 spin_lock_irq(&phba->hbalock);
9293 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9294 if (mp->buffer_tag == tag) {
9295 list_del_init(&mp->list);
9296 pring->postbufq_cnt--;
9297 spin_unlock_irq(&phba->hbalock);
9298 return mp;
9299 }
9300 }
9301
9302 spin_unlock_irq(&phba->hbalock);
9303 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 9304 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
9305 "ring %d Data x%lx x%p x%p x%x\n",
9306 pring->ringno, (unsigned long) tag,
9307 slp->next, slp->prev, pring->postbufq_cnt);
9308
9309 return NULL;
9310}
dea3101e 9311
e59058c4 9312/**
3621a710 9313 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
9314 * @phba: Pointer to HBA context object.
9315 * @pring: Pointer to driver SLI ring object.
9316 * @phys: DMA address of the buffer.
9317 *
9318 * This function searches the buffer list using the dma_address
9319 * of unsolicited event to find the driver's lpfc_dmabuf object
9320 * corresponding to the dma_address. The function returns the
9321 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9322 * This function is called by the ct and els unsolicited event
9323 * handlers to get the buffer associated with the unsolicited
9324 * event.
9325 *
9326 * This function is called with no lock held.
9327 **/
dea3101e
JB
9328struct lpfc_dmabuf *
9329lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9330 dma_addr_t phys)
9331{
9332 struct lpfc_dmabuf *mp, *next_mp;
9333 struct list_head *slp = &pring->postbufq;
9334
25985edc 9335 /* Search postbufq, from the beginning, looking for a match on phys */
2e0fef85 9336 spin_lock_irq(&phba->hbalock);
dea3101e
JB
9337 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9338 if (mp->phys == phys) {
9339 list_del_init(&mp->list);
9340 pring->postbufq_cnt--;
2e0fef85 9341 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
9342 return mp;
9343 }
9344 }
9345
2e0fef85 9346 spin_unlock_irq(&phba->hbalock);
dea3101e 9347 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 9348 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 9349 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 9350 pring->ringno, (unsigned long long)phys,
dea3101e
JB
9351 slp->next, slp->prev, pring->postbufq_cnt);
9352 return NULL;
9353}
9354
e59058c4 9355/**
3621a710 9356 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
9357 * @phba: Pointer to HBA context object.
9358 * @cmdiocb: Pointer to driver command iocb object.
9359 * @rspiocb: Pointer to driver response iocb object.
9360 *
9361 * This function is the completion handler for the abort iocbs for
9362 * ELS commands. This function is called from the ELS ring event
9363 * handler with no lock held. This function frees memory resources
9364 * associated with the abort iocb.
9365 **/
dea3101e 9366static void
2e0fef85
JS
9367lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9368 struct lpfc_iocbq *rspiocb)
dea3101e 9369{
2e0fef85 9370 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 9371 uint16_t abort_iotag, abort_context;
ff78d8f9 9372 struct lpfc_iocbq *abort_iocb = NULL;
2680eeaa
JS
9373
9374 if (irsp->ulpStatus) {
ff78d8f9
JS
9375
9376 /*
9377 * Assume that the port already completed and returned, or
9378 * will return the iocb. Just Log the message.
9379 */
2680eeaa
JS
9380 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9381 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9382
2e0fef85 9383 spin_lock_irq(&phba->hbalock);
45ed1190
JS
9384 if (phba->sli_rev < LPFC_SLI_REV4) {
9385 if (abort_iotag != 0 &&
9386 abort_iotag <= phba->sli.last_iotag)
9387 abort_iocb =
9388 phba->sli.iocbq_lookup[abort_iotag];
9389 } else
9390 /* For sli4 the abort_tag is the XRI,
9391 * so the abort routine puts the iotag of the iocb
9392 * being aborted in the context field of the abort
9393 * IOCB.
9394 */
9395 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 9396
2a9bf3d0
JS
9397 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9398 "0327 Cannot abort els iocb %p "
9399 "with tag %x context %x, abort status %x, "
9400 "abort code %x\n",
9401 abort_iocb, abort_iotag, abort_context,
9402 irsp->ulpStatus, irsp->un.ulpWord[4]);
341af102 9403
ff78d8f9 9404 spin_unlock_irq(&phba->hbalock);
2680eeaa 9405 }
604a3e30 9406 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e
JB
9407 return;
9408}
9409
e59058c4 9410/**
3621a710 9411 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
9412 * @phba: Pointer to HBA context object.
9413 * @cmdiocb: Pointer to driver command iocb object.
9414 * @rspiocb: Pointer to driver response iocb object.
9415 *
9416 * The function is called from SLI ring event handler with no
9417 * lock held. This function is the completion handler for ELS commands
9418 * which are aborted. The function frees memory resources used for
9419 * the aborted ELS commands.
9420 **/
92d7f7b0
JS
9421static void
9422lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9423 struct lpfc_iocbq *rspiocb)
9424{
9425 IOCB_t *irsp = &rspiocb->iocb;
9426
9427 /* ELS cmd tag <ulpIoTag> completes */
9428 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 9429 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 9430 "x%x x%x x%x\n",
e8b62011 9431 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 9432 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
9433 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9434 lpfc_ct_free_iocb(phba, cmdiocb);
9435 else
9436 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
9437 return;
9438}
9439
e59058c4 9440/**
5af5eee7 9441 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
9442 * @phba: Pointer to HBA context object.
9443 * @pring: Pointer to driver SLI ring object.
9444 * @cmdiocb: Pointer to driver command iocb object.
9445 *
5af5eee7
JS
9446 * This function issues an abort iocb for the provided command iocb down to
9447 * the port. Other than the case the outstanding command iocb is an abort
9448 * request, this function issues abort out unconditionally. This function is
9449 * called with hbalock held. The function returns 0 when it fails due to
9450 * memory allocation failure or when the command iocb is an abort request.
e59058c4 9451 **/
5af5eee7
JS
9452static int
9453lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 9454 struct lpfc_iocbq *cmdiocb)
dea3101e 9455{
2e0fef85 9456 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 9457 struct lpfc_iocbq *abtsiocbp;
dea3101e
JB
9458 IOCB_t *icmd = NULL;
9459 IOCB_t *iabt = NULL;
5af5eee7 9460 int retval;
7e56aa25 9461 unsigned long iflags;
07951076 9462
92d7f7b0
JS
9463 /*
9464 * There are certain command types we don't want to abort. And we
9465 * don't want to abort commands that are already in the process of
9466 * being aborted.
07951076
JS
9467 */
9468 icmd = &cmdiocb->iocb;
2e0fef85 9469 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
9470 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9471 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
9472 return 0;
9473
dea3101e 9474 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 9475 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
9476 if (abtsiocbp == NULL)
9477 return 0;
dea3101e 9478
07951076 9479 /* This signals the response to set the correct status
341af102 9480 * before calling the completion handler
07951076
JS
9481 */
9482 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9483
dea3101e 9484 iabt = &abtsiocbp->iocb;
07951076
JS
9485 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9486 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 9487 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 9488 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
9489 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9490 }
da0436e9
JS
9491 else
9492 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
9493 iabt->ulpLe = 1;
9494 iabt->ulpClass = icmd->ulpClass;
dea3101e 9495
5ffc266e
JS
9496 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9497 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
341af102
JS
9498 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9499 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 9500
2e0fef85 9501 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
9502 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9503 else
9504 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 9505
07951076 9506 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
5b8bd0c9 9507
e8b62011
JS
9508 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9509 "0339 Abort xri x%x, original iotag x%x, "
9510 "abort cmd iotag x%x\n",
2a9bf3d0 9511 iabt->un.acxri.abortIoTag,
e8b62011 9512 iabt->un.acxri.abortContextTag,
2a9bf3d0 9513 abtsiocbp->iotag);
7e56aa25
JS
9514
9515 if (phba->sli_rev == LPFC_SLI_REV4) {
9516 /* Note: both hbalock and ring_lock need to be set here */
9517 spin_lock_irqsave(&pring->ring_lock, iflags);
9518 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9519 abtsiocbp, 0);
9520 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9521 } else {
9522 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9523 abtsiocbp, 0);
9524 }
dea3101e 9525
d7c255b2
JS
9526 if (retval)
9527 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
9528
9529 /*
9530 * Caller to this routine should check for IOCB_ERROR
9531 * and handle it properly. This routine no longer removes
9532 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9533 */
9534 return retval;
9535}
9536
9537/**
9538 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9539 * @phba: Pointer to HBA context object.
9540 * @pring: Pointer to driver SLI ring object.
9541 * @cmdiocb: Pointer to driver command iocb object.
9542 *
9543 * This function issues an abort iocb for the provided command iocb. In case
9544 * of unloading, the abort iocb will not be issued to commands on the ELS
9545 * ring. Instead, the callback function shall be changed to those commands
9546 * so that nothing happens when them finishes. This function is called with
9547 * hbalock held. The function returns 0 when the command iocb is an abort
9548 * request.
9549 **/
9550int
9551lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9552 struct lpfc_iocbq *cmdiocb)
9553{
9554 struct lpfc_vport *vport = cmdiocb->vport;
9555 int retval = IOCB_ERROR;
9556 IOCB_t *icmd = NULL;
9557
9558 /*
9559 * There are certain command types we don't want to abort. And we
9560 * don't want to abort commands that are already in the process of
9561 * being aborted.
9562 */
9563 icmd = &cmdiocb->iocb;
9564 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9565 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9566 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9567 return 0;
9568
9569 /*
9570 * If we're unloading, don't abort iocb on the ELS ring, but change
9571 * the callback so that nothing happens when it finishes.
9572 */
9573 if ((vport->load_flag & FC_UNLOADING) &&
9574 (pring->ringno == LPFC_ELS_RING)) {
9575 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9576 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9577 else
9578 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9579 goto abort_iotag_exit;
9580 }
9581
9582 /* Now, we try to issue the abort to the cmdiocb out */
9583 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9584
07951076 9585abort_iotag_exit:
2e0fef85
JS
9586 /*
9587 * Caller to this routine should check for IOCB_ERROR
9588 * and handle it properly. This routine no longer removes
9589 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 9590 */
2e0fef85 9591 return retval;
dea3101e
JB
9592}
9593
5af5eee7
JS
9594/**
9595 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9596 * @phba: Pointer to HBA context object.
9597 * @pring: Pointer to driver SLI ring object.
9598 *
9599 * This function aborts all iocbs in the given ring and frees all the iocb
9600 * objects in txq. This function issues abort iocbs unconditionally for all
9601 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9602 * to complete before the return of this function. The caller is not required
9603 * to hold any locks.
9604 **/
9605static void
9606lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9607{
9608 LIST_HEAD(completions);
9609 struct lpfc_iocbq *iocb, *next_iocb;
9610
9611 if (pring->ringno == LPFC_ELS_RING)
9612 lpfc_fabric_abort_hba(phba);
9613
9614 spin_lock_irq(&phba->hbalock);
9615
9616 /* Take off all the iocbs on txq for cancelling */
9617 list_splice_init(&pring->txq, &completions);
9618 pring->txq_cnt = 0;
9619
9620 /* Next issue ABTS for everything on the txcmplq */
9621 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9622 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9623
9624 spin_unlock_irq(&phba->hbalock);
9625
9626 /* Cancel all the IOCBs from the completions list */
9627 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9628 IOERR_SLI_ABORTED);
9629}
9630
9631/**
9632 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9633 * @phba: pointer to lpfc HBA data structure.
9634 *
9635 * This routine will abort all pending and outstanding iocbs to an HBA.
9636 **/
9637void
9638lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9639{
9640 struct lpfc_sli *psli = &phba->sli;
9641 struct lpfc_sli_ring *pring;
9642 int i;
9643
9644 for (i = 0; i < psli->num_rings; i++) {
9645 pring = &psli->ring[i];
9646 lpfc_sli_iocb_ring_abort(phba, pring);
9647 }
9648}
9649
e59058c4 9650/**
3621a710 9651 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
9652 * @iocbq: Pointer to driver iocb object.
9653 * @vport: Pointer to driver virtual port object.
9654 * @tgt_id: SCSI ID of the target.
9655 * @lun_id: LUN ID of the scsi device.
9656 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9657 *
3621a710 9658 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
9659 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9660 * 0 if the filtering criteria is met for the given iocb and will return
9661 * 1 if the filtering criteria is not met.
9662 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9663 * given iocb is for the SCSI device specified by vport, tgt_id and
9664 * lun_id parameter.
9665 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9666 * given iocb is for the SCSI target specified by vport and tgt_id
9667 * parameters.
9668 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9669 * given iocb is for the SCSI host associated with the given vport.
9670 * This function is called with no locks held.
9671 **/
dea3101e 9672static int
51ef4c26
JS
9673lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9674 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 9675 lpfc_ctx_cmd ctx_cmd)
dea3101e 9676{
0bd4ca25 9677 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e
JB
9678 int rc = 1;
9679
0bd4ca25
JSEC
9680 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9681 return rc;
9682
51ef4c26
JS
9683 if (iocbq->vport != vport)
9684 return rc;
9685
0bd4ca25 9686 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 9687
495a714c 9688 if (lpfc_cmd->pCmd == NULL)
dea3101e
JB
9689 return rc;
9690
9691 switch (ctx_cmd) {
9692 case LPFC_CTX_LUN:
495a714c
JS
9693 if ((lpfc_cmd->rdata->pnode) &&
9694 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9695 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e
JB
9696 rc = 0;
9697 break;
9698 case LPFC_CTX_TGT:
495a714c
JS
9699 if ((lpfc_cmd->rdata->pnode) &&
9700 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e
JB
9701 rc = 0;
9702 break;
dea3101e
JB
9703 case LPFC_CTX_HOST:
9704 rc = 0;
9705 break;
9706 default:
9707 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 9708 __func__, ctx_cmd);
dea3101e
JB
9709 break;
9710 }
9711
9712 return rc;
9713}
9714
e59058c4 9715/**
3621a710 9716 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
9717 * @vport: Pointer to virtual port.
9718 * @tgt_id: SCSI ID of the target.
9719 * @lun_id: LUN ID of the scsi device.
9720 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9721 *
9722 * This function returns number of FCP commands pending for the vport.
9723 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9724 * commands pending on the vport associated with SCSI device specified
9725 * by tgt_id and lun_id parameters.
9726 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9727 * commands pending on the vport associated with SCSI target specified
9728 * by tgt_id parameter.
9729 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9730 * commands pending on the vport.
9731 * This function returns the number of iocbs which satisfy the filter.
9732 * This function is called without any lock held.
9733 **/
dea3101e 9734int
51ef4c26
JS
9735lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9736 lpfc_ctx_cmd ctx_cmd)
dea3101e 9737{
51ef4c26 9738 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
9739 struct lpfc_iocbq *iocbq;
9740 int sum, i;
dea3101e 9741
0bd4ca25
JSEC
9742 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9743 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 9744
51ef4c26
JS
9745 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9746 ctx_cmd) == 0)
0bd4ca25 9747 sum++;
dea3101e 9748 }
0bd4ca25 9749
dea3101e
JB
9750 return sum;
9751}
9752
e59058c4 9753/**
3621a710 9754 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
9755 * @phba: Pointer to HBA context object
9756 * @cmdiocb: Pointer to command iocb object.
9757 * @rspiocb: Pointer to response iocb object.
9758 *
9759 * This function is called when an aborted FCP iocb completes. This
9760 * function is called by the ring event handler with no lock held.
9761 * This function frees the iocb.
9762 **/
5eb95af0 9763void
2e0fef85
JS
9764lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9765 struct lpfc_iocbq *rspiocb)
5eb95af0 9766{
cb69f7de
JS
9767 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9768 "3096 ABORT_XRI_CN completing on xri x%x "
9769 "original iotag x%x, abort cmd iotag x%x "
9770 "status 0x%x, reason 0x%x\n",
9771 cmdiocb->iocb.un.acxri.abortContextTag,
9772 cmdiocb->iocb.un.acxri.abortIoTag,
9773 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9774 rspiocb->iocb.un.ulpWord[4]);
604a3e30 9775 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
9776 return;
9777}
9778
e59058c4 9779/**
3621a710 9780 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
9781 * @vport: Pointer to virtual port.
9782 * @pring: Pointer to driver SLI ring object.
9783 * @tgt_id: SCSI ID of the target.
9784 * @lun_id: LUN ID of the scsi device.
9785 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9786 *
9787 * This function sends an abort command for every SCSI command
9788 * associated with the given virtual port pending on the ring
9789 * filtered by lpfc_sli_validate_fcp_iocb function.
9790 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9791 * FCP iocbs associated with lun specified by tgt_id and lun_id
9792 * parameters
9793 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9794 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9795 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9796 * FCP iocbs associated with virtual port.
9797 * This function returns number of iocbs it failed to abort.
9798 * This function is called with no locks held.
9799 **/
dea3101e 9800int
51ef4c26
JS
9801lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9802 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 9803{
51ef4c26 9804 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
9805 struct lpfc_iocbq *iocbq;
9806 struct lpfc_iocbq *abtsiocb;
dea3101e 9807 IOCB_t *cmd = NULL;
dea3101e 9808 int errcnt = 0, ret_val = 0;
0bd4ca25 9809 int i;
dea3101e 9810
0bd4ca25
JSEC
9811 for (i = 1; i <= phba->sli.last_iotag; i++) {
9812 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 9813
51ef4c26 9814 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 9815 abort_cmd) != 0)
dea3101e
JB
9816 continue;
9817
9818 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 9819 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
9820 if (abtsiocb == NULL) {
9821 errcnt++;
9822 continue;
9823 }
dea3101e 9824
0bd4ca25 9825 cmd = &iocbq->iocb;
dea3101e
JB
9826 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9827 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
9828 if (phba->sli_rev == LPFC_SLI_REV4)
9829 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9830 else
9831 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e
JB
9832 abtsiocb->iocb.ulpLe = 1;
9833 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2e0fef85 9834 abtsiocb->vport = phba->pport;
dea3101e 9835
5ffc266e
JS
9836 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9837 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
341af102
JS
9838 if (iocbq->iocb_flag & LPFC_IO_FCP)
9839 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 9840
2e0fef85 9841 if (lpfc_is_link_up(phba))
dea3101e
JB
9842 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9843 else
9844 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9845
5eb95af0
JSEC
9846 /* Setup callback routine and issue the command. */
9847 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
da0436e9
JS
9848 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9849 abtsiocb, 0);
dea3101e 9850 if (ret_val == IOCB_ERROR) {
604a3e30 9851 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e
JB
9852 errcnt++;
9853 continue;
9854 }
9855 }
9856
9857 return errcnt;
9858}
9859
e59058c4 9860/**
3621a710 9861 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
9862 * @phba: Pointer to HBA context object.
9863 * @cmdiocbq: Pointer to command iocb.
9864 * @rspiocbq: Pointer to response iocb.
9865 *
9866 * This function is the completion handler for iocbs issued using
9867 * lpfc_sli_issue_iocb_wait function. This function is called by the
9868 * ring event handler function without any lock held. This function
9869 * can be called from both worker thread context and interrupt
9870 * context. This function also can be called from other thread which
9871 * cleans up the SLI layer objects.
9872 * This function copy the contents of the response iocb to the
9873 * response iocb memory object provided by the caller of
9874 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9875 * sleeps for the iocb completion.
9876 **/
68876920
JSEC
9877static void
9878lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9879 struct lpfc_iocbq *cmdiocbq,
9880 struct lpfc_iocbq *rspiocbq)
dea3101e 9881{
68876920
JSEC
9882 wait_queue_head_t *pdone_q;
9883 unsigned long iflags;
0f65ff68 9884 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 9885
2e0fef85 9886 spin_lock_irqsave(&phba->hbalock, iflags);
68876920
JSEC
9887 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9888 if (cmdiocbq->context2 && rspiocbq)
9889 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9890 &rspiocbq->iocb, sizeof(IOCB_t));
9891
0f65ff68
JS
9892 /* Set the exchange busy flag for task management commands */
9893 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9894 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9895 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9896 cur_iocbq);
9897 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9898 }
9899
68876920 9900 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
9901 if (pdone_q)
9902 wake_up(pdone_q);
858c9f6c 9903 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e
JB
9904 return;
9905}
9906
d11e31dd
JS
9907/**
9908 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9909 * @phba: Pointer to HBA context object..
9910 * @piocbq: Pointer to command iocb.
9911 * @flag: Flag to test.
9912 *
9913 * This routine grabs the hbalock and then test the iocb_flag to
9914 * see if the passed in flag is set.
9915 * Returns:
9916 * 1 if flag is set.
9917 * 0 if flag is not set.
9918 **/
9919static int
9920lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9921 struct lpfc_iocbq *piocbq, uint32_t flag)
9922{
9923 unsigned long iflags;
9924 int ret;
9925
9926 spin_lock_irqsave(&phba->hbalock, iflags);
9927 ret = piocbq->iocb_flag & flag;
9928 spin_unlock_irqrestore(&phba->hbalock, iflags);
9929 return ret;
9930
9931}
9932
e59058c4 9933/**
3621a710 9934 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
9935 * @phba: Pointer to HBA context object..
9936 * @pring: Pointer to sli ring.
9937 * @piocb: Pointer to command iocb.
9938 * @prspiocbq: Pointer to response iocb.
9939 * @timeout: Timeout in number of seconds.
9940 *
9941 * This function issues the iocb to firmware and waits for the
9942 * iocb to complete. If the iocb command is not
9943 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9944 * Caller should not free the iocb resources if this function
9945 * returns IOCB_TIMEDOUT.
9946 * The function waits for the iocb completion using an
9947 * non-interruptible wait.
9948 * This function will sleep while waiting for iocb completion.
9949 * So, this function should not be called from any context which
9950 * does not allow sleeping. Due to the same reason, this function
9951 * cannot be called with interrupt disabled.
9952 * This function assumes that the iocb completions occur while
9953 * this function sleep. So, this function cannot be called from
9954 * the thread which process iocb completion for this ring.
9955 * This function clears the iocb_flag of the iocb object before
9956 * issuing the iocb and the iocb completion handler sets this
9957 * flag and wakes this thread when the iocb completes.
9958 * The contents of the response iocb will be copied to prspiocbq
9959 * by the completion handler when the command completes.
9960 * This function returns IOCB_SUCCESS when success.
9961 * This function is called with no lock held.
9962 **/
dea3101e 9963int
2e0fef85 9964lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 9965 uint32_t ring_number,
2e0fef85
JS
9966 struct lpfc_iocbq *piocb,
9967 struct lpfc_iocbq *prspiocbq,
68876920 9968 uint32_t timeout)
dea3101e 9969{
7259f0d0 9970 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
9971 long timeleft, timeout_req = 0;
9972 int retval = IOCB_SUCCESS;
875fbdfe 9973 uint32_t creg_val;
0e9bb8d7
JS
9974 struct lpfc_iocbq *iocb;
9975 int txq_cnt = 0;
9976 int txcmplq_cnt = 0;
2a9bf3d0 9977 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea3101e 9978 /*
68876920
JSEC
9979 * If the caller has provided a response iocbq buffer, then context2
9980 * is NULL or its an error.
dea3101e 9981 */
68876920
JSEC
9982 if (prspiocbq) {
9983 if (piocb->context2)
9984 return IOCB_ERROR;
9985 piocb->context2 = prspiocbq;
dea3101e
JB
9986 }
9987
68876920
JSEC
9988 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9989 piocb->context_un.wait_queue = &done_q;
9990 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea3101e 9991
875fbdfe 9992 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
9993 if (lpfc_readl(phba->HCregaddr, &creg_val))
9994 return IOCB_ERROR;
875fbdfe
JSEC
9995 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9996 writel(creg_val, phba->HCregaddr);
9997 readl(phba->HCregaddr); /* flush */
9998 }
9999
2a9bf3d0
JS
10000 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10001 SLI_IOCB_RET_IOCB);
68876920
JSEC
10002 if (retval == IOCB_SUCCESS) {
10003 timeout_req = timeout * HZ;
68876920 10004 timeleft = wait_event_timeout(done_q,
d11e31dd 10005 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 10006 timeout_req);
dea3101e 10007
7054a606
JS
10008 if (piocb->iocb_flag & LPFC_IO_WAKE) {
10009 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 10010 "0331 IOCB wake signaled\n");
7054a606 10011 } else if (timeleft == 0) {
68876920 10012 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
10013 "0338 IOCB wait timeout error - no "
10014 "wake response Data x%x\n", timeout);
68876920 10015 retval = IOCB_TIMEDOUT;
7054a606 10016 } else {
68876920 10017 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
10018 "0330 IOCB wake NOT set, "
10019 "Data x%x x%lx\n",
68876920
JSEC
10020 timeout, (timeleft / jiffies));
10021 retval = IOCB_TIMEDOUT;
dea3101e 10022 }
2a9bf3d0 10023 } else if (retval == IOCB_BUSY) {
0e9bb8d7
JS
10024 if (phba->cfg_log_verbose & LOG_SLI) {
10025 list_for_each_entry(iocb, &pring->txq, list) {
10026 txq_cnt++;
10027 }
10028 list_for_each_entry(iocb, &pring->txcmplq, list) {
10029 txcmplq_cnt++;
10030 }
10031 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10032 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10033 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10034 }
2a9bf3d0 10035 return retval;
68876920
JSEC
10036 } else {
10037 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 10038 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 10039 retval);
68876920 10040 retval = IOCB_ERROR;
dea3101e
JB
10041 }
10042
875fbdfe 10043 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
10044 if (lpfc_readl(phba->HCregaddr, &creg_val))
10045 return IOCB_ERROR;
875fbdfe
JSEC
10046 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10047 writel(creg_val, phba->HCregaddr);
10048 readl(phba->HCregaddr); /* flush */
10049 }
10050
68876920
JSEC
10051 if (prspiocbq)
10052 piocb->context2 = NULL;
10053
10054 piocb->context_un.wait_queue = NULL;
10055 piocb->iocb_cmpl = NULL;
dea3101e
JB
10056 return retval;
10057}
68876920 10058
e59058c4 10059/**
3621a710 10060 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
10061 * @phba: Pointer to HBA context object.
10062 * @pmboxq: Pointer to driver mailbox object.
10063 * @timeout: Timeout in number of seconds.
10064 *
10065 * This function issues the mailbox to firmware and waits for the
10066 * mailbox command to complete. If the mailbox command is not
10067 * completed within timeout seconds, it returns MBX_TIMEOUT.
10068 * The function waits for the mailbox completion using an
10069 * interruptible wait. If the thread is woken up due to a
10070 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10071 * should not free the mailbox resources, if this function returns
10072 * MBX_TIMEOUT.
10073 * This function will sleep while waiting for mailbox completion.
10074 * So, this function should not be called from any context which
10075 * does not allow sleeping. Due to the same reason, this function
10076 * cannot be called with interrupt disabled.
10077 * This function assumes that the mailbox completion occurs while
10078 * this function sleep. So, this function cannot be called from
10079 * the worker thread which processes mailbox completion.
10080 * This function is called in the context of HBA management
10081 * applications.
10082 * This function returns MBX_SUCCESS when successful.
10083 * This function is called with no lock held.
10084 **/
dea3101e 10085int
2e0fef85 10086lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e
JB
10087 uint32_t timeout)
10088{
7259f0d0 10089 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea3101e 10090 int retval;
858c9f6c 10091 unsigned long flag;
dea3101e
JB
10092
10093 /* The caller must leave context1 empty. */
98c9ea5c 10094 if (pmboxq->context1)
2e0fef85 10095 return MBX_NOT_FINISHED;
dea3101e 10096
495a714c 10097 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e
JB
10098 /* setup wake call as IOCB callback */
10099 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10100 /* setup context field to pass wait_queue pointer to wake function */
10101 pmboxq->context1 = &done_q;
10102
dea3101e
JB
10103 /* now issue the command */
10104 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea3101e 10105 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
10106 wait_event_interruptible_timeout(done_q,
10107 pmboxq->mbox_flag & LPFC_MBX_WAKE,
10108 timeout * HZ);
10109
858c9f6c 10110 spin_lock_irqsave(&phba->hbalock, flag);
dea3101e 10111 pmboxq->context1 = NULL;
7054a606
JS
10112 /*
10113 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10114 * else do not free the resources.
10115 */
d7c47992 10116 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 10117 retval = MBX_SUCCESS;
d7c47992
JS
10118 lpfc_sli4_swap_str(phba, pmboxq);
10119 } else {
7054a606 10120 retval = MBX_TIMEOUT;
858c9f6c
JS
10121 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10122 }
10123 spin_unlock_irqrestore(&phba->hbalock, flag);
dea3101e
JB
10124 }
10125
dea3101e
JB
10126 return retval;
10127}
10128
e59058c4 10129/**
3772a991 10130 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
10131 * @phba: Pointer to HBA context.
10132 *
3772a991
JS
10133 * This function is called to shutdown the driver's mailbox sub-system.
10134 * It first marks the mailbox sub-system is in a block state to prevent
10135 * the asynchronous mailbox command from issued off the pending mailbox
10136 * command queue. If the mailbox command sub-system shutdown is due to
10137 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10138 * the mailbox sub-system flush routine to forcefully bring down the
10139 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10140 * as with offline or HBA function reset), this routine will wait for the
10141 * outstanding mailbox command to complete before invoking the mailbox
10142 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 10143 **/
3772a991 10144void
618a5230 10145lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
b4c02652 10146{
3772a991 10147 struct lpfc_sli *psli = &phba->sli;
3772a991 10148 unsigned long timeout;
b4c02652 10149
618a5230
JS
10150 if (mbx_action == LPFC_MBX_NO_WAIT) {
10151 /* delay 100ms for port state */
10152 msleep(100);
10153 lpfc_sli_mbox_sys_flush(phba);
10154 return;
10155 }
a183a15f 10156 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
d7069f09 10157
3772a991
JS
10158 spin_lock_irq(&phba->hbalock);
10159 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
b4c02652 10160
3772a991 10161 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
3772a991
JS
10162 /* Determine how long we might wait for the active mailbox
10163 * command to be gracefully completed by firmware.
10164 */
a183a15f
JS
10165 if (phba->sli.mbox_active)
10166 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10167 phba->sli.mbox_active) *
10168 1000) + jiffies;
10169 spin_unlock_irq(&phba->hbalock);
10170
3772a991
JS
10171 while (phba->sli.mbox_active) {
10172 /* Check active mailbox complete status every 2ms */
10173 msleep(2);
10174 if (time_after(jiffies, timeout))
10175 /* Timeout, let the mailbox flush routine to
10176 * forcefully release active mailbox command
10177 */
10178 break;
10179 }
d7069f09
JS
10180 } else
10181 spin_unlock_irq(&phba->hbalock);
10182
3772a991
JS
10183 lpfc_sli_mbox_sys_flush(phba);
10184}
ed957684 10185
3772a991
JS
10186/**
10187 * lpfc_sli_eratt_read - read sli-3 error attention events
10188 * @phba: Pointer to HBA context.
10189 *
10190 * This function is called to read the SLI3 device error attention registers
10191 * for possible error attention events. The caller must hold the hostlock
10192 * with spin_lock_irq().
10193 *
25985edc 10194 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
10195 * Register and returns 0 otherwise.
10196 **/
10197static int
10198lpfc_sli_eratt_read(struct lpfc_hba *phba)
10199{
10200 uint32_t ha_copy;
b4c02652 10201
3772a991 10202 /* Read chip Host Attention (HA) register */
9940b97b
JS
10203 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10204 goto unplug_err;
10205
3772a991
JS
10206 if (ha_copy & HA_ERATT) {
10207 /* Read host status register to retrieve error event */
9940b97b
JS
10208 if (lpfc_sli_read_hs(phba))
10209 goto unplug_err;
b4c02652 10210
3772a991
JS
10211 /* Check if there is a deferred error condition is active */
10212 if ((HS_FFER1 & phba->work_hs) &&
10213 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 10214 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 10215 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
10216 /* Clear all interrupt enable conditions */
10217 writel(0, phba->HCregaddr);
10218 readl(phba->HCregaddr);
10219 }
10220
10221 /* Set the driver HA work bitmap */
3772a991
JS
10222 phba->work_ha |= HA_ERATT;
10223 /* Indicate polling handles this ERATT */
10224 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
10225 return 1;
10226 }
10227 return 0;
9940b97b
JS
10228
10229unplug_err:
10230 /* Set the driver HS work bitmap */
10231 phba->work_hs |= UNPLUG_ERR;
10232 /* Set the driver HA work bitmap */
10233 phba->work_ha |= HA_ERATT;
10234 /* Indicate polling handles this ERATT */
10235 phba->hba_flag |= HBA_ERATT_HANDLED;
10236 return 1;
b4c02652
JS
10237}
10238
da0436e9
JS
10239/**
10240 * lpfc_sli4_eratt_read - read sli-4 error attention events
10241 * @phba: Pointer to HBA context.
10242 *
10243 * This function is called to read the SLI4 device error attention registers
10244 * for possible error attention events. The caller must hold the hostlock
10245 * with spin_lock_irq().
10246 *
25985edc 10247 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
10248 * Register and returns 0 otherwise.
10249 **/
10250static int
10251lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10252{
10253 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
10254 uint32_t if_type, portsmphr;
10255 struct lpfc_register portstat_reg;
da0436e9 10256
2fcee4bf
JS
10257 /*
10258 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
10259 * registers for error attention. This can be changed later.
10260 */
2fcee4bf
JS
10261 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10262 switch (if_type) {
10263 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
10264 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10265 &uerr_sta_lo) ||
10266 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10267 &uerr_sta_hi)) {
10268 phba->work_hs |= UNPLUG_ERR;
10269 phba->work_ha |= HA_ERATT;
10270 phba->hba_flag |= HBA_ERATT_HANDLED;
10271 return 1;
10272 }
2fcee4bf
JS
10273 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10274 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10276 "1423 HBA Unrecoverable error: "
10277 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10278 "ue_mask_lo_reg=0x%x, "
10279 "ue_mask_hi_reg=0x%x\n",
10280 uerr_sta_lo, uerr_sta_hi,
10281 phba->sli4_hba.ue_mask_lo,
10282 phba->sli4_hba.ue_mask_hi);
10283 phba->work_status[0] = uerr_sta_lo;
10284 phba->work_status[1] = uerr_sta_hi;
10285 phba->work_ha |= HA_ERATT;
10286 phba->hba_flag |= HBA_ERATT_HANDLED;
10287 return 1;
10288 }
10289 break;
10290 case LPFC_SLI_INTF_IF_TYPE_2:
9940b97b
JS
10291 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10292 &portstat_reg.word0) ||
10293 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10294 &portsmphr)){
10295 phba->work_hs |= UNPLUG_ERR;
10296 phba->work_ha |= HA_ERATT;
10297 phba->hba_flag |= HBA_ERATT_HANDLED;
10298 return 1;
10299 }
2fcee4bf
JS
10300 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10301 phba->work_status[0] =
10302 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10303 phba->work_status[1] =
10304 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2e90f4b5 10306 "2885 Port Status Event: "
2fcee4bf
JS
10307 "port status reg 0x%x, "
10308 "port smphr reg 0x%x, "
10309 "error 1=0x%x, error 2=0x%x\n",
10310 portstat_reg.word0,
10311 portsmphr,
10312 phba->work_status[0],
10313 phba->work_status[1]);
10314 phba->work_ha |= HA_ERATT;
10315 phba->hba_flag |= HBA_ERATT_HANDLED;
10316 return 1;
10317 }
10318 break;
10319 case LPFC_SLI_INTF_IF_TYPE_1:
10320 default:
a747c9ce 10321 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
10322 "2886 HBA Error Attention on unsupported "
10323 "if type %d.", if_type);
a747c9ce 10324 return 1;
da0436e9 10325 }
2fcee4bf 10326
da0436e9
JS
10327 return 0;
10328}
10329
e59058c4 10330/**
3621a710 10331 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
10332 * @phba: Pointer to HBA context.
10333 *
3772a991 10334 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
10335 * error attention register bit for error attention events.
10336 *
25985edc 10337 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
10338 * Register and returns 0 otherwise.
10339 **/
10340int
10341lpfc_sli_check_eratt(struct lpfc_hba *phba)
10342{
10343 uint32_t ha_copy;
10344
10345 /* If somebody is waiting to handle an eratt, don't process it
10346 * here. The brdkill function will do this.
10347 */
10348 if (phba->link_flag & LS_IGNORE_ERATT)
10349 return 0;
10350
10351 /* Check if interrupt handler handles this ERATT */
10352 spin_lock_irq(&phba->hbalock);
10353 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10354 /* Interrupt handler has handled ERATT */
10355 spin_unlock_irq(&phba->hbalock);
10356 return 0;
10357 }
10358
a257bf90
JS
10359 /*
10360 * If there is deferred error attention, do not check for error
10361 * attention
10362 */
10363 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10364 spin_unlock_irq(&phba->hbalock);
10365 return 0;
10366 }
10367
3772a991
JS
10368 /* If PCI channel is offline, don't process it */
10369 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 10370 spin_unlock_irq(&phba->hbalock);
3772a991
JS
10371 return 0;
10372 }
10373
10374 switch (phba->sli_rev) {
10375 case LPFC_SLI_REV2:
10376 case LPFC_SLI_REV3:
10377 /* Read chip Host Attention (HA) register */
10378 ha_copy = lpfc_sli_eratt_read(phba);
10379 break;
da0436e9 10380 case LPFC_SLI_REV4:
2fcee4bf 10381 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
10382 ha_copy = lpfc_sli4_eratt_read(phba);
10383 break;
3772a991
JS
10384 default:
10385 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10386 "0299 Invalid SLI revision (%d)\n",
10387 phba->sli_rev);
10388 ha_copy = 0;
10389 break;
9399627f
JS
10390 }
10391 spin_unlock_irq(&phba->hbalock);
3772a991
JS
10392
10393 return ha_copy;
10394}
10395
10396/**
10397 * lpfc_intr_state_check - Check device state for interrupt handling
10398 * @phba: Pointer to HBA context.
10399 *
10400 * This inline routine checks whether a device or its PCI slot is in a state
10401 * that the interrupt should be handled.
10402 *
10403 * This function returns 0 if the device or the PCI slot is in a state that
10404 * interrupt should be handled, otherwise -EIO.
10405 */
10406static inline int
10407lpfc_intr_state_check(struct lpfc_hba *phba)
10408{
10409 /* If the pci channel is offline, ignore all the interrupts */
10410 if (unlikely(pci_channel_offline(phba->pcidev)))
10411 return -EIO;
10412
10413 /* Update device level interrupt statistics */
10414 phba->sli.slistat.sli_intr++;
10415
10416 /* Ignore all interrupts during initialization. */
10417 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10418 return -EIO;
10419
9399627f
JS
10420 return 0;
10421}
10422
10423/**
3772a991 10424 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
10425 * @irq: Interrupt number.
10426 * @dev_id: The device context pointer.
10427 *
9399627f 10428 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
10429 * service routine when device with SLI-3 interface spec is enabled with
10430 * MSI-X multi-message interrupt mode and there are slow-path events in
10431 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10432 * interrupt mode, this function is called as part of the device-level
10433 * interrupt handler. When the PCI slot is in error recovery or the HBA
10434 * is undergoing initialization, the interrupt handler will not process
10435 * the interrupt. The link attention and ELS ring attention events are
10436 * handled by the worker thread. The interrupt handler signals the worker
10437 * thread and returns for these events. This function is called without
10438 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
10439 * structures.
10440 *
10441 * This function returns IRQ_HANDLED when interrupt is handled else it
10442 * returns IRQ_NONE.
e59058c4 10443 **/
dea3101e 10444irqreturn_t
3772a991 10445lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 10446{
2e0fef85 10447 struct lpfc_hba *phba;
a747c9ce 10448 uint32_t ha_copy, hc_copy;
dea3101e
JB
10449 uint32_t work_ha_copy;
10450 unsigned long status;
5b75da2f 10451 unsigned long iflag;
dea3101e
JB
10452 uint32_t control;
10453
92d7f7b0 10454 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
10455 struct lpfc_vport *vport;
10456 struct lpfc_nodelist *ndlp;
10457 struct lpfc_dmabuf *mp;
92d7f7b0
JS
10458 LPFC_MBOXQ_t *pmb;
10459 int rc;
10460
dea3101e
JB
10461 /*
10462 * Get the driver's phba structure from the dev_id and
10463 * assume the HBA is not interrupting.
10464 */
9399627f 10465 phba = (struct lpfc_hba *)dev_id;
dea3101e
JB
10466
10467 if (unlikely(!phba))
10468 return IRQ_NONE;
10469
dea3101e 10470 /*
9399627f
JS
10471 * Stuff needs to be attented to when this function is invoked as an
10472 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 10473 */
9399627f 10474 if (phba->intr_type == MSIX) {
3772a991
JS
10475 /* Check device state for handling interrupt */
10476 if (lpfc_intr_state_check(phba))
9399627f
JS
10477 return IRQ_NONE;
10478 /* Need to read HA REG for slow-path events */
5b75da2f 10479 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
10480 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10481 goto unplug_error;
9399627f
JS
10482 /* If somebody is waiting to handle an eratt don't process it
10483 * here. The brdkill function will do this.
10484 */
10485 if (phba->link_flag & LS_IGNORE_ERATT)
10486 ha_copy &= ~HA_ERATT;
10487 /* Check the need for handling ERATT in interrupt handler */
10488 if (ha_copy & HA_ERATT) {
10489 if (phba->hba_flag & HBA_ERATT_HANDLED)
10490 /* ERATT polling has handled ERATT */
10491 ha_copy &= ~HA_ERATT;
10492 else
10493 /* Indicate interrupt handler handles ERATT */
10494 phba->hba_flag |= HBA_ERATT_HANDLED;
10495 }
a257bf90
JS
10496
10497 /*
10498 * If there is deferred error attention, do not check for any
10499 * interrupt.
10500 */
10501 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 10502 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
10503 return IRQ_NONE;
10504 }
10505
9399627f 10506 /* Clear up only attention source related to slow-path */
9940b97b
JS
10507 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10508 goto unplug_error;
10509
a747c9ce
JS
10510 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10511 HC_LAINT_ENA | HC_ERINT_ENA),
10512 phba->HCregaddr);
9399627f
JS
10513 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10514 phba->HAregaddr);
a747c9ce 10515 writel(hc_copy, phba->HCregaddr);
9399627f 10516 readl(phba->HAregaddr); /* flush */
5b75da2f 10517 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
10518 } else
10519 ha_copy = phba->ha_copy;
dea3101e 10520
dea3101e
JB
10521 work_ha_copy = ha_copy & phba->work_ha_mask;
10522
9399627f 10523 if (work_ha_copy) {
dea3101e
JB
10524 if (work_ha_copy & HA_LATT) {
10525 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10526 /*
10527 * Turn off Link Attention interrupts
10528 * until CLEAR_LA done
10529 */
5b75da2f 10530 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 10531 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
10532 if (lpfc_readl(phba->HCregaddr, &control))
10533 goto unplug_error;
dea3101e
JB
10534 control &= ~HC_LAINT_ENA;
10535 writel(control, phba->HCregaddr);
10536 readl(phba->HCregaddr); /* flush */
5b75da2f 10537 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
10538 }
10539 else
10540 work_ha_copy &= ~HA_LATT;
10541 }
10542
9399627f 10543 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
10544 /*
10545 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10546 * the only slow ring.
10547 */
10548 status = (work_ha_copy &
10549 (HA_RXMASK << (4*LPFC_ELS_RING)));
10550 status >>= (4*LPFC_ELS_RING);
10551 if (status & HA_RXMASK) {
5b75da2f 10552 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
10553 if (lpfc_readl(phba->HCregaddr, &control))
10554 goto unplug_error;
a58cbd52
JS
10555
10556 lpfc_debugfs_slow_ring_trc(phba,
10557 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10558 control, status,
10559 (uint32_t)phba->sli.slistat.sli_intr);
10560
858c9f6c 10561 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
10562 lpfc_debugfs_slow_ring_trc(phba,
10563 "ISR Disable ring:"
10564 "pwork:x%x hawork:x%x wait:x%x",
10565 phba->work_ha, work_ha_copy,
10566 (uint32_t)((unsigned long)
5e9d9b82 10567 &phba->work_waitq));
a58cbd52 10568
858c9f6c
JS
10569 control &=
10570 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e
JB
10571 writel(control, phba->HCregaddr);
10572 readl(phba->HCregaddr); /* flush */
dea3101e 10573 }
a58cbd52
JS
10574 else {
10575 lpfc_debugfs_slow_ring_trc(phba,
10576 "ISR slow ring: pwork:"
10577 "x%x hawork:x%x wait:x%x",
10578 phba->work_ha, work_ha_copy,
10579 (uint32_t)((unsigned long)
5e9d9b82 10580 &phba->work_waitq));
a58cbd52 10581 }
5b75da2f 10582 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
10583 }
10584 }
5b75da2f 10585 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 10586 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
10587 if (lpfc_sli_read_hs(phba))
10588 goto unplug_error;
a257bf90
JS
10589 /*
10590 * Check if there is a deferred error condition
10591 * is active
10592 */
10593 if ((HS_FFER1 & phba->work_hs) &&
10594 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
10595 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10596 phba->work_hs)) {
a257bf90
JS
10597 phba->hba_flag |= DEFER_ERATT;
10598 /* Clear all interrupt enable conditions */
10599 writel(0, phba->HCregaddr);
10600 readl(phba->HCregaddr);
10601 }
10602 }
10603
9399627f 10604 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 10605 pmb = phba->sli.mbox_active;
04c68496 10606 pmbox = &pmb->u.mb;
34b02dcd 10607 mbox = phba->mbox;
858c9f6c 10608 vport = pmb->vport;
92d7f7b0
JS
10609
10610 /* First check out the status word */
10611 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10612 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 10613 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
10614 /*
10615 * Stray Mailbox Interrupt, mbxCommand <cmd>
10616 * mbxStatus <status>
10617 */
09372820 10618 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 10619 LOG_SLI,
e8b62011 10620 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
10621 "Interrupt mbxCommand x%x "
10622 "mbxStatus x%x\n",
e8b62011 10623 (vport ? vport->vpi : 0),
92d7f7b0
JS
10624 pmbox->mbxCommand,
10625 pmbox->mbxStatus);
09372820
JS
10626 /* clear mailbox attention bit */
10627 work_ha_copy &= ~HA_MBATT;
10628 } else {
97eab634 10629 phba->sli.mbox_active = NULL;
5b75da2f 10630 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
10631 phba->last_completion_time = jiffies;
10632 del_timer(&phba->sli.mbox_tmo);
09372820
JS
10633 if (pmb->mbox_cmpl) {
10634 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10635 MAILBOX_CMD_SIZE);
7a470277
JS
10636 if (pmb->out_ext_byte_len &&
10637 pmb->context2)
10638 lpfc_sli_pcimem_bcopy(
10639 phba->mbox_ext,
10640 pmb->context2,
10641 pmb->out_ext_byte_len);
09372820
JS
10642 }
10643 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10644 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10645
10646 lpfc_debugfs_disc_trc(vport,
10647 LPFC_DISC_TRC_MBOX_VPORT,
10648 "MBOX dflt rpi: : "
10649 "status:x%x rpi:x%x",
10650 (uint32_t)pmbox->mbxStatus,
10651 pmbox->un.varWords[0], 0);
10652
10653 if (!pmbox->mbxStatus) {
10654 mp = (struct lpfc_dmabuf *)
10655 (pmb->context1);
10656 ndlp = (struct lpfc_nodelist *)
10657 pmb->context2;
10658
10659 /* Reg_LOGIN of dflt RPI was
10660 * successful. new lets get
10661 * rid of the RPI using the
10662 * same mbox buffer.
10663 */
10664 lpfc_unreg_login(phba,
10665 vport->vpi,
10666 pmbox->un.varWords[0],
10667 pmb);
10668 pmb->mbox_cmpl =
10669 lpfc_mbx_cmpl_dflt_rpi;
10670 pmb->context1 = mp;
10671 pmb->context2 = ndlp;
10672 pmb->vport = vport;
58da1ffb
JS
10673 rc = lpfc_sli_issue_mbox(phba,
10674 pmb,
10675 MBX_NOWAIT);
10676 if (rc != MBX_BUSY)
10677 lpfc_printf_log(phba,
10678 KERN_ERR,
10679 LOG_MBOX | LOG_SLI,
d7c255b2 10680 "0350 rc should have"
6a9c52cf 10681 "been MBX_BUSY\n");
3772a991
JS
10682 if (rc != MBX_NOT_FINISHED)
10683 goto send_current_mbox;
09372820 10684 }
858c9f6c 10685 }
5b75da2f
JS
10686 spin_lock_irqsave(
10687 &phba->pport->work_port_lock,
10688 iflag);
09372820
JS
10689 phba->pport->work_port_events &=
10690 ~WORKER_MBOX_TMO;
5b75da2f
JS
10691 spin_unlock_irqrestore(
10692 &phba->pport->work_port_lock,
10693 iflag);
09372820 10694 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 10695 }
97eab634 10696 } else
5b75da2f 10697 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 10698
92d7f7b0
JS
10699 if ((work_ha_copy & HA_MBATT) &&
10700 (phba->sli.mbox_active == NULL)) {
858c9f6c 10701send_current_mbox:
92d7f7b0 10702 /* Process next mailbox command if there is one */
58da1ffb
JS
10703 do {
10704 rc = lpfc_sli_issue_mbox(phba, NULL,
10705 MBX_NOWAIT);
10706 } while (rc == MBX_NOT_FINISHED);
10707 if (rc != MBX_SUCCESS)
10708 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10709 LOG_SLI, "0349 rc should be "
6a9c52cf 10710 "MBX_SUCCESS\n");
92d7f7b0
JS
10711 }
10712
5b75da2f 10713 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 10714 phba->work_ha |= work_ha_copy;
5b75da2f 10715 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 10716 lpfc_worker_wake_up(phba);
dea3101e 10717 }
9399627f 10718 return IRQ_HANDLED;
9940b97b
JS
10719unplug_error:
10720 spin_unlock_irqrestore(&phba->hbalock, iflag);
10721 return IRQ_HANDLED;
dea3101e 10722
3772a991 10723} /* lpfc_sli_sp_intr_handler */
9399627f
JS
10724
10725/**
3772a991 10726 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
10727 * @irq: Interrupt number.
10728 * @dev_id: The device context pointer.
10729 *
10730 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
10731 * service routine when device with SLI-3 interface spec is enabled with
10732 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10733 * ring event in the HBA. However, when the device is enabled with either
10734 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10735 * device-level interrupt handler. When the PCI slot is in error recovery
10736 * or the HBA is undergoing initialization, the interrupt handler will not
10737 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10738 * the intrrupt context. This function is called without any lock held.
10739 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
10740 *
10741 * This function returns IRQ_HANDLED when interrupt is handled else it
10742 * returns IRQ_NONE.
10743 **/
10744irqreturn_t
3772a991 10745lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
10746{
10747 struct lpfc_hba *phba;
10748 uint32_t ha_copy;
10749 unsigned long status;
5b75da2f 10750 unsigned long iflag;
9399627f
JS
10751
10752 /* Get the driver's phba structure from the dev_id and
10753 * assume the HBA is not interrupting.
10754 */
10755 phba = (struct lpfc_hba *) dev_id;
10756
10757 if (unlikely(!phba))
10758 return IRQ_NONE;
10759
10760 /*
10761 * Stuff needs to be attented to when this function is invoked as an
10762 * individual interrupt handler in MSI-X multi-message interrupt mode
10763 */
10764 if (phba->intr_type == MSIX) {
3772a991
JS
10765 /* Check device state for handling interrupt */
10766 if (lpfc_intr_state_check(phba))
9399627f
JS
10767 return IRQ_NONE;
10768 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
10769 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10770 return IRQ_HANDLED;
9399627f 10771 /* Clear up only attention source related to fast-path */
5b75da2f 10772 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
10773 /*
10774 * If there is deferred error attention, do not check for
10775 * any interrupt.
10776 */
10777 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 10778 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
10779 return IRQ_NONE;
10780 }
9399627f
JS
10781 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10782 phba->HAregaddr);
10783 readl(phba->HAregaddr); /* flush */
5b75da2f 10784 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
10785 } else
10786 ha_copy = phba->ha_copy;
dea3101e
JB
10787
10788 /*
9399627f 10789 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 10790 */
9399627f
JS
10791 ha_copy &= ~(phba->work_ha_mask);
10792
10793 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 10794 status >>= (4*LPFC_FCP_RING);
858c9f6c 10795 if (status & HA_RXMASK)
dea3101e
JB
10796 lpfc_sli_handle_fast_ring_event(phba,
10797 &phba->sli.ring[LPFC_FCP_RING],
10798 status);
a4bc3379
JS
10799
10800 if (phba->cfg_multi_ring_support == 2) {
10801 /*
9399627f
JS
10802 * Process all events on extra ring. Take the optimized path
10803 * for extra ring IO.
a4bc3379 10804 */
9399627f 10805 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 10806 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 10807 if (status & HA_RXMASK) {
a4bc3379
JS
10808 lpfc_sli_handle_fast_ring_event(phba,
10809 &phba->sli.ring[LPFC_EXTRA_RING],
10810 status);
10811 }
10812 }
dea3101e 10813 return IRQ_HANDLED;
3772a991 10814} /* lpfc_sli_fp_intr_handler */
9399627f
JS
10815
10816/**
3772a991 10817 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
10818 * @irq: Interrupt number.
10819 * @dev_id: The device context pointer.
10820 *
3772a991
JS
10821 * This function is the HBA device-level interrupt handler to device with
10822 * SLI-3 interface spec, called from the PCI layer when either MSI or
10823 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10824 * requires driver attention. This function invokes the slow-path interrupt
10825 * attention handling function and fast-path interrupt attention handling
10826 * function in turn to process the relevant HBA attention events. This
10827 * function is called without any lock held. It gets the hbalock to access
10828 * and update SLI data structures.
9399627f
JS
10829 *
10830 * This function returns IRQ_HANDLED when interrupt is handled, else it
10831 * returns IRQ_NONE.
10832 **/
10833irqreturn_t
3772a991 10834lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
10835{
10836 struct lpfc_hba *phba;
10837 irqreturn_t sp_irq_rc, fp_irq_rc;
10838 unsigned long status1, status2;
a747c9ce 10839 uint32_t hc_copy;
9399627f
JS
10840
10841 /*
10842 * Get the driver's phba structure from the dev_id and
10843 * assume the HBA is not interrupting.
10844 */
10845 phba = (struct lpfc_hba *) dev_id;
10846
10847 if (unlikely(!phba))
10848 return IRQ_NONE;
10849
3772a991
JS
10850 /* Check device state for handling interrupt */
10851 if (lpfc_intr_state_check(phba))
9399627f
JS
10852 return IRQ_NONE;
10853
10854 spin_lock(&phba->hbalock);
9940b97b
JS
10855 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10856 spin_unlock(&phba->hbalock);
10857 return IRQ_HANDLED;
10858 }
10859
9399627f
JS
10860 if (unlikely(!phba->ha_copy)) {
10861 spin_unlock(&phba->hbalock);
10862 return IRQ_NONE;
10863 } else if (phba->ha_copy & HA_ERATT) {
10864 if (phba->hba_flag & HBA_ERATT_HANDLED)
10865 /* ERATT polling has handled ERATT */
10866 phba->ha_copy &= ~HA_ERATT;
10867 else
10868 /* Indicate interrupt handler handles ERATT */
10869 phba->hba_flag |= HBA_ERATT_HANDLED;
10870 }
10871
a257bf90
JS
10872 /*
10873 * If there is deferred error attention, do not check for any interrupt.
10874 */
10875 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 10876 spin_unlock(&phba->hbalock);
a257bf90
JS
10877 return IRQ_NONE;
10878 }
10879
9399627f 10880 /* Clear attention sources except link and error attentions */
9940b97b
JS
10881 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10882 spin_unlock(&phba->hbalock);
10883 return IRQ_HANDLED;
10884 }
a747c9ce
JS
10885 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10886 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10887 phba->HCregaddr);
9399627f 10888 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 10889 writel(hc_copy, phba->HCregaddr);
9399627f
JS
10890 readl(phba->HAregaddr); /* flush */
10891 spin_unlock(&phba->hbalock);
10892
10893 /*
10894 * Invokes slow-path host attention interrupt handling as appropriate.
10895 */
10896
10897 /* status of events with mailbox and link attention */
10898 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10899
10900 /* status of events with ELS ring */
10901 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10902 status2 >>= (4*LPFC_ELS_RING);
10903
10904 if (status1 || (status2 & HA_RXMASK))
3772a991 10905 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
10906 else
10907 sp_irq_rc = IRQ_NONE;
10908
10909 /*
10910 * Invoke fast-path host attention interrupt handling as appropriate.
10911 */
10912
10913 /* status of events with FCP ring */
10914 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10915 status1 >>= (4*LPFC_FCP_RING);
10916
10917 /* status of events with extra ring */
10918 if (phba->cfg_multi_ring_support == 2) {
10919 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10920 status2 >>= (4*LPFC_EXTRA_RING);
10921 } else
10922 status2 = 0;
10923
10924 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 10925 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
10926 else
10927 fp_irq_rc = IRQ_NONE;
dea3101e 10928
9399627f
JS
10929 /* Return device-level interrupt handling status */
10930 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 10931} /* lpfc_sli_intr_handler */
4f774513
JS
10932
10933/**
10934 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10935 * @phba: pointer to lpfc hba data structure.
10936 *
10937 * This routine is invoked by the worker thread to process all the pending
10938 * SLI4 FCP abort XRI events.
10939 **/
10940void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10941{
10942 struct lpfc_cq_event *cq_event;
10943
10944 /* First, declare the fcp xri abort event has been handled */
10945 spin_lock_irq(&phba->hbalock);
10946 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10947 spin_unlock_irq(&phba->hbalock);
10948 /* Now, handle all the fcp xri abort events */
10949 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10950 /* Get the first event from the head of the event queue */
10951 spin_lock_irq(&phba->hbalock);
10952 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10953 cq_event, struct lpfc_cq_event, list);
10954 spin_unlock_irq(&phba->hbalock);
10955 /* Notify aborted XRI for FCP work queue */
10956 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10957 /* Free the event processed back to the free pool */
10958 lpfc_sli4_cq_event_release(phba, cq_event);
10959 }
10960}
10961
10962/**
10963 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10964 * @phba: pointer to lpfc hba data structure.
10965 *
10966 * This routine is invoked by the worker thread to process all the pending
10967 * SLI4 els abort xri events.
10968 **/
10969void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10970{
10971 struct lpfc_cq_event *cq_event;
10972
10973 /* First, declare the els xri abort event has been handled */
10974 spin_lock_irq(&phba->hbalock);
10975 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10976 spin_unlock_irq(&phba->hbalock);
10977 /* Now, handle all the els xri abort events */
10978 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10979 /* Get the first event from the head of the event queue */
10980 spin_lock_irq(&phba->hbalock);
10981 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10982 cq_event, struct lpfc_cq_event, list);
10983 spin_unlock_irq(&phba->hbalock);
10984 /* Notify aborted XRI for ELS work queue */
10985 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10986 /* Free the event processed back to the free pool */
10987 lpfc_sli4_cq_event_release(phba, cq_event);
10988 }
10989}
10990
341af102
JS
10991/**
10992 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10993 * @phba: pointer to lpfc hba data structure
10994 * @pIocbIn: pointer to the rspiocbq
10995 * @pIocbOut: pointer to the cmdiocbq
10996 * @wcqe: pointer to the complete wcqe
10997 *
10998 * This routine transfers the fields of a command iocbq to a response iocbq
10999 * by copying all the IOCB fields from command iocbq and transferring the
11000 * completion status information from the complete wcqe.
11001 **/
4f774513 11002static void
341af102
JS
11003lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11004 struct lpfc_iocbq *pIocbIn,
4f774513
JS
11005 struct lpfc_iocbq *pIocbOut,
11006 struct lpfc_wcqe_complete *wcqe)
11007{
341af102 11008 unsigned long iflags;
acd6859b 11009 uint32_t status;
4f774513
JS
11010 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11011
11012 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11013 sizeof(struct lpfc_iocbq) - offset);
4f774513 11014 /* Map WCQE parameters into irspiocb parameters */
acd6859b
JS
11015 status = bf_get(lpfc_wcqe_c_status, wcqe);
11016 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
4f774513
JS
11017 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11018 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11019 pIocbIn->iocb.un.fcpi.fcpi_parm =
11020 pIocbOut->iocb.un.fcpi.fcpi_parm -
11021 wcqe->total_data_placed;
11022 else
11023 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 11024 else {
4f774513 11025 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e
JS
11026 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
11027 }
341af102 11028
acd6859b
JS
11029 /* Convert BG errors for completion status */
11030 if (status == CQE_STATUS_DI_ERROR) {
11031 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11032
11033 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11034 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11035 else
11036 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11037
11038 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11039 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11040 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11041 BGS_GUARD_ERR_MASK;
11042 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11043 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11044 BGS_APPTAG_ERR_MASK;
11045 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11046 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11047 BGS_REFTAG_ERR_MASK;
11048
11049 /* Check to see if there was any good data before the error */
11050 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11051 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11052 BGS_HI_WATER_MARK_PRESENT_MASK;
11053 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11054 wcqe->total_data_placed;
11055 }
11056
11057 /*
11058 * Set ALL the error bits to indicate we don't know what
11059 * type of error it is.
11060 */
11061 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11062 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11063 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11064 BGS_GUARD_ERR_MASK);
11065 }
11066
341af102
JS
11067 /* Pick up HBA exchange busy condition */
11068 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11069 spin_lock_irqsave(&phba->hbalock, iflags);
11070 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11071 spin_unlock_irqrestore(&phba->hbalock, iflags);
11072 }
4f774513
JS
11073}
11074
45ed1190
JS
11075/**
11076 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11077 * @phba: Pointer to HBA context object.
11078 * @wcqe: Pointer to work-queue completion queue entry.
11079 *
11080 * This routine handles an ELS work-queue completion event and construct
11081 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11082 * discovery engine to handle.
11083 *
11084 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11085 **/
11086static struct lpfc_iocbq *
11087lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11088 struct lpfc_iocbq *irspiocbq)
11089{
11090 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11091 struct lpfc_iocbq *cmdiocbq;
11092 struct lpfc_wcqe_complete *wcqe;
11093 unsigned long iflags;
11094
11095 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
7e56aa25 11096 spin_lock_irqsave(&pring->ring_lock, iflags);
45ed1190
JS
11097 pring->stats.iocb_event++;
11098 /* Look up the ELS command IOCB and create pseudo response IOCB */
11099 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11100 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 11101 spin_unlock_irqrestore(&pring->ring_lock, iflags);
45ed1190
JS
11102
11103 if (unlikely(!cmdiocbq)) {
11104 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11105 "0386 ELS complete with no corresponding "
11106 "cmdiocb: iotag (%d)\n",
11107 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11108 lpfc_sli_release_iocbq(phba, irspiocbq);
11109 return NULL;
11110 }
11111
11112 /* Fake the irspiocbq and copy necessary response information */
341af102 11113 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
11114
11115 return irspiocbq;
11116}
11117
04c68496
JS
11118/**
11119 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11120 * @phba: Pointer to HBA context object.
11121 * @cqe: Pointer to mailbox completion queue entry.
11122 *
11123 * This routine process a mailbox completion queue entry with asynchrous
11124 * event.
11125 *
11126 * Return: true if work posted to worker thread, otherwise false.
11127 **/
11128static bool
11129lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11130{
11131 struct lpfc_cq_event *cq_event;
11132 unsigned long iflags;
11133
11134 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11135 "0392 Async Event: word0:x%x, word1:x%x, "
11136 "word2:x%x, word3:x%x\n", mcqe->word0,
11137 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11138
11139 /* Allocate a new internal CQ_EVENT entry */
11140 cq_event = lpfc_sli4_cq_event_alloc(phba);
11141 if (!cq_event) {
11142 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11143 "0394 Failed to allocate CQ_EVENT entry\n");
11144 return false;
11145 }
11146
11147 /* Move the CQE into an asynchronous event entry */
11148 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11149 spin_lock_irqsave(&phba->hbalock, iflags);
11150 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11151 /* Set the async event flag */
11152 phba->hba_flag |= ASYNC_EVENT;
11153 spin_unlock_irqrestore(&phba->hbalock, iflags);
11154
11155 return true;
11156}
11157
11158/**
11159 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11160 * @phba: Pointer to HBA context object.
11161 * @cqe: Pointer to mailbox completion queue entry.
11162 *
11163 * This routine process a mailbox completion queue entry with mailbox
11164 * completion event.
11165 *
11166 * Return: true if work posted to worker thread, otherwise false.
11167 **/
11168static bool
11169lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11170{
11171 uint32_t mcqe_status;
11172 MAILBOX_t *mbox, *pmbox;
11173 struct lpfc_mqe *mqe;
11174 struct lpfc_vport *vport;
11175 struct lpfc_nodelist *ndlp;
11176 struct lpfc_dmabuf *mp;
11177 unsigned long iflags;
11178 LPFC_MBOXQ_t *pmb;
11179 bool workposted = false;
11180 int rc;
11181
11182 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11183 if (!bf_get(lpfc_trailer_completed, mcqe))
11184 goto out_no_mqe_complete;
11185
11186 /* Get the reference to the active mbox command */
11187 spin_lock_irqsave(&phba->hbalock, iflags);
11188 pmb = phba->sli.mbox_active;
11189 if (unlikely(!pmb)) {
11190 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11191 "1832 No pending MBOX command to handle\n");
11192 spin_unlock_irqrestore(&phba->hbalock, iflags);
11193 goto out_no_mqe_complete;
11194 }
11195 spin_unlock_irqrestore(&phba->hbalock, iflags);
11196 mqe = &pmb->u.mqe;
11197 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11198 mbox = phba->mbox;
11199 vport = pmb->vport;
11200
11201 /* Reset heartbeat timer */
11202 phba->last_completion_time = jiffies;
11203 del_timer(&phba->sli.mbox_tmo);
11204
11205 /* Move mbox data to caller's mailbox region, do endian swapping */
11206 if (pmb->mbox_cmpl && mbox)
11207 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 11208
73d91e50
JS
11209 /*
11210 * For mcqe errors, conditionally move a modified error code to
11211 * the mbox so that the error will not be missed.
11212 */
11213 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11214 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11215 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11216 bf_set(lpfc_mqe_status, mqe,
11217 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11218 }
04c68496
JS
11219 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11220 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11221 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11222 "MBOX dflt rpi: status:x%x rpi:x%x",
11223 mcqe_status,
11224 pmbox->un.varWords[0], 0);
11225 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11226 mp = (struct lpfc_dmabuf *)(pmb->context1);
11227 ndlp = (struct lpfc_nodelist *)pmb->context2;
11228 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11229 * RID of the PPI using the same mbox buffer.
11230 */
11231 lpfc_unreg_login(phba, vport->vpi,
11232 pmbox->un.varWords[0], pmb);
11233 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11234 pmb->context1 = mp;
11235 pmb->context2 = ndlp;
11236 pmb->vport = vport;
11237 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11238 if (rc != MBX_BUSY)
11239 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11240 LOG_SLI, "0385 rc should "
11241 "have been MBX_BUSY\n");
11242 if (rc != MBX_NOT_FINISHED)
11243 goto send_current_mbox;
11244 }
11245 }
11246 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11247 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11248 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11249
11250 /* There is mailbox completion work to do */
11251 spin_lock_irqsave(&phba->hbalock, iflags);
11252 __lpfc_mbox_cmpl_put(phba, pmb);
11253 phba->work_ha |= HA_MBATT;
11254 spin_unlock_irqrestore(&phba->hbalock, iflags);
11255 workposted = true;
11256
11257send_current_mbox:
11258 spin_lock_irqsave(&phba->hbalock, iflags);
11259 /* Release the mailbox command posting token */
11260 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11261 /* Setting active mailbox pointer need to be in sync to flag clear */
11262 phba->sli.mbox_active = NULL;
11263 spin_unlock_irqrestore(&phba->hbalock, iflags);
11264 /* Wake up worker thread to post the next pending mailbox command */
11265 lpfc_worker_wake_up(phba);
11266out_no_mqe_complete:
11267 if (bf_get(lpfc_trailer_consumed, mcqe))
11268 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11269 return workposted;
11270}
11271
11272/**
11273 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11274 * @phba: Pointer to HBA context object.
11275 * @cqe: Pointer to mailbox completion queue entry.
11276 *
11277 * This routine process a mailbox completion queue entry, it invokes the
11278 * proper mailbox complete handling or asynchrous event handling routine
11279 * according to the MCQE's async bit.
11280 *
11281 * Return: true if work posted to worker thread, otherwise false.
11282 **/
11283static bool
11284lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11285{
11286 struct lpfc_mcqe mcqe;
11287 bool workposted;
11288
11289 /* Copy the mailbox MCQE and convert endian order as needed */
11290 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11291
11292 /* Invoke the proper event handling routine */
11293 if (!bf_get(lpfc_trailer_async, &mcqe))
11294 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11295 else
11296 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11297 return workposted;
11298}
11299
4f774513
JS
11300/**
11301 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11302 * @phba: Pointer to HBA context object.
2a76a283 11303 * @cq: Pointer to associated CQ
4f774513
JS
11304 * @wcqe: Pointer to work-queue completion queue entry.
11305 *
11306 * This routine handles an ELS work-queue completion event.
11307 *
11308 * Return: true if work posted to worker thread, otherwise false.
11309 **/
11310static bool
2a76a283 11311lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
11312 struct lpfc_wcqe_complete *wcqe)
11313{
4f774513
JS
11314 struct lpfc_iocbq *irspiocbq;
11315 unsigned long iflags;
2a76a283 11316 struct lpfc_sli_ring *pring = cq->pring;
0e9bb8d7
JS
11317 int txq_cnt = 0;
11318 int txcmplq_cnt = 0;
11319 int fcp_txcmplq_cnt = 0;
4f774513 11320
45ed1190 11321 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
11322 irspiocbq = lpfc_sli_get_iocbq(phba);
11323 if (!irspiocbq) {
0e9bb8d7
JS
11324 if (!list_empty(&pring->txq))
11325 txq_cnt++;
11326 if (!list_empty(&pring->txcmplq))
11327 txcmplq_cnt++;
11328 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
11329 fcp_txcmplq_cnt++;
4f774513 11330 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
11331 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11332 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
0e9bb8d7
JS
11333 txq_cnt, phba->iocb_cnt,
11334 fcp_txcmplq_cnt,
11335 txcmplq_cnt);
45ed1190 11336 return false;
4f774513 11337 }
4f774513 11338
45ed1190
JS
11339 /* Save off the slow-path queue event for work thread to process */
11340 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 11341 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 11342 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
11343 &phba->sli4_hba.sp_queue_event);
11344 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 11345 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 11346
45ed1190 11347 return true;
4f774513
JS
11348}
11349
11350/**
11351 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11352 * @phba: Pointer to HBA context object.
11353 * @wcqe: Pointer to work-queue completion queue entry.
11354 *
11355 * This routine handles slow-path WQ entry comsumed event by invoking the
11356 * proper WQ release routine to the slow-path WQ.
11357 **/
11358static void
11359lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11360 struct lpfc_wcqe_release *wcqe)
11361{
2e90f4b5
JS
11362 /* sanity check on queue memory */
11363 if (unlikely(!phba->sli4_hba.els_wq))
11364 return;
4f774513
JS
11365 /* Check for the slow-path ELS work queue */
11366 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11367 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11368 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11369 else
11370 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11371 "2579 Slow-path wqe consume event carries "
11372 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11373 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11374 phba->sli4_hba.els_wq->queue_id);
11375}
11376
11377/**
11378 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11379 * @phba: Pointer to HBA context object.
11380 * @cq: Pointer to a WQ completion queue.
11381 * @wcqe: Pointer to work-queue completion queue entry.
11382 *
11383 * This routine handles an XRI abort event.
11384 *
11385 * Return: true if work posted to worker thread, otherwise false.
11386 **/
11387static bool
11388lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11389 struct lpfc_queue *cq,
11390 struct sli4_wcqe_xri_aborted *wcqe)
11391{
11392 bool workposted = false;
11393 struct lpfc_cq_event *cq_event;
11394 unsigned long iflags;
11395
11396 /* Allocate a new internal CQ_EVENT entry */
11397 cq_event = lpfc_sli4_cq_event_alloc(phba);
11398 if (!cq_event) {
11399 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11400 "0602 Failed to allocate CQ_EVENT entry\n");
11401 return false;
11402 }
11403
11404 /* Move the CQE into the proper xri abort event list */
11405 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11406 switch (cq->subtype) {
11407 case LPFC_FCP:
11408 spin_lock_irqsave(&phba->hbalock, iflags);
11409 list_add_tail(&cq_event->list,
11410 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11411 /* Set the fcp xri abort event flag */
11412 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11413 spin_unlock_irqrestore(&phba->hbalock, iflags);
11414 workposted = true;
11415 break;
11416 case LPFC_ELS:
11417 spin_lock_irqsave(&phba->hbalock, iflags);
11418 list_add_tail(&cq_event->list,
11419 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11420 /* Set the els xri abort event flag */
11421 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11422 spin_unlock_irqrestore(&phba->hbalock, iflags);
11423 workposted = true;
11424 break;
11425 default:
11426 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11427 "0603 Invalid work queue CQE subtype (x%x)\n",
11428 cq->subtype);
11429 workposted = false;
11430 break;
11431 }
11432 return workposted;
11433}
11434
4f774513
JS
11435/**
11436 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
11437 * @phba: Pointer to HBA context object.
11438 * @rcqe: Pointer to receive-queue completion queue entry.
11439 *
11440 * This routine process a receive-queue completion queue entry.
11441 *
11442 * Return: true if work posted to worker thread, otherwise false.
11443 **/
11444static bool
4d9ab994 11445lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 11446{
4f774513
JS
11447 bool workposted = false;
11448 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11449 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11450 struct hbq_dmabuf *dma_buf;
7851fe2c 11451 uint32_t status, rq_id;
4f774513
JS
11452 unsigned long iflags;
11453
2e90f4b5
JS
11454 /* sanity check on queue memory */
11455 if (unlikely(!hrq) || unlikely(!drq))
11456 return workposted;
11457
7851fe2c
JS
11458 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11459 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11460 else
11461 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11462 if (rq_id != hrq->queue_id)
4f774513
JS
11463 goto out;
11464
4d9ab994 11465 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
11466 switch (status) {
11467 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11468 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11469 "2537 Receive Frame Truncated!!\n");
b84daac9 11470 hrq->RQ_buf_trunc++;
4f774513 11471 case FC_STATUS_RQ_SUCCESS:
5ffc266e 11472 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
11473 spin_lock_irqsave(&phba->hbalock, iflags);
11474 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11475 if (!dma_buf) {
b84daac9 11476 hrq->RQ_no_buf_found++;
4f774513
JS
11477 spin_unlock_irqrestore(&phba->hbalock, iflags);
11478 goto out;
11479 }
b84daac9 11480 hrq->RQ_rcv_buf++;
4d9ab994 11481 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
4f774513 11482 /* save off the frame for the word thread to process */
4d9ab994 11483 list_add_tail(&dma_buf->cq_event.list,
45ed1190 11484 &phba->sli4_hba.sp_queue_event);
4f774513 11485 /* Frame received */
45ed1190 11486 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
11487 spin_unlock_irqrestore(&phba->hbalock, iflags);
11488 workposted = true;
11489 break;
11490 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11491 case FC_STATUS_INSUFF_BUF_FRM_DISC:
b84daac9 11492 hrq->RQ_no_posted_buf++;
4f774513
JS
11493 /* Post more buffers if possible */
11494 spin_lock_irqsave(&phba->hbalock, iflags);
11495 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11496 spin_unlock_irqrestore(&phba->hbalock, iflags);
11497 workposted = true;
11498 break;
11499 }
11500out:
11501 return workposted;
4f774513
JS
11502}
11503
4d9ab994
JS
11504/**
11505 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11506 * @phba: Pointer to HBA context object.
11507 * @cq: Pointer to the completion queue.
11508 * @wcqe: Pointer to a completion queue entry.
11509 *
25985edc 11510 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
11511 * entry.
11512 *
11513 * Return: true if work posted to worker thread, otherwise false.
11514 **/
11515static bool
11516lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11517 struct lpfc_cqe *cqe)
11518{
45ed1190 11519 struct lpfc_cqe cqevt;
4d9ab994
JS
11520 bool workposted = false;
11521
11522 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 11523 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
11524
11525 /* Check and process for different type of WCQE and dispatch */
45ed1190 11526 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 11527 case CQE_CODE_COMPL_WQE:
45ed1190 11528 /* Process the WQ/RQ complete event */
bc73905a 11529 phba->last_completion_time = jiffies;
2a76a283 11530 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
45ed1190 11531 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
11532 break;
11533 case CQE_CODE_RELEASE_WQE:
11534 /* Process the WQ release event */
11535 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 11536 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
11537 break;
11538 case CQE_CODE_XRI_ABORTED:
11539 /* Process the WQ XRI abort event */
bc73905a 11540 phba->last_completion_time = jiffies;
4d9ab994 11541 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 11542 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
11543 break;
11544 case CQE_CODE_RECEIVE:
7851fe2c 11545 case CQE_CODE_RECEIVE_V1:
4d9ab994 11546 /* Process the RQ event */
bc73905a 11547 phba->last_completion_time = jiffies;
4d9ab994 11548 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 11549 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
11550 break;
11551 default:
11552 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11553 "0388 Not a valid WCQE code: x%x\n",
45ed1190 11554 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
11555 break;
11556 }
11557 return workposted;
11558}
11559
4f774513
JS
11560/**
11561 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11562 * @phba: Pointer to HBA context object.
11563 * @eqe: Pointer to fast-path event queue entry.
11564 *
11565 * This routine process a event queue entry from the slow-path event queue.
11566 * It will check the MajorCode and MinorCode to determine this is for a
11567 * completion event on a completion queue, if not, an error shall be logged
11568 * and just return. Otherwise, it will get to the corresponding completion
11569 * queue and process all the entries on that completion queue, rearm the
11570 * completion queue, and then return.
11571 *
11572 **/
11573static void
67d12733
JS
11574lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11575 struct lpfc_queue *speq)
4f774513 11576{
67d12733 11577 struct lpfc_queue *cq = NULL, *childq;
4f774513
JS
11578 struct lpfc_cqe *cqe;
11579 bool workposted = false;
11580 int ecount = 0;
11581 uint16_t cqid;
11582
4f774513 11583 /* Get the reference to the corresponding CQ */
cb5172ea 11584 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513 11585
4f774513
JS
11586 list_for_each_entry(childq, &speq->child_list, list) {
11587 if (childq->queue_id == cqid) {
11588 cq = childq;
11589 break;
11590 }
11591 }
11592 if (unlikely(!cq)) {
75baf696
JS
11593 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11594 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11595 "0365 Slow-path CQ identifier "
11596 "(%d) does not exist\n", cqid);
4f774513
JS
11597 return;
11598 }
11599
11600 /* Process all the entries to the CQ */
11601 switch (cq->type) {
11602 case LPFC_MCQ:
11603 while ((cqe = lpfc_sli4_cq_get(cq))) {
11604 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
73d91e50 11605 if (!(++ecount % cq->entry_repost))
4f774513 11606 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
b84daac9 11607 cq->CQ_mbox++;
4f774513
JS
11608 }
11609 break;
11610 case LPFC_WCQ:
11611 while ((cqe = lpfc_sli4_cq_get(cq))) {
0558056c
JS
11612 if (cq->subtype == LPFC_FCP)
11613 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11614 cqe);
11615 else
11616 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11617 cqe);
73d91e50 11618 if (!(++ecount % cq->entry_repost))
4f774513
JS
11619 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11620 }
b84daac9
JS
11621
11622 /* Track the max number of CQEs processed in 1 EQ */
11623 if (ecount > cq->CQ_max_cqe)
11624 cq->CQ_max_cqe = ecount;
4f774513
JS
11625 break;
11626 default:
11627 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11628 "0370 Invalid completion queue type (%d)\n",
11629 cq->type);
11630 return;
11631 }
11632
11633 /* Catch the no cq entry condition, log an error */
11634 if (unlikely(ecount == 0))
11635 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11636 "0371 No entry from the CQ: identifier "
11637 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11638
11639 /* In any case, flash and re-arm the RCQ */
11640 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11641
11642 /* wake up worker thread if there are works to be done */
11643 if (workposted)
11644 lpfc_worker_wake_up(phba);
11645}
11646
11647/**
11648 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
2a76a283
JS
11649 * @phba: Pointer to HBA context object.
11650 * @cq: Pointer to associated CQ
11651 * @wcqe: Pointer to work-queue completion queue entry.
4f774513
JS
11652 *
11653 * This routine process a fast-path work queue completion entry from fast-path
11654 * event queue for FCP command response completion.
11655 **/
11656static void
2a76a283 11657lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
11658 struct lpfc_wcqe_complete *wcqe)
11659{
2a76a283 11660 struct lpfc_sli_ring *pring = cq->pring;
4f774513
JS
11661 struct lpfc_iocbq *cmdiocbq;
11662 struct lpfc_iocbq irspiocbq;
11663 unsigned long iflags;
11664
4f774513
JS
11665 /* Check for response status */
11666 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11667 /* If resource errors reported from HBA, reduce queue
11668 * depth of the SCSI device.
11669 */
e3d2b802
JS
11670 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
11671 IOSTAT_LOCAL_REJECT)) &&
11672 ((wcqe->parameter & IOERR_PARAM_MASK) ==
11673 IOERR_NO_RESOURCES))
4f774513 11674 phba->lpfc_rampdown_queue_depth(phba);
e3d2b802 11675
4f774513
JS
11676 /* Log the error status */
11677 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11678 "0373 FCP complete error: status=x%x, "
11679 "hw_status=x%x, total_data_specified=%d, "
11680 "parameter=x%x, word3=x%x\n",
11681 bf_get(lpfc_wcqe_c_status, wcqe),
11682 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11683 wcqe->total_data_placed, wcqe->parameter,
11684 wcqe->word3);
11685 }
11686
11687 /* Look up the FCP command IOCB and create pseudo response IOCB */
7e56aa25
JS
11688 spin_lock_irqsave(&pring->ring_lock, iflags);
11689 pring->stats.iocb_event++;
4f774513
JS
11690 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11691 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 11692 spin_unlock_irqrestore(&pring->ring_lock, iflags);
4f774513
JS
11693 if (unlikely(!cmdiocbq)) {
11694 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11695 "0374 FCP complete with no corresponding "
11696 "cmdiocb: iotag (%d)\n",
11697 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11698 return;
11699 }
11700 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11701 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11702 "0375 FCP cmdiocb not callback function "
11703 "iotag: (%d)\n",
11704 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11705 return;
11706 }
11707
11708 /* Fake the irspiocb and copy necessary response information */
341af102 11709 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 11710
0f65ff68
JS
11711 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11712 spin_lock_irqsave(&phba->hbalock, iflags);
11713 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11714 spin_unlock_irqrestore(&phba->hbalock, iflags);
11715 }
11716
4f774513
JS
11717 /* Pass the cmd_iocb and the rsp state to the upper layer */
11718 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11719}
11720
11721/**
11722 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11723 * @phba: Pointer to HBA context object.
11724 * @cq: Pointer to completion queue.
11725 * @wcqe: Pointer to work-queue completion queue entry.
11726 *
11727 * This routine handles an fast-path WQ entry comsumed event by invoking the
11728 * proper WQ release routine to the slow-path WQ.
11729 **/
11730static void
11731lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11732 struct lpfc_wcqe_release *wcqe)
11733{
11734 struct lpfc_queue *childwq;
11735 bool wqid_matched = false;
11736 uint16_t fcp_wqid;
11737
11738 /* Check for fast-path FCP work queue release */
11739 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11740 list_for_each_entry(childwq, &cq->child_list, list) {
11741 if (childwq->queue_id == fcp_wqid) {
11742 lpfc_sli4_wq_release(childwq,
11743 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11744 wqid_matched = true;
11745 break;
11746 }
11747 }
11748 /* Report warning log message if no match found */
11749 if (wqid_matched != true)
11750 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11751 "2580 Fast-path wqe consume event carries "
11752 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11753}
11754
11755/**
11756 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11757 * @cq: Pointer to the completion queue.
11758 * @eqe: Pointer to fast-path completion queue entry.
11759 *
11760 * This routine process a fast-path work queue completion entry from fast-path
11761 * event queue for FCP command response completion.
11762 **/
11763static int
11764lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11765 struct lpfc_cqe *cqe)
11766{
11767 struct lpfc_wcqe_release wcqe;
11768 bool workposted = false;
11769
11770 /* Copy the work queue CQE and convert endian order if needed */
11771 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11772
11773 /* Check and process for different type of WCQE and dispatch */
11774 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11775 case CQE_CODE_COMPL_WQE:
b84daac9 11776 cq->CQ_wq++;
4f774513 11777 /* Process the WQ complete event */
98fc5dd9 11778 phba->last_completion_time = jiffies;
2a76a283 11779 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
4f774513
JS
11780 (struct lpfc_wcqe_complete *)&wcqe);
11781 break;
11782 case CQE_CODE_RELEASE_WQE:
b84daac9 11783 cq->CQ_release_wqe++;
4f774513
JS
11784 /* Process the WQ release event */
11785 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11786 (struct lpfc_wcqe_release *)&wcqe);
11787 break;
11788 case CQE_CODE_XRI_ABORTED:
b84daac9 11789 cq->CQ_xri_aborted++;
4f774513 11790 /* Process the WQ XRI abort event */
bc73905a 11791 phba->last_completion_time = jiffies;
4f774513
JS
11792 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11793 (struct sli4_wcqe_xri_aborted *)&wcqe);
11794 break;
11795 default:
11796 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11797 "0144 Not a valid WCQE code: x%x\n",
11798 bf_get(lpfc_wcqe_c_code, &wcqe));
11799 break;
11800 }
11801 return workposted;
11802}
11803
11804/**
67d12733 11805 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
4f774513
JS
11806 * @phba: Pointer to HBA context object.
11807 * @eqe: Pointer to fast-path event queue entry.
11808 *
11809 * This routine process a event queue entry from the fast-path event queue.
11810 * It will check the MajorCode and MinorCode to determine this is for a
11811 * completion event on a completion queue, if not, an error shall be logged
11812 * and just return. Otherwise, it will get to the corresponding completion
11813 * queue and process all the entries on the completion queue, rearm the
11814 * completion queue, and then return.
11815 **/
11816static void
67d12733
JS
11817lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11818 uint32_t qidx)
4f774513
JS
11819{
11820 struct lpfc_queue *cq;
11821 struct lpfc_cqe *cqe;
11822 bool workposted = false;
11823 uint16_t cqid;
11824 int ecount = 0;
11825
cb5172ea 11826 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513 11827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
67d12733 11828 "0366 Not a valid completion "
4f774513 11829 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
11830 bf_get_le32(lpfc_eqe_major_code, eqe),
11831 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
11832 return;
11833 }
11834
67d12733
JS
11835 /* Get the reference to the corresponding CQ */
11836 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11837
11838 /* Check if this is a Slow path event */
11839 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
11840 lpfc_sli4_sp_handle_eqe(phba, eqe,
11841 phba->sli4_hba.hba_eq[qidx]);
11842 return;
11843 }
11844
2e90f4b5
JS
11845 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11846 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11847 "3146 Fast-path completion queues "
11848 "does not exist\n");
11849 return;
11850 }
67d12733 11851 cq = phba->sli4_hba.fcp_cq[qidx];
4f774513 11852 if (unlikely(!cq)) {
75baf696
JS
11853 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11854 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11855 "0367 Fast-path completion queue "
67d12733 11856 "(%d) does not exist\n", qidx);
4f774513
JS
11857 return;
11858 }
11859
4f774513
JS
11860 if (unlikely(cqid != cq->queue_id)) {
11861 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11862 "0368 Miss-matched fast-path completion "
11863 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11864 cqid, cq->queue_id);
11865 return;
11866 }
11867
11868 /* Process all the entries to the CQ */
11869 while ((cqe = lpfc_sli4_cq_get(cq))) {
11870 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
73d91e50 11871 if (!(++ecount % cq->entry_repost))
4f774513
JS
11872 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11873 }
11874
b84daac9
JS
11875 /* Track the max number of CQEs processed in 1 EQ */
11876 if (ecount > cq->CQ_max_cqe)
11877 cq->CQ_max_cqe = ecount;
11878
4f774513
JS
11879 /* Catch the no cq entry condition */
11880 if (unlikely(ecount == 0))
11881 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11882 "0369 No entry from fast-path completion "
11883 "queue fcpcqid=%d\n", cq->queue_id);
11884
11885 /* In any case, flash and re-arm the CQ */
11886 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11887
11888 /* wake up worker thread if there are works to be done */
11889 if (workposted)
11890 lpfc_worker_wake_up(phba);
11891}
11892
11893static void
11894lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11895{
11896 struct lpfc_eqe *eqe;
11897
11898 /* walk all the EQ entries and drop on the floor */
11899 while ((eqe = lpfc_sli4_eq_get(eq)))
11900 ;
11901
11902 /* Clear and re-arm the EQ */
11903 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11904}
11905
11906/**
67d12733 11907 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
4f774513
JS
11908 * @irq: Interrupt number.
11909 * @dev_id: The device context pointer.
11910 *
11911 * This function is directly called from the PCI layer as an interrupt
11912 * service routine when device with SLI-4 interface spec is enabled with
11913 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11914 * ring event in the HBA. However, when the device is enabled with either
11915 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11916 * device-level interrupt handler. When the PCI slot is in error recovery
11917 * or the HBA is undergoing initialization, the interrupt handler will not
11918 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11919 * the intrrupt context. This function is called without any lock held.
11920 * It gets the hbalock to access and update SLI data structures. Note that,
11921 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11922 * equal to that of FCP CQ index.
11923 *
67d12733
JS
11924 * The link attention and ELS ring attention events are handled
11925 * by the worker thread. The interrupt handler signals the worker thread
11926 * and returns for these events. This function is called without any lock
11927 * held. It gets the hbalock to access and update SLI data structures.
11928 *
4f774513
JS
11929 * This function returns IRQ_HANDLED when interrupt is handled else it
11930 * returns IRQ_NONE.
11931 **/
11932irqreturn_t
67d12733 11933lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
4f774513
JS
11934{
11935 struct lpfc_hba *phba;
11936 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11937 struct lpfc_queue *fpeq;
11938 struct lpfc_eqe *eqe;
11939 unsigned long iflag;
11940 int ecount = 0;
962bc51b 11941 int fcp_eqidx;
4f774513
JS
11942
11943 /* Get the driver's phba structure from the dev_id */
11944 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11945 phba = fcp_eq_hdl->phba;
11946 fcp_eqidx = fcp_eq_hdl->idx;
11947
11948 if (unlikely(!phba))
11949 return IRQ_NONE;
67d12733 11950 if (unlikely(!phba->sli4_hba.hba_eq))
5350d872 11951 return IRQ_NONE;
4f774513
JS
11952
11953 /* Get to the EQ struct associated with this vector */
67d12733 11954 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
2e90f4b5
JS
11955 if (unlikely(!fpeq))
11956 return IRQ_NONE;
4f774513 11957
ba20c853
JS
11958 if (lpfc_fcp_look_ahead) {
11959 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
11960 lpfc_sli4_eq_clr_intr(fpeq);
11961 else {
11962 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
11963 return IRQ_NONE;
11964 }
11965 }
11966
4f774513
JS
11967 /* Check device state for handling interrupt */
11968 if (unlikely(lpfc_intr_state_check(phba))) {
b84daac9 11969 fpeq->EQ_badstate++;
4f774513
JS
11970 /* Check again for link_state with lock held */
11971 spin_lock_irqsave(&phba->hbalock, iflag);
11972 if (phba->link_state < LPFC_LINK_DOWN)
11973 /* Flush, clear interrupt, and rearm the EQ */
11974 lpfc_sli4_eq_flush(phba, fpeq);
11975 spin_unlock_irqrestore(&phba->hbalock, iflag);
ba20c853
JS
11976 if (lpfc_fcp_look_ahead)
11977 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
4f774513
JS
11978 return IRQ_NONE;
11979 }
11980
11981 /*
11982 * Process all the event on FCP fast-path EQ
11983 */
11984 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
67d12733 11985 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
73d91e50 11986 if (!(++ecount % fpeq->entry_repost))
4f774513 11987 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
b84daac9 11988 fpeq->EQ_processed++;
4f774513
JS
11989 }
11990
b84daac9
JS
11991 /* Track the max number of EQEs processed in 1 intr */
11992 if (ecount > fpeq->EQ_max_eqe)
11993 fpeq->EQ_max_eqe = ecount;
11994
4f774513
JS
11995 /* Always clear and re-arm the fast-path EQ */
11996 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11997
11998 if (unlikely(ecount == 0)) {
b84daac9 11999 fpeq->EQ_no_entry++;
ba20c853
JS
12000
12001 if (lpfc_fcp_look_ahead) {
12002 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12003 return IRQ_NONE;
12004 }
12005
4f774513
JS
12006 if (phba->intr_type == MSIX)
12007 /* MSI-X treated interrupt served as no EQ share INT */
12008 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12009 "0358 MSI-X interrupt with no EQE\n");
12010 else
12011 /* Non MSI-X treated on interrupt as EQ share INT */
12012 return IRQ_NONE;
12013 }
12014
ba20c853
JS
12015 if (lpfc_fcp_look_ahead)
12016 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
4f774513
JS
12017 return IRQ_HANDLED;
12018} /* lpfc_sli4_fp_intr_handler */
12019
12020/**
12021 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12022 * @irq: Interrupt number.
12023 * @dev_id: The device context pointer.
12024 *
12025 * This function is the device-level interrupt handler to device with SLI-4
12026 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12027 * interrupt mode is enabled and there is an event in the HBA which requires
12028 * driver attention. This function invokes the slow-path interrupt attention
12029 * handling function and fast-path interrupt attention handling function in
12030 * turn to process the relevant HBA attention events. This function is called
12031 * without any lock held. It gets the hbalock to access and update SLI data
12032 * structures.
12033 *
12034 * This function returns IRQ_HANDLED when interrupt is handled, else it
12035 * returns IRQ_NONE.
12036 **/
12037irqreturn_t
12038lpfc_sli4_intr_handler(int irq, void *dev_id)
12039{
12040 struct lpfc_hba *phba;
67d12733
JS
12041 irqreturn_t hba_irq_rc;
12042 bool hba_handled = false;
962bc51b 12043 int fcp_eqidx;
4f774513
JS
12044
12045 /* Get the driver's phba structure from the dev_id */
12046 phba = (struct lpfc_hba *)dev_id;
12047
12048 if (unlikely(!phba))
12049 return IRQ_NONE;
12050
4f774513
JS
12051 /*
12052 * Invoke fast-path host attention interrupt handling as appropriate.
12053 */
67d12733
JS
12054 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12055 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
4f774513 12056 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
67d12733
JS
12057 if (hba_irq_rc == IRQ_HANDLED)
12058 hba_handled |= true;
4f774513
JS
12059 }
12060
67d12733 12061 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
4f774513
JS
12062} /* lpfc_sli4_intr_handler */
12063
12064/**
12065 * lpfc_sli4_queue_free - free a queue structure and associated memory
12066 * @queue: The queue structure to free.
12067 *
b595076a 12068 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
12069 * the host resident queue. This function must be called after destroying the
12070 * queue on the HBA.
12071 **/
12072void
12073lpfc_sli4_queue_free(struct lpfc_queue *queue)
12074{
12075 struct lpfc_dmabuf *dmabuf;
12076
12077 if (!queue)
12078 return;
12079
12080 while (!list_empty(&queue->page_list)) {
12081 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12082 list);
49198b37 12083 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
4f774513
JS
12084 dmabuf->virt, dmabuf->phys);
12085 kfree(dmabuf);
12086 }
12087 kfree(queue);
12088 return;
12089}
12090
12091/**
12092 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12093 * @phba: The HBA that this queue is being created on.
12094 * @entry_size: The size of each queue entry for this queue.
12095 * @entry count: The number of entries that this queue will handle.
12096 *
12097 * This function allocates a queue structure and the DMAable memory used for
12098 * the host resident queue. This function must be called before creating the
12099 * queue on the HBA.
12100 **/
12101struct lpfc_queue *
12102lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12103 uint32_t entry_count)
12104{
12105 struct lpfc_queue *queue;
12106 struct lpfc_dmabuf *dmabuf;
12107 int x, total_qe_count;
12108 void *dma_pointer;
cb5172ea 12109 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 12110
cb5172ea
JS
12111 if (!phba->sli4_hba.pc_sli4_params.supported)
12112 hw_page_size = SLI4_PAGE_SIZE;
12113
4f774513
JS
12114 queue = kzalloc(sizeof(struct lpfc_queue) +
12115 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12116 if (!queue)
12117 return NULL;
cb5172ea
JS
12118 queue->page_count = (ALIGN(entry_size * entry_count,
12119 hw_page_size))/hw_page_size;
4f774513
JS
12120 INIT_LIST_HEAD(&queue->list);
12121 INIT_LIST_HEAD(&queue->page_list);
12122 INIT_LIST_HEAD(&queue->child_list);
12123 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12124 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12125 if (!dmabuf)
12126 goto out_fail;
12127 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
cb5172ea 12128 hw_page_size, &dmabuf->phys,
4f774513
JS
12129 GFP_KERNEL);
12130 if (!dmabuf->virt) {
12131 kfree(dmabuf);
12132 goto out_fail;
12133 }
cb5172ea 12134 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
12135 dmabuf->buffer_tag = x;
12136 list_add_tail(&dmabuf->list, &queue->page_list);
12137 /* initialize queue's entry array */
12138 dma_pointer = dmabuf->virt;
12139 for (; total_qe_count < entry_count &&
cb5172ea 12140 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
12141 total_qe_count++, dma_pointer += entry_size) {
12142 queue->qe[total_qe_count].address = dma_pointer;
12143 }
12144 }
12145 queue->entry_size = entry_size;
12146 queue->entry_count = entry_count;
73d91e50
JS
12147
12148 /*
12149 * entry_repost is calculated based on the number of entries in the
12150 * queue. This works out except for RQs. If buffers are NOT initially
12151 * posted for every RQE, entry_repost should be adjusted accordingly.
12152 */
12153 queue->entry_repost = (entry_count >> 3);
12154 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12155 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
4f774513
JS
12156 queue->phba = phba;
12157
12158 return queue;
12159out_fail:
12160 lpfc_sli4_queue_free(queue);
12161 return NULL;
12162}
12163
962bc51b
JS
12164/**
12165 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
12166 * @phba: HBA structure that indicates port to create a queue on.
12167 * @pci_barset: PCI BAR set flag.
12168 *
12169 * This function shall perform iomap of the specified PCI BAR address to host
12170 * memory address if not already done so and return it. The returned host
12171 * memory address can be NULL.
12172 */
12173static void __iomem *
12174lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
12175{
12176 struct pci_dev *pdev;
12177 unsigned long bar_map, bar_map_len;
12178
12179 if (!phba->pcidev)
12180 return NULL;
12181 else
12182 pdev = phba->pcidev;
12183
12184 switch (pci_barset) {
12185 case WQ_PCI_BAR_0_AND_1:
12186 if (!phba->pci_bar0_memmap_p) {
12187 bar_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
12188 bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
12189 phba->pci_bar0_memmap_p = ioremap(bar_map, bar_map_len);
12190 }
12191 return phba->pci_bar0_memmap_p;
12192 case WQ_PCI_BAR_2_AND_3:
12193 if (!phba->pci_bar2_memmap_p) {
12194 bar_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
12195 bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
12196 phba->pci_bar2_memmap_p = ioremap(bar_map, bar_map_len);
12197 }
12198 return phba->pci_bar2_memmap_p;
12199 case WQ_PCI_BAR_4_AND_5:
12200 if (!phba->pci_bar4_memmap_p) {
12201 bar_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
12202 bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
12203 phba->pci_bar4_memmap_p = ioremap(bar_map, bar_map_len);
12204 }
12205 return phba->pci_bar4_memmap_p;
12206 default:
12207 break;
12208 }
12209 return NULL;
12210}
12211
173edbb2
JS
12212/**
12213 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12214 * @phba: HBA structure that indicates port to create a queue on.
12215 * @startq: The starting FCP EQ to modify
12216 *
12217 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12218 *
12219 * The @phba struct is used to send mailbox command to HBA. The @startq
12220 * is used to get the starting FCP EQ to change.
12221 * This function is asynchronous and will wait for the mailbox
12222 * command to finish before continuing.
12223 *
12224 * On success this function will return a zero. If unable to allocate enough
12225 * memory this function will return -ENOMEM. If the queue create mailbox command
12226 * fails this function will return -ENXIO.
12227 **/
12228uint32_t
12229lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12230{
12231 struct lpfc_mbx_modify_eq_delay *eq_delay;
12232 LPFC_MBOXQ_t *mbox;
12233 struct lpfc_queue *eq;
12234 int cnt, rc, length, status = 0;
12235 uint32_t shdr_status, shdr_add_status;
ee02006b 12236 uint32_t result;
173edbb2
JS
12237 int fcp_eqidx;
12238 union lpfc_sli4_cfg_shdr *shdr;
12239 uint16_t dmult;
12240
67d12733 12241 if (startq >= phba->cfg_fcp_io_channel)
173edbb2
JS
12242 return 0;
12243
12244 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12245 if (!mbox)
12246 return -ENOMEM;
12247 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12248 sizeof(struct lpfc_sli4_cfg_mhdr));
12249 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12250 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12251 length, LPFC_SLI4_MBX_EMBED);
12252 eq_delay = &mbox->u.mqe.un.eq_delay;
12253
12254 /* Calculate delay multiper from maximum interrupt per second */
ee02006b
JS
12255 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
12256 if (result > LPFC_DMULT_CONST)
12257 dmult = 0;
12258 else
12259 dmult = LPFC_DMULT_CONST/result - 1;
173edbb2
JS
12260
12261 cnt = 0;
67d12733 12262 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
173edbb2 12263 fcp_eqidx++) {
67d12733 12264 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
173edbb2
JS
12265 if (!eq)
12266 continue;
12267 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12268 eq_delay->u.request.eq[cnt].phase = 0;
12269 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12270 cnt++;
12271 if (cnt >= LPFC_MAX_EQ_DELAY)
12272 break;
12273 }
12274 eq_delay->u.request.num_eq = cnt;
12275
12276 mbox->vport = phba->pport;
12277 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12278 mbox->context1 = NULL;
12279 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12280 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12281 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12282 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12283 if (shdr_status || shdr_add_status || rc) {
12284 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12285 "2512 MODIFY_EQ_DELAY mailbox failed with "
12286 "status x%x add_status x%x, mbx status x%x\n",
12287 shdr_status, shdr_add_status, rc);
12288 status = -ENXIO;
12289 }
12290 mempool_free(mbox, phba->mbox_mem_pool);
12291 return status;
12292}
12293
4f774513
JS
12294/**
12295 * lpfc_eq_create - Create an Event Queue on the HBA
12296 * @phba: HBA structure that indicates port to create a queue on.
12297 * @eq: The queue structure to use to create the event queue.
12298 * @imax: The maximum interrupt per second limit.
12299 *
12300 * This function creates an event queue, as detailed in @eq, on a port,
12301 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12302 *
12303 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12304 * is used to get the entry count and entry size that are necessary to
12305 * determine the number of pages to allocate and use for this queue. This
12306 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12307 * event queue. This function is asynchronous and will wait for the mailbox
12308 * command to finish before continuing.
12309 *
12310 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
12311 * memory this function will return -ENOMEM. If the queue create mailbox command
12312 * fails this function will return -ENXIO.
4f774513
JS
12313 **/
12314uint32_t
ee02006b 12315lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
4f774513
JS
12316{
12317 struct lpfc_mbx_eq_create *eq_create;
12318 LPFC_MBOXQ_t *mbox;
12319 int rc, length, status = 0;
12320 struct lpfc_dmabuf *dmabuf;
12321 uint32_t shdr_status, shdr_add_status;
12322 union lpfc_sli4_cfg_shdr *shdr;
12323 uint16_t dmult;
49198b37
JS
12324 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12325
2e90f4b5
JS
12326 /* sanity check on queue memory */
12327 if (!eq)
12328 return -ENODEV;
49198b37
JS
12329 if (!phba->sli4_hba.pc_sli4_params.supported)
12330 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
12331
12332 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12333 if (!mbox)
12334 return -ENOMEM;
12335 length = (sizeof(struct lpfc_mbx_eq_create) -
12336 sizeof(struct lpfc_sli4_cfg_mhdr));
12337 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12338 LPFC_MBOX_OPCODE_EQ_CREATE,
12339 length, LPFC_SLI4_MBX_EMBED);
12340 eq_create = &mbox->u.mqe.un.eq_create;
12341 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12342 eq->page_count);
12343 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12344 LPFC_EQE_SIZE);
12345 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12346 /* Calculate delay multiper from maximum interrupt per second */
ee02006b
JS
12347 if (imax > LPFC_DMULT_CONST)
12348 dmult = 0;
12349 else
12350 dmult = LPFC_DMULT_CONST/imax - 1;
4f774513
JS
12351 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12352 dmult);
12353 switch (eq->entry_count) {
12354 default:
12355 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12356 "0360 Unsupported EQ count. (%d)\n",
12357 eq->entry_count);
12358 if (eq->entry_count < 256)
12359 return -EINVAL;
12360 /* otherwise default to smallest count (drop through) */
12361 case 256:
12362 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12363 LPFC_EQ_CNT_256);
12364 break;
12365 case 512:
12366 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12367 LPFC_EQ_CNT_512);
12368 break;
12369 case 1024:
12370 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12371 LPFC_EQ_CNT_1024);
12372 break;
12373 case 2048:
12374 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12375 LPFC_EQ_CNT_2048);
12376 break;
12377 case 4096:
12378 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12379 LPFC_EQ_CNT_4096);
12380 break;
12381 }
12382 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 12383 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
12384 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12385 putPaddrLow(dmabuf->phys);
12386 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12387 putPaddrHigh(dmabuf->phys);
12388 }
12389 mbox->vport = phba->pport;
12390 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12391 mbox->context1 = NULL;
12392 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12393 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12394 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12395 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12396 if (shdr_status || shdr_add_status || rc) {
12397 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12398 "2500 EQ_CREATE mailbox failed with "
12399 "status x%x add_status x%x, mbx status x%x\n",
12400 shdr_status, shdr_add_status, rc);
12401 status = -ENXIO;
12402 }
12403 eq->type = LPFC_EQ;
12404 eq->subtype = LPFC_NONE;
12405 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12406 if (eq->queue_id == 0xFFFF)
12407 status = -ENXIO;
12408 eq->host_index = 0;
12409 eq->hba_index = 0;
12410
8fa38513 12411 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
12412 return status;
12413}
12414
12415/**
12416 * lpfc_cq_create - Create a Completion Queue on the HBA
12417 * @phba: HBA structure that indicates port to create a queue on.
12418 * @cq: The queue structure to use to create the completion queue.
12419 * @eq: The event queue to bind this completion queue to.
12420 *
12421 * This function creates a completion queue, as detailed in @wq, on a port,
12422 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12423 *
12424 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12425 * is used to get the entry count and entry size that are necessary to
12426 * determine the number of pages to allocate and use for this queue. The @eq
12427 * is used to indicate which event queue to bind this completion queue to. This
12428 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12429 * completion queue. This function is asynchronous and will wait for the mailbox
12430 * command to finish before continuing.
12431 *
12432 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
12433 * memory this function will return -ENOMEM. If the queue create mailbox command
12434 * fails this function will return -ENXIO.
4f774513
JS
12435 **/
12436uint32_t
12437lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12438 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12439{
12440 struct lpfc_mbx_cq_create *cq_create;
12441 struct lpfc_dmabuf *dmabuf;
12442 LPFC_MBOXQ_t *mbox;
12443 int rc, length, status = 0;
12444 uint32_t shdr_status, shdr_add_status;
12445 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
12446 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12447
2e90f4b5
JS
12448 /* sanity check on queue memory */
12449 if (!cq || !eq)
12450 return -ENODEV;
49198b37
JS
12451 if (!phba->sli4_hba.pc_sli4_params.supported)
12452 hw_page_size = SLI4_PAGE_SIZE;
12453
4f774513
JS
12454 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12455 if (!mbox)
12456 return -ENOMEM;
12457 length = (sizeof(struct lpfc_mbx_cq_create) -
12458 sizeof(struct lpfc_sli4_cfg_mhdr));
12459 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12460 LPFC_MBOX_OPCODE_CQ_CREATE,
12461 length, LPFC_SLI4_MBX_EMBED);
12462 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 12463 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
12464 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12465 cq->page_count);
12466 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12467 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
12468 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12469 phba->sli4_hba.pc_sli4_params.cqv);
12470 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
c31098ce
JS
12471 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12472 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
5a6f133e
JS
12473 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12474 eq->queue_id);
12475 } else {
12476 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12477 eq->queue_id);
12478 }
4f774513
JS
12479 switch (cq->entry_count) {
12480 default:
12481 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12482 "0361 Unsupported CQ count. (%d)\n",
12483 cq->entry_count);
4f4c1863
JS
12484 if (cq->entry_count < 256) {
12485 status = -EINVAL;
12486 goto out;
12487 }
4f774513
JS
12488 /* otherwise default to smallest count (drop through) */
12489 case 256:
12490 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12491 LPFC_CQ_CNT_256);
12492 break;
12493 case 512:
12494 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12495 LPFC_CQ_CNT_512);
12496 break;
12497 case 1024:
12498 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12499 LPFC_CQ_CNT_1024);
12500 break;
12501 }
12502 list_for_each_entry(dmabuf, &cq->page_list, list) {
49198b37 12503 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
12504 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12505 putPaddrLow(dmabuf->phys);
12506 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12507 putPaddrHigh(dmabuf->phys);
12508 }
12509 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12510
12511 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
12512 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12513 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12514 if (shdr_status || shdr_add_status || rc) {
12515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12516 "2501 CQ_CREATE mailbox failed with "
12517 "status x%x add_status x%x, mbx status x%x\n",
12518 shdr_status, shdr_add_status, rc);
12519 status = -ENXIO;
12520 goto out;
12521 }
12522 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12523 if (cq->queue_id == 0xFFFF) {
12524 status = -ENXIO;
12525 goto out;
12526 }
12527 /* link the cq onto the parent eq child list */
12528 list_add_tail(&cq->list, &eq->child_list);
12529 /* Set up completion queue's type and subtype */
12530 cq->type = type;
12531 cq->subtype = subtype;
12532 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 12533 cq->assoc_qid = eq->queue_id;
4f774513
JS
12534 cq->host_index = 0;
12535 cq->hba_index = 0;
4f774513 12536
8fa38513
JS
12537out:
12538 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
12539 return status;
12540}
12541
b19a061a
JS
12542/**
12543 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
12544 * @phba: HBA structure that indicates port to create a queue on.
12545 * @mq: The queue structure to use to create the mailbox queue.
12546 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12547 * @cq: The completion queue to associate with this cq.
12548 *
12549 * This function provides failback (fb) functionality when the
12550 * mq_create_ext fails on older FW generations. It's purpose is identical
12551 * to mq_create_ext otherwise.
12552 *
12553 * This routine cannot fail as all attributes were previously accessed and
12554 * initialized in mq_create_ext.
12555 **/
12556static void
12557lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12558 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
12559{
12560 struct lpfc_mbx_mq_create *mq_create;
12561 struct lpfc_dmabuf *dmabuf;
12562 int length;
12563
12564 length = (sizeof(struct lpfc_mbx_mq_create) -
12565 sizeof(struct lpfc_sli4_cfg_mhdr));
12566 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12567 LPFC_MBOX_OPCODE_MQ_CREATE,
12568 length, LPFC_SLI4_MBX_EMBED);
12569 mq_create = &mbox->u.mqe.un.mq_create;
12570 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
12571 mq->page_count);
12572 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
12573 cq->queue_id);
12574 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12575 switch (mq->entry_count) {
12576 case 16:
5a6f133e
JS
12577 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12578 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
12579 break;
12580 case 32:
5a6f133e
JS
12581 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12582 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
12583 break;
12584 case 64:
5a6f133e
JS
12585 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12586 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
12587 break;
12588 case 128:
5a6f133e
JS
12589 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12590 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
12591 break;
12592 }
12593 list_for_each_entry(dmabuf, &mq->page_list, list) {
12594 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12595 putPaddrLow(dmabuf->phys);
12596 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12597 putPaddrHigh(dmabuf->phys);
12598 }
12599}
12600
04c68496
JS
12601/**
12602 * lpfc_mq_create - Create a mailbox Queue on the HBA
12603 * @phba: HBA structure that indicates port to create a queue on.
12604 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
12605 * @cq: The completion queue to associate with this cq.
12606 * @subtype: The queue's subtype.
04c68496
JS
12607 *
12608 * This function creates a mailbox queue, as detailed in @mq, on a port,
12609 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12610 *
12611 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12612 * is used to get the entry count and entry size that are necessary to
12613 * determine the number of pages to allocate and use for this queue. This
12614 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12615 * mailbox queue. This function is asynchronous and will wait for the mailbox
12616 * command to finish before continuing.
12617 *
12618 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
12619 * memory this function will return -ENOMEM. If the queue create mailbox command
12620 * fails this function will return -ENXIO.
04c68496 12621 **/
b19a061a 12622int32_t
04c68496
JS
12623lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12624 struct lpfc_queue *cq, uint32_t subtype)
12625{
12626 struct lpfc_mbx_mq_create *mq_create;
b19a061a 12627 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
12628 struct lpfc_dmabuf *dmabuf;
12629 LPFC_MBOXQ_t *mbox;
12630 int rc, length, status = 0;
12631 uint32_t shdr_status, shdr_add_status;
12632 union lpfc_sli4_cfg_shdr *shdr;
49198b37 12633 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 12634
2e90f4b5
JS
12635 /* sanity check on queue memory */
12636 if (!mq || !cq)
12637 return -ENODEV;
49198b37
JS
12638 if (!phba->sli4_hba.pc_sli4_params.supported)
12639 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 12640
04c68496
JS
12641 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12642 if (!mbox)
12643 return -ENOMEM;
b19a061a 12644 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
12645 sizeof(struct lpfc_sli4_cfg_mhdr));
12646 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 12647 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 12648 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
12649
12650 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 12651 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
12652 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12653 &mq_create_ext->u.request, mq->page_count);
12654 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12655 &mq_create_ext->u.request, 1);
12656 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
12657 &mq_create_ext->u.request, 1);
12658 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12659 &mq_create_ext->u.request, 1);
70f3c073
JS
12660 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12661 &mq_create_ext->u.request, 1);
12662 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12663 &mq_create_ext->u.request, 1);
b19a061a 12664 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
12665 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12666 phba->sli4_hba.pc_sli4_params.mqv);
12667 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12668 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12669 cq->queue_id);
12670 else
12671 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12672 cq->queue_id);
04c68496
JS
12673 switch (mq->entry_count) {
12674 default:
12675 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12676 "0362 Unsupported MQ count. (%d)\n",
12677 mq->entry_count);
4f4c1863
JS
12678 if (mq->entry_count < 16) {
12679 status = -EINVAL;
12680 goto out;
12681 }
04c68496
JS
12682 /* otherwise default to smallest count (drop through) */
12683 case 16:
5a6f133e
JS
12684 bf_set(lpfc_mq_context_ring_size,
12685 &mq_create_ext->u.request.context,
12686 LPFC_MQ_RING_SIZE_16);
04c68496
JS
12687 break;
12688 case 32:
5a6f133e
JS
12689 bf_set(lpfc_mq_context_ring_size,
12690 &mq_create_ext->u.request.context,
12691 LPFC_MQ_RING_SIZE_32);
04c68496
JS
12692 break;
12693 case 64:
5a6f133e
JS
12694 bf_set(lpfc_mq_context_ring_size,
12695 &mq_create_ext->u.request.context,
12696 LPFC_MQ_RING_SIZE_64);
04c68496
JS
12697 break;
12698 case 128:
5a6f133e
JS
12699 bf_set(lpfc_mq_context_ring_size,
12700 &mq_create_ext->u.request.context,
12701 LPFC_MQ_RING_SIZE_128);
04c68496
JS
12702 break;
12703 }
12704 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 12705 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 12706 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 12707 putPaddrLow(dmabuf->phys);
b19a061a 12708 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
12709 putPaddrHigh(dmabuf->phys);
12710 }
12711 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
12712 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12713 &mq_create_ext->u.response);
12714 if (rc != MBX_SUCCESS) {
12715 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12716 "2795 MQ_CREATE_EXT failed with "
12717 "status x%x. Failback to MQ_CREATE.\n",
12718 rc);
12719 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12720 mq_create = &mbox->u.mqe.un.mq_create;
12721 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12722 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12723 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12724 &mq_create->u.response);
12725 }
12726
04c68496 12727 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
12728 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12729 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12730 if (shdr_status || shdr_add_status || rc) {
12731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12732 "2502 MQ_CREATE mailbox failed with "
12733 "status x%x add_status x%x, mbx status x%x\n",
12734 shdr_status, shdr_add_status, rc);
12735 status = -ENXIO;
12736 goto out;
12737 }
04c68496
JS
12738 if (mq->queue_id == 0xFFFF) {
12739 status = -ENXIO;
12740 goto out;
12741 }
12742 mq->type = LPFC_MQ;
2a622bfb 12743 mq->assoc_qid = cq->queue_id;
04c68496
JS
12744 mq->subtype = subtype;
12745 mq->host_index = 0;
12746 mq->hba_index = 0;
12747
12748 /* link the mq onto the parent cq child list */
12749 list_add_tail(&mq->list, &cq->child_list);
12750out:
8fa38513 12751 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
12752 return status;
12753}
12754
4f774513
JS
12755/**
12756 * lpfc_wq_create - Create a Work Queue on the HBA
12757 * @phba: HBA structure that indicates port to create a queue on.
12758 * @wq: The queue structure to use to create the work queue.
12759 * @cq: The completion queue to bind this work queue to.
12760 * @subtype: The subtype of the work queue indicating its functionality.
12761 *
12762 * This function creates a work queue, as detailed in @wq, on a port, described
12763 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12764 *
12765 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12766 * is used to get the entry count and entry size that are necessary to
12767 * determine the number of pages to allocate and use for this queue. The @cq
12768 * is used to indicate which completion queue to bind this work queue to. This
12769 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12770 * work queue. This function is asynchronous and will wait for the mailbox
12771 * command to finish before continuing.
12772 *
12773 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
12774 * memory this function will return -ENOMEM. If the queue create mailbox command
12775 * fails this function will return -ENXIO.
4f774513
JS
12776 **/
12777uint32_t
12778lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12779 struct lpfc_queue *cq, uint32_t subtype)
12780{
12781 struct lpfc_mbx_wq_create *wq_create;
12782 struct lpfc_dmabuf *dmabuf;
12783 LPFC_MBOXQ_t *mbox;
12784 int rc, length, status = 0;
12785 uint32_t shdr_status, shdr_add_status;
12786 union lpfc_sli4_cfg_shdr *shdr;
49198b37 12787 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 12788 struct dma_address *page;
962bc51b
JS
12789 void __iomem *bar_memmap_p;
12790 uint32_t db_offset;
12791 uint16_t pci_barset;
49198b37 12792
2e90f4b5
JS
12793 /* sanity check on queue memory */
12794 if (!wq || !cq)
12795 return -ENODEV;
49198b37
JS
12796 if (!phba->sli4_hba.pc_sli4_params.supported)
12797 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
12798
12799 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12800 if (!mbox)
12801 return -ENOMEM;
12802 length = (sizeof(struct lpfc_mbx_wq_create) -
12803 sizeof(struct lpfc_sli4_cfg_mhdr));
12804 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12805 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12806 length, LPFC_SLI4_MBX_EMBED);
12807 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 12808 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
12809 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12810 wq->page_count);
12811 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12812 cq->queue_id);
5a6f133e
JS
12813 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12814 phba->sli4_hba.pc_sli4_params.wqv);
962bc51b 12815
5a6f133e
JS
12816 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12817 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12818 wq->entry_count);
12819 switch (wq->entry_size) {
12820 default:
12821 case 64:
12822 bf_set(lpfc_mbx_wq_create_wqe_size,
12823 &wq_create->u.request_1,
12824 LPFC_WQ_WQE_SIZE_64);
12825 break;
12826 case 128:
12827 bf_set(lpfc_mbx_wq_create_wqe_size,
12828 &wq_create->u.request_1,
12829 LPFC_WQ_WQE_SIZE_128);
12830 break;
12831 }
12832 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12833 (PAGE_SIZE/SLI4_PAGE_SIZE));
12834 page = wq_create->u.request_1.page;
12835 } else {
12836 page = wq_create->u.request.page;
12837 }
4f774513 12838 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 12839 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
12840 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12841 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513 12842 }
962bc51b
JS
12843
12844 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
12845 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
12846
4f774513
JS
12847 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12848 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
12849 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12850 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12851 if (shdr_status || shdr_add_status || rc) {
12852 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12853 "2503 WQ_CREATE mailbox failed with "
12854 "status x%x add_status x%x, mbx status x%x\n",
12855 shdr_status, shdr_add_status, rc);
12856 status = -ENXIO;
12857 goto out;
12858 }
12859 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12860 if (wq->queue_id == 0xFFFF) {
12861 status = -ENXIO;
12862 goto out;
12863 }
962bc51b
JS
12864 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
12865 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
12866 &wq_create->u.response);
12867 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
12868 (wq->db_format != LPFC_DB_RING_FORMAT)) {
12869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12870 "3265 WQ[%d] doorbell format not "
12871 "supported: x%x\n", wq->queue_id,
12872 wq->db_format);
12873 status = -EINVAL;
12874 goto out;
12875 }
12876 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
12877 &wq_create->u.response);
12878 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
12879 if (!bar_memmap_p) {
12880 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12881 "3263 WQ[%d] failed to memmap pci "
12882 "barset:x%x\n", wq->queue_id,
12883 pci_barset);
12884 status = -ENOMEM;
12885 goto out;
12886 }
12887 db_offset = wq_create->u.response.doorbell_offset;
12888 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
12889 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
12890 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12891 "3252 WQ[%d] doorbell offset not "
12892 "supported: x%x\n", wq->queue_id,
12893 db_offset);
12894 status = -EINVAL;
12895 goto out;
12896 }
12897 wq->db_regaddr = bar_memmap_p + db_offset;
12898 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12899 "3264 WQ[%d]: barset:x%x, offset:x%x\n",
12900 wq->queue_id, pci_barset, db_offset);
12901 } else {
12902 wq->db_format = LPFC_DB_LIST_FORMAT;
12903 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
12904 }
4f774513 12905 wq->type = LPFC_WQ;
2a622bfb 12906 wq->assoc_qid = cq->queue_id;
4f774513
JS
12907 wq->subtype = subtype;
12908 wq->host_index = 0;
12909 wq->hba_index = 0;
ff78d8f9 12910 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
4f774513
JS
12911
12912 /* link the wq onto the parent cq child list */
12913 list_add_tail(&wq->list, &cq->child_list);
12914out:
8fa38513 12915 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
12916 return status;
12917}
12918
73d91e50
JS
12919/**
12920 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12921 * @phba: HBA structure that indicates port to create a queue on.
12922 * @rq: The queue structure to use for the receive queue.
12923 * @qno: The associated HBQ number
12924 *
12925 *
12926 * For SLI4 we need to adjust the RQ repost value based on
12927 * the number of buffers that are initially posted to the RQ.
12928 */
12929void
12930lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12931{
12932 uint32_t cnt;
12933
2e90f4b5
JS
12934 /* sanity check on queue memory */
12935 if (!rq)
12936 return;
73d91e50
JS
12937 cnt = lpfc_hbq_defs[qno]->entry_count;
12938
12939 /* Recalc repost for RQs based on buffers initially posted */
12940 cnt = (cnt >> 3);
12941 if (cnt < LPFC_QUEUE_MIN_REPOST)
12942 cnt = LPFC_QUEUE_MIN_REPOST;
12943
12944 rq->entry_repost = cnt;
12945}
12946
4f774513
JS
12947/**
12948 * lpfc_rq_create - Create a Receive Queue on the HBA
12949 * @phba: HBA structure that indicates port to create a queue on.
12950 * @hrq: The queue structure to use to create the header receive queue.
12951 * @drq: The queue structure to use to create the data receive queue.
12952 * @cq: The completion queue to bind this work queue to.
12953 *
12954 * This function creates a receive buffer queue pair , as detailed in @hrq and
12955 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12956 * to the HBA.
12957 *
12958 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12959 * struct is used to get the entry count that is necessary to determine the
12960 * number of pages to use for this queue. The @cq is used to indicate which
12961 * completion queue to bind received buffers that are posted to these queues to.
12962 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12963 * receive queue pair. This function is asynchronous and will wait for the
12964 * mailbox command to finish before continuing.
12965 *
12966 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
12967 * memory this function will return -ENOMEM. If the queue create mailbox command
12968 * fails this function will return -ENXIO.
4f774513
JS
12969 **/
12970uint32_t
12971lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12972 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12973{
12974 struct lpfc_mbx_rq_create *rq_create;
12975 struct lpfc_dmabuf *dmabuf;
12976 LPFC_MBOXQ_t *mbox;
12977 int rc, length, status = 0;
12978 uint32_t shdr_status, shdr_add_status;
12979 union lpfc_sli4_cfg_shdr *shdr;
49198b37 12980 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
962bc51b
JS
12981 void __iomem *bar_memmap_p;
12982 uint32_t db_offset;
12983 uint16_t pci_barset;
49198b37 12984
2e90f4b5
JS
12985 /* sanity check on queue memory */
12986 if (!hrq || !drq || !cq)
12987 return -ENODEV;
49198b37
JS
12988 if (!phba->sli4_hba.pc_sli4_params.supported)
12989 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
12990
12991 if (hrq->entry_count != drq->entry_count)
12992 return -EINVAL;
12993 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12994 if (!mbox)
12995 return -ENOMEM;
12996 length = (sizeof(struct lpfc_mbx_rq_create) -
12997 sizeof(struct lpfc_sli4_cfg_mhdr));
12998 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12999 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13000 length, LPFC_SLI4_MBX_EMBED);
13001 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
13002 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13003 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13004 phba->sli4_hba.pc_sli4_params.rqv);
13005 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13006 bf_set(lpfc_rq_context_rqe_count_1,
13007 &rq_create->u.request.context,
13008 hrq->entry_count);
13009 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
13010 bf_set(lpfc_rq_context_rqe_size,
13011 &rq_create->u.request.context,
13012 LPFC_RQE_SIZE_8);
13013 bf_set(lpfc_rq_context_page_size,
13014 &rq_create->u.request.context,
13015 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
13016 } else {
13017 switch (hrq->entry_count) {
13018 default:
13019 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13020 "2535 Unsupported RQ count. (%d)\n",
13021 hrq->entry_count);
4f4c1863
JS
13022 if (hrq->entry_count < 512) {
13023 status = -EINVAL;
13024 goto out;
13025 }
5a6f133e
JS
13026 /* otherwise default to smallest count (drop through) */
13027 case 512:
13028 bf_set(lpfc_rq_context_rqe_count,
13029 &rq_create->u.request.context,
13030 LPFC_RQ_RING_SIZE_512);
13031 break;
13032 case 1024:
13033 bf_set(lpfc_rq_context_rqe_count,
13034 &rq_create->u.request.context,
13035 LPFC_RQ_RING_SIZE_1024);
13036 break;
13037 case 2048:
13038 bf_set(lpfc_rq_context_rqe_count,
13039 &rq_create->u.request.context,
13040 LPFC_RQ_RING_SIZE_2048);
13041 break;
13042 case 4096:
13043 bf_set(lpfc_rq_context_rqe_count,
13044 &rq_create->u.request.context,
13045 LPFC_RQ_RING_SIZE_4096);
13046 break;
13047 }
13048 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13049 LPFC_HDR_BUF_SIZE);
4f774513
JS
13050 }
13051 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13052 cq->queue_id);
13053 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13054 hrq->page_count);
4f774513 13055 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 13056 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
13057 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13058 putPaddrLow(dmabuf->phys);
13059 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13060 putPaddrHigh(dmabuf->phys);
13061 }
962bc51b
JS
13062 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13063 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13064
4f774513
JS
13065 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13066 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
13067 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13068 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13069 if (shdr_status || shdr_add_status || rc) {
13070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13071 "2504 RQ_CREATE mailbox failed with "
13072 "status x%x add_status x%x, mbx status x%x\n",
13073 shdr_status, shdr_add_status, rc);
13074 status = -ENXIO;
13075 goto out;
13076 }
13077 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13078 if (hrq->queue_id == 0xFFFF) {
13079 status = -ENXIO;
13080 goto out;
13081 }
962bc51b
JS
13082
13083 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13084 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13085 &rq_create->u.response);
13086 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13087 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13088 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13089 "3262 RQ [%d] doorbell format not "
13090 "supported: x%x\n", hrq->queue_id,
13091 hrq->db_format);
13092 status = -EINVAL;
13093 goto out;
13094 }
13095
13096 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13097 &rq_create->u.response);
13098 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13099 if (!bar_memmap_p) {
13100 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13101 "3269 RQ[%d] failed to memmap pci "
13102 "barset:x%x\n", hrq->queue_id,
13103 pci_barset);
13104 status = -ENOMEM;
13105 goto out;
13106 }
13107
13108 db_offset = rq_create->u.response.doorbell_offset;
13109 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13110 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13111 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13112 "3270 RQ[%d] doorbell offset not "
13113 "supported: x%x\n", hrq->queue_id,
13114 db_offset);
13115 status = -EINVAL;
13116 goto out;
13117 }
13118 hrq->db_regaddr = bar_memmap_p + db_offset;
13119 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13120 "3266 RQ[qid:%d]: barset:x%x, offset:x%x\n",
13121 hrq->queue_id, pci_barset, db_offset);
13122 } else {
13123 hrq->db_format = LPFC_DB_RING_FORMAT;
13124 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
13125 }
4f774513 13126 hrq->type = LPFC_HRQ;
2a622bfb 13127 hrq->assoc_qid = cq->queue_id;
4f774513
JS
13128 hrq->subtype = subtype;
13129 hrq->host_index = 0;
13130 hrq->hba_index = 0;
13131
13132 /* now create the data queue */
13133 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13134 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13135 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
13136 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13137 phba->sli4_hba.pc_sli4_params.rqv);
13138 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13139 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 13140 &rq_create->u.request.context, hrq->entry_count);
5a6f133e 13141 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
c31098ce
JS
13142 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
13143 LPFC_RQE_SIZE_8);
13144 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
13145 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
13146 } else {
13147 switch (drq->entry_count) {
13148 default:
13149 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13150 "2536 Unsupported RQ count. (%d)\n",
13151 drq->entry_count);
4f4c1863
JS
13152 if (drq->entry_count < 512) {
13153 status = -EINVAL;
13154 goto out;
13155 }
5a6f133e
JS
13156 /* otherwise default to smallest count (drop through) */
13157 case 512:
13158 bf_set(lpfc_rq_context_rqe_count,
13159 &rq_create->u.request.context,
13160 LPFC_RQ_RING_SIZE_512);
13161 break;
13162 case 1024:
13163 bf_set(lpfc_rq_context_rqe_count,
13164 &rq_create->u.request.context,
13165 LPFC_RQ_RING_SIZE_1024);
13166 break;
13167 case 2048:
13168 bf_set(lpfc_rq_context_rqe_count,
13169 &rq_create->u.request.context,
13170 LPFC_RQ_RING_SIZE_2048);
13171 break;
13172 case 4096:
13173 bf_set(lpfc_rq_context_rqe_count,
13174 &rq_create->u.request.context,
13175 LPFC_RQ_RING_SIZE_4096);
13176 break;
13177 }
13178 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13179 LPFC_DATA_BUF_SIZE);
4f774513
JS
13180 }
13181 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13182 cq->queue_id);
13183 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13184 drq->page_count);
4f774513
JS
13185 list_for_each_entry(dmabuf, &drq->page_list, list) {
13186 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13187 putPaddrLow(dmabuf->phys);
13188 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13189 putPaddrHigh(dmabuf->phys);
13190 }
962bc51b
JS
13191 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13192 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
4f774513
JS
13193 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13194 /* The IOCTL status is embedded in the mailbox subheader. */
13195 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13196 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13197 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13198 if (shdr_status || shdr_add_status || rc) {
13199 status = -ENXIO;
13200 goto out;
13201 }
13202 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13203 if (drq->queue_id == 0xFFFF) {
13204 status = -ENXIO;
13205 goto out;
13206 }
13207 drq->type = LPFC_DRQ;
2a622bfb 13208 drq->assoc_qid = cq->queue_id;
4f774513
JS
13209 drq->subtype = subtype;
13210 drq->host_index = 0;
13211 drq->hba_index = 0;
13212
13213 /* link the header and data RQs onto the parent cq child list */
13214 list_add_tail(&hrq->list, &cq->child_list);
13215 list_add_tail(&drq->list, &cq->child_list);
13216
13217out:
8fa38513 13218 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
13219 return status;
13220}
13221
13222/**
13223 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13224 * @eq: The queue structure associated with the queue to destroy.
13225 *
13226 * This function destroys a queue, as detailed in @eq by sending an mailbox
13227 * command, specific to the type of queue, to the HBA.
13228 *
13229 * The @eq struct is used to get the queue ID of the queue to destroy.
13230 *
13231 * On success this function will return a zero. If the queue destroy mailbox
d439d286 13232 * command fails this function will return -ENXIO.
4f774513
JS
13233 **/
13234uint32_t
13235lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13236{
13237 LPFC_MBOXQ_t *mbox;
13238 int rc, length, status = 0;
13239 uint32_t shdr_status, shdr_add_status;
13240 union lpfc_sli4_cfg_shdr *shdr;
13241
2e90f4b5 13242 /* sanity check on queue memory */
4f774513
JS
13243 if (!eq)
13244 return -ENODEV;
13245 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13246 if (!mbox)
13247 return -ENOMEM;
13248 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13249 sizeof(struct lpfc_sli4_cfg_mhdr));
13250 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13251 LPFC_MBOX_OPCODE_EQ_DESTROY,
13252 length, LPFC_SLI4_MBX_EMBED);
13253 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13254 eq->queue_id);
13255 mbox->vport = eq->phba->pport;
13256 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13257
13258 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13259 /* The IOCTL status is embedded in the mailbox subheader. */
13260 shdr = (union lpfc_sli4_cfg_shdr *)
13261 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13262 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13263 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13264 if (shdr_status || shdr_add_status || rc) {
13265 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13266 "2505 EQ_DESTROY mailbox failed with "
13267 "status x%x add_status x%x, mbx status x%x\n",
13268 shdr_status, shdr_add_status, rc);
13269 status = -ENXIO;
13270 }
13271
13272 /* Remove eq from any list */
13273 list_del_init(&eq->list);
8fa38513 13274 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
13275 return status;
13276}
13277
13278/**
13279 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13280 * @cq: The queue structure associated with the queue to destroy.
13281 *
13282 * This function destroys a queue, as detailed in @cq by sending an mailbox
13283 * command, specific to the type of queue, to the HBA.
13284 *
13285 * The @cq struct is used to get the queue ID of the queue to destroy.
13286 *
13287 * On success this function will return a zero. If the queue destroy mailbox
d439d286 13288 * command fails this function will return -ENXIO.
4f774513
JS
13289 **/
13290uint32_t
13291lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13292{
13293 LPFC_MBOXQ_t *mbox;
13294 int rc, length, status = 0;
13295 uint32_t shdr_status, shdr_add_status;
13296 union lpfc_sli4_cfg_shdr *shdr;
13297
2e90f4b5 13298 /* sanity check on queue memory */
4f774513
JS
13299 if (!cq)
13300 return -ENODEV;
13301 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13302 if (!mbox)
13303 return -ENOMEM;
13304 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13305 sizeof(struct lpfc_sli4_cfg_mhdr));
13306 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13307 LPFC_MBOX_OPCODE_CQ_DESTROY,
13308 length, LPFC_SLI4_MBX_EMBED);
13309 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13310 cq->queue_id);
13311 mbox->vport = cq->phba->pport;
13312 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13313 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13314 /* The IOCTL status is embedded in the mailbox subheader. */
13315 shdr = (union lpfc_sli4_cfg_shdr *)
13316 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13317 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13318 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13319 if (shdr_status || shdr_add_status || rc) {
13320 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13321 "2506 CQ_DESTROY mailbox failed with "
13322 "status x%x add_status x%x, mbx status x%x\n",
13323 shdr_status, shdr_add_status, rc);
13324 status = -ENXIO;
13325 }
13326 /* Remove cq from any list */
13327 list_del_init(&cq->list);
8fa38513 13328 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
13329 return status;
13330}
13331
04c68496
JS
13332/**
13333 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13334 * @qm: The queue structure associated with the queue to destroy.
13335 *
13336 * This function destroys a queue, as detailed in @mq by sending an mailbox
13337 * command, specific to the type of queue, to the HBA.
13338 *
13339 * The @mq struct is used to get the queue ID of the queue to destroy.
13340 *
13341 * On success this function will return a zero. If the queue destroy mailbox
d439d286 13342 * command fails this function will return -ENXIO.
04c68496
JS
13343 **/
13344uint32_t
13345lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13346{
13347 LPFC_MBOXQ_t *mbox;
13348 int rc, length, status = 0;
13349 uint32_t shdr_status, shdr_add_status;
13350 union lpfc_sli4_cfg_shdr *shdr;
13351
2e90f4b5 13352 /* sanity check on queue memory */
04c68496
JS
13353 if (!mq)
13354 return -ENODEV;
13355 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13356 if (!mbox)
13357 return -ENOMEM;
13358 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13359 sizeof(struct lpfc_sli4_cfg_mhdr));
13360 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13361 LPFC_MBOX_OPCODE_MQ_DESTROY,
13362 length, LPFC_SLI4_MBX_EMBED);
13363 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13364 mq->queue_id);
13365 mbox->vport = mq->phba->pport;
13366 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13367 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13368 /* The IOCTL status is embedded in the mailbox subheader. */
13369 shdr = (union lpfc_sli4_cfg_shdr *)
13370 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13371 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13372 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13373 if (shdr_status || shdr_add_status || rc) {
13374 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13375 "2507 MQ_DESTROY mailbox failed with "
13376 "status x%x add_status x%x, mbx status x%x\n",
13377 shdr_status, shdr_add_status, rc);
13378 status = -ENXIO;
13379 }
13380 /* Remove mq from any list */
13381 list_del_init(&mq->list);
8fa38513 13382 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
13383 return status;
13384}
13385
4f774513
JS
13386/**
13387 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13388 * @wq: The queue structure associated with the queue to destroy.
13389 *
13390 * This function destroys a queue, as detailed in @wq by sending an mailbox
13391 * command, specific to the type of queue, to the HBA.
13392 *
13393 * The @wq struct is used to get the queue ID of the queue to destroy.
13394 *
13395 * On success this function will return a zero. If the queue destroy mailbox
d439d286 13396 * command fails this function will return -ENXIO.
4f774513
JS
13397 **/
13398uint32_t
13399lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13400{
13401 LPFC_MBOXQ_t *mbox;
13402 int rc, length, status = 0;
13403 uint32_t shdr_status, shdr_add_status;
13404 union lpfc_sli4_cfg_shdr *shdr;
13405
2e90f4b5 13406 /* sanity check on queue memory */
4f774513
JS
13407 if (!wq)
13408 return -ENODEV;
13409 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13410 if (!mbox)
13411 return -ENOMEM;
13412 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13413 sizeof(struct lpfc_sli4_cfg_mhdr));
13414 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13415 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13416 length, LPFC_SLI4_MBX_EMBED);
13417 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13418 wq->queue_id);
13419 mbox->vport = wq->phba->pport;
13420 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13421 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13422 shdr = (union lpfc_sli4_cfg_shdr *)
13423 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13424 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13425 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13426 if (shdr_status || shdr_add_status || rc) {
13427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13428 "2508 WQ_DESTROY mailbox failed with "
13429 "status x%x add_status x%x, mbx status x%x\n",
13430 shdr_status, shdr_add_status, rc);
13431 status = -ENXIO;
13432 }
13433 /* Remove wq from any list */
13434 list_del_init(&wq->list);
8fa38513 13435 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
13436 return status;
13437}
13438
13439/**
13440 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13441 * @rq: The queue structure associated with the queue to destroy.
13442 *
13443 * This function destroys a queue, as detailed in @rq by sending an mailbox
13444 * command, specific to the type of queue, to the HBA.
13445 *
13446 * The @rq struct is used to get the queue ID of the queue to destroy.
13447 *
13448 * On success this function will return a zero. If the queue destroy mailbox
d439d286 13449 * command fails this function will return -ENXIO.
4f774513
JS
13450 **/
13451uint32_t
13452lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13453 struct lpfc_queue *drq)
13454{
13455 LPFC_MBOXQ_t *mbox;
13456 int rc, length, status = 0;
13457 uint32_t shdr_status, shdr_add_status;
13458 union lpfc_sli4_cfg_shdr *shdr;
13459
2e90f4b5 13460 /* sanity check on queue memory */
4f774513
JS
13461 if (!hrq || !drq)
13462 return -ENODEV;
13463 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13464 if (!mbox)
13465 return -ENOMEM;
13466 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 13467 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
13468 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13469 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13470 length, LPFC_SLI4_MBX_EMBED);
13471 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13472 hrq->queue_id);
13473 mbox->vport = hrq->phba->pport;
13474 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13475 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13476 /* The IOCTL status is embedded in the mailbox subheader. */
13477 shdr = (union lpfc_sli4_cfg_shdr *)
13478 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13479 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13480 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13481 if (shdr_status || shdr_add_status || rc) {
13482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13483 "2509 RQ_DESTROY mailbox failed with "
13484 "status x%x add_status x%x, mbx status x%x\n",
13485 shdr_status, shdr_add_status, rc);
13486 if (rc != MBX_TIMEOUT)
13487 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13488 return -ENXIO;
13489 }
13490 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13491 drq->queue_id);
13492 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13493 shdr = (union lpfc_sli4_cfg_shdr *)
13494 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13495 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13496 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13497 if (shdr_status || shdr_add_status || rc) {
13498 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13499 "2510 RQ_DESTROY mailbox failed with "
13500 "status x%x add_status x%x, mbx status x%x\n",
13501 shdr_status, shdr_add_status, rc);
13502 status = -ENXIO;
13503 }
13504 list_del_init(&hrq->list);
13505 list_del_init(&drq->list);
8fa38513 13506 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
13507 return status;
13508}
13509
13510/**
13511 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13512 * @phba: The virtual port for which this call being executed.
13513 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13514 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13515 * @xritag: the xritag that ties this io to the SGL pages.
13516 *
13517 * This routine will post the sgl pages for the IO that has the xritag
13518 * that is in the iocbq structure. The xritag is assigned during iocbq
13519 * creation and persists for as long as the driver is loaded.
13520 * if the caller has fewer than 256 scatter gather segments to map then
13521 * pdma_phys_addr1 should be 0.
13522 * If the caller needs to map more than 256 scatter gather segment then
13523 * pdma_phys_addr1 should be a valid physical address.
13524 * physical address for SGLs must be 64 byte aligned.
13525 * If you are going to map 2 SGL's then the first one must have 256 entries
13526 * the second sgl can have between 1 and 256 entries.
13527 *
13528 * Return codes:
13529 * 0 - Success
13530 * -ENXIO, -ENOMEM - Failure
13531 **/
13532int
13533lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13534 dma_addr_t pdma_phys_addr0,
13535 dma_addr_t pdma_phys_addr1,
13536 uint16_t xritag)
13537{
13538 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13539 LPFC_MBOXQ_t *mbox;
13540 int rc;
13541 uint32_t shdr_status, shdr_add_status;
6d368e53 13542 uint32_t mbox_tmo;
4f774513
JS
13543 union lpfc_sli4_cfg_shdr *shdr;
13544
13545 if (xritag == NO_XRI) {
13546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13547 "0364 Invalid param:\n");
13548 return -EINVAL;
13549 }
13550
13551 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13552 if (!mbox)
13553 return -ENOMEM;
13554
13555 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13556 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13557 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 13558 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
13559
13560 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13561 &mbox->u.mqe.un.post_sgl_pages;
13562 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13563 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13564
13565 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13566 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13567 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13568 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13569
13570 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13571 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13572 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13573 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13574 if (!phba->sli4_hba.intr_enable)
13575 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 13576 else {
a183a15f 13577 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
13578 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13579 }
4f774513
JS
13580 /* The IOCTL status is embedded in the mailbox subheader. */
13581 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13582 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13583 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13584 if (rc != MBX_TIMEOUT)
13585 mempool_free(mbox, phba->mbox_mem_pool);
13586 if (shdr_status || shdr_add_status || rc) {
13587 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13588 "2511 POST_SGL mailbox failed with "
13589 "status x%x add_status x%x, mbx status x%x\n",
13590 shdr_status, shdr_add_status, rc);
13591 rc = -ENXIO;
13592 }
13593 return 0;
13594}
4f774513 13595
6d368e53 13596/**
88a2cfbb 13597 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
13598 * @phba: pointer to lpfc hba data structure.
13599 *
13600 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
13601 * HBA consistent with the SLI-4 interface spec. This routine
13602 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13603 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 13604 *
88a2cfbb
JS
13605 * Returns
13606 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13607 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13608 **/
6d368e53
JS
13609uint16_t
13610lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13611{
13612 unsigned long xri;
13613
13614 /*
13615 * Fetch the next logical xri. Because this index is logical,
13616 * the driver starts at 0 each time.
13617 */
13618 spin_lock_irq(&phba->hbalock);
13619 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13620 phba->sli4_hba.max_cfg_param.max_xri, 0);
13621 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13622 spin_unlock_irq(&phba->hbalock);
13623 return NO_XRI;
13624 } else {
13625 set_bit(xri, phba->sli4_hba.xri_bmask);
13626 phba->sli4_hba.max_cfg_param.xri_used++;
6d368e53 13627 }
6d368e53
JS
13628 spin_unlock_irq(&phba->hbalock);
13629 return xri;
13630}
13631
13632/**
13633 * lpfc_sli4_free_xri - Release an xri for reuse.
13634 * @phba: pointer to lpfc hba data structure.
13635 *
13636 * This routine is invoked to release an xri to the pool of
13637 * available rpis maintained by the driver.
13638 **/
13639void
13640__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13641{
13642 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
6d368e53
JS
13643 phba->sli4_hba.max_cfg_param.xri_used--;
13644 }
13645}
13646
13647/**
13648 * lpfc_sli4_free_xri - Release an xri for reuse.
13649 * @phba: pointer to lpfc hba data structure.
13650 *
13651 * This routine is invoked to release an xri to the pool of
13652 * available rpis maintained by the driver.
13653 **/
13654void
13655lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13656{
13657 spin_lock_irq(&phba->hbalock);
13658 __lpfc_sli4_free_xri(phba, xri);
13659 spin_unlock_irq(&phba->hbalock);
13660}
13661
4f774513
JS
13662/**
13663 * lpfc_sli4_next_xritag - Get an xritag for the io
13664 * @phba: Pointer to HBA context object.
13665 *
13666 * This function gets an xritag for the iocb. If there is no unused xritag
13667 * it will return 0xffff.
13668 * The function returns the allocated xritag if successful, else returns zero.
13669 * Zero is not a valid xritag.
13670 * The caller is not required to hold any lock.
13671 **/
13672uint16_t
13673lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13674{
6d368e53 13675 uint16_t xri_index;
4f774513 13676
6d368e53 13677 xri_index = lpfc_sli4_alloc_xri(phba);
81378052
JS
13678 if (xri_index == NO_XRI)
13679 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13680 "2004 Failed to allocate XRI.last XRITAG is %d"
13681 " Max XRI is %d, Used XRI is %d\n",
13682 xri_index,
13683 phba->sli4_hba.max_cfg_param.max_xri,
13684 phba->sli4_hba.max_cfg_param.xri_used);
13685 return xri_index;
4f774513
JS
13686}
13687
13688/**
6d368e53 13689 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
4f774513 13690 * @phba: pointer to lpfc hba data structure.
8a9d2e80
JS
13691 * @post_sgl_list: pointer to els sgl entry list.
13692 * @count: number of els sgl entries on the list.
4f774513
JS
13693 *
13694 * This routine is invoked to post a block of driver's sgl pages to the
13695 * HBA using non-embedded mailbox command. No Lock is held. This routine
13696 * is only called when the driver is loading and after all IO has been
13697 * stopped.
13698 **/
8a9d2e80
JS
13699static int
13700lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13701 struct list_head *post_sgl_list,
13702 int post_cnt)
4f774513 13703{
8a9d2e80 13704 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4f774513
JS
13705 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13706 struct sgl_page_pairs *sgl_pg_pairs;
13707 void *viraddr;
13708 LPFC_MBOXQ_t *mbox;
13709 uint32_t reqlen, alloclen, pg_pairs;
13710 uint32_t mbox_tmo;
8a9d2e80
JS
13711 uint16_t xritag_start = 0;
13712 int rc = 0;
4f774513
JS
13713 uint32_t shdr_status, shdr_add_status;
13714 union lpfc_sli4_cfg_shdr *shdr;
13715
8a9d2e80 13716 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
4f774513 13717 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 13718 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
13719 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13720 "2559 Block sgl registration required DMA "
13721 "size (%d) great than a page\n", reqlen);
13722 return -ENOMEM;
13723 }
13724 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 13725 if (!mbox)
4f774513 13726 return -ENOMEM;
4f774513
JS
13727
13728 /* Allocate DMA memory and set up the non-embedded mailbox command */
13729 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13730 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13731 LPFC_SLI4_MBX_NEMBED);
13732
13733 if (alloclen < reqlen) {
13734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13735 "0285 Allocated DMA memory size (%d) is "
13736 "less than the requested DMA memory "
13737 "size (%d)\n", alloclen, reqlen);
13738 lpfc_sli4_mbox_cmd_free(phba, mbox);
13739 return -ENOMEM;
13740 }
4f774513 13741 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 13742 viraddr = mbox->sge_array->addr[0];
4f774513
JS
13743 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13744 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13745
8a9d2e80
JS
13746 pg_pairs = 0;
13747 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
4f774513
JS
13748 /* Set up the sge entry */
13749 sgl_pg_pairs->sgl_pg0_addr_lo =
13750 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13751 sgl_pg_pairs->sgl_pg0_addr_hi =
13752 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13753 sgl_pg_pairs->sgl_pg1_addr_lo =
13754 cpu_to_le32(putPaddrLow(0));
13755 sgl_pg_pairs->sgl_pg1_addr_hi =
13756 cpu_to_le32(putPaddrHigh(0));
6d368e53 13757
4f774513
JS
13758 /* Keep the first xritag on the list */
13759 if (pg_pairs == 0)
13760 xritag_start = sglq_entry->sli4_xritag;
13761 sgl_pg_pairs++;
8a9d2e80 13762 pg_pairs++;
4f774513 13763 }
6d368e53
JS
13764
13765 /* Complete initialization and perform endian conversion. */
4f774513 13766 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
8a9d2e80 13767 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
4f774513 13768 sgl->word0 = cpu_to_le32(sgl->word0);
4f774513
JS
13769 if (!phba->sli4_hba.intr_enable)
13770 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13771 else {
a183a15f 13772 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
13773 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13774 }
13775 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13776 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13777 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13778 if (rc != MBX_TIMEOUT)
13779 lpfc_sli4_mbox_cmd_free(phba, mbox);
13780 if (shdr_status || shdr_add_status || rc) {
13781 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13782 "2513 POST_SGL_BLOCK mailbox command failed "
13783 "status x%x add_status x%x mbx status x%x\n",
13784 shdr_status, shdr_add_status, rc);
13785 rc = -ENXIO;
13786 }
13787 return rc;
13788}
13789
13790/**
13791 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13792 * @phba: pointer to lpfc hba data structure.
13793 * @sblist: pointer to scsi buffer list.
13794 * @count: number of scsi buffers on the list.
13795 *
13796 * This routine is invoked to post a block of @count scsi sgl pages from a
13797 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13798 * No Lock is held.
13799 *
13800 **/
13801int
8a9d2e80
JS
13802lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13803 struct list_head *sblist,
13804 int count)
4f774513
JS
13805{
13806 struct lpfc_scsi_buf *psb;
13807 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13808 struct sgl_page_pairs *sgl_pg_pairs;
13809 void *viraddr;
13810 LPFC_MBOXQ_t *mbox;
13811 uint32_t reqlen, alloclen, pg_pairs;
13812 uint32_t mbox_tmo;
13813 uint16_t xritag_start = 0;
13814 int rc = 0;
13815 uint32_t shdr_status, shdr_add_status;
13816 dma_addr_t pdma_phys_bpl1;
13817 union lpfc_sli4_cfg_shdr *shdr;
13818
13819 /* Calculate the requested length of the dma memory */
8a9d2e80 13820 reqlen = count * sizeof(struct sgl_page_pairs) +
4f774513 13821 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 13822 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
13823 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13824 "0217 Block sgl registration required DMA "
13825 "size (%d) great than a page\n", reqlen);
13826 return -ENOMEM;
13827 }
13828 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13829 if (!mbox) {
13830 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13831 "0283 Failed to allocate mbox cmd memory\n");
13832 return -ENOMEM;
13833 }
13834
13835 /* Allocate DMA memory and set up the non-embedded mailbox command */
13836 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13837 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13838 LPFC_SLI4_MBX_NEMBED);
13839
13840 if (alloclen < reqlen) {
13841 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13842 "2561 Allocated DMA memory size (%d) is "
13843 "less than the requested DMA memory "
13844 "size (%d)\n", alloclen, reqlen);
13845 lpfc_sli4_mbox_cmd_free(phba, mbox);
13846 return -ENOMEM;
13847 }
6d368e53 13848
4f774513 13849 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
13850 viraddr = mbox->sge_array->addr[0];
13851
13852 /* Set up the SGL pages in the non-embedded DMA pages */
13853 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13854 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13855
13856 pg_pairs = 0;
13857 list_for_each_entry(psb, sblist, list) {
13858 /* Set up the sge entry */
13859 sgl_pg_pairs->sgl_pg0_addr_lo =
13860 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13861 sgl_pg_pairs->sgl_pg0_addr_hi =
13862 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13863 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13864 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13865 else
13866 pdma_phys_bpl1 = 0;
13867 sgl_pg_pairs->sgl_pg1_addr_lo =
13868 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13869 sgl_pg_pairs->sgl_pg1_addr_hi =
13870 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13871 /* Keep the first xritag on the list */
13872 if (pg_pairs == 0)
13873 xritag_start = psb->cur_iocbq.sli4_xritag;
13874 sgl_pg_pairs++;
13875 pg_pairs++;
13876 }
13877 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13878 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13879 /* Perform endian conversion if necessary */
13880 sgl->word0 = cpu_to_le32(sgl->word0);
13881
13882 if (!phba->sli4_hba.intr_enable)
13883 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13884 else {
a183a15f 13885 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
13886 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13887 }
13888 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13889 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13890 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13891 if (rc != MBX_TIMEOUT)
13892 lpfc_sli4_mbox_cmd_free(phba, mbox);
13893 if (shdr_status || shdr_add_status || rc) {
13894 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13895 "2564 POST_SGL_BLOCK mailbox command failed "
13896 "status x%x add_status x%x mbx status x%x\n",
13897 shdr_status, shdr_add_status, rc);
13898 rc = -ENXIO;
13899 }
13900 return rc;
13901}
13902
13903/**
13904 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13905 * @phba: pointer to lpfc_hba struct that the frame was received on
13906 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13907 *
13908 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13909 * valid type of frame that the LPFC driver will handle. This function will
13910 * return a zero if the frame is a valid frame or a non zero value when the
13911 * frame does not pass the check.
13912 **/
13913static int
13914lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13915{
474ffb74
TH
13916 /* make rctl_names static to save stack space */
13917 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
4f774513
JS
13918 char *type_names[] = FC_TYPE_NAMES_INIT;
13919 struct fc_vft_header *fc_vft_hdr;
546fc854 13920 uint32_t *header = (uint32_t *) fc_hdr;
4f774513
JS
13921
13922 switch (fc_hdr->fh_r_ctl) {
13923 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13924 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13925 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13926 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13927 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13928 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13929 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13930 case FC_RCTL_DD_CMD_STATUS: /* command status */
13931 case FC_RCTL_ELS_REQ: /* extended link services request */
13932 case FC_RCTL_ELS_REP: /* extended link services reply */
13933 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13934 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13935 case FC_RCTL_BA_NOP: /* basic link service NOP */
13936 case FC_RCTL_BA_ABTS: /* basic link service abort */
13937 case FC_RCTL_BA_RMC: /* remove connection */
13938 case FC_RCTL_BA_ACC: /* basic accept */
13939 case FC_RCTL_BA_RJT: /* basic reject */
13940 case FC_RCTL_BA_PRMT:
13941 case FC_RCTL_ACK_1: /* acknowledge_1 */
13942 case FC_RCTL_ACK_0: /* acknowledge_0 */
13943 case FC_RCTL_P_RJT: /* port reject */
13944 case FC_RCTL_F_RJT: /* fabric reject */
13945 case FC_RCTL_P_BSY: /* port busy */
13946 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13947 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13948 case FC_RCTL_LCR: /* link credit reset */
13949 case FC_RCTL_END: /* end */
13950 break;
13951 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13952 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13953 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13954 return lpfc_fc_frame_check(phba, fc_hdr);
13955 default:
13956 goto drop;
13957 }
13958 switch (fc_hdr->fh_type) {
13959 case FC_TYPE_BLS:
13960 case FC_TYPE_ELS:
13961 case FC_TYPE_FCP:
13962 case FC_TYPE_CT:
13963 break;
13964 case FC_TYPE_IP:
13965 case FC_TYPE_ILS:
13966 default:
13967 goto drop;
13968 }
546fc854 13969
4f774513 13970 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
546fc854
JS
13971 "2538 Received frame rctl:%s type:%s "
13972 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
4f774513 13973 rctl_names[fc_hdr->fh_r_ctl],
546fc854
JS
13974 type_names[fc_hdr->fh_type],
13975 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13976 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13977 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
4f774513
JS
13978 return 0;
13979drop:
13980 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13981 "2539 Dropped frame rctl:%s type:%s\n",
13982 rctl_names[fc_hdr->fh_r_ctl],
13983 type_names[fc_hdr->fh_type]);
13984 return 1;
13985}
13986
13987/**
13988 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13989 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13990 *
13991 * This function processes the FC header to retrieve the VFI from the VF
13992 * header, if one exists. This function will return the VFI if one exists
13993 * or 0 if no VSAN Header exists.
13994 **/
13995static uint32_t
13996lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13997{
13998 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13999
14000 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14001 return 0;
14002 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14003}
14004
14005/**
14006 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14007 * @phba: Pointer to the HBA structure to search for the vport on
14008 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14009 * @fcfi: The FC Fabric ID that the frame came from
14010 *
14011 * This function searches the @phba for a vport that matches the content of the
14012 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14013 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14014 * returns the matching vport pointer or NULL if unable to match frame to a
14015 * vport.
14016 **/
14017static struct lpfc_vport *
14018lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14019 uint16_t fcfi)
14020{
14021 struct lpfc_vport **vports;
14022 struct lpfc_vport *vport = NULL;
14023 int i;
14024 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14025 fc_hdr->fh_d_id[1] << 8 |
14026 fc_hdr->fh_d_id[2]);
939723a4 14027
bf08611b
JS
14028 if (did == Fabric_DID)
14029 return phba->pport;
939723a4
JS
14030 if ((phba->pport->fc_flag & FC_PT2PT) &&
14031 !(phba->link_state == LPFC_HBA_READY))
14032 return phba->pport;
14033
4f774513
JS
14034 vports = lpfc_create_vport_work_array(phba);
14035 if (vports != NULL)
14036 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14037 if (phba->fcf.fcfi == fcfi &&
14038 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14039 vports[i]->fc_myDID == did) {
14040 vport = vports[i];
14041 break;
14042 }
14043 }
14044 lpfc_destroy_vport_work_array(phba, vports);
14045 return vport;
14046}
14047
45ed1190
JS
14048/**
14049 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14050 * @vport: The vport to work on.
14051 *
14052 * This function updates the receive sequence time stamp for this vport. The
14053 * receive sequence time stamp indicates the time that the last frame of the
14054 * the sequence that has been idle for the longest amount of time was received.
14055 * the driver uses this time stamp to indicate if any received sequences have
14056 * timed out.
14057 **/
14058void
14059lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14060{
14061 struct lpfc_dmabuf *h_buf;
14062 struct hbq_dmabuf *dmabuf = NULL;
14063
14064 /* get the oldest sequence on the rcv list */
14065 h_buf = list_get_first(&vport->rcv_buffer_list,
14066 struct lpfc_dmabuf, list);
14067 if (!h_buf)
14068 return;
14069 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14070 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14071}
14072
14073/**
14074 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14075 * @vport: The vport that the received sequences were sent to.
14076 *
14077 * This function cleans up all outstanding received sequences. This is called
14078 * by the driver when a link event or user action invalidates all the received
14079 * sequences.
14080 **/
14081void
14082lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14083{
14084 struct lpfc_dmabuf *h_buf, *hnext;
14085 struct lpfc_dmabuf *d_buf, *dnext;
14086 struct hbq_dmabuf *dmabuf = NULL;
14087
14088 /* start with the oldest sequence on the rcv list */
14089 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14090 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14091 list_del_init(&dmabuf->hbuf.list);
14092 list_for_each_entry_safe(d_buf, dnext,
14093 &dmabuf->dbuf.list, list) {
14094 list_del_init(&d_buf->list);
14095 lpfc_in_buf_free(vport->phba, d_buf);
14096 }
14097 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14098 }
14099}
14100
14101/**
14102 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14103 * @vport: The vport that the received sequences were sent to.
14104 *
14105 * This function determines whether any received sequences have timed out by
14106 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14107 * indicates that there is at least one timed out sequence this routine will
14108 * go through the received sequences one at a time from most inactive to most
14109 * active to determine which ones need to be cleaned up. Once it has determined
14110 * that a sequence needs to be cleaned up it will simply free up the resources
14111 * without sending an abort.
14112 **/
14113void
14114lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14115{
14116 struct lpfc_dmabuf *h_buf, *hnext;
14117 struct lpfc_dmabuf *d_buf, *dnext;
14118 struct hbq_dmabuf *dmabuf = NULL;
14119 unsigned long timeout;
14120 int abort_count = 0;
14121
14122 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14123 vport->rcv_buffer_time_stamp);
14124 if (list_empty(&vport->rcv_buffer_list) ||
14125 time_before(jiffies, timeout))
14126 return;
14127 /* start with the oldest sequence on the rcv list */
14128 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14129 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14130 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14131 dmabuf->time_stamp);
14132 if (time_before(jiffies, timeout))
14133 break;
14134 abort_count++;
14135 list_del_init(&dmabuf->hbuf.list);
14136 list_for_each_entry_safe(d_buf, dnext,
14137 &dmabuf->dbuf.list, list) {
14138 list_del_init(&d_buf->list);
14139 lpfc_in_buf_free(vport->phba, d_buf);
14140 }
14141 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14142 }
14143 if (abort_count)
14144 lpfc_update_rcv_time_stamp(vport);
14145}
14146
4f774513
JS
14147/**
14148 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
14149 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
14150 *
14151 * This function searches through the existing incomplete sequences that have
14152 * been sent to this @vport. If the frame matches one of the incomplete
14153 * sequences then the dbuf in the @dmabuf is added to the list of frames that
14154 * make up that sequence. If no sequence is found that matches this frame then
14155 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
14156 * This function returns a pointer to the first dmabuf in the sequence list that
14157 * the frame was linked to.
14158 **/
14159static struct hbq_dmabuf *
14160lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14161{
14162 struct fc_frame_header *new_hdr;
14163 struct fc_frame_header *temp_hdr;
14164 struct lpfc_dmabuf *d_buf;
14165 struct lpfc_dmabuf *h_buf;
14166 struct hbq_dmabuf *seq_dmabuf = NULL;
14167 struct hbq_dmabuf *temp_dmabuf = NULL;
14168
4d9ab994 14169 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 14170 dmabuf->time_stamp = jiffies;
4f774513
JS
14171 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14172 /* Use the hdr_buf to find the sequence that this frame belongs to */
14173 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14174 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14175 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14176 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14177 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14178 continue;
14179 /* found a pending sequence that matches this frame */
14180 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14181 break;
14182 }
14183 if (!seq_dmabuf) {
14184 /*
14185 * This indicates first frame received for this sequence.
14186 * Queue the buffer on the vport's rcv_buffer_list.
14187 */
14188 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 14189 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
14190 return dmabuf;
14191 }
14192 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
14193 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
14194 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
14195 list_del_init(&seq_dmabuf->hbuf.list);
14196 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14197 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 14198 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
14199 return dmabuf;
14200 }
45ed1190
JS
14201 /* move this sequence to the tail to indicate a young sequence */
14202 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14203 seq_dmabuf->time_stamp = jiffies;
14204 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
14205 if (list_empty(&seq_dmabuf->dbuf.list)) {
14206 temp_hdr = dmabuf->hbuf.virt;
14207 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14208 return seq_dmabuf;
14209 }
4f774513
JS
14210 /* find the correct place in the sequence to insert this frame */
14211 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14212 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14213 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14214 /*
14215 * If the frame's sequence count is greater than the frame on
14216 * the list then insert the frame right after this frame
14217 */
eeead811
JS
14218 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14219 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513
JS
14220 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14221 return seq_dmabuf;
14222 }
14223 }
14224 return NULL;
14225}
14226
6669f9bb
JS
14227/**
14228 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14229 * @vport: pointer to a vitural port
14230 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14231 *
14232 * This function tries to abort from the partially assembed sequence, described
14233 * by the information from basic abbort @dmabuf. It checks to see whether such
14234 * partially assembled sequence held by the driver. If so, it shall free up all
14235 * the frames from the partially assembled sequence.
14236 *
14237 * Return
14238 * true -- if there is matching partially assembled sequence present and all
14239 * the frames freed with the sequence;
14240 * false -- if there is no matching partially assembled sequence present so
14241 * nothing got aborted in the lower layer driver
14242 **/
14243static bool
14244lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14245 struct hbq_dmabuf *dmabuf)
14246{
14247 struct fc_frame_header *new_hdr;
14248 struct fc_frame_header *temp_hdr;
14249 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14250 struct hbq_dmabuf *seq_dmabuf = NULL;
14251
14252 /* Use the hdr_buf to find the sequence that matches this frame */
14253 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14254 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14255 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14256 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14257 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14258 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14259 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14260 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14261 continue;
14262 /* found a pending sequence that matches this frame */
14263 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14264 break;
14265 }
14266
14267 /* Free up all the frames from the partially assembled sequence */
14268 if (seq_dmabuf) {
14269 list_for_each_entry_safe(d_buf, n_buf,
14270 &seq_dmabuf->dbuf.list, list) {
14271 list_del_init(&d_buf->list);
14272 lpfc_in_buf_free(vport->phba, d_buf);
14273 }
14274 return true;
14275 }
14276 return false;
14277}
14278
6dd9e31c
JS
14279/**
14280 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
14281 * @vport: pointer to a vitural port
14282 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14283 *
14284 * This function tries to abort from the assembed sequence from upper level
14285 * protocol, described by the information from basic abbort @dmabuf. It
14286 * checks to see whether such pending context exists at upper level protocol.
14287 * If so, it shall clean up the pending context.
14288 *
14289 * Return
14290 * true -- if there is matching pending context of the sequence cleaned
14291 * at ulp;
14292 * false -- if there is no matching pending context of the sequence present
14293 * at ulp.
14294 **/
14295static bool
14296lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14297{
14298 struct lpfc_hba *phba = vport->phba;
14299 int handled;
14300
14301 /* Accepting abort at ulp with SLI4 only */
14302 if (phba->sli_rev < LPFC_SLI_REV4)
14303 return false;
14304
14305 /* Register all caring upper level protocols to attend abort */
14306 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
14307 if (handled)
14308 return true;
14309
14310 return false;
14311}
14312
6669f9bb 14313/**
546fc854 14314 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
14315 * @phba: Pointer to HBA context object.
14316 * @cmd_iocbq: pointer to the command iocbq structure.
14317 * @rsp_iocbq: pointer to the response iocbq structure.
14318 *
546fc854 14319 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
14320 * event. It properly releases the memory allocated to the sequence abort
14321 * accept iocb.
14322 **/
14323static void
546fc854 14324lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
14325 struct lpfc_iocbq *cmd_iocbq,
14326 struct lpfc_iocbq *rsp_iocbq)
14327{
6dd9e31c
JS
14328 struct lpfc_nodelist *ndlp;
14329
14330 if (cmd_iocbq) {
14331 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
14332 lpfc_nlp_put(ndlp);
14333 lpfc_nlp_not_used(ndlp);
6669f9bb 14334 lpfc_sli_release_iocbq(phba, cmd_iocbq);
6dd9e31c 14335 }
6b5151fd
JS
14336
14337 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14338 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14339 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14340 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14341 rsp_iocbq->iocb.ulpStatus,
14342 rsp_iocbq->iocb.un.ulpWord[4]);
6669f9bb
JS
14343}
14344
6d368e53
JS
14345/**
14346 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14347 * @phba: Pointer to HBA context object.
14348 * @xri: xri id in transaction.
14349 *
14350 * This function validates the xri maps to the known range of XRIs allocated an
14351 * used by the driver.
14352 **/
7851fe2c 14353uint16_t
6d368e53
JS
14354lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14355 uint16_t xri)
14356{
14357 int i;
14358
14359 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14360 if (xri == phba->sli4_hba.xri_ids[i])
14361 return i;
14362 }
14363 return NO_XRI;
14364}
14365
6669f9bb 14366/**
546fc854 14367 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
14368 * @phba: Pointer to HBA context object.
14369 * @fc_hdr: pointer to a FC frame header.
14370 *
546fc854 14371 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
14372 * event after aborting the sequence handling.
14373 **/
14374static void
6dd9e31c
JS
14375lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
14376 struct fc_frame_header *fc_hdr, bool aborted)
6669f9bb 14377{
6dd9e31c 14378 struct lpfc_hba *phba = vport->phba;
6669f9bb
JS
14379 struct lpfc_iocbq *ctiocb = NULL;
14380 struct lpfc_nodelist *ndlp;
ee0f4fe1 14381 uint16_t oxid, rxid, xri, lxri;
5ffc266e 14382 uint32_t sid, fctl;
6669f9bb 14383 IOCB_t *icmd;
546fc854 14384 int rc;
6669f9bb
JS
14385
14386 if (!lpfc_is_link_up(phba))
14387 return;
14388
14389 sid = sli4_sid_from_fc_hdr(fc_hdr);
14390 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 14391 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb 14392
6dd9e31c 14393 ndlp = lpfc_findnode_did(vport, sid);
6669f9bb 14394 if (!ndlp) {
6dd9e31c
JS
14395 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
14396 if (!ndlp) {
14397 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14398 "1268 Failed to allocate ndlp for "
14399 "oxid:x%x SID:x%x\n", oxid, sid);
14400 return;
14401 }
14402 lpfc_nlp_init(vport, ndlp, sid);
14403 /* Put ndlp onto pport node list */
14404 lpfc_enqueue_node(vport, ndlp);
14405 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
14406 /* re-setup ndlp without removing from node list */
14407 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
14408 if (!ndlp) {
14409 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14410 "3275 Failed to active ndlp found "
14411 "for oxid:x%x SID:x%x\n", oxid, sid);
14412 return;
14413 }
6669f9bb
JS
14414 }
14415
546fc854 14416 /* Allocate buffer for rsp iocb */
6669f9bb
JS
14417 ctiocb = lpfc_sli_get_iocbq(phba);
14418 if (!ctiocb)
14419 return;
14420
5ffc266e
JS
14421 /* Extract the F_CTL field from FC_HDR */
14422 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14423
6669f9bb 14424 icmd = &ctiocb->iocb;
6669f9bb 14425 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 14426 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
14427 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14428 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14429 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14430
14431 /* Fill in the rest of iocb fields */
14432 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14433 icmd->ulpBdeCount = 0;
14434 icmd->ulpLe = 1;
14435 icmd->ulpClass = CLASS3;
6d368e53 14436 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
6dd9e31c 14437 ctiocb->context1 = lpfc_nlp_get(ndlp);
6669f9bb 14438
6669f9bb
JS
14439 ctiocb->iocb_cmpl = NULL;
14440 ctiocb->vport = phba->pport;
546fc854 14441 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 14442 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
14443 ctiocb->sli4_xritag = NO_XRI;
14444
ee0f4fe1
JS
14445 if (fctl & FC_FC_EX_CTX)
14446 /* Exchange responder sent the abort so we
14447 * own the oxid.
14448 */
14449 xri = oxid;
14450 else
14451 xri = rxid;
14452 lxri = lpfc_sli4_xri_inrange(phba, xri);
14453 if (lxri != NO_XRI)
14454 lpfc_set_rrq_active(phba, ndlp, lxri,
14455 (xri == oxid) ? rxid : oxid, 0);
6dd9e31c
JS
14456 /* For BA_ABTS from exchange responder, if the logical xri with
14457 * the oxid maps to the FCP XRI range, the port no longer has
14458 * that exchange context, send a BLS_RJT. Override the IOCB for
14459 * a BA_RJT.
14460 */
14461 if ((fctl & FC_FC_EX_CTX) &&
14462 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
14463 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14464 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14465 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14466 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14467 }
14468
14469 /* If BA_ABTS failed to abort a partially assembled receive sequence,
14470 * the driver no longer has that exchange, send a BLS_RJT. Override
14471 * the IOCB for a BA_RJT.
546fc854 14472 */
6dd9e31c 14473 if (aborted == false) {
546fc854
JS
14474 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14475 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14476 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14477 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14478 }
6669f9bb 14479
5ffc266e
JS
14480 if (fctl & FC_FC_EX_CTX) {
14481 /* ABTS sent by responder to CT exchange, construction
14482 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14483 * field and RX_ID from ABTS for RX_ID field.
14484 */
546fc854 14485 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
5ffc266e
JS
14486 } else {
14487 /* ABTS sent by initiator to CT exchange, construction
14488 * of BA_ACC will need to allocate a new XRI as for the
f09c3acc 14489 * XRI_TAG field.
5ffc266e 14490 */
546fc854 14491 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
5ffc266e 14492 }
f09c3acc 14493 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
546fc854 14494 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 14495
546fc854 14496 /* Xmit CT abts response on exchange <xid> */
6dd9e31c
JS
14497 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
14498 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14499 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
546fc854
JS
14500
14501 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14502 if (rc == IOCB_ERROR) {
6dd9e31c
JS
14503 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
14504 "2925 Failed to issue CT ABTS RSP x%x on "
14505 "xri x%x, Data x%x\n",
14506 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14507 phba->link_state);
14508 lpfc_nlp_put(ndlp);
14509 ctiocb->context1 = NULL;
546fc854
JS
14510 lpfc_sli_release_iocbq(phba, ctiocb);
14511 }
6669f9bb
JS
14512}
14513
14514/**
14515 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14516 * @vport: Pointer to the vport on which this sequence was received
14517 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14518 *
14519 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14520 * receive sequence is only partially assembed by the driver, it shall abort
14521 * the partially assembled frames for the sequence. Otherwise, if the
14522 * unsolicited receive sequence has been completely assembled and passed to
14523 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14524 * unsolicited sequence has been aborted. After that, it will issue a basic
14525 * accept to accept the abort.
14526 **/
14527void
14528lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14529 struct hbq_dmabuf *dmabuf)
14530{
14531 struct lpfc_hba *phba = vport->phba;
14532 struct fc_frame_header fc_hdr;
5ffc266e 14533 uint32_t fctl;
6dd9e31c 14534 bool aborted;
6669f9bb 14535
6669f9bb
JS
14536 /* Make a copy of fc_hdr before the dmabuf being released */
14537 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 14538 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 14539
5ffc266e 14540 if (fctl & FC_FC_EX_CTX) {
6dd9e31c
JS
14541 /* ABTS by responder to exchange, no cleanup needed */
14542 aborted = true;
5ffc266e 14543 } else {
6dd9e31c
JS
14544 /* ABTS by initiator to exchange, need to do cleanup */
14545 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14546 if (aborted == false)
14547 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
5ffc266e 14548 }
6dd9e31c
JS
14549 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14550
14551 /* Respond with BA_ACC or BA_RJT accordingly */
14552 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
6669f9bb
JS
14553}
14554
4f774513
JS
14555/**
14556 * lpfc_seq_complete - Indicates if a sequence is complete
14557 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14558 *
14559 * This function checks the sequence, starting with the frame described by
14560 * @dmabuf, to see if all the frames associated with this sequence are present.
14561 * the frames associated with this sequence are linked to the @dmabuf using the
14562 * dbuf list. This function looks for two major things. 1) That the first frame
14563 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14564 * set. 3) That there are no holes in the sequence count. The function will
14565 * return 1 when the sequence is complete, otherwise it will return 0.
14566 **/
14567static int
14568lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14569{
14570 struct fc_frame_header *hdr;
14571 struct lpfc_dmabuf *d_buf;
14572 struct hbq_dmabuf *seq_dmabuf;
14573 uint32_t fctl;
14574 int seq_count = 0;
14575
14576 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14577 /* make sure first fame of sequence has a sequence count of zero */
14578 if (hdr->fh_seq_cnt != seq_count)
14579 return 0;
14580 fctl = (hdr->fh_f_ctl[0] << 16 |
14581 hdr->fh_f_ctl[1] << 8 |
14582 hdr->fh_f_ctl[2]);
14583 /* If last frame of sequence we can return success. */
14584 if (fctl & FC_FC_END_SEQ)
14585 return 1;
14586 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14587 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14588 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14589 /* If there is a hole in the sequence count then fail. */
eeead811 14590 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
14591 return 0;
14592 fctl = (hdr->fh_f_ctl[0] << 16 |
14593 hdr->fh_f_ctl[1] << 8 |
14594 hdr->fh_f_ctl[2]);
14595 /* If last frame of sequence we can return success. */
14596 if (fctl & FC_FC_END_SEQ)
14597 return 1;
14598 }
14599 return 0;
14600}
14601
14602/**
14603 * lpfc_prep_seq - Prep sequence for ULP processing
14604 * @vport: Pointer to the vport on which this sequence was received
14605 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14606 *
14607 * This function takes a sequence, described by a list of frames, and creates
14608 * a list of iocbq structures to describe the sequence. This iocbq list will be
14609 * used to issue to the generic unsolicited sequence handler. This routine
14610 * returns a pointer to the first iocbq in the list. If the function is unable
14611 * to allocate an iocbq then it throw out the received frames that were not
14612 * able to be described and return a pointer to the first iocbq. If unable to
14613 * allocate any iocbqs (including the first) this function will return NULL.
14614 **/
14615static struct lpfc_iocbq *
14616lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14617{
7851fe2c 14618 struct hbq_dmabuf *hbq_buf;
4f774513
JS
14619 struct lpfc_dmabuf *d_buf, *n_buf;
14620 struct lpfc_iocbq *first_iocbq, *iocbq;
14621 struct fc_frame_header *fc_hdr;
14622 uint32_t sid;
7851fe2c 14623 uint32_t len, tot_len;
eeead811 14624 struct ulp_bde64 *pbde;
4f774513
JS
14625
14626 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14627 /* remove from receive buffer list */
14628 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 14629 lpfc_update_rcv_time_stamp(vport);
4f774513 14630 /* get the Remote Port's SID */
6669f9bb 14631 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 14632 tot_len = 0;
4f774513
JS
14633 /* Get an iocbq struct to fill in. */
14634 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14635 if (first_iocbq) {
14636 /* Initialize the first IOCB. */
8fa38513 14637 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513 14638 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
939723a4
JS
14639
14640 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14641 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14642 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14643 first_iocbq->iocb.un.rcvels.parmRo =
14644 sli4_did_from_fc_hdr(fc_hdr);
14645 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14646 } else
14647 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
14648 first_iocbq->iocb.ulpContext = NO_XRI;
14649 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14650 be16_to_cpu(fc_hdr->fh_ox_id);
14651 /* iocbq is prepped for internal consumption. Physical vpi. */
14652 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14653 vport->phba->vpi_ids[vport->vpi];
4f774513
JS
14654 /* put the first buffer into the first IOCBq */
14655 first_iocbq->context2 = &seq_dmabuf->dbuf;
14656 first_iocbq->context3 = NULL;
14657 first_iocbq->iocb.ulpBdeCount = 1;
14658 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14659 LPFC_DATA_BUF_SIZE;
14660 first_iocbq->iocb.un.rcvels.remoteID = sid;
7851fe2c 14661 tot_len = bf_get(lpfc_rcqe_length,
4d9ab994 14662 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
7851fe2c 14663 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
14664 }
14665 iocbq = first_iocbq;
14666 /*
14667 * Each IOCBq can have two Buffers assigned, so go through the list
14668 * of buffers for this sequence and save two buffers in each IOCBq
14669 */
14670 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14671 if (!iocbq) {
14672 lpfc_in_buf_free(vport->phba, d_buf);
14673 continue;
14674 }
14675 if (!iocbq->context3) {
14676 iocbq->context3 = d_buf;
14677 iocbq->iocb.ulpBdeCount++;
eeead811
JS
14678 pbde = (struct ulp_bde64 *)
14679 &iocbq->iocb.unsli3.sli3Words[4];
14680 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
7851fe2c
JS
14681
14682 /* We need to get the size out of the right CQE */
14683 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14684 len = bf_get(lpfc_rcqe_length,
14685 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14686 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14687 tot_len += len;
4f774513
JS
14688 } else {
14689 iocbq = lpfc_sli_get_iocbq(vport->phba);
14690 if (!iocbq) {
14691 if (first_iocbq) {
14692 first_iocbq->iocb.ulpStatus =
14693 IOSTAT_FCP_RSP_ERROR;
14694 first_iocbq->iocb.un.ulpWord[4] =
14695 IOERR_NO_RESOURCES;
14696 }
14697 lpfc_in_buf_free(vport->phba, d_buf);
14698 continue;
14699 }
14700 iocbq->context2 = d_buf;
14701 iocbq->context3 = NULL;
14702 iocbq->iocb.ulpBdeCount = 1;
14703 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14704 LPFC_DATA_BUF_SIZE;
7851fe2c
JS
14705
14706 /* We need to get the size out of the right CQE */
14707 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14708 len = bf_get(lpfc_rcqe_length,
14709 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14710 tot_len += len;
14711 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14712
4f774513
JS
14713 iocbq->iocb.un.rcvels.remoteID = sid;
14714 list_add_tail(&iocbq->list, &first_iocbq->list);
14715 }
14716 }
14717 return first_iocbq;
14718}
14719
6669f9bb
JS
14720static void
14721lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14722 struct hbq_dmabuf *seq_dmabuf)
14723{
14724 struct fc_frame_header *fc_hdr;
14725 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14726 struct lpfc_hba *phba = vport->phba;
14727
14728 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14729 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14730 if (!iocbq) {
14731 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14732 "2707 Ring %d handler: Failed to allocate "
14733 "iocb Rctl x%x Type x%x received\n",
14734 LPFC_ELS_RING,
14735 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14736 return;
14737 }
14738 if (!lpfc_complete_unsol_iocb(phba,
14739 &phba->sli.ring[LPFC_ELS_RING],
14740 iocbq, fc_hdr->fh_r_ctl,
14741 fc_hdr->fh_type))
6d368e53 14742 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
14743 "2540 Ring %d handler: unexpected Rctl "
14744 "x%x Type x%x received\n",
14745 LPFC_ELS_RING,
14746 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14747
14748 /* Free iocb created in lpfc_prep_seq */
14749 list_for_each_entry_safe(curr_iocb, next_iocb,
14750 &iocbq->list, list) {
14751 list_del_init(&curr_iocb->list);
14752 lpfc_sli_release_iocbq(phba, curr_iocb);
14753 }
14754 lpfc_sli_release_iocbq(phba, iocbq);
14755}
14756
4f774513
JS
14757/**
14758 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14759 * @phba: Pointer to HBA context object.
14760 *
14761 * This function is called with no lock held. This function processes all
14762 * the received buffers and gives it to upper layers when a received buffer
14763 * indicates that it is the final frame in the sequence. The interrupt
14764 * service routine processes received buffers at interrupt contexts and adds
14765 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14766 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14767 * appropriate receive function when the final frame in a sequence is received.
14768 **/
4d9ab994
JS
14769void
14770lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14771 struct hbq_dmabuf *dmabuf)
4f774513 14772{
4d9ab994 14773 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
14774 struct fc_frame_header *fc_hdr;
14775 struct lpfc_vport *vport;
14776 uint32_t fcfi;
939723a4 14777 uint32_t did;
4f774513 14778
4f774513 14779 /* Process each received buffer */
4d9ab994
JS
14780 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14781 /* check to see if this a valid type of frame */
14782 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14783 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14784 return;
14785 }
7851fe2c
JS
14786 if ((bf_get(lpfc_cqe_code,
14787 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14788 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14789 &dmabuf->cq_event.cqe.rcqe_cmpl);
14790 else
14791 fcfi = bf_get(lpfc_rcqe_fcf_id,
14792 &dmabuf->cq_event.cqe.rcqe_cmpl);
939723a4 14793
4d9ab994 14794 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
939723a4 14795 if (!vport) {
4d9ab994
JS
14796 /* throw out the frame */
14797 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14798 return;
14799 }
939723a4
JS
14800
14801 /* d_id this frame is directed to */
14802 did = sli4_did_from_fc_hdr(fc_hdr);
14803
14804 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14805 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14806 (did != Fabric_DID)) {
14807 /*
14808 * Throw out the frame if we are not pt2pt.
14809 * The pt2pt protocol allows for discovery frames
14810 * to be received without a registered VPI.
14811 */
14812 if (!(vport->fc_flag & FC_PT2PT) ||
14813 (phba->link_state == LPFC_HBA_READY)) {
14814 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14815 return;
14816 }
14817 }
14818
6669f9bb
JS
14819 /* Handle the basic abort sequence (BA_ABTS) event */
14820 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14821 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14822 return;
14823 }
14824
4d9ab994
JS
14825 /* Link this frame */
14826 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14827 if (!seq_dmabuf) {
14828 /* unable to add frame to vport - throw it out */
14829 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14830 return;
14831 }
14832 /* If not last frame in sequence continue processing frames. */
def9c7a9 14833 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 14834 return;
def9c7a9 14835
6669f9bb
JS
14836 /* Send the complete sequence to the upper layer protocol */
14837 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 14838}
6fb120a7
JS
14839
14840/**
14841 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14842 * @phba: pointer to lpfc hba data structure.
14843 *
14844 * This routine is invoked to post rpi header templates to the
14845 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
14846 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14847 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
14848 *
14849 * This routine does not require any locks. It's usage is expected
14850 * to be driver load or reset recovery when the driver is
14851 * sequential.
14852 *
14853 * Return codes
af901ca1 14854 * 0 - successful
d439d286 14855 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
14856 * When this error occurs, the driver is not guaranteed
14857 * to have any rpi regions posted to the device and
14858 * must either attempt to repost the regions or take a
14859 * fatal error.
14860 **/
14861int
14862lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14863{
14864 struct lpfc_rpi_hdr *rpi_page;
14865 uint32_t rc = 0;
6d368e53
JS
14866 uint16_t lrpi = 0;
14867
14868 /* SLI4 ports that support extents do not require RPI headers. */
14869 if (!phba->sli4_hba.rpi_hdrs_in_use)
14870 goto exit;
14871 if (phba->sli4_hba.extents_in_use)
14872 return -EIO;
6fb120a7 14873
6fb120a7 14874 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
14875 /*
14876 * Assign the rpi headers a physical rpi only if the driver
14877 * has not initialized those resources. A port reset only
14878 * needs the headers posted.
14879 */
14880 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14881 LPFC_RPI_RSRC_RDY)
14882 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14883
6fb120a7
JS
14884 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14885 if (rc != MBX_SUCCESS) {
14886 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14887 "2008 Error %d posting all rpi "
14888 "headers\n", rc);
14889 rc = -EIO;
14890 break;
14891 }
14892 }
14893
6d368e53
JS
14894 exit:
14895 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14896 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
14897 return rc;
14898}
14899
14900/**
14901 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14902 * @phba: pointer to lpfc hba data structure.
14903 * @rpi_page: pointer to the rpi memory region.
14904 *
14905 * This routine is invoked to post a single rpi header to the
14906 * HBA consistent with the SLI-4 interface spec. This memory region
14907 * maps up to 64 rpi context regions.
14908 *
14909 * Return codes
af901ca1 14910 * 0 - successful
d439d286
JS
14911 * -ENOMEM - No available memory
14912 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
14913 **/
14914int
14915lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14916{
14917 LPFC_MBOXQ_t *mboxq;
14918 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14919 uint32_t rc = 0;
6fb120a7
JS
14920 uint32_t shdr_status, shdr_add_status;
14921 union lpfc_sli4_cfg_shdr *shdr;
14922
6d368e53
JS
14923 /* SLI4 ports that support extents do not require RPI headers. */
14924 if (!phba->sli4_hba.rpi_hdrs_in_use)
14925 return rc;
14926 if (phba->sli4_hba.extents_in_use)
14927 return -EIO;
14928
6fb120a7
JS
14929 /* The port is notified of the header region via a mailbox command. */
14930 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14931 if (!mboxq) {
14932 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14933 "2001 Unable to allocate memory for issuing "
14934 "SLI_CONFIG_SPECIAL mailbox command\n");
14935 return -ENOMEM;
14936 }
14937
14938 /* Post all rpi memory regions to the port. */
14939 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
14940 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14941 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14942 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
14943 sizeof(struct lpfc_sli4_cfg_mhdr),
14944 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
14945
14946
14947 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
14948 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14949 rpi_page->start_rpi);
6d368e53
JS
14950 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14951 hdr_tmpl, rpi_page->page_count);
14952
6fb120a7
JS
14953 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14954 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 14955 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
14956 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14957 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14958 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14959 if (rc != MBX_TIMEOUT)
14960 mempool_free(mboxq, phba->mbox_mem_pool);
14961 if (shdr_status || shdr_add_status || rc) {
14962 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14963 "2514 POST_RPI_HDR mailbox failed with "
14964 "status x%x add_status x%x, mbx status x%x\n",
14965 shdr_status, shdr_add_status, rc);
14966 rc = -ENXIO;
14967 }
14968 return rc;
14969}
14970
14971/**
14972 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14973 * @phba: pointer to lpfc hba data structure.
14974 *
14975 * This routine is invoked to post rpi header templates to the
14976 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
14977 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14978 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
14979 *
14980 * Returns
af901ca1 14981 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
14982 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14983 **/
14984int
14985lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14986{
6d368e53
JS
14987 unsigned long rpi;
14988 uint16_t max_rpi, rpi_limit;
14989 uint16_t rpi_remaining, lrpi = 0;
6fb120a7
JS
14990 struct lpfc_rpi_hdr *rpi_hdr;
14991
14992 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6fb120a7
JS
14993 rpi_limit = phba->sli4_hba.next_rpi;
14994
14995 /*
6d368e53
JS
14996 * Fetch the next logical rpi. Because this index is logical,
14997 * the driver starts at 0 each time.
6fb120a7
JS
14998 */
14999 spin_lock_irq(&phba->hbalock);
6d368e53
JS
15000 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15001 if (rpi >= rpi_limit)
6fb120a7
JS
15002 rpi = LPFC_RPI_ALLOC_ERROR;
15003 else {
15004 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15005 phba->sli4_hba.max_cfg_param.rpi_used++;
15006 phba->sli4_hba.rpi_count++;
15007 }
15008
15009 /*
15010 * Don't try to allocate more rpi header regions if the device limit
6d368e53 15011 * has been exhausted.
6fb120a7
JS
15012 */
15013 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15014 (phba->sli4_hba.rpi_count >= max_rpi)) {
15015 spin_unlock_irq(&phba->hbalock);
15016 return rpi;
15017 }
15018
6d368e53
JS
15019 /*
15020 * RPI header postings are not required for SLI4 ports capable of
15021 * extents.
15022 */
15023 if (!phba->sli4_hba.rpi_hdrs_in_use) {
15024 spin_unlock_irq(&phba->hbalock);
15025 return rpi;
15026 }
15027
6fb120a7
JS
15028 /*
15029 * If the driver is running low on rpi resources, allocate another
15030 * page now. Note that the next_rpi value is used because
15031 * it represents how many are actually in use whereas max_rpi notes
15032 * how many are supported max by the device.
15033 */
6d368e53 15034 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
6fb120a7
JS
15035 spin_unlock_irq(&phba->hbalock);
15036 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15037 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15038 if (!rpi_hdr) {
15039 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15040 "2002 Error Could not grow rpi "
15041 "count\n");
15042 } else {
6d368e53
JS
15043 lrpi = rpi_hdr->start_rpi;
15044 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
15045 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15046 }
15047 }
15048
15049 return rpi;
15050}
15051
d7c47992
JS
15052/**
15053 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15054 * @phba: pointer to lpfc hba data structure.
15055 *
15056 * This routine is invoked to release an rpi to the pool of
15057 * available rpis maintained by the driver.
15058 **/
15059void
15060__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15061{
15062 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15063 phba->sli4_hba.rpi_count--;
15064 phba->sli4_hba.max_cfg_param.rpi_used--;
15065 }
15066}
15067
6fb120a7
JS
15068/**
15069 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15070 * @phba: pointer to lpfc hba data structure.
15071 *
15072 * This routine is invoked to release an rpi to the pool of
15073 * available rpis maintained by the driver.
15074 **/
15075void
15076lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15077{
15078 spin_lock_irq(&phba->hbalock);
d7c47992 15079 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
15080 spin_unlock_irq(&phba->hbalock);
15081}
15082
15083/**
15084 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15085 * @phba: pointer to lpfc hba data structure.
15086 *
15087 * This routine is invoked to remove the memory region that
15088 * provided rpi via a bitmask.
15089 **/
15090void
15091lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15092{
15093 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
15094 kfree(phba->sli4_hba.rpi_ids);
15095 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
15096}
15097
15098/**
15099 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
15100 * @phba: pointer to lpfc hba data structure.
15101 *
15102 * This routine is invoked to remove the memory region that
15103 * provided rpi via a bitmask.
15104 **/
15105int
6b5151fd
JS
15106lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
15107 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
6fb120a7
JS
15108{
15109 LPFC_MBOXQ_t *mboxq;
15110 struct lpfc_hba *phba = ndlp->phba;
15111 int rc;
15112
15113 /* The port is notified of the header region via a mailbox command. */
15114 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15115 if (!mboxq)
15116 return -ENOMEM;
15117
15118 /* Post all rpi memory regions to the port. */
15119 lpfc_resume_rpi(mboxq, ndlp);
6b5151fd
JS
15120 if (cmpl) {
15121 mboxq->mbox_cmpl = cmpl;
15122 mboxq->context1 = arg;
15123 mboxq->context2 = ndlp;
72859909
JS
15124 } else
15125 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6b5151fd 15126 mboxq->vport = ndlp->vport;
6fb120a7
JS
15127 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15128 if (rc == MBX_NOT_FINISHED) {
15129 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15130 "2010 Resume RPI Mailbox failed "
15131 "status %d, mbxStatus x%x\n", rc,
15132 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15133 mempool_free(mboxq, phba->mbox_mem_pool);
15134 return -EIO;
15135 }
15136 return 0;
15137}
15138
15139/**
15140 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 15141 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 15142 *
76a95d75 15143 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
15144 *
15145 * Returns:
15146 * 0 success
15147 * -Evalue otherwise
15148 **/
15149int
76a95d75 15150lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
15151{
15152 LPFC_MBOXQ_t *mboxq;
15153 int rc = 0;
6a9c52cf 15154 int retval = MBX_SUCCESS;
6fb120a7 15155 uint32_t mbox_tmo;
76a95d75 15156 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
15157 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15158 if (!mboxq)
15159 return -ENOMEM;
76a95d75 15160 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 15161 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 15162 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 15163 if (rc != MBX_SUCCESS) {
76a95d75 15164 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
15165 "2022 INIT VPI Mailbox failed "
15166 "status %d, mbxStatus x%x\n", rc,
15167 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 15168 retval = -EIO;
6fb120a7 15169 }
6a9c52cf 15170 if (rc != MBX_TIMEOUT)
76a95d75 15171 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
15172
15173 return retval;
6fb120a7
JS
15174}
15175
15176/**
15177 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
15178 * @phba: pointer to lpfc hba data structure.
15179 * @mboxq: Pointer to mailbox object.
15180 *
15181 * This routine is invoked to manually add a single FCF record. The caller
15182 * must pass a completely initialized FCF_Record. This routine takes
15183 * care of the nonembedded mailbox operations.
15184 **/
15185static void
15186lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
15187{
15188 void *virt_addr;
15189 union lpfc_sli4_cfg_shdr *shdr;
15190 uint32_t shdr_status, shdr_add_status;
15191
15192 virt_addr = mboxq->sge_array->addr[0];
15193 /* The IOCTL status is embedded in the mailbox subheader. */
15194 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
15195 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15196 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15197
15198 if ((shdr_status || shdr_add_status) &&
15199 (shdr_status != STATUS_FCF_IN_USE))
15200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15201 "2558 ADD_FCF_RECORD mailbox failed with "
15202 "status x%x add_status x%x\n",
15203 shdr_status, shdr_add_status);
15204
15205 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15206}
15207
15208/**
15209 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
15210 * @phba: pointer to lpfc hba data structure.
15211 * @fcf_record: pointer to the initialized fcf record to add.
15212 *
15213 * This routine is invoked to manually add a single FCF record. The caller
15214 * must pass a completely initialized FCF_Record. This routine takes
15215 * care of the nonembedded mailbox operations.
15216 **/
15217int
15218lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
15219{
15220 int rc = 0;
15221 LPFC_MBOXQ_t *mboxq;
15222 uint8_t *bytep;
15223 void *virt_addr;
15224 dma_addr_t phys_addr;
15225 struct lpfc_mbx_sge sge;
15226 uint32_t alloc_len, req_len;
15227 uint32_t fcfindex;
15228
15229 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15230 if (!mboxq) {
15231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15232 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
15233 return -ENOMEM;
15234 }
15235
15236 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
15237 sizeof(uint32_t);
15238
15239 /* Allocate DMA memory and set up the non-embedded mailbox command */
15240 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15241 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
15242 req_len, LPFC_SLI4_MBX_NEMBED);
15243 if (alloc_len < req_len) {
15244 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15245 "2523 Allocated DMA memory size (x%x) is "
15246 "less than the requested DMA memory "
15247 "size (x%x)\n", alloc_len, req_len);
15248 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15249 return -ENOMEM;
15250 }
15251
15252 /*
15253 * Get the first SGE entry from the non-embedded DMA memory. This
15254 * routine only uses a single SGE.
15255 */
15256 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
15257 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
6fb120a7
JS
15258 virt_addr = mboxq->sge_array->addr[0];
15259 /*
15260 * Configure the FCF record for FCFI 0. This is the driver's
15261 * hardcoded default and gets used in nonFIP mode.
15262 */
15263 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
15264 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
15265 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15266
15267 /*
15268 * Copy the fcf_index and the FCF Record Data. The data starts after
15269 * the FCoE header plus word10. The data copy needs to be endian
15270 * correct.
15271 */
15272 bytep += sizeof(uint32_t);
15273 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15274 mboxq->vport = phba->pport;
15275 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15276 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15277 if (rc == MBX_NOT_FINISHED) {
15278 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15279 "2515 ADD_FCF_RECORD mailbox failed with "
15280 "status 0x%x\n", rc);
15281 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15282 rc = -EIO;
15283 } else
15284 rc = 0;
15285
15286 return rc;
15287}
15288
15289/**
15290 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15291 * @phba: pointer to lpfc hba data structure.
15292 * @fcf_record: pointer to the fcf record to write the default data.
15293 * @fcf_index: FCF table entry index.
15294 *
15295 * This routine is invoked to build the driver's default FCF record. The
15296 * values used are hardcoded. This routine handles memory initialization.
15297 *
15298 **/
15299void
15300lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15301 struct fcf_record *fcf_record,
15302 uint16_t fcf_index)
15303{
15304 memset(fcf_record, 0, sizeof(struct fcf_record));
15305 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15306 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15307 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15308 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15309 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15310 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15311 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15312 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15313 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15314 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15315 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15316 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15317 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 15318 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
15319 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15320 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15321 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15322 /* Set the VLAN bit map */
15323 if (phba->valid_vlan) {
15324 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15325 = 1 << (phba->vlan_id % 8);
15326 }
15327}
15328
15329/**
0c9ab6f5 15330 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
15331 * @phba: pointer to lpfc hba data structure.
15332 * @fcf_index: FCF table entry offset.
15333 *
0c9ab6f5
JS
15334 * This routine is invoked to scan the entire FCF table by reading FCF
15335 * record and processing it one at a time starting from the @fcf_index
15336 * for initial FCF discovery or fast FCF failover rediscovery.
15337 *
25985edc 15338 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 15339 * otherwise.
6fb120a7
JS
15340 **/
15341int
0c9ab6f5 15342lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
15343{
15344 int rc = 0, error;
15345 LPFC_MBOXQ_t *mboxq;
6fb120a7 15346
32b9793f 15347 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
80c17849 15348 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
6fb120a7
JS
15349 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15350 if (!mboxq) {
15351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15352 "2000 Failed to allocate mbox for "
15353 "READ_FCF cmd\n");
4d9ab994 15354 error = -ENOMEM;
0c9ab6f5 15355 goto fail_fcf_scan;
6fb120a7 15356 }
ecfd03c6 15357 /* Construct the read FCF record mailbox command */
0c9ab6f5 15358 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
15359 if (rc) {
15360 error = -EINVAL;
0c9ab6f5 15361 goto fail_fcf_scan;
6fb120a7 15362 }
ecfd03c6 15363 /* Issue the mailbox command asynchronously */
6fb120a7 15364 mboxq->vport = phba->pport;
0c9ab6f5 15365 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
15366
15367 spin_lock_irq(&phba->hbalock);
15368 phba->hba_flag |= FCF_TS_INPROG;
15369 spin_unlock_irq(&phba->hbalock);
15370
6fb120a7 15371 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 15372 if (rc == MBX_NOT_FINISHED)
6fb120a7 15373 error = -EIO;
ecfd03c6 15374 else {
38b92ef8
JS
15375 /* Reset eligible FCF count for new scan */
15376 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 15377 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 15378 error = 0;
32b9793f 15379 }
0c9ab6f5 15380fail_fcf_scan:
4d9ab994
JS
15381 if (error) {
15382 if (mboxq)
15383 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 15384 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 15385 spin_lock_irq(&phba->hbalock);
a93ff37a 15386 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
15387 spin_unlock_irq(&phba->hbalock);
15388 }
6fb120a7
JS
15389 return error;
15390}
a0c87cbd 15391
0c9ab6f5 15392/**
a93ff37a 15393 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
15394 * @phba: pointer to lpfc hba data structure.
15395 * @fcf_index: FCF table entry offset.
15396 *
15397 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 15398 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 15399 *
25985edc 15400 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
15401 * otherwise.
15402 **/
15403int
15404lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15405{
15406 int rc = 0, error;
15407 LPFC_MBOXQ_t *mboxq;
15408
15409 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15410 if (!mboxq) {
15411 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15412 "2763 Failed to allocate mbox for "
15413 "READ_FCF cmd\n");
15414 error = -ENOMEM;
15415 goto fail_fcf_read;
15416 }
15417 /* Construct the read FCF record mailbox command */
15418 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15419 if (rc) {
15420 error = -EINVAL;
15421 goto fail_fcf_read;
15422 }
15423 /* Issue the mailbox command asynchronously */
15424 mboxq->vport = phba->pport;
15425 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15426 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15427 if (rc == MBX_NOT_FINISHED)
15428 error = -EIO;
15429 else
15430 error = 0;
15431
15432fail_fcf_read:
15433 if (error && mboxq)
15434 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15435 return error;
15436}
15437
15438/**
15439 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15440 * @phba: pointer to lpfc hba data structure.
15441 * @fcf_index: FCF table entry offset.
15442 *
15443 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 15444 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 15445 *
25985edc 15446 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
15447 * otherwise.
15448 **/
15449int
15450lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15451{
15452 int rc = 0, error;
15453 LPFC_MBOXQ_t *mboxq;
15454
15455 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15456 if (!mboxq) {
15457 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15458 "2758 Failed to allocate mbox for "
15459 "READ_FCF cmd\n");
15460 error = -ENOMEM;
15461 goto fail_fcf_read;
15462 }
15463 /* Construct the read FCF record mailbox command */
15464 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15465 if (rc) {
15466 error = -EINVAL;
15467 goto fail_fcf_read;
15468 }
15469 /* Issue the mailbox command asynchronously */
15470 mboxq->vport = phba->pport;
15471 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15472 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15473 if (rc == MBX_NOT_FINISHED)
15474 error = -EIO;
15475 else
15476 error = 0;
15477
15478fail_fcf_read:
15479 if (error && mboxq)
15480 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15481 return error;
15482}
15483
7d791df7
JS
15484/**
15485 * lpfc_check_next_fcf_pri
15486 * phba pointer to the lpfc_hba struct for this port.
15487 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15488 * routine when the rr_bmask is empty. The FCF indecies are put into the
15489 * rr_bmask based on their priority level. Starting from the highest priority
15490 * to the lowest. The most likely FCF candidate will be in the highest
15491 * priority group. When this routine is called it searches the fcf_pri list for
15492 * next lowest priority group and repopulates the rr_bmask with only those
15493 * fcf_indexes.
15494 * returns:
15495 * 1=success 0=failure
15496 **/
15497int
15498lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15499{
15500 uint16_t next_fcf_pri;
15501 uint16_t last_index;
15502 struct lpfc_fcf_pri *fcf_pri;
15503 int rc;
15504 int ret = 0;
15505
15506 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15507 LPFC_SLI4_FCF_TBL_INDX_MAX);
15508 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15509 "3060 Last IDX %d\n", last_index);
15510 if (list_empty(&phba->fcf.fcf_pri_list)) {
15511 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15512 "3061 Last IDX %d\n", last_index);
15513 return 0; /* Empty rr list */
15514 }
15515 next_fcf_pri = 0;
15516 /*
15517 * Clear the rr_bmask and set all of the bits that are at this
15518 * priority.
15519 */
15520 memset(phba->fcf.fcf_rr_bmask, 0,
15521 sizeof(*phba->fcf.fcf_rr_bmask));
15522 spin_lock_irq(&phba->hbalock);
15523 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15524 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15525 continue;
15526 /*
15527 * the 1st priority that has not FLOGI failed
15528 * will be the highest.
15529 */
15530 if (!next_fcf_pri)
15531 next_fcf_pri = fcf_pri->fcf_rec.priority;
15532 spin_unlock_irq(&phba->hbalock);
15533 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15534 rc = lpfc_sli4_fcf_rr_index_set(phba,
15535 fcf_pri->fcf_rec.fcf_index);
15536 if (rc)
15537 return 0;
15538 }
15539 spin_lock_irq(&phba->hbalock);
15540 }
15541 /*
15542 * if next_fcf_pri was not set above and the list is not empty then
15543 * we have failed flogis on all of them. So reset flogi failed
4907cb7b 15544 * and start at the beginning.
7d791df7
JS
15545 */
15546 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15547 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15548 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15549 /*
15550 * the 1st priority that has not FLOGI failed
15551 * will be the highest.
15552 */
15553 if (!next_fcf_pri)
15554 next_fcf_pri = fcf_pri->fcf_rec.priority;
15555 spin_unlock_irq(&phba->hbalock);
15556 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15557 rc = lpfc_sli4_fcf_rr_index_set(phba,
15558 fcf_pri->fcf_rec.fcf_index);
15559 if (rc)
15560 return 0;
15561 }
15562 spin_lock_irq(&phba->hbalock);
15563 }
15564 } else
15565 ret = 1;
15566 spin_unlock_irq(&phba->hbalock);
15567
15568 return ret;
15569}
0c9ab6f5
JS
15570/**
15571 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15572 * @phba: pointer to lpfc hba data structure.
15573 *
15574 * This routine is to get the next eligible FCF record index in a round
15575 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 15576 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
15577 * shall be returned, otherwise, the next eligible FCF record's index
15578 * shall be returned.
15579 **/
15580uint16_t
15581lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15582{
15583 uint16_t next_fcf_index;
15584
421c6622 15585initial_priority:
3804dc84 15586 /* Search start from next bit of currently registered FCF index */
421c6622
JS
15587 next_fcf_index = phba->fcf.current_rec.fcf_indx;
15588
7d791df7 15589next_priority:
421c6622
JS
15590 /* Determine the next fcf index to check */
15591 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
15592 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15593 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
15594 next_fcf_index);
15595
0c9ab6f5 15596 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
15597 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15598 /*
15599 * If we have wrapped then we need to clear the bits that
15600 * have been tested so that we can detect when we should
15601 * change the priority level.
15602 */
0c9ab6f5
JS
15603 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15604 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
15605 }
15606
3804dc84
JS
15607
15608 /* Check roundrobin failover list empty condition */
7d791df7
JS
15609 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15610 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15611 /*
15612 * If next fcf index is not found check if there are lower
15613 * Priority level fcf's in the fcf_priority list.
15614 * Set up the rr_bmask with all of the avaiable fcf bits
15615 * at that level and continue the selection process.
15616 */
15617 if (lpfc_check_next_fcf_pri_level(phba))
421c6622 15618 goto initial_priority;
3804dc84
JS
15619 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15620 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
15621 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15622 return LPFC_FCOE_FCF_NEXT_NONE;
15623 else {
15624 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15625 "3063 Only FCF available idx %d, flag %x\n",
15626 next_fcf_index,
15627 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15628 return next_fcf_index;
15629 }
3804dc84
JS
15630 }
15631
7d791df7
JS
15632 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15633 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15634 LPFC_FCF_FLOGI_FAILED)
15635 goto next_priority;
15636
3804dc84 15637 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
15638 "2845 Get next roundrobin failover FCF (x%x)\n",
15639 next_fcf_index);
15640
0c9ab6f5
JS
15641 return next_fcf_index;
15642}
15643
15644/**
15645 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15646 * @phba: pointer to lpfc hba data structure.
15647 *
15648 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 15649 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
15650 * does not go beyond the range of the driver allocated bmask dimension
15651 * before setting the bit.
15652 *
15653 * Returns 0 if the index bit successfully set, otherwise, it returns
15654 * -EINVAL.
15655 **/
15656int
15657lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15658{
15659 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15660 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
15661 "2610 FCF (x%x) reached driver's book "
15662 "keeping dimension:x%x\n",
0c9ab6f5
JS
15663 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15664 return -EINVAL;
15665 }
15666 /* Set the eligible FCF record index bmask */
15667 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15668
3804dc84 15669 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 15670 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
15671 "bmask\n", fcf_index);
15672
0c9ab6f5
JS
15673 return 0;
15674}
15675
15676/**
3804dc84 15677 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
15678 * @phba: pointer to lpfc hba data structure.
15679 *
15680 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 15681 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
15682 * does not go beyond the range of the driver allocated bmask dimension
15683 * before clearing the bit.
15684 **/
15685void
15686lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15687{
7d791df7 15688 struct lpfc_fcf_pri *fcf_pri;
0c9ab6f5
JS
15689 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15690 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
15691 "2762 FCF (x%x) reached driver's book "
15692 "keeping dimension:x%x\n",
0c9ab6f5
JS
15693 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15694 return;
15695 }
15696 /* Clear the eligible FCF record index bmask */
7d791df7
JS
15697 spin_lock_irq(&phba->hbalock);
15698 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15699 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15700 list_del_init(&fcf_pri->list);
15701 break;
15702 }
15703 }
15704 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 15705 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
15706
15707 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 15708 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 15709 "bmask\n", fcf_index);
0c9ab6f5
JS
15710}
15711
ecfd03c6
JS
15712/**
15713 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15714 * @phba: pointer to lpfc hba data structure.
15715 *
15716 * This routine is the completion routine for the rediscover FCF table mailbox
15717 * command. If the mailbox command returned failure, it will try to stop the
15718 * FCF rediscover wait timer.
15719 **/
15720void
15721lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15722{
15723 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15724 uint32_t shdr_status, shdr_add_status;
15725
15726 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15727
15728 shdr_status = bf_get(lpfc_mbox_hdr_status,
15729 &redisc_fcf->header.cfg_shdr.response);
15730 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15731 &redisc_fcf->header.cfg_shdr.response);
15732 if (shdr_status || shdr_add_status) {
0c9ab6f5 15733 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
15734 "2746 Requesting for FCF rediscovery failed "
15735 "status x%x add_status x%x\n",
15736 shdr_status, shdr_add_status);
0c9ab6f5 15737 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 15738 spin_lock_irq(&phba->hbalock);
0c9ab6f5 15739 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
15740 spin_unlock_irq(&phba->hbalock);
15741 /*
15742 * CVL event triggered FCF rediscover request failed,
15743 * last resort to re-try current registered FCF entry.
15744 */
15745 lpfc_retry_pport_discovery(phba);
15746 } else {
15747 spin_lock_irq(&phba->hbalock);
0c9ab6f5 15748 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
15749 spin_unlock_irq(&phba->hbalock);
15750 /*
15751 * DEAD FCF event triggered FCF rediscover request
15752 * failed, last resort to fail over as a link down
15753 * to FCF registration.
15754 */
15755 lpfc_sli4_fcf_dead_failthrough(phba);
15756 }
0c9ab6f5
JS
15757 } else {
15758 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 15759 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
15760 /*
15761 * Start FCF rediscovery wait timer for pending FCF
15762 * before rescan FCF record table.
15763 */
15764 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 15765 }
ecfd03c6
JS
15766
15767 mempool_free(mbox, phba->mbox_mem_pool);
15768}
15769
15770/**
3804dc84 15771 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
15772 * @phba: pointer to lpfc hba data structure.
15773 *
15774 * This routine is invoked to request for rediscovery of the entire FCF table
15775 * by the port.
15776 **/
15777int
15778lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15779{
15780 LPFC_MBOXQ_t *mbox;
15781 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15782 int rc, length;
15783
0c9ab6f5
JS
15784 /* Cancel retry delay timers to all vports before FCF rediscover */
15785 lpfc_cancel_all_vport_retry_delay_timer(phba);
15786
ecfd03c6
JS
15787 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15788 if (!mbox) {
15789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15790 "2745 Failed to allocate mbox for "
15791 "requesting FCF rediscover.\n");
15792 return -ENOMEM;
15793 }
15794
15795 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15796 sizeof(struct lpfc_sli4_cfg_mhdr));
15797 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15798 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15799 length, LPFC_SLI4_MBX_EMBED);
15800
15801 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15802 /* Set count to 0 for invalidating the entire FCF database */
15803 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15804
15805 /* Issue the mailbox command asynchronously */
15806 mbox->vport = phba->pport;
15807 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15808 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15809
15810 if (rc == MBX_NOT_FINISHED) {
15811 mempool_free(mbox, phba->mbox_mem_pool);
15812 return -EIO;
15813 }
15814 return 0;
15815}
15816
fc2b989b
JS
15817/**
15818 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15819 * @phba: pointer to lpfc hba data structure.
15820 *
15821 * This function is the failover routine as a last resort to the FCF DEAD
15822 * event when driver failed to perform fast FCF failover.
15823 **/
15824void
15825lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15826{
15827 uint32_t link_state;
15828
15829 /*
15830 * Last resort as FCF DEAD event failover will treat this as
15831 * a link down, but save the link state because we don't want
15832 * it to be changed to Link Down unless it is already down.
15833 */
15834 link_state = phba->link_state;
15835 lpfc_linkdown(phba);
15836 phba->link_state = link_state;
15837
15838 /* Unregister FCF if no devices connected to it */
15839 lpfc_unregister_unused_fcf(phba);
15840}
15841
a0c87cbd 15842/**
026abb87 15843 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 15844 * @phba: pointer to lpfc hba data structure.
026abb87 15845 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 15846 *
026abb87
JS
15847 * This function gets SLI3 port configure region 23 data through memory dump
15848 * mailbox command. When it successfully retrieves data, the size of the data
15849 * will be returned, otherwise, 0 will be returned.
a0c87cbd 15850 **/
026abb87
JS
15851static uint32_t
15852lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
15853{
15854 LPFC_MBOXQ_t *pmb = NULL;
15855 MAILBOX_t *mb;
026abb87 15856 uint32_t offset = 0;
a0c87cbd
JS
15857 int rc;
15858
026abb87
JS
15859 if (!rgn23_data)
15860 return 0;
15861
a0c87cbd
JS
15862 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15863 if (!pmb) {
15864 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
15865 "2600 failed to allocate mailbox memory\n");
15866 return 0;
a0c87cbd
JS
15867 }
15868 mb = &pmb->u.mb;
15869
a0c87cbd
JS
15870 do {
15871 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15872 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15873
15874 if (rc != MBX_SUCCESS) {
15875 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
15876 "2601 failed to read config "
15877 "region 23, rc 0x%x Status 0x%x\n",
15878 rc, mb->mbxStatus);
a0c87cbd
JS
15879 mb->un.varDmp.word_cnt = 0;
15880 }
15881 /*
15882 * dump mem may return a zero when finished or we got a
15883 * mailbox error, either way we are done.
15884 */
15885 if (mb->un.varDmp.word_cnt == 0)
15886 break;
15887 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15888 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15889
15890 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
15891 rgn23_data + offset,
15892 mb->un.varDmp.word_cnt);
a0c87cbd
JS
15893 offset += mb->un.varDmp.word_cnt;
15894 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15895
026abb87
JS
15896 mempool_free(pmb, phba->mbox_mem_pool);
15897 return offset;
15898}
15899
15900/**
15901 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15902 * @phba: pointer to lpfc hba data structure.
15903 * @rgn23_data: pointer to configure region 23 data.
15904 *
15905 * This function gets SLI4 port configure region 23 data through memory dump
15906 * mailbox command. When it successfully retrieves data, the size of the data
15907 * will be returned, otherwise, 0 will be returned.
15908 **/
15909static uint32_t
15910lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15911{
15912 LPFC_MBOXQ_t *mboxq = NULL;
15913 struct lpfc_dmabuf *mp = NULL;
15914 struct lpfc_mqe *mqe;
15915 uint32_t data_length = 0;
15916 int rc;
15917
15918 if (!rgn23_data)
15919 return 0;
15920
15921 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15922 if (!mboxq) {
15923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15924 "3105 failed to allocate mailbox memory\n");
15925 return 0;
15926 }
15927
15928 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15929 goto out;
15930 mqe = &mboxq->u.mqe;
15931 mp = (struct lpfc_dmabuf *) mboxq->context1;
15932 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15933 if (rc)
15934 goto out;
15935 data_length = mqe->un.mb_words[5];
15936 if (data_length == 0)
15937 goto out;
15938 if (data_length > DMP_RGN23_SIZE) {
15939 data_length = 0;
15940 goto out;
15941 }
15942 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15943out:
15944 mempool_free(mboxq, phba->mbox_mem_pool);
15945 if (mp) {
15946 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15947 kfree(mp);
15948 }
15949 return data_length;
15950}
15951
15952/**
15953 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15954 * @phba: pointer to lpfc hba data structure.
15955 *
15956 * This function read region 23 and parse TLV for port status to
15957 * decide if the user disaled the port. If the TLV indicates the
15958 * port is disabled, the hba_flag is set accordingly.
15959 **/
15960void
15961lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15962{
15963 uint8_t *rgn23_data = NULL;
15964 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15965 uint32_t offset = 0;
15966
15967 /* Get adapter Region 23 data */
15968 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15969 if (!rgn23_data)
15970 goto out;
15971
15972 if (phba->sli_rev < LPFC_SLI_REV4)
15973 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15974 else {
15975 if_type = bf_get(lpfc_sli_intf_if_type,
15976 &phba->sli4_hba.sli_intf);
15977 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15978 goto out;
15979 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15980 }
a0c87cbd
JS
15981
15982 if (!data_size)
15983 goto out;
15984
15985 /* Check the region signature first */
15986 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15987 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15988 "2619 Config region 23 has bad signature\n");
15989 goto out;
15990 }
15991 offset += 4;
15992
15993 /* Check the data structure version */
15994 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15995 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15996 "2620 Config region 23 has bad version\n");
15997 goto out;
15998 }
15999 offset += 4;
16000
16001 /* Parse TLV entries in the region */
16002 while (offset < data_size) {
16003 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16004 break;
16005 /*
16006 * If the TLV is not driver specific TLV or driver id is
16007 * not linux driver id, skip the record.
16008 */
16009 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16010 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16011 (rgn23_data[offset + 3] != 0)) {
16012 offset += rgn23_data[offset + 1] * 4 + 4;
16013 continue;
16014 }
16015
16016 /* Driver found a driver specific TLV in the config region */
16017 sub_tlv_len = rgn23_data[offset + 1] * 4;
16018 offset += 4;
16019 tlv_offset = 0;
16020
16021 /*
16022 * Search for configured port state sub-TLV.
16023 */
16024 while ((offset < data_size) &&
16025 (tlv_offset < sub_tlv_len)) {
16026 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16027 offset += 4;
16028 tlv_offset += 4;
16029 break;
16030 }
16031 if (rgn23_data[offset] != PORT_STE_TYPE) {
16032 offset += rgn23_data[offset + 1] * 4 + 4;
16033 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16034 continue;
16035 }
16036
16037 /* This HBA contains PORT_STE configured */
16038 if (!rgn23_data[offset + 2])
16039 phba->hba_flag |= LINK_DISABLED;
16040
16041 goto out;
16042 }
16043 }
026abb87 16044
a0c87cbd 16045out:
a0c87cbd
JS
16046 kfree(rgn23_data);
16047 return;
16048}
695a814e 16049
52d52440
JS
16050/**
16051 * lpfc_wr_object - write an object to the firmware
16052 * @phba: HBA structure that indicates port to create a queue on.
16053 * @dmabuf_list: list of dmabufs to write to the port.
16054 * @size: the total byte value of the objects to write to the port.
16055 * @offset: the current offset to be used to start the transfer.
16056 *
16057 * This routine will create a wr_object mailbox command to send to the port.
16058 * the mailbox command will be constructed using the dma buffers described in
16059 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16060 * BDEs that the imbedded mailbox can support. The @offset variable will be
16061 * used to indicate the starting offset of the transfer and will also return
16062 * the offset after the write object mailbox has completed. @size is used to
16063 * determine the end of the object and whether the eof bit should be set.
16064 *
16065 * Return 0 is successful and offset will contain the the new offset to use
16066 * for the next write.
16067 * Return negative value for error cases.
16068 **/
16069int
16070lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16071 uint32_t size, uint32_t *offset)
16072{
16073 struct lpfc_mbx_wr_object *wr_object;
16074 LPFC_MBOXQ_t *mbox;
16075 int rc = 0, i = 0;
16076 uint32_t shdr_status, shdr_add_status;
16077 uint32_t mbox_tmo;
16078 union lpfc_sli4_cfg_shdr *shdr;
16079 struct lpfc_dmabuf *dmabuf;
16080 uint32_t written = 0;
16081
16082 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16083 if (!mbox)
16084 return -ENOMEM;
16085
16086 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16087 LPFC_MBOX_OPCODE_WRITE_OBJECT,
16088 sizeof(struct lpfc_mbx_wr_object) -
16089 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16090
16091 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
16092 wr_object->u.request.write_offset = *offset;
16093 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
16094 wr_object->u.request.object_name[0] =
16095 cpu_to_le32(wr_object->u.request.object_name[0]);
16096 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
16097 list_for_each_entry(dmabuf, dmabuf_list, list) {
16098 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
16099 break;
16100 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
16101 wr_object->u.request.bde[i].addrHigh =
16102 putPaddrHigh(dmabuf->phys);
16103 if (written + SLI4_PAGE_SIZE >= size) {
16104 wr_object->u.request.bde[i].tus.f.bdeSize =
16105 (size - written);
16106 written += (size - written);
16107 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
16108 } else {
16109 wr_object->u.request.bde[i].tus.f.bdeSize =
16110 SLI4_PAGE_SIZE;
16111 written += SLI4_PAGE_SIZE;
16112 }
16113 i++;
16114 }
16115 wr_object->u.request.bde_count = i;
16116 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
16117 if (!phba->sli4_hba.intr_enable)
16118 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16119 else {
a183a15f 16120 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
16121 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16122 }
16123 /* The IOCTL status is embedded in the mailbox subheader. */
16124 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
16125 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16126 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16127 if (rc != MBX_TIMEOUT)
16128 mempool_free(mbox, phba->mbox_mem_pool);
16129 if (shdr_status || shdr_add_status || rc) {
16130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16131 "3025 Write Object mailbox failed with "
16132 "status x%x add_status x%x, mbx status x%x\n",
16133 shdr_status, shdr_add_status, rc);
16134 rc = -ENXIO;
16135 } else
16136 *offset += wr_object->u.response.actual_write_length;
16137 return rc;
16138}
16139
695a814e
JS
16140/**
16141 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
16142 * @vport: pointer to vport data structure.
16143 *
16144 * This function iterate through the mailboxq and clean up all REG_LOGIN
16145 * and REG_VPI mailbox commands associated with the vport. This function
16146 * is called when driver want to restart discovery of the vport due to
16147 * a Clear Virtual Link event.
16148 **/
16149void
16150lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
16151{
16152 struct lpfc_hba *phba = vport->phba;
16153 LPFC_MBOXQ_t *mb, *nextmb;
16154 struct lpfc_dmabuf *mp;
78730cfe 16155 struct lpfc_nodelist *ndlp;
d439d286 16156 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 16157 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 16158 LIST_HEAD(mbox_cmd_list);
63e801ce 16159 uint8_t restart_loop;
695a814e 16160
d439d286 16161 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
16162 spin_lock_irq(&phba->hbalock);
16163 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
16164 if (mb->vport != vport)
16165 continue;
16166
16167 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16168 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16169 continue;
16170
d439d286
JS
16171 list_del(&mb->list);
16172 list_add_tail(&mb->list, &mbox_cmd_list);
16173 }
16174 /* Clean up active mailbox command with the vport */
16175 mb = phba->sli.mbox_active;
16176 if (mb && (mb->vport == vport)) {
16177 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
16178 (mb->u.mb.mbxCommand == MBX_REG_VPI))
16179 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16180 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16181 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
16182 /* Put reference count for delayed processing */
16183 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
16184 /* Unregister the RPI when mailbox complete */
16185 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16186 }
16187 }
63e801ce
JS
16188 /* Cleanup any mailbox completions which are not yet processed */
16189 do {
16190 restart_loop = 0;
16191 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
16192 /*
16193 * If this mailox is already processed or it is
16194 * for another vport ignore it.
16195 */
16196 if ((mb->vport != vport) ||
16197 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
16198 continue;
16199
16200 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16201 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16202 continue;
16203
16204 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16205 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16206 ndlp = (struct lpfc_nodelist *)mb->context2;
16207 /* Unregister the RPI when mailbox complete */
16208 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16209 restart_loop = 1;
16210 spin_unlock_irq(&phba->hbalock);
16211 spin_lock(shost->host_lock);
16212 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16213 spin_unlock(shost->host_lock);
16214 spin_lock_irq(&phba->hbalock);
16215 break;
16216 }
16217 }
16218 } while (restart_loop);
16219
d439d286
JS
16220 spin_unlock_irq(&phba->hbalock);
16221
16222 /* Release the cleaned-up mailbox commands */
16223 while (!list_empty(&mbox_cmd_list)) {
16224 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
16225 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16226 mp = (struct lpfc_dmabuf *) (mb->context1);
16227 if (mp) {
16228 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
16229 kfree(mp);
16230 }
78730cfe 16231 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 16232 mb->context2 = NULL;
78730cfe 16233 if (ndlp) {
ec21b3b0 16234 spin_lock(shost->host_lock);
589a52d6 16235 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 16236 spin_unlock(shost->host_lock);
78730cfe 16237 lpfc_nlp_put(ndlp);
78730cfe 16238 }
695a814e 16239 }
695a814e
JS
16240 mempool_free(mb, phba->mbox_mem_pool);
16241 }
d439d286
JS
16242
16243 /* Release the ndlp with the cleaned-up active mailbox command */
16244 if (act_mbx_ndlp) {
16245 spin_lock(shost->host_lock);
16246 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16247 spin_unlock(shost->host_lock);
16248 lpfc_nlp_put(act_mbx_ndlp);
695a814e 16249 }
695a814e
JS
16250}
16251
2a9bf3d0
JS
16252/**
16253 * lpfc_drain_txq - Drain the txq
16254 * @phba: Pointer to HBA context object.
16255 *
16256 * This function attempt to submit IOCBs on the txq
16257 * to the adapter. For SLI4 adapters, the txq contains
16258 * ELS IOCBs that have been deferred because the there
16259 * are no SGLs. This congestion can occur with large
16260 * vport counts during node discovery.
16261 **/
16262
16263uint32_t
16264lpfc_drain_txq(struct lpfc_hba *phba)
16265{
16266 LIST_HEAD(completions);
16267 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
16268 struct lpfc_iocbq *piocbq = 0;
16269 unsigned long iflags = 0;
16270 char *fail_msg = NULL;
16271 struct lpfc_sglq *sglq;
16272 union lpfc_wqe wqe;
0e9bb8d7 16273 int txq_cnt = 0;
2a9bf3d0
JS
16274
16275 spin_lock_irqsave(&phba->hbalock, iflags);
0e9bb8d7
JS
16276 list_for_each_entry(piocbq, &pring->txq, list) {
16277 txq_cnt++;
16278 }
16279
16280 if (txq_cnt > pring->txq_max)
16281 pring->txq_max = txq_cnt;
2a9bf3d0
JS
16282
16283 spin_unlock_irqrestore(&phba->hbalock, iflags);
16284
0e9bb8d7 16285 while (!list_empty(&pring->txq)) {
2a9bf3d0
JS
16286 spin_lock_irqsave(&phba->hbalock, iflags);
16287
19ca7609 16288 piocbq = lpfc_sli_ringtx_get(phba, pring);
a629852a
JS
16289 if (!piocbq) {
16290 spin_unlock_irqrestore(&phba->hbalock, iflags);
16291 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16292 "2823 txq empty and txq_cnt is %d\n ",
0e9bb8d7 16293 txq_cnt);
a629852a
JS
16294 break;
16295 }
19ca7609 16296 sglq = __lpfc_sli_get_sglq(phba, piocbq);
2a9bf3d0 16297 if (!sglq) {
19ca7609 16298 __lpfc_sli_ringtx_put(phba, pring, piocbq);
2a9bf3d0
JS
16299 spin_unlock_irqrestore(&phba->hbalock, iflags);
16300 break;
2a9bf3d0 16301 }
0e9bb8d7 16302 txq_cnt--;
2a9bf3d0
JS
16303
16304 /* The xri and iocb resources secured,
16305 * attempt to issue request
16306 */
6d368e53 16307 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
16308 piocbq->sli4_xritag = sglq->sli4_xritag;
16309 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16310 fail_msg = "to convert bpl to sgl";
16311 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16312 fail_msg = "to convert iocb to wqe";
16313 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16314 fail_msg = " - Wq is full";
16315 else
16316 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16317
16318 if (fail_msg) {
16319 /* Failed means we can't issue and need to cancel */
16320 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16321 "2822 IOCB failed %s iotag 0x%x "
16322 "xri 0x%x\n",
16323 fail_msg,
16324 piocbq->iotag, piocbq->sli4_xritag);
16325 list_add_tail(&piocbq->list, &completions);
16326 }
16327 spin_unlock_irqrestore(&phba->hbalock, iflags);
16328 }
16329
2a9bf3d0
JS
16330 /* Cancel all the IOCBs that cannot be issued */
16331 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16332 IOERR_SLI_ABORTED);
16333
0e9bb8d7 16334 return txq_cnt;
2a9bf3d0 16335}