scsi: lpfc: Update copyrights for 12.8.0.7 and 12.8.0.8 changes
[linux-2.6-microblaze.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2021 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23
24 #include <linux/blkdev.h>
25 #include <linux/pci.h>
26 #include <linux/interrupt.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/lockdep.h>
30
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_transport_fc.h>
36 #include <scsi/fc/fc_fs.h>
37 #include <linux/aer.h>
38 #include <linux/crash_dump.h>
39 #ifdef CONFIG_X86
40 #include <asm/set_memory.h>
41 #endif
42
43 #include "lpfc_hw4.h"
44 #include "lpfc_hw.h"
45 #include "lpfc_sli.h"
46 #include "lpfc_sli4.h"
47 #include "lpfc_nl.h"
48 #include "lpfc_disc.h"
49 #include "lpfc.h"
50 #include "lpfc_scsi.h"
51 #include "lpfc_nvme.h"
52 #include "lpfc_crtn.h"
53 #include "lpfc_logmsg.h"
54 #include "lpfc_compat.h"
55 #include "lpfc_debugfs.h"
56 #include "lpfc_vport.h"
57 #include "lpfc_version.h"
58
59 /* There are only four IOCB completion types. */
60 typedef enum _lpfc_iocb_type {
61         LPFC_UNKNOWN_IOCB,
62         LPFC_UNSOL_IOCB,
63         LPFC_SOL_IOCB,
64         LPFC_ABORT_IOCB
65 } lpfc_iocb_type;
66
67
68 /* Provide function prototypes local to this module. */
69 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
70                                   uint32_t);
71 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
72                               uint8_t *, uint32_t *);
73 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
74                                                          struct lpfc_iocbq *);
75 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
76                                       struct hbq_dmabuf *);
77 static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
78                                           struct hbq_dmabuf *dmabuf);
79 static bool lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba,
80                                    struct lpfc_queue *cq, struct lpfc_cqe *cqe);
81 static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
82                                        int);
83 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
84                                      struct lpfc_queue *eq,
85                                      struct lpfc_eqe *eqe);
86 static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
87 static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
88 static struct lpfc_cqe *lpfc_sli4_cq_get(struct lpfc_queue *q);
89 static void __lpfc_sli4_consume_cqe(struct lpfc_hba *phba,
90                                     struct lpfc_queue *cq,
91                                     struct lpfc_cqe *cqe);
92
93 union lpfc_wqe128 lpfc_iread_cmd_template;
94 union lpfc_wqe128 lpfc_iwrite_cmd_template;
95 union lpfc_wqe128 lpfc_icmnd_cmd_template;
96
97 static IOCB_t *
98 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
99 {
100         return &iocbq->iocb;
101 }
102
103 /* Setup WQE templates for IOs */
104 void lpfc_wqe_cmd_template(void)
105 {
106         union lpfc_wqe128 *wqe;
107
108         /* IREAD template */
109         wqe = &lpfc_iread_cmd_template;
110         memset(wqe, 0, sizeof(union lpfc_wqe128));
111
112         /* Word 0, 1, 2 - BDE is variable */
113
114         /* Word 3 - cmd_buff_len, payload_offset_len is zero */
115
116         /* Word 4 - total_xfer_len is variable */
117
118         /* Word 5 - is zero */
119
120         /* Word 6 - ctxt_tag, xri_tag is variable */
121
122         /* Word 7 */
123         bf_set(wqe_cmnd, &wqe->fcp_iread.wqe_com, CMD_FCP_IREAD64_WQE);
124         bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, PARM_READ_CHECK);
125         bf_set(wqe_class, &wqe->fcp_iread.wqe_com, CLASS3);
126         bf_set(wqe_ct, &wqe->fcp_iread.wqe_com, SLI4_CT_RPI);
127
128         /* Word 8 - abort_tag is variable */
129
130         /* Word 9  - reqtag is variable */
131
132         /* Word 10 - dbde, wqes is variable */
133         bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
134         bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
135         bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com, LPFC_WQE_LENLOC_WORD4);
136         bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
137         bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
138
139         /* Word 11 - pbde is variable */
140         bf_set(wqe_cmd_type, &wqe->fcp_iread.wqe_com, COMMAND_DATA_IN);
141         bf_set(wqe_cqid, &wqe->fcp_iread.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
142         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
143
144         /* Word 12 - is zero */
145
146         /* Word 13, 14, 15 - PBDE is variable */
147
148         /* IWRITE template */
149         wqe = &lpfc_iwrite_cmd_template;
150         memset(wqe, 0, sizeof(union lpfc_wqe128));
151
152         /* Word 0, 1, 2 - BDE is variable */
153
154         /* Word 3 - cmd_buff_len, payload_offset_len is zero */
155
156         /* Word 4 - total_xfer_len is variable */
157
158         /* Word 5 - initial_xfer_len is variable */
159
160         /* Word 6 - ctxt_tag, xri_tag is variable */
161
162         /* Word 7 */
163         bf_set(wqe_cmnd, &wqe->fcp_iwrite.wqe_com, CMD_FCP_IWRITE64_WQE);
164         bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, PARM_READ_CHECK);
165         bf_set(wqe_class, &wqe->fcp_iwrite.wqe_com, CLASS3);
166         bf_set(wqe_ct, &wqe->fcp_iwrite.wqe_com, SLI4_CT_RPI);
167
168         /* Word 8 - abort_tag is variable */
169
170         /* Word 9  - reqtag is variable */
171
172         /* Word 10 - dbde, wqes is variable */
173         bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
174         bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
175         bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_LENLOC_WORD4);
176         bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
177         bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
178
179         /* Word 11 - pbde is variable */
180         bf_set(wqe_cmd_type, &wqe->fcp_iwrite.wqe_com, COMMAND_DATA_OUT);
181         bf_set(wqe_cqid, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
182         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
183
184         /* Word 12 - is zero */
185
186         /* Word 13, 14, 15 - PBDE is variable */
187
188         /* ICMND template */
189         wqe = &lpfc_icmnd_cmd_template;
190         memset(wqe, 0, sizeof(union lpfc_wqe128));
191
192         /* Word 0, 1, 2 - BDE is variable */
193
194         /* Word 3 - payload_offset_len is variable */
195
196         /* Word 4, 5 - is zero */
197
198         /* Word 6 - ctxt_tag, xri_tag is variable */
199
200         /* Word 7 */
201         bf_set(wqe_cmnd, &wqe->fcp_icmd.wqe_com, CMD_FCP_ICMND64_WQE);
202         bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
203         bf_set(wqe_class, &wqe->fcp_icmd.wqe_com, CLASS3);
204         bf_set(wqe_ct, &wqe->fcp_icmd.wqe_com, SLI4_CT_RPI);
205
206         /* Word 8 - abort_tag is variable */
207
208         /* Word 9  - reqtag is variable */
209
210         /* Word 10 - dbde, wqes is variable */
211         bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
212         bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_NONE);
213         bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com, LPFC_WQE_LENLOC_NONE);
214         bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
215         bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
216
217         /* Word 11 */
218         bf_set(wqe_cmd_type, &wqe->fcp_icmd.wqe_com, COMMAND_DATA_IN);
219         bf_set(wqe_cqid, &wqe->fcp_icmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
220         bf_set(wqe_pbde, &wqe->fcp_icmd.wqe_com, 0);
221
222         /* Word 12, 13, 14, 15 - is zero */
223 }
224
225 #if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
226 /**
227  * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
228  * @srcp: Source memory pointer.
229  * @destp: Destination memory pointer.
230  * @cnt: Number of words required to be copied.
231  *       Must be a multiple of sizeof(uint64_t)
232  *
233  * This function is used for copying data between driver memory
234  * and the SLI WQ. This function also changes the endianness
235  * of each word if native endianness is different from SLI
236  * endianness. This function can be called with or without
237  * lock.
238  **/
239 static void
240 lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
241 {
242         uint64_t *src = srcp;
243         uint64_t *dest = destp;
244         int i;
245
246         for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
247                 *dest++ = *src++;
248 }
249 #else
250 #define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
251 #endif
252
253 /**
254  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
255  * @q: The Work Queue to operate on.
256  * @wqe: The work Queue Entry to put on the Work queue.
257  *
258  * This routine will copy the contents of @wqe to the next available entry on
259  * the @q. This function will then ring the Work Queue Doorbell to signal the
260  * HBA to start processing the Work Queue Entry. This function returns 0 if
261  * successful. If no entries are available on @q then this function will return
262  * -ENOMEM.
263  * The caller is expected to hold the hbalock when calling this routine.
264  **/
265 static int
266 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
267 {
268         union lpfc_wqe *temp_wqe;
269         struct lpfc_register doorbell;
270         uint32_t host_index;
271         uint32_t idx;
272         uint32_t i = 0;
273         uint8_t *tmp;
274         u32 if_type;
275
276         /* sanity check on queue memory */
277         if (unlikely(!q))
278                 return -ENOMEM;
279
280         temp_wqe = lpfc_sli4_qe(q, q->host_index);
281
282         /* If the host has not yet processed the next entry then we are done */
283         idx = ((q->host_index + 1) % q->entry_count);
284         if (idx == q->hba_index) {
285                 q->WQ_overflow++;
286                 return -EBUSY;
287         }
288         q->WQ_posted++;
289         /* set consumption flag every once in a while */
290         if (!((q->host_index + 1) % q->notify_interval))
291                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
292         else
293                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
294         if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
295                 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
296         lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
297         if (q->dpp_enable && q->phba->cfg_enable_dpp) {
298                 /* write to DPP aperture taking advatage of Combined Writes */
299                 tmp = (uint8_t *)temp_wqe;
300 #ifdef __raw_writeq
301                 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
302                         __raw_writeq(*((uint64_t *)(tmp + i)),
303                                         q->dpp_regaddr + i);
304 #else
305                 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
306                         __raw_writel(*((uint32_t *)(tmp + i)),
307                                         q->dpp_regaddr + i);
308 #endif
309         }
310         /* ensure WQE bcopy and DPP flushed before doorbell write */
311         wmb();
312
313         /* Update the host index before invoking device */
314         host_index = q->host_index;
315
316         q->host_index = idx;
317
318         /* Ring Doorbell */
319         doorbell.word0 = 0;
320         if (q->db_format == LPFC_DB_LIST_FORMAT) {
321                 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
322                         bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
323                         bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
324                         bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
325                             q->dpp_id);
326                         bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
327                             q->queue_id);
328                 } else {
329                         bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
330                         bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
331
332                         /* Leave bits <23:16> clear for if_type 6 dpp */
333                         if_type = bf_get(lpfc_sli_intf_if_type,
334                                          &q->phba->sli4_hba.sli_intf);
335                         if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
336                                 bf_set(lpfc_wq_db_list_fm_index, &doorbell,
337                                        host_index);
338                 }
339         } else if (q->db_format == LPFC_DB_RING_FORMAT) {
340                 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
341                 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
342         } else {
343                 return -EINVAL;
344         }
345         writel(doorbell.word0, q->db_regaddr);
346
347         return 0;
348 }
349
350 /**
351  * lpfc_sli4_wq_release - Updates internal hba index for WQ
352  * @q: The Work Queue to operate on.
353  * @index: The index to advance the hba index to.
354  *
355  * This routine will update the HBA index of a queue to reflect consumption of
356  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
357  * an entry the host calls this function to update the queue's internal
358  * pointers.
359  **/
360 static void
361 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
362 {
363         /* sanity check on queue memory */
364         if (unlikely(!q))
365                 return;
366
367         q->hba_index = index;
368 }
369
370 /**
371  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
372  * @q: The Mailbox Queue to operate on.
373  * @mqe: The Mailbox Queue Entry to put on the Work queue.
374  *
375  * This routine will copy the contents of @mqe to the next available entry on
376  * the @q. This function will then ring the Work Queue Doorbell to signal the
377  * HBA to start processing the Work Queue Entry. This function returns 0 if
378  * successful. If no entries are available on @q then this function will return
379  * -ENOMEM.
380  * The caller is expected to hold the hbalock when calling this routine.
381  **/
382 static uint32_t
383 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
384 {
385         struct lpfc_mqe *temp_mqe;
386         struct lpfc_register doorbell;
387
388         /* sanity check on queue memory */
389         if (unlikely(!q))
390                 return -ENOMEM;
391         temp_mqe = lpfc_sli4_qe(q, q->host_index);
392
393         /* If the host has not yet processed the next entry then we are done */
394         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
395                 return -ENOMEM;
396         lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
397         /* Save off the mailbox pointer for completion */
398         q->phba->mbox = (MAILBOX_t *)temp_mqe;
399
400         /* Update the host index before invoking device */
401         q->host_index = ((q->host_index + 1) % q->entry_count);
402
403         /* Ring Doorbell */
404         doorbell.word0 = 0;
405         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
406         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
407         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
408         return 0;
409 }
410
411 /**
412  * lpfc_sli4_mq_release - Updates internal hba index for MQ
413  * @q: The Mailbox Queue to operate on.
414  *
415  * This routine will update the HBA index of a queue to reflect consumption of
416  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
417  * an entry the host calls this function to update the queue's internal
418  * pointers. This routine returns the number of entries that were consumed by
419  * the HBA.
420  **/
421 static uint32_t
422 lpfc_sli4_mq_release(struct lpfc_queue *q)
423 {
424         /* sanity check on queue memory */
425         if (unlikely(!q))
426                 return 0;
427
428         /* Clear the mailbox pointer for completion */
429         q->phba->mbox = NULL;
430         q->hba_index = ((q->hba_index + 1) % q->entry_count);
431         return 1;
432 }
433
434 /**
435  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
436  * @q: The Event Queue to get the first valid EQE from
437  *
438  * This routine will get the first valid Event Queue Entry from @q, update
439  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
440  * the Queue (no more work to do), or the Queue is full of EQEs that have been
441  * processed, but not popped back to the HBA then this routine will return NULL.
442  **/
443 static struct lpfc_eqe *
444 lpfc_sli4_eq_get(struct lpfc_queue *q)
445 {
446         struct lpfc_eqe *eqe;
447
448         /* sanity check on queue memory */
449         if (unlikely(!q))
450                 return NULL;
451         eqe = lpfc_sli4_qe(q, q->host_index);
452
453         /* If the next EQE is not valid then we are done */
454         if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
455                 return NULL;
456
457         /*
458          * insert barrier for instruction interlock : data from the hardware
459          * must have the valid bit checked before it can be copied and acted
460          * upon. Speculative instructions were allowing a bcopy at the start
461          * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
462          * after our return, to copy data before the valid bit check above
463          * was done. As such, some of the copied data was stale. The barrier
464          * ensures the check is before any data is copied.
465          */
466         mb();
467         return eqe;
468 }
469
470 /**
471  * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
472  * @q: The Event Queue to disable interrupts
473  *
474  **/
475 void
476 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
477 {
478         struct lpfc_register doorbell;
479
480         doorbell.word0 = 0;
481         bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
482         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
483         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
484                 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
485         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
486         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
487 }
488
489 /**
490  * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
491  * @q: The Event Queue to disable interrupts
492  *
493  **/
494 void
495 lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
496 {
497         struct lpfc_register doorbell;
498
499         doorbell.word0 = 0;
500         bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
501         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
502 }
503
504 /**
505  * lpfc_sli4_write_eq_db - write EQ DB for eqe's consumed or arm state
506  * @phba: adapter with EQ
507  * @q: The Event Queue that the host has completed processing for.
508  * @count: Number of elements that have been consumed
509  * @arm: Indicates whether the host wants to arms this CQ.
510  *
511  * This routine will notify the HBA, by ringing the doorbell, that count
512  * number of EQEs have been processed. The @arm parameter indicates whether
513  * the queue should be rearmed when ringing the doorbell.
514  **/
515 void
516 lpfc_sli4_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
517                      uint32_t count, bool arm)
518 {
519         struct lpfc_register doorbell;
520
521         /* sanity check on queue memory */
522         if (unlikely(!q || (count == 0 && !arm)))
523                 return;
524
525         /* ring doorbell for number popped */
526         doorbell.word0 = 0;
527         if (arm) {
528                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
529                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
530         }
531         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
532         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
533         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
534                         (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
535         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
536         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
537         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
538         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
539                 readl(q->phba->sli4_hba.EQDBregaddr);
540 }
541
542 /**
543  * lpfc_sli4_if6_write_eq_db - write EQ DB for eqe's consumed or arm state
544  * @phba: adapter with EQ
545  * @q: The Event Queue that the host has completed processing for.
546  * @count: Number of elements that have been consumed
547  * @arm: Indicates whether the host wants to arms this CQ.
548  *
549  * This routine will notify the HBA, by ringing the doorbell, that count
550  * number of EQEs have been processed. The @arm parameter indicates whether
551  * the queue should be rearmed when ringing the doorbell.
552  **/
553 void
554 lpfc_sli4_if6_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
555                           uint32_t count, bool arm)
556 {
557         struct lpfc_register doorbell;
558
559         /* sanity check on queue memory */
560         if (unlikely(!q || (count == 0 && !arm)))
561                 return;
562
563         /* ring doorbell for number popped */
564         doorbell.word0 = 0;
565         if (arm)
566                 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
567         bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, count);
568         bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
569         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
570         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
571         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
572                 readl(q->phba->sli4_hba.EQDBregaddr);
573 }
574
575 static void
576 __lpfc_sli4_consume_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
577                         struct lpfc_eqe *eqe)
578 {
579         if (!phba->sli4_hba.pc_sli4_params.eqav)
580                 bf_set_le32(lpfc_eqe_valid, eqe, 0);
581
582         eq->host_index = ((eq->host_index + 1) % eq->entry_count);
583
584         /* if the index wrapped around, toggle the valid bit */
585         if (phba->sli4_hba.pc_sli4_params.eqav && !eq->host_index)
586                 eq->qe_valid = (eq->qe_valid) ? 0 : 1;
587 }
588
589 static void
590 lpfc_sli4_eqcq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
591 {
592         struct lpfc_eqe *eqe = NULL;
593         u32 eq_count = 0, cq_count = 0;
594         struct lpfc_cqe *cqe = NULL;
595         struct lpfc_queue *cq = NULL, *childq = NULL;
596         int cqid = 0;
597
598         /* walk all the EQ entries and drop on the floor */
599         eqe = lpfc_sli4_eq_get(eq);
600         while (eqe) {
601                 /* Get the reference to the corresponding CQ */
602                 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
603                 cq = NULL;
604
605                 list_for_each_entry(childq, &eq->child_list, list) {
606                         if (childq->queue_id == cqid) {
607                                 cq = childq;
608                                 break;
609                         }
610                 }
611                 /* If CQ is valid, iterate through it and drop all the CQEs */
612                 if (cq) {
613                         cqe = lpfc_sli4_cq_get(cq);
614                         while (cqe) {
615                                 __lpfc_sli4_consume_cqe(phba, cq, cqe);
616                                 cq_count++;
617                                 cqe = lpfc_sli4_cq_get(cq);
618                         }
619                         /* Clear and re-arm the CQ */
620                         phba->sli4_hba.sli4_write_cq_db(phba, cq, cq_count,
621                             LPFC_QUEUE_REARM);
622                         cq_count = 0;
623                 }
624                 __lpfc_sli4_consume_eqe(phba, eq, eqe);
625                 eq_count++;
626                 eqe = lpfc_sli4_eq_get(eq);
627         }
628
629         /* Clear and re-arm the EQ */
630         phba->sli4_hba.sli4_write_eq_db(phba, eq, eq_count, LPFC_QUEUE_REARM);
631 }
632
633 static int
634 lpfc_sli4_process_eq(struct lpfc_hba *phba, struct lpfc_queue *eq,
635                      uint8_t rearm)
636 {
637         struct lpfc_eqe *eqe;
638         int count = 0, consumed = 0;
639
640         if (cmpxchg(&eq->queue_claimed, 0, 1) != 0)
641                 goto rearm_and_exit;
642
643         eqe = lpfc_sli4_eq_get(eq);
644         while (eqe) {
645                 lpfc_sli4_hba_handle_eqe(phba, eq, eqe);
646                 __lpfc_sli4_consume_eqe(phba, eq, eqe);
647
648                 consumed++;
649                 if (!(++count % eq->max_proc_limit))
650                         break;
651
652                 if (!(count % eq->notify_interval)) {
653                         phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed,
654                                                         LPFC_QUEUE_NOARM);
655                         consumed = 0;
656                 }
657
658                 eqe = lpfc_sli4_eq_get(eq);
659         }
660         eq->EQ_processed += count;
661
662         /* Track the max number of EQEs processed in 1 intr */
663         if (count > eq->EQ_max_eqe)
664                 eq->EQ_max_eqe = count;
665
666         xchg(&eq->queue_claimed, 0);
667
668 rearm_and_exit:
669         /* Always clear the EQ. */
670         phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed, rearm);
671
672         return count;
673 }
674
675 /**
676  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
677  * @q: The Completion Queue to get the first valid CQE from
678  *
679  * This routine will get the first valid Completion Queue Entry from @q, update
680  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
681  * the Queue (no more work to do), or the Queue is full of CQEs that have been
682  * processed, but not popped back to the HBA then this routine will return NULL.
683  **/
684 static struct lpfc_cqe *
685 lpfc_sli4_cq_get(struct lpfc_queue *q)
686 {
687         struct lpfc_cqe *cqe;
688
689         /* sanity check on queue memory */
690         if (unlikely(!q))
691                 return NULL;
692         cqe = lpfc_sli4_qe(q, q->host_index);
693
694         /* If the next CQE is not valid then we are done */
695         if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
696                 return NULL;
697
698         /*
699          * insert barrier for instruction interlock : data from the hardware
700          * must have the valid bit checked before it can be copied and acted
701          * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
702          * instructions allowing action on content before valid bit checked,
703          * add barrier here as well. May not be needed as "content" is a
704          * single 32-bit entity here (vs multi word structure for cq's).
705          */
706         mb();
707         return cqe;
708 }
709
710 static void
711 __lpfc_sli4_consume_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
712                         struct lpfc_cqe *cqe)
713 {
714         if (!phba->sli4_hba.pc_sli4_params.cqav)
715                 bf_set_le32(lpfc_cqe_valid, cqe, 0);
716
717         cq->host_index = ((cq->host_index + 1) % cq->entry_count);
718
719         /* if the index wrapped around, toggle the valid bit */
720         if (phba->sli4_hba.pc_sli4_params.cqav && !cq->host_index)
721                 cq->qe_valid = (cq->qe_valid) ? 0 : 1;
722 }
723
724 /**
725  * lpfc_sli4_write_cq_db - write cq DB for entries consumed or arm state.
726  * @phba: the adapter with the CQ
727  * @q: The Completion Queue that the host has completed processing for.
728  * @count: the number of elements that were consumed
729  * @arm: Indicates whether the host wants to arms this CQ.
730  *
731  * This routine will notify the HBA, by ringing the doorbell, that the
732  * CQEs have been processed. The @arm parameter specifies whether the
733  * queue should be rearmed when ringing the doorbell.
734  **/
735 void
736 lpfc_sli4_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
737                      uint32_t count, bool arm)
738 {
739         struct lpfc_register doorbell;
740
741         /* sanity check on queue memory */
742         if (unlikely(!q || (count == 0 && !arm)))
743                 return;
744
745         /* ring doorbell for number popped */
746         doorbell.word0 = 0;
747         if (arm)
748                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
749         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
750         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
751         bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
752                         (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
753         bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
754         writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
755 }
756
757 /**
758  * lpfc_sli4_if6_write_cq_db - write cq DB for entries consumed or arm state.
759  * @phba: the adapter with the CQ
760  * @q: The Completion Queue that the host has completed processing for.
761  * @count: the number of elements that were consumed
762  * @arm: Indicates whether the host wants to arms this CQ.
763  *
764  * This routine will notify the HBA, by ringing the doorbell, that the
765  * CQEs have been processed. The @arm parameter specifies whether the
766  * queue should be rearmed when ringing the doorbell.
767  **/
768 void
769 lpfc_sli4_if6_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
770                          uint32_t count, bool arm)
771 {
772         struct lpfc_register doorbell;
773
774         /* sanity check on queue memory */
775         if (unlikely(!q || (count == 0 && !arm)))
776                 return;
777
778         /* ring doorbell for number popped */
779         doorbell.word0 = 0;
780         if (arm)
781                 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
782         bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, count);
783         bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
784         writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
785 }
786
787 /*
788  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
789  *
790  * This routine will copy the contents of @wqe to the next available entry on
791  * the @q. This function will then ring the Receive Queue Doorbell to signal the
792  * HBA to start processing the Receive Queue Entry. This function returns the
793  * index that the rqe was copied to if successful. If no entries are available
794  * on @q then this function will return -ENOMEM.
795  * The caller is expected to hold the hbalock when calling this routine.
796  **/
797 int
798 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
799                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
800 {
801         struct lpfc_rqe *temp_hrqe;
802         struct lpfc_rqe *temp_drqe;
803         struct lpfc_register doorbell;
804         int hq_put_index;
805         int dq_put_index;
806
807         /* sanity check on queue memory */
808         if (unlikely(!hq) || unlikely(!dq))
809                 return -ENOMEM;
810         hq_put_index = hq->host_index;
811         dq_put_index = dq->host_index;
812         temp_hrqe = lpfc_sli4_qe(hq, hq_put_index);
813         temp_drqe = lpfc_sli4_qe(dq, dq_put_index);
814
815         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
816                 return -EINVAL;
817         if (hq_put_index != dq_put_index)
818                 return -EINVAL;
819         /* If the host has not yet processed the next entry then we are done */
820         if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
821                 return -EBUSY;
822         lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
823         lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
824
825         /* Update the host index to point to the next slot */
826         hq->host_index = ((hq_put_index + 1) % hq->entry_count);
827         dq->host_index = ((dq_put_index + 1) % dq->entry_count);
828         hq->RQ_buf_posted++;
829
830         /* Ring The Header Receive Queue Doorbell */
831         if (!(hq->host_index % hq->notify_interval)) {
832                 doorbell.word0 = 0;
833                 if (hq->db_format == LPFC_DB_RING_FORMAT) {
834                         bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
835                                hq->notify_interval);
836                         bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
837                 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
838                         bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
839                                hq->notify_interval);
840                         bf_set(lpfc_rq_db_list_fm_index, &doorbell,
841                                hq->host_index);
842                         bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
843                 } else {
844                         return -EINVAL;
845                 }
846                 writel(doorbell.word0, hq->db_regaddr);
847         }
848         return hq_put_index;
849 }
850
851 /*
852  * lpfc_sli4_rq_release - Updates internal hba index for RQ
853  *
854  * This routine will update the HBA index of a queue to reflect consumption of
855  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
856  * consumed an entry the host calls this function to update the queue's
857  * internal pointers. This routine returns the number of entries that were
858  * consumed by the HBA.
859  **/
860 static uint32_t
861 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
862 {
863         /* sanity check on queue memory */
864         if (unlikely(!hq) || unlikely(!dq))
865                 return 0;
866
867         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
868                 return 0;
869         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
870         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
871         return 1;
872 }
873
874 /**
875  * lpfc_cmd_iocb - Get next command iocb entry in the ring
876  * @phba: Pointer to HBA context object.
877  * @pring: Pointer to driver SLI ring object.
878  *
879  * This function returns pointer to next command iocb entry
880  * in the command ring. The caller must hold hbalock to prevent
881  * other threads consume the next command iocb.
882  * SLI-2/SLI-3 provide different sized iocbs.
883  **/
884 static inline IOCB_t *
885 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
886 {
887         return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
888                            pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
889 }
890
891 /**
892  * lpfc_resp_iocb - Get next response iocb entry in the ring
893  * @phba: Pointer to HBA context object.
894  * @pring: Pointer to driver SLI ring object.
895  *
896  * This function returns pointer to next response iocb entry
897  * in the response ring. The caller must hold hbalock to make sure
898  * that no other thread consume the next response iocb.
899  * SLI-2/SLI-3 provide different sized iocbs.
900  **/
901 static inline IOCB_t *
902 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
903 {
904         return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
905                            pring->sli.sli3.rspidx * phba->iocb_rsp_size);
906 }
907
908 /**
909  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
910  * @phba: Pointer to HBA context object.
911  *
912  * This function is called with hbalock held. This function
913  * allocates a new driver iocb object from the iocb pool. If the
914  * allocation is successful, it returns pointer to the newly
915  * allocated iocb object else it returns NULL.
916  **/
917 struct lpfc_iocbq *
918 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
919 {
920         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
921         struct lpfc_iocbq * iocbq = NULL;
922
923         lockdep_assert_held(&phba->hbalock);
924
925         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
926         if (iocbq)
927                 phba->iocb_cnt++;
928         if (phba->iocb_cnt > phba->iocb_max)
929                 phba->iocb_max = phba->iocb_cnt;
930         return iocbq;
931 }
932
933 /**
934  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
935  * @phba: Pointer to HBA context object.
936  * @xritag: XRI value.
937  *
938  * This function clears the sglq pointer from the array of acive
939  * sglq's. The xritag that is passed in is used to index into the
940  * array. Before the xritag can be used it needs to be adjusted
941  * by subtracting the xribase.
942  *
943  * Returns sglq ponter = success, NULL = Failure.
944  **/
945 struct lpfc_sglq *
946 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
947 {
948         struct lpfc_sglq *sglq;
949
950         sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
951         phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
952         return sglq;
953 }
954
955 /**
956  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
957  * @phba: Pointer to HBA context object.
958  * @xritag: XRI value.
959  *
960  * This function returns the sglq pointer from the array of acive
961  * sglq's. The xritag that is passed in is used to index into the
962  * array. Before the xritag can be used it needs to be adjusted
963  * by subtracting the xribase.
964  *
965  * Returns sglq ponter = success, NULL = Failure.
966  **/
967 struct lpfc_sglq *
968 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
969 {
970         struct lpfc_sglq *sglq;
971
972         sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];
973         return sglq;
974 }
975
976 /**
977  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
978  * @phba: Pointer to HBA context object.
979  * @xritag: xri used in this exchange.
980  * @rrq: The RRQ to be cleared.
981  *
982  **/
983 void
984 lpfc_clr_rrq_active(struct lpfc_hba *phba,
985                     uint16_t xritag,
986                     struct lpfc_node_rrq *rrq)
987 {
988         struct lpfc_nodelist *ndlp = NULL;
989
990         /* Lookup did to verify if did is still active on this vport */
991         if (rrq->vport)
992                 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
993
994         if (!ndlp)
995                 goto out;
996
997         if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
998                 rrq->send_rrq = 0;
999                 rrq->xritag = 0;
1000                 rrq->rrq_stop_time = 0;
1001         }
1002 out:
1003         mempool_free(rrq, phba->rrq_pool);
1004 }
1005
1006 /**
1007  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
1008  * @phba: Pointer to HBA context object.
1009  *
1010  * This function is called with hbalock held. This function
1011  * Checks if stop_time (ratov from setting rrq active) has
1012  * been reached, if it has and the send_rrq flag is set then
1013  * it will call lpfc_send_rrq. If the send_rrq flag is not set
1014  * then it will just call the routine to clear the rrq and
1015  * free the rrq resource.
1016  * The timer is set to the next rrq that is going to expire before
1017  * leaving the routine.
1018  *
1019  **/
1020 void
1021 lpfc_handle_rrq_active(struct lpfc_hba *phba)
1022 {
1023         struct lpfc_node_rrq *rrq;
1024         struct lpfc_node_rrq *nextrrq;
1025         unsigned long next_time;
1026         unsigned long iflags;
1027         LIST_HEAD(send_rrq);
1028
1029         spin_lock_irqsave(&phba->hbalock, iflags);
1030         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1031         next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
1032         list_for_each_entry_safe(rrq, nextrrq,
1033                                  &phba->active_rrq_list, list) {
1034                 if (time_after(jiffies, rrq->rrq_stop_time))
1035                         list_move(&rrq->list, &send_rrq);
1036                 else if (time_before(rrq->rrq_stop_time, next_time))
1037                         next_time = rrq->rrq_stop_time;
1038         }
1039         spin_unlock_irqrestore(&phba->hbalock, iflags);
1040         if ((!list_empty(&phba->active_rrq_list)) &&
1041             (!(phba->pport->load_flag & FC_UNLOADING)))
1042                 mod_timer(&phba->rrq_tmr, next_time);
1043         list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
1044                 list_del(&rrq->list);
1045                 if (!rrq->send_rrq) {
1046                         /* this call will free the rrq */
1047                         lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1048                 } else if (lpfc_send_rrq(phba, rrq)) {
1049                         /* if we send the rrq then the completion handler
1050                         *  will clear the bit in the xribitmap.
1051                         */
1052                         lpfc_clr_rrq_active(phba, rrq->xritag,
1053                                             rrq);
1054                 }
1055         }
1056 }
1057
1058 /**
1059  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
1060  * @vport: Pointer to vport context object.
1061  * @xri: The xri used in the exchange.
1062  * @did: The targets DID for this exchange.
1063  *
1064  * returns NULL = rrq not found in the phba->active_rrq_list.
1065  *         rrq = rrq for this xri and target.
1066  **/
1067 struct lpfc_node_rrq *
1068 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
1069 {
1070         struct lpfc_hba *phba = vport->phba;
1071         struct lpfc_node_rrq *rrq;
1072         struct lpfc_node_rrq *nextrrq;
1073         unsigned long iflags;
1074
1075         if (phba->sli_rev != LPFC_SLI_REV4)
1076                 return NULL;
1077         spin_lock_irqsave(&phba->hbalock, iflags);
1078         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
1079                 if (rrq->vport == vport && rrq->xritag == xri &&
1080                                 rrq->nlp_DID == did){
1081                         list_del(&rrq->list);
1082                         spin_unlock_irqrestore(&phba->hbalock, iflags);
1083                         return rrq;
1084                 }
1085         }
1086         spin_unlock_irqrestore(&phba->hbalock, iflags);
1087         return NULL;
1088 }
1089
1090 /**
1091  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
1092  * @vport: Pointer to vport context object.
1093  * @ndlp: Pointer to the lpfc_node_list structure.
1094  * If ndlp is NULL Remove all active RRQs for this vport from the
1095  * phba->active_rrq_list and clear the rrq.
1096  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
1097  **/
1098 void
1099 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1100
1101 {
1102         struct lpfc_hba *phba = vport->phba;
1103         struct lpfc_node_rrq *rrq;
1104         struct lpfc_node_rrq *nextrrq;
1105         unsigned long iflags;
1106         LIST_HEAD(rrq_list);
1107
1108         if (phba->sli_rev != LPFC_SLI_REV4)
1109                 return;
1110         if (!ndlp) {
1111                 lpfc_sli4_vport_delete_els_xri_aborted(vport);
1112                 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
1113         }
1114         spin_lock_irqsave(&phba->hbalock, iflags);
1115         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
1116                 if (rrq->vport != vport)
1117                         continue;
1118
1119                 if (!ndlp || ndlp == lpfc_findnode_did(vport, rrq->nlp_DID))
1120                         list_move(&rrq->list, &rrq_list);
1121
1122         }
1123         spin_unlock_irqrestore(&phba->hbalock, iflags);
1124
1125         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1126                 list_del(&rrq->list);
1127                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1128         }
1129 }
1130
1131 /**
1132  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
1133  * @phba: Pointer to HBA context object.
1134  * @ndlp: Targets nodelist pointer for this exchange.
1135  * @xritag: the xri in the bitmap to test.
1136  *
1137  * This function returns:
1138  * 0 = rrq not active for this xri
1139  * 1 = rrq is valid for this xri.
1140  **/
1141 int
1142 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1143                         uint16_t  xritag)
1144 {
1145         if (!ndlp)
1146                 return 0;
1147         if (!ndlp->active_rrqs_xri_bitmap)
1148                 return 0;
1149         if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1150                 return 1;
1151         else
1152                 return 0;
1153 }
1154
1155 /**
1156  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1157  * @phba: Pointer to HBA context object.
1158  * @ndlp: nodelist pointer for this target.
1159  * @xritag: xri used in this exchange.
1160  * @rxid: Remote Exchange ID.
1161  * @send_rrq: Flag used to determine if we should send rrq els cmd.
1162  *
1163  * This function takes the hbalock.
1164  * The active bit is always set in the active rrq xri_bitmap even
1165  * if there is no slot avaiable for the other rrq information.
1166  *
1167  * returns 0 rrq actived for this xri
1168  *         < 0 No memory or invalid ndlp.
1169  **/
1170 int
1171 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1172                     uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
1173 {
1174         unsigned long iflags;
1175         struct lpfc_node_rrq *rrq;
1176         int empty;
1177
1178         if (!ndlp)
1179                 return -EINVAL;
1180
1181         if (!phba->cfg_enable_rrq)
1182                 return -EINVAL;
1183
1184         spin_lock_irqsave(&phba->hbalock, iflags);
1185         if (phba->pport->load_flag & FC_UNLOADING) {
1186                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1187                 goto out;
1188         }
1189
1190         if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1191                 goto out;
1192
1193         if (!ndlp->active_rrqs_xri_bitmap)
1194                 goto out;
1195
1196         if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1197                 goto out;
1198
1199         spin_unlock_irqrestore(&phba->hbalock, iflags);
1200         rrq = mempool_alloc(phba->rrq_pool, GFP_ATOMIC);
1201         if (!rrq) {
1202                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1203                                 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1204                                 " DID:0x%x Send:%d\n",
1205                                 xritag, rxid, ndlp->nlp_DID, send_rrq);
1206                 return -EINVAL;
1207         }
1208         if (phba->cfg_enable_rrq == 1)
1209                 rrq->send_rrq = send_rrq;
1210         else
1211                 rrq->send_rrq = 0;
1212         rrq->xritag = xritag;
1213         rrq->rrq_stop_time = jiffies +
1214                                 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
1215         rrq->nlp_DID = ndlp->nlp_DID;
1216         rrq->vport = ndlp->vport;
1217         rrq->rxid = rxid;
1218         spin_lock_irqsave(&phba->hbalock, iflags);
1219         empty = list_empty(&phba->active_rrq_list);
1220         list_add_tail(&rrq->list, &phba->active_rrq_list);
1221         phba->hba_flag |= HBA_RRQ_ACTIVE;
1222         if (empty)
1223                 lpfc_worker_wake_up(phba);
1224         spin_unlock_irqrestore(&phba->hbalock, iflags);
1225         return 0;
1226 out:
1227         spin_unlock_irqrestore(&phba->hbalock, iflags);
1228         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1229                         "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1230                         " DID:0x%x Send:%d\n",
1231                         xritag, rxid, ndlp->nlp_DID, send_rrq);
1232         return -EINVAL;
1233 }
1234
1235 /**
1236  * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
1237  * @phba: Pointer to HBA context object.
1238  * @piocbq: Pointer to the iocbq.
1239  *
1240  * The driver calls this function with either the nvme ls ring lock
1241  * or the fc els ring lock held depending on the iocb usage.  This function
1242  * gets a new driver sglq object from the sglq list. If the list is not empty
1243  * then it is successful, it returns pointer to the newly allocated sglq
1244  * object else it returns NULL.
1245  **/
1246 static struct lpfc_sglq *
1247 __lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1248 {
1249         struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
1250         struct lpfc_sglq *sglq = NULL;
1251         struct lpfc_sglq *start_sglq = NULL;
1252         struct lpfc_io_buf *lpfc_cmd;
1253         struct lpfc_nodelist *ndlp;
1254         struct lpfc_sli_ring *pring = NULL;
1255         int found = 0;
1256
1257         if (piocbq->iocb_flag & LPFC_IO_NVME_LS)
1258                 pring =  phba->sli4_hba.nvmels_wq->pring;
1259         else
1260                 pring = lpfc_phba_elsring(phba);
1261
1262         lockdep_assert_held(&pring->ring_lock);
1263
1264         if (piocbq->iocb_flag &  LPFC_IO_FCP) {
1265                 lpfc_cmd = (struct lpfc_io_buf *) piocbq->context1;
1266                 ndlp = lpfc_cmd->rdata->pnode;
1267         } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
1268                         !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
1269                 ndlp = piocbq->context_un.ndlp;
1270         } else  if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1271                 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1272                         ndlp = NULL;
1273                 else
1274                         ndlp = piocbq->context_un.ndlp;
1275         } else {
1276                 ndlp = piocbq->context1;
1277         }
1278
1279         spin_lock(&phba->sli4_hba.sgl_list_lock);
1280         list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
1281         start_sglq = sglq;
1282         while (!found) {
1283                 if (!sglq)
1284                         break;
1285                 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1286                     test_bit(sglq->sli4_lxritag,
1287                     ndlp->active_rrqs_xri_bitmap)) {
1288                         /* This xri has an rrq outstanding for this DID.
1289                          * put it back in the list and get another xri.
1290                          */
1291                         list_add_tail(&sglq->list, lpfc_els_sgl_list);
1292                         sglq = NULL;
1293                         list_remove_head(lpfc_els_sgl_list, sglq,
1294                                                 struct lpfc_sglq, list);
1295                         if (sglq == start_sglq) {
1296                                 list_add_tail(&sglq->list, lpfc_els_sgl_list);
1297                                 sglq = NULL;
1298                                 break;
1299                         } else
1300                                 continue;
1301                 }
1302                 sglq->ndlp = ndlp;
1303                 found = 1;
1304                 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1305                 sglq->state = SGL_ALLOCATED;
1306         }
1307         spin_unlock(&phba->sli4_hba.sgl_list_lock);
1308         return sglq;
1309 }
1310
1311 /**
1312  * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1313  * @phba: Pointer to HBA context object.
1314  * @piocbq: Pointer to the iocbq.
1315  *
1316  * This function is called with the sgl_list lock held. This function
1317  * gets a new driver sglq object from the sglq list. If the
1318  * list is not empty then it is successful, it returns pointer to the newly
1319  * allocated sglq object else it returns NULL.
1320  **/
1321 struct lpfc_sglq *
1322 __lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1323 {
1324         struct list_head *lpfc_nvmet_sgl_list;
1325         struct lpfc_sglq *sglq = NULL;
1326
1327         lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1328
1329         lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1330
1331         list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1332         if (!sglq)
1333                 return NULL;
1334         phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1335         sglq->state = SGL_ALLOCATED;
1336         return sglq;
1337 }
1338
1339 /**
1340  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
1341  * @phba: Pointer to HBA context object.
1342  *
1343  * This function is called with no lock held. This function
1344  * allocates a new driver iocb object from the iocb pool. If the
1345  * allocation is successful, it returns pointer to the newly
1346  * allocated iocb object else it returns NULL.
1347  **/
1348 struct lpfc_iocbq *
1349 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1350 {
1351         struct lpfc_iocbq * iocbq = NULL;
1352         unsigned long iflags;
1353
1354         spin_lock_irqsave(&phba->hbalock, iflags);
1355         iocbq = __lpfc_sli_get_iocbq(phba);
1356         spin_unlock_irqrestore(&phba->hbalock, iflags);
1357         return iocbq;
1358 }
1359
1360 /**
1361  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1362  * @phba: Pointer to HBA context object.
1363  * @iocbq: Pointer to driver iocb object.
1364  *
1365  * This function is called to release the driver iocb object
1366  * to the iocb pool. The iotag in the iocb object
1367  * does not change for each use of the iocb object. This function
1368  * clears all other fields of the iocb object when it is freed.
1369  * The sqlq structure that holds the xritag and phys and virtual
1370  * mappings for the scatter gather list is retrieved from the
1371  * active array of sglq. The get of the sglq pointer also clears
1372  * the entry in the array. If the status of the IO indiactes that
1373  * this IO was aborted then the sglq entry it put on the
1374  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1375  * IO has good status or fails for any other reason then the sglq
1376  * entry is added to the free list (lpfc_els_sgl_list). The hbalock is
1377  *  asserted held in the code path calling this routine.
1378  **/
1379 static void
1380 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1381 {
1382         struct lpfc_sglq *sglq;
1383         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1384         unsigned long iflag = 0;
1385         struct lpfc_sli_ring *pring;
1386
1387         if (iocbq->sli4_xritag == NO_XRI)
1388                 sglq = NULL;
1389         else
1390                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1391
1392
1393         if (sglq)  {
1394                 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1395                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1396                                           iflag);
1397                         sglq->state = SGL_FREED;
1398                         sglq->ndlp = NULL;
1399                         list_add_tail(&sglq->list,
1400                                       &phba->sli4_hba.lpfc_nvmet_sgl_list);
1401                         spin_unlock_irqrestore(
1402                                 &phba->sli4_hba.sgl_list_lock, iflag);
1403                         goto out;
1404                 }
1405
1406                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1407                         (sglq->state != SGL_XRI_ABORTED)) {
1408                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1409                                           iflag);
1410
1411                         /* Check if we can get a reference on ndlp */
1412                         if (sglq->ndlp && !lpfc_nlp_get(sglq->ndlp))
1413                                 sglq->ndlp = NULL;
1414
1415                         list_add(&sglq->list,
1416                                  &phba->sli4_hba.lpfc_abts_els_sgl_list);
1417                         spin_unlock_irqrestore(
1418                                 &phba->sli4_hba.sgl_list_lock, iflag);
1419                 } else {
1420                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1421                                           iflag);
1422                         sglq->state = SGL_FREED;
1423                         sglq->ndlp = NULL;
1424                         list_add_tail(&sglq->list,
1425                                       &phba->sli4_hba.lpfc_els_sgl_list);
1426                         spin_unlock_irqrestore(
1427                                 &phba->sli4_hba.sgl_list_lock, iflag);
1428                         pring = lpfc_phba_elsring(phba);
1429                         /* Check if TXQ queue needs to be serviced */
1430                         if (pring && (!list_empty(&pring->txq)))
1431                                 lpfc_worker_wake_up(phba);
1432                 }
1433         }
1434
1435 out:
1436         /*
1437          * Clean all volatile data fields, preserve iotag and node struct.
1438          */
1439         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1440         iocbq->sli4_lxritag = NO_XRI;
1441         iocbq->sli4_xritag = NO_XRI;
1442         iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1443                               LPFC_IO_NVME_LS);
1444         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1445 }
1446
1447
1448 /**
1449  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1450  * @phba: Pointer to HBA context object.
1451  * @iocbq: Pointer to driver iocb object.
1452  *
1453  * This function is called to release the driver iocb object to the
1454  * iocb pool. The iotag in the iocb object does not change for each
1455  * use of the iocb object. This function clears all other fields of
1456  * the iocb object when it is freed. The hbalock is asserted held in
1457  * the code path calling this routine.
1458  **/
1459 static void
1460 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1461 {
1462         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1463
1464         /*
1465          * Clean all volatile data fields, preserve iotag and node struct.
1466          */
1467         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1468         iocbq->sli4_xritag = NO_XRI;
1469         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1470 }
1471
1472 /**
1473  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1474  * @phba: Pointer to HBA context object.
1475  * @iocbq: Pointer to driver iocb object.
1476  *
1477  * This function is called with hbalock held to release driver
1478  * iocb object to the iocb pool. The iotag in the iocb object
1479  * does not change for each use of the iocb object. This function
1480  * clears all other fields of the iocb object when it is freed.
1481  **/
1482 static void
1483 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1484 {
1485         lockdep_assert_held(&phba->hbalock);
1486
1487         phba->__lpfc_sli_release_iocbq(phba, iocbq);
1488         phba->iocb_cnt--;
1489 }
1490
1491 /**
1492  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1493  * @phba: Pointer to HBA context object.
1494  * @iocbq: Pointer to driver iocb object.
1495  *
1496  * This function is called with no lock held to release the iocb to
1497  * iocb pool.
1498  **/
1499 void
1500 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1501 {
1502         unsigned long iflags;
1503
1504         /*
1505          * Clean all volatile data fields, preserve iotag and node struct.
1506          */
1507         spin_lock_irqsave(&phba->hbalock, iflags);
1508         __lpfc_sli_release_iocbq(phba, iocbq);
1509         spin_unlock_irqrestore(&phba->hbalock, iflags);
1510 }
1511
1512 /**
1513  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1514  * @phba: Pointer to HBA context object.
1515  * @iocblist: List of IOCBs.
1516  * @ulpstatus: ULP status in IOCB command field.
1517  * @ulpWord4: ULP word-4 in IOCB command field.
1518  *
1519  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1520  * on the list by invoking the complete callback function associated with the
1521  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1522  * fields.
1523  **/
1524 void
1525 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1526                       uint32_t ulpstatus, uint32_t ulpWord4)
1527 {
1528         struct lpfc_iocbq *piocb;
1529
1530         while (!list_empty(iocblist)) {
1531                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1532                 if (piocb->wqe_cmpl) {
1533                         if (piocb->iocb_flag & LPFC_IO_NVME)
1534                                 lpfc_nvme_cancel_iocb(phba, piocb,
1535                                                       ulpstatus, ulpWord4);
1536                         else
1537                                 lpfc_sli_release_iocbq(phba, piocb);
1538
1539                 } else if (piocb->iocb_cmpl) {
1540                         piocb->iocb.ulpStatus = ulpstatus;
1541                         piocb->iocb.un.ulpWord[4] = ulpWord4;
1542                         (piocb->iocb_cmpl) (phba, piocb, piocb);
1543                 } else {
1544                         lpfc_sli_release_iocbq(phba, piocb);
1545                 }
1546         }
1547         return;
1548 }
1549
1550 /**
1551  * lpfc_sli_iocb_cmd_type - Get the iocb type
1552  * @iocb_cmnd: iocb command code.
1553  *
1554  * This function is called by ring event handler function to get the iocb type.
1555  * This function translates the iocb command to an iocb command type used to
1556  * decide the final disposition of each completed IOCB.
1557  * The function returns
1558  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1559  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1560  * LPFC_ABORT_IOCB   if it is an abort iocb
1561  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1562  *
1563  * The caller is not required to hold any lock.
1564  **/
1565 static lpfc_iocb_type
1566 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1567 {
1568         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1569
1570         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1571                 return 0;
1572
1573         switch (iocb_cmnd) {
1574         case CMD_XMIT_SEQUENCE_CR:
1575         case CMD_XMIT_SEQUENCE_CX:
1576         case CMD_XMIT_BCAST_CN:
1577         case CMD_XMIT_BCAST_CX:
1578         case CMD_ELS_REQUEST_CR:
1579         case CMD_ELS_REQUEST_CX:
1580         case CMD_CREATE_XRI_CR:
1581         case CMD_CREATE_XRI_CX:
1582         case CMD_GET_RPI_CN:
1583         case CMD_XMIT_ELS_RSP_CX:
1584         case CMD_GET_RPI_CR:
1585         case CMD_FCP_IWRITE_CR:
1586         case CMD_FCP_IWRITE_CX:
1587         case CMD_FCP_IREAD_CR:
1588         case CMD_FCP_IREAD_CX:
1589         case CMD_FCP_ICMND_CR:
1590         case CMD_FCP_ICMND_CX:
1591         case CMD_FCP_TSEND_CX:
1592         case CMD_FCP_TRSP_CX:
1593         case CMD_FCP_TRECEIVE_CX:
1594         case CMD_FCP_AUTO_TRSP_CX:
1595         case CMD_ADAPTER_MSG:
1596         case CMD_ADAPTER_DUMP:
1597         case CMD_XMIT_SEQUENCE64_CR:
1598         case CMD_XMIT_SEQUENCE64_CX:
1599         case CMD_XMIT_BCAST64_CN:
1600         case CMD_XMIT_BCAST64_CX:
1601         case CMD_ELS_REQUEST64_CR:
1602         case CMD_ELS_REQUEST64_CX:
1603         case CMD_FCP_IWRITE64_CR:
1604         case CMD_FCP_IWRITE64_CX:
1605         case CMD_FCP_IREAD64_CR:
1606         case CMD_FCP_IREAD64_CX:
1607         case CMD_FCP_ICMND64_CR:
1608         case CMD_FCP_ICMND64_CX:
1609         case CMD_FCP_TSEND64_CX:
1610         case CMD_FCP_TRSP64_CX:
1611         case CMD_FCP_TRECEIVE64_CX:
1612         case CMD_GEN_REQUEST64_CR:
1613         case CMD_GEN_REQUEST64_CX:
1614         case CMD_XMIT_ELS_RSP64_CX:
1615         case DSSCMD_IWRITE64_CR:
1616         case DSSCMD_IWRITE64_CX:
1617         case DSSCMD_IREAD64_CR:
1618         case DSSCMD_IREAD64_CX:
1619         case CMD_SEND_FRAME:
1620                 type = LPFC_SOL_IOCB;
1621                 break;
1622         case CMD_ABORT_XRI_CN:
1623         case CMD_ABORT_XRI_CX:
1624         case CMD_CLOSE_XRI_CN:
1625         case CMD_CLOSE_XRI_CX:
1626         case CMD_XRI_ABORTED_CX:
1627         case CMD_ABORT_MXRI64_CN:
1628         case CMD_XMIT_BLS_RSP64_CX:
1629                 type = LPFC_ABORT_IOCB;
1630                 break;
1631         case CMD_RCV_SEQUENCE_CX:
1632         case CMD_RCV_ELS_REQ_CX:
1633         case CMD_RCV_SEQUENCE64_CX:
1634         case CMD_RCV_ELS_REQ64_CX:
1635         case CMD_ASYNC_STATUS:
1636         case CMD_IOCB_RCV_SEQ64_CX:
1637         case CMD_IOCB_RCV_ELS64_CX:
1638         case CMD_IOCB_RCV_CONT64_CX:
1639         case CMD_IOCB_RET_XRI64_CX:
1640                 type = LPFC_UNSOL_IOCB;
1641                 break;
1642         case CMD_IOCB_XMIT_MSEQ64_CR:
1643         case CMD_IOCB_XMIT_MSEQ64_CX:
1644         case CMD_IOCB_RCV_SEQ_LIST64_CX:
1645         case CMD_IOCB_RCV_ELS_LIST64_CX:
1646         case CMD_IOCB_CLOSE_EXTENDED_CN:
1647         case CMD_IOCB_ABORT_EXTENDED_CN:
1648         case CMD_IOCB_RET_HBQE64_CN:
1649         case CMD_IOCB_FCP_IBIDIR64_CR:
1650         case CMD_IOCB_FCP_IBIDIR64_CX:
1651         case CMD_IOCB_FCP_ITASKMGT64_CX:
1652         case CMD_IOCB_LOGENTRY_CN:
1653         case CMD_IOCB_LOGENTRY_ASYNC_CN:
1654                 printk("%s - Unhandled SLI-3 Command x%x\n",
1655                                 __func__, iocb_cmnd);
1656                 type = LPFC_UNKNOWN_IOCB;
1657                 break;
1658         default:
1659                 type = LPFC_UNKNOWN_IOCB;
1660                 break;
1661         }
1662
1663         return type;
1664 }
1665
1666 /**
1667  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1668  * @phba: Pointer to HBA context object.
1669  *
1670  * This function is called from SLI initialization code
1671  * to configure every ring of the HBA's SLI interface. The
1672  * caller is not required to hold any lock. This function issues
1673  * a config_ring mailbox command for each ring.
1674  * This function returns zero if successful else returns a negative
1675  * error code.
1676  **/
1677 static int
1678 lpfc_sli_ring_map(struct lpfc_hba *phba)
1679 {
1680         struct lpfc_sli *psli = &phba->sli;
1681         LPFC_MBOXQ_t *pmb;
1682         MAILBOX_t *pmbox;
1683         int i, rc, ret = 0;
1684
1685         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1686         if (!pmb)
1687                 return -ENOMEM;
1688         pmbox = &pmb->u.mb;
1689         phba->link_state = LPFC_INIT_MBX_CMDS;
1690         for (i = 0; i < psli->num_rings; i++) {
1691                 lpfc_config_ring(phba, i, pmb);
1692                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1693                 if (rc != MBX_SUCCESS) {
1694                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1695                                         "0446 Adapter failed to init (%d), "
1696                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1697                                         "ring %d\n",
1698                                         rc, pmbox->mbxCommand,
1699                                         pmbox->mbxStatus, i);
1700                         phba->link_state = LPFC_HBA_ERROR;
1701                         ret = -ENXIO;
1702                         break;
1703                 }
1704         }
1705         mempool_free(pmb, phba->mbox_mem_pool);
1706         return ret;
1707 }
1708
1709 /**
1710  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1711  * @phba: Pointer to HBA context object.
1712  * @pring: Pointer to driver SLI ring object.
1713  * @piocb: Pointer to the driver iocb object.
1714  *
1715  * The driver calls this function with the hbalock held for SLI3 ports or
1716  * the ring lock held for SLI4 ports. The function adds the
1717  * new iocb to txcmplq of the given ring. This function always returns
1718  * 0. If this function is called for ELS ring, this function checks if
1719  * there is a vport associated with the ELS command. This function also
1720  * starts els_tmofunc timer if this is an ELS command.
1721  **/
1722 static int
1723 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1724                         struct lpfc_iocbq *piocb)
1725 {
1726         if (phba->sli_rev == LPFC_SLI_REV4)
1727                 lockdep_assert_held(&pring->ring_lock);
1728         else
1729                 lockdep_assert_held(&phba->hbalock);
1730
1731         BUG_ON(!piocb);
1732
1733         list_add_tail(&piocb->list, &pring->txcmplq);
1734         piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1735         pring->txcmplq_cnt++;
1736
1737         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1738            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1739            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1740                 BUG_ON(!piocb->vport);
1741                 if (!(piocb->vport->load_flag & FC_UNLOADING))
1742                         mod_timer(&piocb->vport->els_tmofunc,
1743                                   jiffies +
1744                                   msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1745         }
1746
1747         return 0;
1748 }
1749
1750 /**
1751  * lpfc_sli_ringtx_get - Get first element of the txq
1752  * @phba: Pointer to HBA context object.
1753  * @pring: Pointer to driver SLI ring object.
1754  *
1755  * This function is called with hbalock held to get next
1756  * iocb in txq of the given ring. If there is any iocb in
1757  * the txq, the function returns first iocb in the list after
1758  * removing the iocb from the list, else it returns NULL.
1759  **/
1760 struct lpfc_iocbq *
1761 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1762 {
1763         struct lpfc_iocbq *cmd_iocb;
1764
1765         lockdep_assert_held(&phba->hbalock);
1766
1767         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1768         return cmd_iocb;
1769 }
1770
1771 /**
1772  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1773  * @phba: Pointer to HBA context object.
1774  * @pring: Pointer to driver SLI ring object.
1775  *
1776  * This function is called with hbalock held and the caller must post the
1777  * iocb without releasing the lock. If the caller releases the lock,
1778  * iocb slot returned by the function is not guaranteed to be available.
1779  * The function returns pointer to the next available iocb slot if there
1780  * is available slot in the ring, else it returns NULL.
1781  * If the get index of the ring is ahead of the put index, the function
1782  * will post an error attention event to the worker thread to take the
1783  * HBA to offline state.
1784  **/
1785 static IOCB_t *
1786 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1787 {
1788         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1789         uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
1790
1791         lockdep_assert_held(&phba->hbalock);
1792
1793         if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1794            (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1795                 pring->sli.sli3.next_cmdidx = 0;
1796
1797         if (unlikely(pring->sli.sli3.local_getidx ==
1798                 pring->sli.sli3.next_cmdidx)) {
1799
1800                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
1801
1802                 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
1803                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1804                                         "0315 Ring %d issue: portCmdGet %d "
1805                                         "is bigger than cmd ring %d\n",
1806                                         pring->ringno,
1807                                         pring->sli.sli3.local_getidx,
1808                                         max_cmd_idx);
1809
1810                         phba->link_state = LPFC_HBA_ERROR;
1811                         /*
1812                          * All error attention handlers are posted to
1813                          * worker thread
1814                          */
1815                         phba->work_ha |= HA_ERATT;
1816                         phba->work_hs = HS_FFER3;
1817
1818                         lpfc_worker_wake_up(phba);
1819
1820                         return NULL;
1821                 }
1822
1823                 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
1824                         return NULL;
1825         }
1826
1827         return lpfc_cmd_iocb(phba, pring);
1828 }
1829
1830 /**
1831  * lpfc_sli_next_iotag - Get an iotag for the iocb
1832  * @phba: Pointer to HBA context object.
1833  * @iocbq: Pointer to driver iocb object.
1834  *
1835  * This function gets an iotag for the iocb. If there is no unused iotag and
1836  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1837  * array and assigns a new iotag.
1838  * The function returns the allocated iotag if successful, else returns zero.
1839  * Zero is not a valid iotag.
1840  * The caller is not required to hold any lock.
1841  **/
1842 uint16_t
1843 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1844 {
1845         struct lpfc_iocbq **new_arr;
1846         struct lpfc_iocbq **old_arr;
1847         size_t new_len;
1848         struct lpfc_sli *psli = &phba->sli;
1849         uint16_t iotag;
1850
1851         spin_lock_irq(&phba->hbalock);
1852         iotag = psli->last_iotag;
1853         if(++iotag < psli->iocbq_lookup_len) {
1854                 psli->last_iotag = iotag;
1855                 psli->iocbq_lookup[iotag] = iocbq;
1856                 spin_unlock_irq(&phba->hbalock);
1857                 iocbq->iotag = iotag;
1858                 return iotag;
1859         } else if (psli->iocbq_lookup_len < (0xffff
1860                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1861                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1862                 spin_unlock_irq(&phba->hbalock);
1863                 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
1864                                   GFP_KERNEL);
1865                 if (new_arr) {
1866                         spin_lock_irq(&phba->hbalock);
1867                         old_arr = psli->iocbq_lookup;
1868                         if (new_len <= psli->iocbq_lookup_len) {
1869                                 /* highly unprobable case */
1870                                 kfree(new_arr);
1871                                 iotag = psli->last_iotag;
1872                                 if(++iotag < psli->iocbq_lookup_len) {
1873                                         psli->last_iotag = iotag;
1874                                         psli->iocbq_lookup[iotag] = iocbq;
1875                                         spin_unlock_irq(&phba->hbalock);
1876                                         iocbq->iotag = iotag;
1877                                         return iotag;
1878                                 }
1879                                 spin_unlock_irq(&phba->hbalock);
1880                                 return 0;
1881                         }
1882                         if (psli->iocbq_lookup)
1883                                 memcpy(new_arr, old_arr,
1884                                        ((psli->last_iotag  + 1) *
1885                                         sizeof (struct lpfc_iocbq *)));
1886                         psli->iocbq_lookup = new_arr;
1887                         psli->iocbq_lookup_len = new_len;
1888                         psli->last_iotag = iotag;
1889                         psli->iocbq_lookup[iotag] = iocbq;
1890                         spin_unlock_irq(&phba->hbalock);
1891                         iocbq->iotag = iotag;
1892                         kfree(old_arr);
1893                         return iotag;
1894                 }
1895         } else
1896                 spin_unlock_irq(&phba->hbalock);
1897
1898         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1899                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1900                         psli->last_iotag);
1901
1902         return 0;
1903 }
1904
1905 /**
1906  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1907  * @phba: Pointer to HBA context object.
1908  * @pring: Pointer to driver SLI ring object.
1909  * @iocb: Pointer to iocb slot in the ring.
1910  * @nextiocb: Pointer to driver iocb object which need to be
1911  *            posted to firmware.
1912  *
1913  * This function is called to post a new iocb to the firmware. This
1914  * function copies the new iocb to ring iocb slot and updates the
1915  * ring pointers. It adds the new iocb to txcmplq if there is
1916  * a completion call back for this iocb else the function will free the
1917  * iocb object.  The hbalock is asserted held in the code path calling
1918  * this routine.
1919  **/
1920 static void
1921 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1922                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1923 {
1924         /*
1925          * Set up an iotag
1926          */
1927         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1928
1929
1930         if (pring->ringno == LPFC_ELS_RING) {
1931                 lpfc_debugfs_slow_ring_trc(phba,
1932                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1933                         *(((uint32_t *) &nextiocb->iocb) + 4),
1934                         *(((uint32_t *) &nextiocb->iocb) + 6),
1935                         *(((uint32_t *) &nextiocb->iocb) + 7));
1936         }
1937
1938         /*
1939          * Issue iocb command to adapter
1940          */
1941         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1942         wmb();
1943         pring->stats.iocb_cmd++;
1944
1945         /*
1946          * If there is no completion routine to call, we can release the
1947          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1948          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1949          */
1950         if (nextiocb->iocb_cmpl)
1951                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1952         else
1953                 __lpfc_sli_release_iocbq(phba, nextiocb);
1954
1955         /*
1956          * Let the HBA know what IOCB slot will be the next one the
1957          * driver will put a command into.
1958          */
1959         pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1960         writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1961 }
1962
1963 /**
1964  * lpfc_sli_update_full_ring - Update the chip attention register
1965  * @phba: Pointer to HBA context object.
1966  * @pring: Pointer to driver SLI ring object.
1967  *
1968  * The caller is not required to hold any lock for calling this function.
1969  * This function updates the chip attention bits for the ring to inform firmware
1970  * that there are pending work to be done for this ring and requests an
1971  * interrupt when there is space available in the ring. This function is
1972  * called when the driver is unable to post more iocbs to the ring due
1973  * to unavailability of space in the ring.
1974  **/
1975 static void
1976 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1977 {
1978         int ringno = pring->ringno;
1979
1980         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1981
1982         wmb();
1983
1984         /*
1985          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1986          * The HBA will tell us when an IOCB entry is available.
1987          */
1988         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1989         readl(phba->CAregaddr); /* flush */
1990
1991         pring->stats.iocb_cmd_full++;
1992 }
1993
1994 /**
1995  * lpfc_sli_update_ring - Update chip attention register
1996  * @phba: Pointer to HBA context object.
1997  * @pring: Pointer to driver SLI ring object.
1998  *
1999  * This function updates the chip attention register bit for the
2000  * given ring to inform HBA that there is more work to be done
2001  * in this ring. The caller is not required to hold any lock.
2002  **/
2003 static void
2004 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2005 {
2006         int ringno = pring->ringno;
2007
2008         /*
2009          * Tell the HBA that there is work to do in this ring.
2010          */
2011         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
2012                 wmb();
2013                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
2014                 readl(phba->CAregaddr); /* flush */
2015         }
2016 }
2017
2018 /**
2019  * lpfc_sli_resume_iocb - Process iocbs in the txq
2020  * @phba: Pointer to HBA context object.
2021  * @pring: Pointer to driver SLI ring object.
2022  *
2023  * This function is called with hbalock held to post pending iocbs
2024  * in the txq to the firmware. This function is called when driver
2025  * detects space available in the ring.
2026  **/
2027 static void
2028 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2029 {
2030         IOCB_t *iocb;
2031         struct lpfc_iocbq *nextiocb;
2032
2033         lockdep_assert_held(&phba->hbalock);
2034
2035         /*
2036          * Check to see if:
2037          *  (a) there is anything on the txq to send
2038          *  (b) link is up
2039          *  (c) link attention events can be processed (fcp ring only)
2040          *  (d) IOCB processing is not blocked by the outstanding mbox command.
2041          */
2042
2043         if (lpfc_is_link_up(phba) &&
2044             (!list_empty(&pring->txq)) &&
2045             (pring->ringno != LPFC_FCP_RING ||
2046              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
2047
2048                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2049                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
2050                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2051
2052                 if (iocb)
2053                         lpfc_sli_update_ring(phba, pring);
2054                 else
2055                         lpfc_sli_update_full_ring(phba, pring);
2056         }
2057
2058         return;
2059 }
2060
2061 /**
2062  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
2063  * @phba: Pointer to HBA context object.
2064  * @hbqno: HBQ number.
2065  *
2066  * This function is called with hbalock held to get the next
2067  * available slot for the given HBQ. If there is free slot
2068  * available for the HBQ it will return pointer to the next available
2069  * HBQ entry else it will return NULL.
2070  **/
2071 static struct lpfc_hbq_entry *
2072 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
2073 {
2074         struct hbq_s *hbqp = &phba->hbqs[hbqno];
2075
2076         lockdep_assert_held(&phba->hbalock);
2077
2078         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
2079             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
2080                 hbqp->next_hbqPutIdx = 0;
2081
2082         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
2083                 uint32_t raw_index = phba->hbq_get[hbqno];
2084                 uint32_t getidx = le32_to_cpu(raw_index);
2085
2086                 hbqp->local_hbqGetIdx = getidx;
2087
2088                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
2089                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2090                                         "1802 HBQ %d: local_hbqGetIdx "
2091                                         "%u is > than hbqp->entry_count %u\n",
2092                                         hbqno, hbqp->local_hbqGetIdx,
2093                                         hbqp->entry_count);
2094
2095                         phba->link_state = LPFC_HBA_ERROR;
2096                         return NULL;
2097                 }
2098
2099                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
2100                         return NULL;
2101         }
2102
2103         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
2104                         hbqp->hbqPutIdx;
2105 }
2106
2107 /**
2108  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
2109  * @phba: Pointer to HBA context object.
2110  *
2111  * This function is called with no lock held to free all the
2112  * hbq buffers while uninitializing the SLI interface. It also
2113  * frees the HBQ buffers returned by the firmware but not yet
2114  * processed by the upper layers.
2115  **/
2116 void
2117 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
2118 {
2119         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2120         struct hbq_dmabuf *hbq_buf;
2121         unsigned long flags;
2122         int i, hbq_count;
2123
2124         hbq_count = lpfc_sli_hbq_count();
2125         /* Return all memory used by all HBQs */
2126         spin_lock_irqsave(&phba->hbalock, flags);
2127         for (i = 0; i < hbq_count; ++i) {
2128                 list_for_each_entry_safe(dmabuf, next_dmabuf,
2129                                 &phba->hbqs[i].hbq_buffer_list, list) {
2130                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
2131                         list_del(&hbq_buf->dbuf.list);
2132                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2133                 }
2134                 phba->hbqs[i].buffer_count = 0;
2135         }
2136
2137         /* Mark the HBQs not in use */
2138         phba->hbq_in_use = 0;
2139         spin_unlock_irqrestore(&phba->hbalock, flags);
2140 }
2141
2142 /**
2143  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
2144  * @phba: Pointer to HBA context object.
2145  * @hbqno: HBQ number.
2146  * @hbq_buf: Pointer to HBQ buffer.
2147  *
2148  * This function is called with the hbalock held to post a
2149  * hbq buffer to the firmware. If the function finds an empty
2150  * slot in the HBQ, it will post the buffer. The function will return
2151  * pointer to the hbq entry if it successfully post the buffer
2152  * else it will return NULL.
2153  **/
2154 static int
2155 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
2156                          struct hbq_dmabuf *hbq_buf)
2157 {
2158         lockdep_assert_held(&phba->hbalock);
2159         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2160 }
2161
2162 /**
2163  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2164  * @phba: Pointer to HBA context object.
2165  * @hbqno: HBQ number.
2166  * @hbq_buf: Pointer to HBQ buffer.
2167  *
2168  * This function is called with the hbalock held to post a hbq buffer to the
2169  * firmware. If the function finds an empty slot in the HBQ, it will post the
2170  * buffer and place it on the hbq_buffer_list. The function will return zero if
2171  * it successfully post the buffer else it will return an error.
2172  **/
2173 static int
2174 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2175                             struct hbq_dmabuf *hbq_buf)
2176 {
2177         struct lpfc_hbq_entry *hbqe;
2178         dma_addr_t physaddr = hbq_buf->dbuf.phys;
2179
2180         lockdep_assert_held(&phba->hbalock);
2181         /* Get next HBQ entry slot to use */
2182         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2183         if (hbqe) {
2184                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2185
2186                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2187                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
2188                 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
2189                 hbqe->bde.tus.f.bdeFlags = 0;
2190                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2191                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2192                                 /* Sync SLIM */
2193                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2194                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
2195                                 /* flush */
2196                 readl(phba->hbq_put + hbqno);
2197                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
2198                 return 0;
2199         } else
2200                 return -ENOMEM;
2201 }
2202
2203 /**
2204  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2205  * @phba: Pointer to HBA context object.
2206  * @hbqno: HBQ number.
2207  * @hbq_buf: Pointer to HBQ buffer.
2208  *
2209  * This function is called with the hbalock held to post an RQE to the SLI4
2210  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2211  * the hbq_buffer_list and return zero, otherwise it will return an error.
2212  **/
2213 static int
2214 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2215                             struct hbq_dmabuf *hbq_buf)
2216 {
2217         int rc;
2218         struct lpfc_rqe hrqe;
2219         struct lpfc_rqe drqe;
2220         struct lpfc_queue *hrq;
2221         struct lpfc_queue *drq;
2222
2223         if (hbqno != LPFC_ELS_HBQ)
2224                 return 1;
2225         hrq = phba->sli4_hba.hdr_rq;
2226         drq = phba->sli4_hba.dat_rq;
2227
2228         lockdep_assert_held(&phba->hbalock);
2229         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2230         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2231         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2232         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
2233         rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
2234         if (rc < 0)
2235                 return rc;
2236         hbq_buf->tag = (rc | (hbqno << 16));
2237         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2238         return 0;
2239 }
2240
2241 /* HBQ for ELS and CT traffic. */
2242 static struct lpfc_hbq_init lpfc_els_hbq = {
2243         .rn = 1,
2244         .entry_count = 256,
2245         .mask_count = 0,
2246         .profile = 0,
2247         .ring_mask = (1 << LPFC_ELS_RING),
2248         .buffer_count = 0,
2249         .init_count = 40,
2250         .add_count = 40,
2251 };
2252
2253 /* Array of HBQs */
2254 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
2255         &lpfc_els_hbq,
2256 };
2257
2258 /**
2259  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
2260  * @phba: Pointer to HBA context object.
2261  * @hbqno: HBQ number.
2262  * @count: Number of HBQ buffers to be posted.
2263  *
2264  * This function is called with no lock held to post more hbq buffers to the
2265  * given HBQ. The function returns the number of HBQ buffers successfully
2266  * posted.
2267  **/
2268 static int
2269 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2270 {
2271         uint32_t i, posted = 0;
2272         unsigned long flags;
2273         struct hbq_dmabuf *hbq_buffer;
2274         LIST_HEAD(hbq_buf_list);
2275         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
2276                 return 0;
2277
2278         if ((phba->hbqs[hbqno].buffer_count + count) >
2279             lpfc_hbq_defs[hbqno]->entry_count)
2280                 count = lpfc_hbq_defs[hbqno]->entry_count -
2281                                         phba->hbqs[hbqno].buffer_count;
2282         if (!count)
2283                 return 0;
2284         /* Allocate HBQ entries */
2285         for (i = 0; i < count; i++) {
2286                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2287                 if (!hbq_buffer)
2288                         break;
2289                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2290         }
2291         /* Check whether HBQ is still in use */
2292         spin_lock_irqsave(&phba->hbalock, flags);
2293         if (!phba->hbq_in_use)
2294                 goto err;
2295         while (!list_empty(&hbq_buf_list)) {
2296                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2297                                  dbuf.list);
2298                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2299                                       (hbqno << 16));
2300                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
2301                         phba->hbqs[hbqno].buffer_count++;
2302                         posted++;
2303                 } else
2304                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2305         }
2306         spin_unlock_irqrestore(&phba->hbalock, flags);
2307         return posted;
2308 err:
2309         spin_unlock_irqrestore(&phba->hbalock, flags);
2310         while (!list_empty(&hbq_buf_list)) {
2311                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2312                                  dbuf.list);
2313                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2314         }
2315         return 0;
2316 }
2317
2318 /**
2319  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
2320  * @phba: Pointer to HBA context object.
2321  * @qno: HBQ number.
2322  *
2323  * This function posts more buffers to the HBQ. This function
2324  * is called with no lock held. The function returns the number of HBQ entries
2325  * successfully allocated.
2326  **/
2327 int
2328 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
2329 {
2330         if (phba->sli_rev == LPFC_SLI_REV4)
2331                 return 0;
2332         else
2333                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2334                                          lpfc_hbq_defs[qno]->add_count);
2335 }
2336
2337 /**
2338  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
2339  * @phba: Pointer to HBA context object.
2340  * @qno:  HBQ queue number.
2341  *
2342  * This function is called from SLI initialization code path with
2343  * no lock held to post initial HBQ buffers to firmware. The
2344  * function returns the number of HBQ entries successfully allocated.
2345  **/
2346 static int
2347 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2348 {
2349         if (phba->sli_rev == LPFC_SLI_REV4)
2350                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2351                                         lpfc_hbq_defs[qno]->entry_count);
2352         else
2353                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2354                                          lpfc_hbq_defs[qno]->init_count);
2355 }
2356
2357 /*
2358  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2359  *
2360  * This function removes the first hbq buffer on an hbq list and returns a
2361  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2362  **/
2363 static struct hbq_dmabuf *
2364 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2365 {
2366         struct lpfc_dmabuf *d_buf;
2367
2368         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2369         if (!d_buf)
2370                 return NULL;
2371         return container_of(d_buf, struct hbq_dmabuf, dbuf);
2372 }
2373
2374 /**
2375  * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2376  * @phba: Pointer to HBA context object.
2377  * @hrq: HBQ number.
2378  *
2379  * This function removes the first RQ buffer on an RQ buffer list and returns a
2380  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2381  **/
2382 static struct rqb_dmabuf *
2383 lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2384 {
2385         struct lpfc_dmabuf *h_buf;
2386         struct lpfc_rqb *rqbp;
2387
2388         rqbp = hrq->rqbp;
2389         list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2390                          struct lpfc_dmabuf, list);
2391         if (!h_buf)
2392                 return NULL;
2393         rqbp->buffer_count--;
2394         return container_of(h_buf, struct rqb_dmabuf, hbuf);
2395 }
2396
2397 /**
2398  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
2399  * @phba: Pointer to HBA context object.
2400  * @tag: Tag of the hbq buffer.
2401  *
2402  * This function searches for the hbq buffer associated with the given tag in
2403  * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2404  * otherwise it returns NULL.
2405  **/
2406 static struct hbq_dmabuf *
2407 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2408 {
2409         struct lpfc_dmabuf *d_buf;
2410         struct hbq_dmabuf *hbq_buf;
2411         uint32_t hbqno;
2412
2413         hbqno = tag >> 16;
2414         if (hbqno >= LPFC_MAX_HBQS)
2415                 return NULL;
2416
2417         spin_lock_irq(&phba->hbalock);
2418         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2419                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2420                 if (hbq_buf->tag == tag) {
2421                         spin_unlock_irq(&phba->hbalock);
2422                         return hbq_buf;
2423                 }
2424         }
2425         spin_unlock_irq(&phba->hbalock);
2426         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2427                         "1803 Bad hbq tag. Data: x%x x%x\n",
2428                         tag, phba->hbqs[tag >> 16].buffer_count);
2429         return NULL;
2430 }
2431
2432 /**
2433  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2434  * @phba: Pointer to HBA context object.
2435  * @hbq_buffer: Pointer to HBQ buffer.
2436  *
2437  * This function is called with hbalock. This function gives back
2438  * the hbq buffer to firmware. If the HBQ does not have space to
2439  * post the buffer, it will free the buffer.
2440  **/
2441 void
2442 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2443 {
2444         uint32_t hbqno;
2445
2446         if (hbq_buffer) {
2447                 hbqno = hbq_buffer->tag >> 16;
2448                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2449                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2450         }
2451 }
2452
2453 /**
2454  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2455  * @mbxCommand: mailbox command code.
2456  *
2457  * This function is called by the mailbox event handler function to verify
2458  * that the completed mailbox command is a legitimate mailbox command. If the
2459  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2460  * and the mailbox event handler will take the HBA offline.
2461  **/
2462 static int
2463 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2464 {
2465         uint8_t ret;
2466
2467         switch (mbxCommand) {
2468         case MBX_LOAD_SM:
2469         case MBX_READ_NV:
2470         case MBX_WRITE_NV:
2471         case MBX_WRITE_VPARMS:
2472         case MBX_RUN_BIU_DIAG:
2473         case MBX_INIT_LINK:
2474         case MBX_DOWN_LINK:
2475         case MBX_CONFIG_LINK:
2476         case MBX_CONFIG_RING:
2477         case MBX_RESET_RING:
2478         case MBX_READ_CONFIG:
2479         case MBX_READ_RCONFIG:
2480         case MBX_READ_SPARM:
2481         case MBX_READ_STATUS:
2482         case MBX_READ_RPI:
2483         case MBX_READ_XRI:
2484         case MBX_READ_REV:
2485         case MBX_READ_LNK_STAT:
2486         case MBX_REG_LOGIN:
2487         case MBX_UNREG_LOGIN:
2488         case MBX_CLEAR_LA:
2489         case MBX_DUMP_MEMORY:
2490         case MBX_DUMP_CONTEXT:
2491         case MBX_RUN_DIAGS:
2492         case MBX_RESTART:
2493         case MBX_UPDATE_CFG:
2494         case MBX_DOWN_LOAD:
2495         case MBX_DEL_LD_ENTRY:
2496         case MBX_RUN_PROGRAM:
2497         case MBX_SET_MASK:
2498         case MBX_SET_VARIABLE:
2499         case MBX_UNREG_D_ID:
2500         case MBX_KILL_BOARD:
2501         case MBX_CONFIG_FARP:
2502         case MBX_BEACON:
2503         case MBX_LOAD_AREA:
2504         case MBX_RUN_BIU_DIAG64:
2505         case MBX_CONFIG_PORT:
2506         case MBX_READ_SPARM64:
2507         case MBX_READ_RPI64:
2508         case MBX_REG_LOGIN64:
2509         case MBX_READ_TOPOLOGY:
2510         case MBX_WRITE_WWN:
2511         case MBX_SET_DEBUG:
2512         case MBX_LOAD_EXP_ROM:
2513         case MBX_ASYNCEVT_ENABLE:
2514         case MBX_REG_VPI:
2515         case MBX_UNREG_VPI:
2516         case MBX_HEARTBEAT:
2517         case MBX_PORT_CAPABILITIES:
2518         case MBX_PORT_IOV_CONTROL:
2519         case MBX_SLI4_CONFIG:
2520         case MBX_SLI4_REQ_FTRS:
2521         case MBX_REG_FCFI:
2522         case MBX_UNREG_FCFI:
2523         case MBX_REG_VFI:
2524         case MBX_UNREG_VFI:
2525         case MBX_INIT_VPI:
2526         case MBX_INIT_VFI:
2527         case MBX_RESUME_RPI:
2528         case MBX_READ_EVENT_LOG_STATUS:
2529         case MBX_READ_EVENT_LOG:
2530         case MBX_SECURITY_MGMT:
2531         case MBX_AUTH_PORT:
2532         case MBX_ACCESS_VDATA:
2533                 ret = mbxCommand;
2534                 break;
2535         default:
2536                 ret = MBX_SHUTDOWN;
2537                 break;
2538         }
2539         return ret;
2540 }
2541
2542 /**
2543  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2544  * @phba: Pointer to HBA context object.
2545  * @pmboxq: Pointer to mailbox command.
2546  *
2547  * This is completion handler function for mailbox commands issued from
2548  * lpfc_sli_issue_mbox_wait function. This function is called by the
2549  * mailbox event handler function with no lock held. This function
2550  * will wake up thread waiting on the wait queue pointed by context1
2551  * of the mailbox.
2552  **/
2553 void
2554 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2555 {
2556         unsigned long drvr_flag;
2557         struct completion *pmbox_done;
2558
2559         /*
2560          * If pmbox_done is empty, the driver thread gave up waiting and
2561          * continued running.
2562          */
2563         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2564         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2565         pmbox_done = (struct completion *)pmboxq->context3;
2566         if (pmbox_done)
2567                 complete(pmbox_done);
2568         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2569         return;
2570 }
2571
2572 static void
2573 __lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2574 {
2575         unsigned long iflags;
2576
2577         if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
2578                 lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
2579                 spin_lock_irqsave(&ndlp->lock, iflags);
2580                 ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
2581                 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
2582                 spin_unlock_irqrestore(&ndlp->lock, iflags);
2583         }
2584         ndlp->nlp_flag &= ~NLP_UNREG_INP;
2585 }
2586
2587 /**
2588  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2589  * @phba: Pointer to HBA context object.
2590  * @pmb: Pointer to mailbox object.
2591  *
2592  * This function is the default mailbox completion handler. It
2593  * frees the memory resources associated with the completed mailbox
2594  * command. If the completed command is a REG_LOGIN mailbox command,
2595  * this function will issue a UREG_LOGIN to re-claim the RPI.
2596  **/
2597 void
2598 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2599 {
2600         struct lpfc_vport  *vport = pmb->vport;
2601         struct lpfc_dmabuf *mp;
2602         struct lpfc_nodelist *ndlp;
2603         struct Scsi_Host *shost;
2604         uint16_t rpi, vpi;
2605         int rc;
2606
2607         mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
2608
2609         if (mp) {
2610                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2611                 kfree(mp);
2612         }
2613
2614         /*
2615          * If a REG_LOGIN succeeded  after node is destroyed or node
2616          * is in re-discovery driver need to cleanup the RPI.
2617          */
2618         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2619             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2620             !pmb->u.mb.mbxStatus) {
2621                 rpi = pmb->u.mb.un.varWords[0];
2622                 vpi = pmb->u.mb.un.varRegLogin.vpi;
2623                 if (phba->sli_rev == LPFC_SLI_REV4)
2624                         vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
2625                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2626                 pmb->vport = vport;
2627                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2628                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2629                 if (rc != MBX_NOT_FINISHED)
2630                         return;
2631         }
2632
2633         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2634                 !(phba->pport->load_flag & FC_UNLOADING) &&
2635                 !pmb->u.mb.mbxStatus) {
2636                 shost = lpfc_shost_from_vport(vport);
2637                 spin_lock_irq(shost->host_lock);
2638                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2639                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2640                 spin_unlock_irq(shost->host_lock);
2641         }
2642
2643         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2644                 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2645                 lpfc_nlp_put(ndlp);
2646                 pmb->ctx_buf = NULL;
2647                 pmb->ctx_ndlp = NULL;
2648         }
2649
2650         if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2651                 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2652
2653                 /* Check to see if there are any deferred events to process */
2654                 if (ndlp) {
2655                         lpfc_printf_vlog(
2656                                 vport,
2657                                 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2658                                 "1438 UNREG cmpl deferred mbox x%x "
2659                                 "on NPort x%x Data: x%x x%x %px x%x x%x\n",
2660                                 ndlp->nlp_rpi, ndlp->nlp_DID,
2661                                 ndlp->nlp_flag, ndlp->nlp_defer_did,
2662                                 ndlp, vport->load_flag, kref_read(&ndlp->kref));
2663
2664                         if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2665                             (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
2666                                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
2667                                 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2668                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2669                         } else {
2670                                 __lpfc_sli_rpi_release(vport, ndlp);
2671                         }
2672
2673                         /* The unreg_login mailbox is complete and had a
2674                          * reference that has to be released.  The PLOGI
2675                          * got its own ref.
2676                          */
2677                         lpfc_nlp_put(ndlp);
2678                         pmb->ctx_ndlp = NULL;
2679                 }
2680         }
2681
2682         /* Check security permission status on INIT_LINK mailbox command */
2683         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2684             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2685                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2686                                 "2860 SLI authentication is required "
2687                                 "for INIT_LINK but has not done yet\n");
2688
2689         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2690                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2691         else
2692                 mempool_free(pmb, phba->mbox_mem_pool);
2693 }
2694  /**
2695  * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2696  * @phba: Pointer to HBA context object.
2697  * @pmb: Pointer to mailbox object.
2698  *
2699  * This function is the unreg rpi mailbox completion handler. It
2700  * frees the memory resources associated with the completed mailbox
2701  * command. An additional reference is put on the ndlp to prevent
2702  * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2703  * the unreg mailbox command completes, this routine puts the
2704  * reference back.
2705  *
2706  **/
2707 void
2708 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2709 {
2710         struct lpfc_vport  *vport = pmb->vport;
2711         struct lpfc_nodelist *ndlp;
2712
2713         ndlp = pmb->ctx_ndlp;
2714         if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2715                 if (phba->sli_rev == LPFC_SLI_REV4 &&
2716                     (bf_get(lpfc_sli_intf_if_type,
2717                      &phba->sli4_hba.sli_intf) >=
2718                      LPFC_SLI_INTF_IF_TYPE_2)) {
2719                         if (ndlp) {
2720                                 lpfc_printf_vlog(
2721                                          vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2722                                          "0010 UNREG_LOGIN vpi:%x "
2723                                          "rpi:%x DID:%x defer x%x flg x%x "
2724                                          "%px\n",
2725                                          vport->vpi, ndlp->nlp_rpi,
2726                                          ndlp->nlp_DID, ndlp->nlp_defer_did,
2727                                          ndlp->nlp_flag,
2728                                          ndlp);
2729                                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2730
2731                                 /* Check to see if there are any deferred
2732                                  * events to process
2733                                  */
2734                                 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2735                                     (ndlp->nlp_defer_did !=
2736                                     NLP_EVT_NOTHING_PENDING)) {
2737                                         lpfc_printf_vlog(
2738                                                 vport, KERN_INFO, LOG_DISCOVERY,
2739                                                 "4111 UNREG cmpl deferred "
2740                                                 "clr x%x on "
2741                                                 "NPort x%x Data: x%x x%px\n",
2742                                                 ndlp->nlp_rpi, ndlp->nlp_DID,
2743                                                 ndlp->nlp_defer_did, ndlp);
2744                                         ndlp->nlp_flag &= ~NLP_UNREG_INP;
2745                                         ndlp->nlp_defer_did =
2746                                                 NLP_EVT_NOTHING_PENDING;
2747                                         lpfc_issue_els_plogi(
2748                                                 vport, ndlp->nlp_DID, 0);
2749                                 } else {
2750                                         __lpfc_sli_rpi_release(vport, ndlp);
2751                                 }
2752
2753                                 lpfc_nlp_put(ndlp);
2754                         }
2755                 }
2756         }
2757
2758         mempool_free(pmb, phba->mbox_mem_pool);
2759 }
2760
2761 /**
2762  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2763  * @phba: Pointer to HBA context object.
2764  *
2765  * This function is called with no lock held. This function processes all
2766  * the completed mailbox commands and gives it to upper layers. The interrupt
2767  * service routine processes mailbox completion interrupt and adds completed
2768  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2769  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2770  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2771  * function returns the mailbox commands to the upper layer by calling the
2772  * completion handler function of each mailbox.
2773  **/
2774 int
2775 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2776 {
2777         MAILBOX_t *pmbox;
2778         LPFC_MBOXQ_t *pmb;
2779         int rc;
2780         LIST_HEAD(cmplq);
2781
2782         phba->sli.slistat.mbox_event++;
2783
2784         /* Get all completed mailboxe buffers into the cmplq */
2785         spin_lock_irq(&phba->hbalock);
2786         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2787         spin_unlock_irq(&phba->hbalock);
2788
2789         /* Get a Mailbox buffer to setup mailbox commands for callback */
2790         do {
2791                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2792                 if (pmb == NULL)
2793                         break;
2794
2795                 pmbox = &pmb->u.mb;
2796
2797                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2798                         if (pmb->vport) {
2799                                 lpfc_debugfs_disc_trc(pmb->vport,
2800                                         LPFC_DISC_TRC_MBOX_VPORT,
2801                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2802                                         (uint32_t)pmbox->mbxCommand,
2803                                         pmbox->un.varWords[0],
2804                                         pmbox->un.varWords[1]);
2805                         }
2806                         else {
2807                                 lpfc_debugfs_disc_trc(phba->pport,
2808                                         LPFC_DISC_TRC_MBOX,
2809                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2810                                         (uint32_t)pmbox->mbxCommand,
2811                                         pmbox->un.varWords[0],
2812                                         pmbox->un.varWords[1]);
2813                         }
2814                 }
2815
2816                 /*
2817                  * It is a fatal error if unknown mbox command completion.
2818                  */
2819                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2820                     MBX_SHUTDOWN) {
2821                         /* Unknown mailbox command compl */
2822                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2823                                         "(%d):0323 Unknown Mailbox command "
2824                                         "x%x (x%x/x%x) Cmpl\n",
2825                                         pmb->vport ? pmb->vport->vpi :
2826                                         LPFC_VPORT_UNKNOWN,
2827                                         pmbox->mbxCommand,
2828                                         lpfc_sli_config_mbox_subsys_get(phba,
2829                                                                         pmb),
2830                                         lpfc_sli_config_mbox_opcode_get(phba,
2831                                                                         pmb));
2832                         phba->link_state = LPFC_HBA_ERROR;
2833                         phba->work_hs = HS_FFER3;
2834                         lpfc_handle_eratt(phba);
2835                         continue;
2836                 }
2837
2838                 if (pmbox->mbxStatus) {
2839                         phba->sli.slistat.mbox_stat_err++;
2840                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2841                                 /* Mbox cmd cmpl error - RETRYing */
2842                                 lpfc_printf_log(phba, KERN_INFO,
2843                                         LOG_MBOX | LOG_SLI,
2844                                         "(%d):0305 Mbox cmd cmpl "
2845                                         "error - RETRYing Data: x%x "
2846                                         "(x%x/x%x) x%x x%x x%x\n",
2847                                         pmb->vport ? pmb->vport->vpi :
2848                                         LPFC_VPORT_UNKNOWN,
2849                                         pmbox->mbxCommand,
2850                                         lpfc_sli_config_mbox_subsys_get(phba,
2851                                                                         pmb),
2852                                         lpfc_sli_config_mbox_opcode_get(phba,
2853                                                                         pmb),
2854                                         pmbox->mbxStatus,
2855                                         pmbox->un.varWords[0],
2856                                         pmb->vport ? pmb->vport->port_state :
2857                                         LPFC_VPORT_UNKNOWN);
2858                                 pmbox->mbxStatus = 0;
2859                                 pmbox->mbxOwner = OWN_HOST;
2860                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2861                                 if (rc != MBX_NOT_FINISHED)
2862                                         continue;
2863                         }
2864                 }
2865
2866                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2867                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2868                                 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
2869                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2870                                 "x%x x%x x%x\n",
2871                                 pmb->vport ? pmb->vport->vpi : 0,
2872                                 pmbox->mbxCommand,
2873                                 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2874                                 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2875                                 pmb->mbox_cmpl,
2876                                 *((uint32_t *) pmbox),
2877                                 pmbox->un.varWords[0],
2878                                 pmbox->un.varWords[1],
2879                                 pmbox->un.varWords[2],
2880                                 pmbox->un.varWords[3],
2881                                 pmbox->un.varWords[4],
2882                                 pmbox->un.varWords[5],
2883                                 pmbox->un.varWords[6],
2884                                 pmbox->un.varWords[7],
2885                                 pmbox->un.varWords[8],
2886                                 pmbox->un.varWords[9],
2887                                 pmbox->un.varWords[10]);
2888
2889                 if (pmb->mbox_cmpl)
2890                         pmb->mbox_cmpl(phba,pmb);
2891         } while (1);
2892         return 0;
2893 }
2894
2895 /**
2896  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2897  * @phba: Pointer to HBA context object.
2898  * @pring: Pointer to driver SLI ring object.
2899  * @tag: buffer tag.
2900  *
2901  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2902  * is set in the tag the buffer is posted for a particular exchange,
2903  * the function will return the buffer without replacing the buffer.
2904  * If the buffer is for unsolicited ELS or CT traffic, this function
2905  * returns the buffer and also posts another buffer to the firmware.
2906  **/
2907 static struct lpfc_dmabuf *
2908 lpfc_sli_get_buff(struct lpfc_hba *phba,
2909                   struct lpfc_sli_ring *pring,
2910                   uint32_t tag)
2911 {
2912         struct hbq_dmabuf *hbq_entry;
2913
2914         if (tag & QUE_BUFTAG_BIT)
2915                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2916         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2917         if (!hbq_entry)
2918                 return NULL;
2919         return &hbq_entry->dbuf;
2920 }
2921
2922 /**
2923  * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
2924  *                              containing a NVME LS request.
2925  * @phba: pointer to lpfc hba data structure.
2926  * @piocb: pointer to the iocbq struct representing the sequence starting
2927  *        frame.
2928  *
2929  * This routine initially validates the NVME LS, validates there is a login
2930  * with the port that sent the LS, and then calls the appropriate nvme host
2931  * or target LS request handler.
2932  **/
2933 static void
2934 lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
2935 {
2936         struct lpfc_nodelist *ndlp;
2937         struct lpfc_dmabuf *d_buf;
2938         struct hbq_dmabuf *nvmebuf;
2939         struct fc_frame_header *fc_hdr;
2940         struct lpfc_async_xchg_ctx *axchg = NULL;
2941         char *failwhy = NULL;
2942         uint32_t oxid, sid, did, fctl, size;
2943         int ret = 1;
2944
2945         d_buf = piocb->context2;
2946
2947         nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2948         fc_hdr = nvmebuf->hbuf.virt;
2949         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2950         sid = sli4_sid_from_fc_hdr(fc_hdr);
2951         did = sli4_did_from_fc_hdr(fc_hdr);
2952         fctl = (fc_hdr->fh_f_ctl[0] << 16 |
2953                 fc_hdr->fh_f_ctl[1] << 8 |
2954                 fc_hdr->fh_f_ctl[2]);
2955         size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
2956
2957         lpfc_nvmeio_data(phba, "NVME LS    RCV: xri x%x sz %d from %06x\n",
2958                          oxid, size, sid);
2959
2960         if (phba->pport->load_flag & FC_UNLOADING) {
2961                 failwhy = "Driver Unloading";
2962         } else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
2963                 failwhy = "NVME FC4 Disabled";
2964         } else if (!phba->nvmet_support && !phba->pport->localport) {
2965                 failwhy = "No Localport";
2966         } else if (phba->nvmet_support && !phba->targetport) {
2967                 failwhy = "No Targetport";
2968         } else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
2969                 failwhy = "Bad NVME LS R_CTL";
2970         } else if (unlikely((fctl & 0x00FF0000) !=
2971                         (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
2972                 failwhy = "Bad NVME LS F_CTL";
2973         } else {
2974                 axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);
2975                 if (!axchg)
2976                         failwhy = "No CTX memory";
2977         }
2978
2979         if (unlikely(failwhy)) {
2980                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2981                                 "6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
2982                                 sid, oxid, failwhy);
2983                 goto out_fail;
2984         }
2985
2986         /* validate the source of the LS is logged in */
2987         ndlp = lpfc_findnode_did(phba->pport, sid);
2988         if (!ndlp ||
2989             ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2990              (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2991                 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2992                                 "6216 NVME Unsol rcv: No ndlp: "
2993                                 "NPort_ID x%x oxid x%x\n",
2994                                 sid, oxid);
2995                 goto out_fail;
2996         }
2997
2998         axchg->phba = phba;
2999         axchg->ndlp = ndlp;
3000         axchg->size = size;
3001         axchg->oxid = oxid;
3002         axchg->sid = sid;
3003         axchg->wqeq = NULL;
3004         axchg->state = LPFC_NVME_STE_LS_RCV;
3005         axchg->entry_cnt = 1;
3006         axchg->rqb_buffer = (void *)nvmebuf;
3007         axchg->hdwq = &phba->sli4_hba.hdwq[0];
3008         axchg->payload = nvmebuf->dbuf.virt;
3009         INIT_LIST_HEAD(&axchg->list);
3010
3011         if (phba->nvmet_support) {
3012                 ret = lpfc_nvmet_handle_lsreq(phba, axchg);
3013                 spin_lock_irq(&ndlp->lock);
3014                 if (!ret && !(ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH)) {
3015                         ndlp->fc4_xpt_flags |= NLP_XPT_HAS_HH;
3016                         spin_unlock_irq(&ndlp->lock);
3017
3018                         /* This reference is a single occurrence to hold the
3019                          * node valid until the nvmet transport calls
3020                          * host_release.
3021                          */
3022                         if (!lpfc_nlp_get(ndlp))
3023                                 goto out_fail;
3024
3025                         lpfc_printf_log(phba, KERN_ERR, LOG_NODE,
3026                                         "6206 NVMET unsol ls_req ndlp %p "
3027                                         "DID x%x xflags x%x refcnt %d\n",
3028                                         ndlp, ndlp->nlp_DID,
3029                                         ndlp->fc4_xpt_flags,
3030                                         kref_read(&ndlp->kref));
3031                 } else {
3032                         spin_unlock_irq(&ndlp->lock);
3033                 }
3034         } else {
3035                 ret = lpfc_nvme_handle_lsreq(phba, axchg);
3036         }
3037
3038         /* if zero, LS was successfully handled. If non-zero, LS not handled */
3039         if (!ret)
3040                 return;
3041
3042 out_fail:
3043         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3044                         "6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
3045                         "NVMe%s handler failed %d\n",
3046                         did, sid, oxid,
3047                         (phba->nvmet_support) ? "T" : "I", ret);
3048
3049         /* recycle receive buffer */
3050         lpfc_in_buf_free(phba, &nvmebuf->dbuf);
3051
3052         /* If start of new exchange, abort it */
3053         if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
3054                 ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
3055
3056         if (ret)
3057                 kfree(axchg);
3058 }
3059
3060 /**
3061  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
3062  * @phba: Pointer to HBA context object.
3063  * @pring: Pointer to driver SLI ring object.
3064  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
3065  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
3066  * @fch_type: the type for the first frame of the sequence.
3067  *
3068  * This function is called with no lock held. This function uses the r_ctl and
3069  * type of the received sequence to find the correct callback function to call
3070  * to process the sequence.
3071  **/
3072 static int
3073 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3074                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
3075                          uint32_t fch_type)
3076 {
3077         int i;
3078
3079         switch (fch_type) {
3080         case FC_TYPE_NVME:
3081                 lpfc_nvme_unsol_ls_handler(phba, saveq);
3082                 return 1;
3083         default:
3084                 break;
3085         }
3086
3087         /* unSolicited Responses */
3088         if (pring->prt[0].profile) {
3089                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
3090                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
3091                                                                         saveq);
3092                 return 1;
3093         }
3094         /* We must search, based on rctl / type
3095            for the right routine */
3096         for (i = 0; i < pring->num_mask; i++) {
3097                 if ((pring->prt[i].rctl == fch_r_ctl) &&
3098                     (pring->prt[i].type == fch_type)) {
3099                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
3100                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
3101                                                 (phba, pring, saveq);
3102                         return 1;
3103                 }
3104         }
3105         return 0;
3106 }
3107
3108 /**
3109  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
3110  * @phba: Pointer to HBA context object.
3111  * @pring: Pointer to driver SLI ring object.
3112  * @saveq: Pointer to the unsolicited iocb.
3113  *
3114  * This function is called with no lock held by the ring event handler
3115  * when there is an unsolicited iocb posted to the response ring by the
3116  * firmware. This function gets the buffer associated with the iocbs
3117  * and calls the event handler for the ring. This function handles both
3118  * qring buffers and hbq buffers.
3119  * When the function returns 1 the caller can free the iocb object otherwise
3120  * upper layer functions will free the iocb objects.
3121  **/
3122 static int
3123 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3124                             struct lpfc_iocbq *saveq)
3125 {
3126         IOCB_t           * irsp;
3127         WORD5            * w5p;
3128         uint32_t           Rctl, Type;
3129         struct lpfc_iocbq *iocbq;
3130         struct lpfc_dmabuf *dmzbuf;
3131
3132         irsp = &(saveq->iocb);
3133
3134         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
3135                 if (pring->lpfc_sli_rcv_async_status)
3136                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
3137                 else
3138                         lpfc_printf_log(phba,
3139                                         KERN_WARNING,
3140                                         LOG_SLI,
3141                                         "0316 Ring %d handler: unexpected "
3142                                         "ASYNC_STATUS iocb received evt_code "
3143                                         "0x%x\n",
3144                                         pring->ringno,
3145                                         irsp->un.asyncstat.evt_code);
3146                 return 1;
3147         }
3148
3149         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
3150                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
3151                 if (irsp->ulpBdeCount > 0) {
3152                         dmzbuf = lpfc_sli_get_buff(phba, pring,
3153                                         irsp->un.ulpWord[3]);
3154                         lpfc_in_buf_free(phba, dmzbuf);
3155                 }
3156
3157                 if (irsp->ulpBdeCount > 1) {
3158                         dmzbuf = lpfc_sli_get_buff(phba, pring,
3159                                         irsp->unsli3.sli3Words[3]);
3160                         lpfc_in_buf_free(phba, dmzbuf);
3161                 }
3162
3163                 if (irsp->ulpBdeCount > 2) {
3164                         dmzbuf = lpfc_sli_get_buff(phba, pring,
3165                                 irsp->unsli3.sli3Words[7]);
3166                         lpfc_in_buf_free(phba, dmzbuf);
3167                 }
3168
3169                 return 1;
3170         }
3171
3172         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3173                 if (irsp->ulpBdeCount != 0) {
3174                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
3175                                                 irsp->un.ulpWord[3]);
3176                         if (!saveq->context2)
3177                                 lpfc_printf_log(phba,
3178                                         KERN_ERR,
3179                                         LOG_SLI,
3180                                         "0341 Ring %d Cannot find buffer for "
3181                                         "an unsolicited iocb. tag 0x%x\n",
3182                                         pring->ringno,
3183                                         irsp->un.ulpWord[3]);
3184                 }
3185                 if (irsp->ulpBdeCount == 2) {
3186                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
3187                                                 irsp->unsli3.sli3Words[7]);
3188                         if (!saveq->context3)
3189                                 lpfc_printf_log(phba,
3190                                         KERN_ERR,
3191                                         LOG_SLI,
3192                                         "0342 Ring %d Cannot find buffer for an"
3193                                         " unsolicited iocb. tag 0x%x\n",
3194                                         pring->ringno,
3195                                         irsp->unsli3.sli3Words[7]);
3196                 }
3197                 list_for_each_entry(iocbq, &saveq->list, list) {
3198                         irsp = &(iocbq->iocb);
3199                         if (irsp->ulpBdeCount != 0) {
3200                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
3201                                                         irsp->un.ulpWord[3]);
3202                                 if (!iocbq->context2)
3203                                         lpfc_printf_log(phba,
3204                                                 KERN_ERR,
3205                                                 LOG_SLI,
3206                                                 "0343 Ring %d Cannot find "
3207                                                 "buffer for an unsolicited iocb"
3208                                                 ". tag 0x%x\n", pring->ringno,
3209                                                 irsp->un.ulpWord[3]);
3210                         }
3211                         if (irsp->ulpBdeCount == 2) {
3212                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
3213                                                 irsp->unsli3.sli3Words[7]);
3214                                 if (!iocbq->context3)
3215                                         lpfc_printf_log(phba,
3216                                                 KERN_ERR,
3217                                                 LOG_SLI,
3218                                                 "0344 Ring %d Cannot find "
3219                                                 "buffer for an unsolicited "
3220                                                 "iocb. tag 0x%x\n",
3221                                                 pring->ringno,
3222                                                 irsp->unsli3.sli3Words[7]);
3223                         }
3224                 }
3225         }
3226         if (irsp->ulpBdeCount != 0 &&
3227             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
3228              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
3229                 int found = 0;
3230
3231                 /* search continue save q for same XRI */
3232                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
3233                         if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
3234                                 saveq->iocb.unsli3.rcvsli3.ox_id) {
3235                                 list_add_tail(&saveq->list, &iocbq->list);
3236                                 found = 1;
3237                                 break;
3238                         }
3239                 }
3240                 if (!found)
3241                         list_add_tail(&saveq->clist,
3242                                       &pring->iocb_continue_saveq);
3243                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
3244                         list_del_init(&iocbq->clist);
3245                         saveq = iocbq;
3246                         irsp = &(saveq->iocb);
3247                 } else
3248                         return 0;
3249         }
3250         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
3251             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
3252             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
3253                 Rctl = FC_RCTL_ELS_REQ;
3254                 Type = FC_TYPE_ELS;
3255         } else {
3256                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
3257                 Rctl = w5p->hcsw.Rctl;
3258                 Type = w5p->hcsw.Type;
3259
3260                 /* Firmware Workaround */
3261                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
3262                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
3263                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
3264                         Rctl = FC_RCTL_ELS_REQ;
3265                         Type = FC_TYPE_ELS;
3266                         w5p->hcsw.Rctl = Rctl;
3267                         w5p->hcsw.Type = Type;
3268                 }
3269         }
3270
3271         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
3272                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3273                                 "0313 Ring %d handler: unexpected Rctl x%x "
3274                                 "Type x%x received\n",
3275                                 pring->ringno, Rctl, Type);
3276
3277         return 1;
3278 }
3279
3280 /**
3281  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
3282  * @phba: Pointer to HBA context object.
3283  * @pring: Pointer to driver SLI ring object.
3284  * @prspiocb: Pointer to response iocb object.
3285  *
3286  * This function looks up the iocb_lookup table to get the command iocb
3287  * corresponding to the given response iocb using the iotag of the
3288  * response iocb. The driver calls this function with the hbalock held
3289  * for SLI3 ports or the ring lock held for SLI4 ports.
3290  * This function returns the command iocb object if it finds the command
3291  * iocb else returns NULL.
3292  **/
3293 static struct lpfc_iocbq *
3294 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3295                       struct lpfc_sli_ring *pring,
3296                       struct lpfc_iocbq *prspiocb)
3297 {
3298         struct lpfc_iocbq *cmd_iocb = NULL;
3299         uint16_t iotag;
3300         spinlock_t *temp_lock = NULL;
3301         unsigned long iflag = 0;
3302
3303         if (phba->sli_rev == LPFC_SLI_REV4)
3304                 temp_lock = &pring->ring_lock;
3305         else
3306                 temp_lock = &phba->hbalock;
3307
3308         spin_lock_irqsave(temp_lock, iflag);
3309         iotag = prspiocb->iocb.ulpIoTag;
3310
3311         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3312                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
3313                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3314                         /* remove from txcmpl queue list */
3315                         list_del_init(&cmd_iocb->list);
3316                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3317                         pring->txcmplq_cnt--;
3318                         spin_unlock_irqrestore(temp_lock, iflag);
3319                         return cmd_iocb;
3320                 }
3321         }
3322
3323         spin_unlock_irqrestore(temp_lock, iflag);
3324         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3325                         "0317 iotag x%x is out of "
3326                         "range: max iotag x%x wd0 x%x\n",
3327                         iotag, phba->sli.last_iotag,
3328                         *(((uint32_t *) &prspiocb->iocb) + 7));
3329         return NULL;
3330 }
3331
3332 /**
3333  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3334  * @phba: Pointer to HBA context object.
3335  * @pring: Pointer to driver SLI ring object.
3336  * @iotag: IOCB tag.
3337  *
3338  * This function looks up the iocb_lookup table to get the command iocb
3339  * corresponding to the given iotag. The driver calls this function with
3340  * the ring lock held because this function is an SLI4 port only helper.
3341  * This function returns the command iocb object if it finds the command
3342  * iocb else returns NULL.
3343  **/
3344 static struct lpfc_iocbq *
3345 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3346                              struct lpfc_sli_ring *pring, uint16_t iotag)
3347 {
3348         struct lpfc_iocbq *cmd_iocb = NULL;
3349         spinlock_t *temp_lock = NULL;
3350         unsigned long iflag = 0;
3351
3352         if (phba->sli_rev == LPFC_SLI_REV4)
3353                 temp_lock = &pring->ring_lock;
3354         else
3355                 temp_lock = &phba->hbalock;
3356
3357         spin_lock_irqsave(temp_lock, iflag);
3358         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3359                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
3360                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3361                         /* remove from txcmpl queue list */
3362                         list_del_init(&cmd_iocb->list);
3363                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3364                         pring->txcmplq_cnt--;
3365                         spin_unlock_irqrestore(temp_lock, iflag);
3366                         return cmd_iocb;
3367                 }
3368         }
3369
3370         spin_unlock_irqrestore(temp_lock, iflag);
3371         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3372                         "0372 iotag x%x lookup error: max iotag (x%x) "
3373                         "iocb_flag x%x\n",
3374                         iotag, phba->sli.last_iotag,
3375                         cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3376         return NULL;
3377 }
3378
3379 /**
3380  * lpfc_sli_process_sol_iocb - process solicited iocb completion
3381  * @phba: Pointer to HBA context object.
3382  * @pring: Pointer to driver SLI ring object.
3383  * @saveq: Pointer to the response iocb to be processed.
3384  *
3385  * This function is called by the ring event handler for non-fcp
3386  * rings when there is a new response iocb in the response ring.
3387  * The caller is not required to hold any locks. This function
3388  * gets the command iocb associated with the response iocb and
3389  * calls the completion handler for the command iocb. If there
3390  * is no completion handler, the function will free the resources
3391  * associated with command iocb. If the response iocb is for
3392  * an already aborted command iocb, the status of the completion
3393  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3394  * This function always returns 1.
3395  **/
3396 static int
3397 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3398                           struct lpfc_iocbq *saveq)
3399 {
3400         struct lpfc_iocbq *cmdiocbp;
3401         int rc = 1;
3402         unsigned long iflag;
3403
3404         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
3405         if (cmdiocbp) {
3406                 if (cmdiocbp->iocb_cmpl) {
3407                         /*
3408                          * If an ELS command failed send an event to mgmt
3409                          * application.
3410                          */
3411                         if (saveq->iocb.ulpStatus &&
3412                              (pring->ringno == LPFC_ELS_RING) &&
3413                              (cmdiocbp->iocb.ulpCommand ==
3414                                 CMD_ELS_REQUEST64_CR))
3415                                 lpfc_send_els_failure_event(phba,
3416                                         cmdiocbp, saveq);
3417
3418                         /*
3419                          * Post all ELS completions to the worker thread.
3420                          * All other are passed to the completion callback.
3421                          */
3422                         if (pring->ringno == LPFC_ELS_RING) {
3423                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3424                                     (cmdiocbp->iocb_flag &
3425                                                         LPFC_DRIVER_ABORTED)) {
3426                                         spin_lock_irqsave(&phba->hbalock,
3427                                                           iflag);
3428                                         cmdiocbp->iocb_flag &=
3429                                                 ~LPFC_DRIVER_ABORTED;
3430                                         spin_unlock_irqrestore(&phba->hbalock,
3431                                                                iflag);
3432                                         saveq->iocb.ulpStatus =
3433                                                 IOSTAT_LOCAL_REJECT;
3434                                         saveq->iocb.un.ulpWord[4] =
3435                                                 IOERR_SLI_ABORTED;
3436
3437                                         /* Firmware could still be in progress
3438                                          * of DMAing payload, so don't free data
3439                                          * buffer till after a hbeat.
3440                                          */
3441                                         spin_lock_irqsave(&phba->hbalock,
3442                                                           iflag);
3443                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
3444                                         spin_unlock_irqrestore(&phba->hbalock,
3445                                                                iflag);
3446                                 }
3447                                 if (phba->sli_rev == LPFC_SLI_REV4) {
3448                                         if (saveq->iocb_flag &
3449                                             LPFC_EXCHANGE_BUSY) {
3450                                                 /* Set cmdiocb flag for the
3451                                                  * exchange busy so sgl (xri)
3452                                                  * will not be released until
3453                                                  * the abort xri is received
3454                                                  * from hba.
3455                                                  */
3456                                                 spin_lock_irqsave(
3457                                                         &phba->hbalock, iflag);
3458                                                 cmdiocbp->iocb_flag |=
3459                                                         LPFC_EXCHANGE_BUSY;
3460                                                 spin_unlock_irqrestore(
3461                                                         &phba->hbalock, iflag);
3462                                         }
3463                                         if (cmdiocbp->iocb_flag &
3464                                             LPFC_DRIVER_ABORTED) {
3465                                                 /*
3466                                                  * Clear LPFC_DRIVER_ABORTED
3467                                                  * bit in case it was driver
3468                                                  * initiated abort.
3469                                                  */
3470                                                 spin_lock_irqsave(
3471                                                         &phba->hbalock, iflag);
3472                                                 cmdiocbp->iocb_flag &=
3473                                                         ~LPFC_DRIVER_ABORTED;
3474                                                 spin_unlock_irqrestore(
3475                                                         &phba->hbalock, iflag);
3476                                                 cmdiocbp->iocb.ulpStatus =
3477                                                         IOSTAT_LOCAL_REJECT;
3478                                                 cmdiocbp->iocb.un.ulpWord[4] =
3479                                                         IOERR_ABORT_REQUESTED;
3480                                                 /*
3481                                                  * For SLI4, irsiocb contains
3482                                                  * NO_XRI in sli_xritag, it
3483                                                  * shall not affect releasing
3484                                                  * sgl (xri) process.
3485                                                  */
3486                                                 saveq->iocb.ulpStatus =
3487                                                         IOSTAT_LOCAL_REJECT;
3488                                                 saveq->iocb.un.ulpWord[4] =
3489                                                         IOERR_SLI_ABORTED;
3490                                                 spin_lock_irqsave(
3491                                                         &phba->hbalock, iflag);
3492                                                 saveq->iocb_flag |=
3493                                                         LPFC_DELAY_MEM_FREE;
3494                                                 spin_unlock_irqrestore(
3495                                                         &phba->hbalock, iflag);
3496                                         }
3497                                 }
3498                         }
3499                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
3500                 } else
3501                         lpfc_sli_release_iocbq(phba, cmdiocbp);
3502         } else {
3503                 /*
3504                  * Unknown initiating command based on the response iotag.
3505                  * This could be the case on the ELS ring because of
3506                  * lpfc_els_abort().
3507                  */
3508                 if (pring->ringno != LPFC_ELS_RING) {
3509                         /*
3510                          * Ring <ringno> handler: unexpected completion IoTag
3511                          * <IoTag>
3512                          */
3513                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3514                                          "0322 Ring %d handler: "
3515                                          "unexpected completion IoTag x%x "
3516                                          "Data: x%x x%x x%x x%x\n",
3517                                          pring->ringno,
3518                                          saveq->iocb.ulpIoTag,
3519                                          saveq->iocb.ulpStatus,
3520                                          saveq->iocb.un.ulpWord[4],
3521                                          saveq->iocb.ulpCommand,
3522                                          saveq->iocb.ulpContext);
3523                 }
3524         }
3525
3526         return rc;
3527 }
3528
3529 /**
3530  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
3531  * @phba: Pointer to HBA context object.
3532  * @pring: Pointer to driver SLI ring object.
3533  *
3534  * This function is called from the iocb ring event handlers when
3535  * put pointer is ahead of the get pointer for a ring. This function signal
3536  * an error attention condition to the worker thread and the worker
3537  * thread will transition the HBA to offline state.
3538  **/
3539 static void
3540 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3541 {
3542         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3543         /*
3544          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3545          * rsp ring <portRspMax>
3546          */
3547         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3548                         "0312 Ring %d handler: portRspPut %d "
3549                         "is bigger than rsp ring %d\n",
3550                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
3551                         pring->sli.sli3.numRiocb);
3552
3553         phba->link_state = LPFC_HBA_ERROR;
3554
3555         /*
3556          * All error attention handlers are posted to
3557          * worker thread
3558          */
3559         phba->work_ha |= HA_ERATT;
3560         phba->work_hs = HS_FFER3;
3561
3562         lpfc_worker_wake_up(phba);
3563
3564         return;
3565 }
3566
3567 /**
3568  * lpfc_poll_eratt - Error attention polling timer timeout handler
3569  * @t: Context to fetch pointer to address of HBA context object from.
3570  *
3571  * This function is invoked by the Error Attention polling timer when the
3572  * timer times out. It will check the SLI Error Attention register for
3573  * possible attention events. If so, it will post an Error Attention event
3574  * and wake up worker thread to process it. Otherwise, it will set up the
3575  * Error Attention polling timer for the next poll.
3576  **/
3577 void lpfc_poll_eratt(struct timer_list *t)
3578 {
3579         struct lpfc_hba *phba;
3580         uint32_t eratt = 0;
3581         uint64_t sli_intr, cnt;
3582
3583         phba = from_timer(phba, t, eratt_poll);
3584
3585         /* Here we will also keep track of interrupts per sec of the hba */
3586         sli_intr = phba->sli.slistat.sli_intr;
3587
3588         if (phba->sli.slistat.sli_prev_intr > sli_intr)
3589                 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3590                         sli_intr);
3591         else
3592                 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3593
3594         /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3595         do_div(cnt, phba->eratt_poll_interval);
3596         phba->sli.slistat.sli_ips = cnt;
3597
3598         phba->sli.slistat.sli_prev_intr = sli_intr;
3599
3600         /* Check chip HA register for error event */
3601         eratt = lpfc_sli_check_eratt(phba);
3602
3603         if (eratt)
3604                 /* Tell the worker thread there is work to do */
3605                 lpfc_worker_wake_up(phba);
3606         else
3607                 /* Restart the timer for next eratt poll */
3608                 mod_timer(&phba->eratt_poll,
3609                           jiffies +
3610                           msecs_to_jiffies(1000 * phba->eratt_poll_interval));
3611         return;
3612 }
3613
3614
3615 /**
3616  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3617  * @phba: Pointer to HBA context object.
3618  * @pring: Pointer to driver SLI ring object.
3619  * @mask: Host attention register mask for this ring.
3620  *
3621  * This function is called from the interrupt context when there is a ring
3622  * event for the fcp ring. The caller does not hold any lock.
3623  * The function processes each response iocb in the response ring until it
3624  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3625  * LE bit set. The function will call the completion handler of the command iocb
3626  * if the response iocb indicates a completion for a command iocb or it is
3627  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3628  * function if this is an unsolicited iocb.
3629  * This routine presumes LPFC_FCP_RING handling and doesn't bother
3630  * to check it explicitly.
3631  */
3632 int
3633 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3634                                 struct lpfc_sli_ring *pring, uint32_t mask)
3635 {
3636         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3637         IOCB_t *irsp = NULL;
3638         IOCB_t *entry = NULL;
3639         struct lpfc_iocbq *cmdiocbq = NULL;
3640         struct lpfc_iocbq rspiocbq;
3641         uint32_t status;
3642         uint32_t portRspPut, portRspMax;
3643         int rc = 1;
3644         lpfc_iocb_type type;
3645         unsigned long iflag;
3646         uint32_t rsp_cmpl = 0;
3647
3648         spin_lock_irqsave(&phba->hbalock, iflag);
3649         pring->stats.iocb_event++;
3650
3651         /*
3652          * The next available response entry should never exceed the maximum
3653          * entries.  If it does, treat it as an adapter hardware error.
3654          */
3655         portRspMax = pring->sli.sli3.numRiocb;
3656         portRspPut = le32_to_cpu(pgp->rspPutInx);
3657         if (unlikely(portRspPut >= portRspMax)) {
3658                 lpfc_sli_rsp_pointers_error(phba, pring);
3659                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3660                 return 1;
3661         }
3662         if (phba->fcp_ring_in_use) {
3663                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3664                 return 1;
3665         } else
3666                 phba->fcp_ring_in_use = 1;
3667
3668         rmb();
3669         while (pring->sli.sli3.rspidx != portRspPut) {
3670                 /*
3671                  * Fetch an entry off the ring and copy it into a local data
3672                  * structure.  The copy involves a byte-swap since the
3673                  * network byte order and pci byte orders are different.
3674                  */
3675                 entry = lpfc_resp_iocb(phba, pring);
3676                 phba->last_completion_time = jiffies;
3677
3678                 if (++pring->sli.sli3.rspidx >= portRspMax)
3679                         pring->sli.sli3.rspidx = 0;
3680
3681                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3682                                       (uint32_t *) &rspiocbq.iocb,
3683                                       phba->iocb_rsp_size);
3684                 INIT_LIST_HEAD(&(rspiocbq.list));
3685                 irsp = &rspiocbq.iocb;
3686
3687                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3688                 pring->stats.iocb_rsp++;
3689                 rsp_cmpl++;
3690
3691                 if (unlikely(irsp->ulpStatus)) {
3692                         /*
3693                          * If resource errors reported from HBA, reduce
3694                          * queuedepths of the SCSI device.
3695                          */
3696                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3697                             ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3698                              IOERR_NO_RESOURCES)) {
3699                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3700                                 phba->lpfc_rampdown_queue_depth(phba);
3701                                 spin_lock_irqsave(&phba->hbalock, iflag);
3702                         }
3703
3704                         /* Rsp ring <ringno> error: IOCB */
3705                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3706                                         "0336 Rsp Ring %d error: IOCB Data: "
3707                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3708                                         pring->ringno,
3709                                         irsp->un.ulpWord[0],
3710                                         irsp->un.ulpWord[1],
3711                                         irsp->un.ulpWord[2],
3712                                         irsp->un.ulpWord[3],
3713                                         irsp->un.ulpWord[4],
3714                                         irsp->un.ulpWord[5],
3715                                         *(uint32_t *)&irsp->un1,
3716                                         *((uint32_t *)&irsp->un1 + 1));
3717                 }
3718
3719                 switch (type) {
3720                 case LPFC_ABORT_IOCB:
3721                 case LPFC_SOL_IOCB:
3722                         /*
3723                          * Idle exchange closed via ABTS from port.  No iocb
3724                          * resources need to be recovered.
3725                          */
3726                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3727                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3728                                                 "0333 IOCB cmd 0x%x"
3729                                                 " processed. Skipping"
3730                                                 " completion\n",
3731                                                 irsp->ulpCommand);
3732                                 break;
3733                         }
3734
3735                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3736                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3737                                                          &rspiocbq);
3738                         spin_lock_irqsave(&phba->hbalock, iflag);
3739                         if (unlikely(!cmdiocbq))
3740                                 break;
3741                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3742                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3743                         if (cmdiocbq->iocb_cmpl) {
3744                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3745                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3746                                                       &rspiocbq);
3747                                 spin_lock_irqsave(&phba->hbalock, iflag);
3748                         }
3749                         break;
3750                 case LPFC_UNSOL_IOCB:
3751                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3752                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3753                         spin_lock_irqsave(&phba->hbalock, iflag);
3754                         break;
3755                 default:
3756                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3757                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3758                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3759                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3760                                        MAX_MSG_DATA);
3761                                 dev_warn(&((phba->pcidev)->dev),
3762                                          "lpfc%d: %s\n",
3763                                          phba->brd_no, adaptermsg);
3764                         } else {
3765                                 /* Unknown IOCB command */
3766                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3767                                                 "0334 Unknown IOCB command "
3768                                                 "Data: x%x, x%x x%x x%x x%x\n",
3769                                                 type, irsp->ulpCommand,
3770                                                 irsp->ulpStatus,
3771                                                 irsp->ulpIoTag,
3772                                                 irsp->ulpContext);
3773                         }
3774                         break;
3775                 }
3776
3777                 /*
3778                  * The response IOCB has been processed.  Update the ring
3779                  * pointer in SLIM.  If the port response put pointer has not
3780                  * been updated, sync the pgp->rspPutInx and fetch the new port
3781                  * response put pointer.
3782                  */
3783                 writel(pring->sli.sli3.rspidx,
3784                         &phba->host_gp[pring->ringno].rspGetInx);
3785
3786                 if (pring->sli.sli3.rspidx == portRspPut)
3787                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3788         }
3789
3790         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3791                 pring->stats.iocb_rsp_full++;
3792                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3793                 writel(status, phba->CAregaddr);
3794                 readl(phba->CAregaddr);
3795         }
3796         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3797                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3798                 pring->stats.iocb_cmd_empty++;
3799
3800                 /* Force update of the local copy of cmdGetInx */
3801                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3802                 lpfc_sli_resume_iocb(phba, pring);
3803
3804                 if ((pring->lpfc_sli_cmd_available))
3805                         (pring->lpfc_sli_cmd_available) (phba, pring);
3806
3807         }
3808
3809         phba->fcp_ring_in_use = 0;
3810         spin_unlock_irqrestore(&phba->hbalock, iflag);
3811         return rc;
3812 }
3813
3814 /**
3815  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3816  * @phba: Pointer to HBA context object.
3817  * @pring: Pointer to driver SLI ring object.
3818  * @rspiocbp: Pointer to driver response IOCB object.
3819  *
3820  * This function is called from the worker thread when there is a slow-path
3821  * response IOCB to process. This function chains all the response iocbs until
3822  * seeing the iocb with the LE bit set. The function will call
3823  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3824  * completion of a command iocb. The function will call the
3825  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3826  * The function frees the resources or calls the completion handler if this
3827  * iocb is an abort completion. The function returns NULL when the response
3828  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3829  * this function shall chain the iocb on to the iocb_continueq and return the
3830  * response iocb passed in.
3831  **/
3832 static struct lpfc_iocbq *
3833 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3834                         struct lpfc_iocbq *rspiocbp)
3835 {
3836         struct lpfc_iocbq *saveq;
3837         struct lpfc_iocbq *cmdiocbp;
3838         struct lpfc_iocbq *next_iocb;
3839         IOCB_t *irsp = NULL;
3840         uint32_t free_saveq;
3841         uint8_t iocb_cmd_type;
3842         lpfc_iocb_type type;
3843         unsigned long iflag;
3844         int rc;
3845
3846         spin_lock_irqsave(&phba->hbalock, iflag);
3847         /* First add the response iocb to the countinueq list */
3848         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3849         pring->iocb_continueq_cnt++;
3850
3851         /* Now, determine whether the list is completed for processing */
3852         irsp = &rspiocbp->iocb;
3853         if (irsp->ulpLe) {
3854                 /*
3855                  * By default, the driver expects to free all resources
3856                  * associated with this iocb completion.
3857                  */
3858                 free_saveq = 1;
3859                 saveq = list_get_first(&pring->iocb_continueq,
3860                                        struct lpfc_iocbq, list);
3861                 irsp = &(saveq->iocb);
3862                 list_del_init(&pring->iocb_continueq);
3863                 pring->iocb_continueq_cnt = 0;
3864
3865                 pring->stats.iocb_rsp++;
3866
3867                 /*
3868                  * If resource errors reported from HBA, reduce
3869                  * queuedepths of the SCSI device.
3870                  */
3871                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3872                     ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3873                      IOERR_NO_RESOURCES)) {
3874                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3875                         phba->lpfc_rampdown_queue_depth(phba);
3876                         spin_lock_irqsave(&phba->hbalock, iflag);
3877                 }
3878
3879                 if (irsp->ulpStatus) {
3880                         /* Rsp ring <ringno> error: IOCB */
3881                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3882                                         "0328 Rsp Ring %d error: "
3883                                         "IOCB Data: "
3884                                         "x%x x%x x%x x%x "
3885                                         "x%x x%x x%x x%x "
3886                                         "x%x x%x x%x x%x "
3887                                         "x%x x%x x%x x%x\n",
3888                                         pring->ringno,
3889                                         irsp->un.ulpWord[0],
3890                                         irsp->un.ulpWord[1],
3891                                         irsp->un.ulpWord[2],
3892                                         irsp->un.ulpWord[3],
3893                                         irsp->un.ulpWord[4],
3894                                         irsp->un.ulpWord[5],
3895                                         *(((uint32_t *) irsp) + 6),
3896                                         *(((uint32_t *) irsp) + 7),
3897                                         *(((uint32_t *) irsp) + 8),
3898                                         *(((uint32_t *) irsp) + 9),
3899                                         *(((uint32_t *) irsp) + 10),
3900                                         *(((uint32_t *) irsp) + 11),
3901                                         *(((uint32_t *) irsp) + 12),
3902                                         *(((uint32_t *) irsp) + 13),
3903                                         *(((uint32_t *) irsp) + 14),
3904                                         *(((uint32_t *) irsp) + 15));
3905                 }
3906
3907                 /*
3908                  * Fetch the IOCB command type and call the correct completion
3909                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3910                  * get freed back to the lpfc_iocb_list by the discovery
3911                  * kernel thread.
3912                  */
3913                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3914                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3915                 switch (type) {
3916                 case LPFC_SOL_IOCB:
3917                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3918                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3919                         spin_lock_irqsave(&phba->hbalock, iflag);
3920                         break;
3921
3922                 case LPFC_UNSOL_IOCB:
3923                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3924                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3925                         spin_lock_irqsave(&phba->hbalock, iflag);
3926                         if (!rc)
3927                                 free_saveq = 0;
3928                         break;
3929
3930                 case LPFC_ABORT_IOCB:
3931                         cmdiocbp = NULL;
3932                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX) {
3933                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3934                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3935                                                                  saveq);
3936                                 spin_lock_irqsave(&phba->hbalock, iflag);
3937                         }
3938                         if (cmdiocbp) {
3939                                 /* Call the specified completion routine */
3940                                 if (cmdiocbp->iocb_cmpl) {
3941                                         spin_unlock_irqrestore(&phba->hbalock,
3942                                                                iflag);
3943                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3944                                                               saveq);
3945                                         spin_lock_irqsave(&phba->hbalock,
3946                                                           iflag);
3947                                 } else
3948                                         __lpfc_sli_release_iocbq(phba,
3949                                                                  cmdiocbp);
3950                         }
3951                         break;
3952
3953                 case LPFC_UNKNOWN_IOCB:
3954                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3955                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3956                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3957                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3958                                        MAX_MSG_DATA);
3959                                 dev_warn(&((phba->pcidev)->dev),
3960                                          "lpfc%d: %s\n",
3961                                          phba->brd_no, adaptermsg);
3962                         } else {
3963                                 /* Unknown IOCB command */
3964                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3965                                                 "0335 Unknown IOCB "
3966                                                 "command Data: x%x "
3967                                                 "x%x x%x x%x\n",
3968                                                 irsp->ulpCommand,
3969                                                 irsp->ulpStatus,
3970                                                 irsp->ulpIoTag,
3971                                                 irsp->ulpContext);
3972                         }
3973                         break;
3974                 }
3975
3976                 if (free_saveq) {
3977                         list_for_each_entry_safe(rspiocbp, next_iocb,
3978                                                  &saveq->list, list) {
3979                                 list_del_init(&rspiocbp->list);
3980                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3981                         }
3982                         __lpfc_sli_release_iocbq(phba, saveq);
3983                 }
3984                 rspiocbp = NULL;
3985         }
3986         spin_unlock_irqrestore(&phba->hbalock, iflag);
3987         return rspiocbp;
3988 }
3989
3990 /**
3991  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3992  * @phba: Pointer to HBA context object.
3993  * @pring: Pointer to driver SLI ring object.
3994  * @mask: Host attention register mask for this ring.
3995  *
3996  * This routine wraps the actual slow_ring event process routine from the
3997  * API jump table function pointer from the lpfc_hba struct.
3998  **/
3999 void
4000 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
4001                                 struct lpfc_sli_ring *pring, uint32_t mask)
4002 {
4003         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
4004 }
4005
4006 /**
4007  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
4008  * @phba: Pointer to HBA context object.
4009  * @pring: Pointer to driver SLI ring object.
4010  * @mask: Host attention register mask for this ring.
4011  *
4012  * This function is called from the worker thread when there is a ring event
4013  * for non-fcp rings. The caller does not hold any lock. The function will
4014  * remove each response iocb in the response ring and calls the handle
4015  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4016  **/
4017 static void
4018 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
4019                                    struct lpfc_sli_ring *pring, uint32_t mask)
4020 {
4021         struct lpfc_pgp *pgp;
4022         IOCB_t *entry;
4023         IOCB_t *irsp = NULL;
4024         struct lpfc_iocbq *rspiocbp = NULL;
4025         uint32_t portRspPut, portRspMax;
4026         unsigned long iflag;
4027         uint32_t status;
4028
4029         pgp = &phba->port_gp[pring->ringno];
4030         spin_lock_irqsave(&phba->hbalock, iflag);
4031         pring->stats.iocb_event++;
4032
4033         /*
4034          * The next available response entry should never exceed the maximum
4035          * entries.  If it does, treat it as an adapter hardware error.
4036          */
4037         portRspMax = pring->sli.sli3.numRiocb;
4038         portRspPut = le32_to_cpu(pgp->rspPutInx);
4039         if (portRspPut >= portRspMax) {
4040                 /*
4041                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
4042                  * rsp ring <portRspMax>
4043                  */
4044                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4045                                 "0303 Ring %d handler: portRspPut %d "
4046                                 "is bigger than rsp ring %d\n",
4047                                 pring->ringno, portRspPut, portRspMax);
4048
4049                 phba->link_state = LPFC_HBA_ERROR;
4050                 spin_unlock_irqrestore(&phba->hbalock, iflag);
4051
4052                 phba->work_hs = HS_FFER3;
4053                 lpfc_handle_eratt(phba);
4054
4055                 return;
4056         }
4057
4058         rmb();
4059         while (pring->sli.sli3.rspidx != portRspPut) {
4060                 /*
4061                  * Build a completion list and call the appropriate handler.
4062                  * The process is to get the next available response iocb, get
4063                  * a free iocb from the list, copy the response data into the
4064                  * free iocb, insert to the continuation list, and update the
4065                  * next response index to slim.  This process makes response
4066                  * iocb's in the ring available to DMA as fast as possible but
4067                  * pays a penalty for a copy operation.  Since the iocb is
4068                  * only 32 bytes, this penalty is considered small relative to
4069                  * the PCI reads for register values and a slim write.  When
4070                  * the ulpLe field is set, the entire Command has been
4071                  * received.
4072                  */
4073                 entry = lpfc_resp_iocb(phba, pring);
4074
4075                 phba->last_completion_time = jiffies;
4076                 rspiocbp = __lpfc_sli_get_iocbq(phba);
4077                 if (rspiocbp == NULL) {
4078                         printk(KERN_ERR "%s: out of buffers! Failing "
4079                                "completion.\n", __func__);
4080                         break;
4081                 }
4082
4083                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
4084                                       phba->iocb_rsp_size);
4085                 irsp = &rspiocbp->iocb;
4086
4087                 if (++pring->sli.sli3.rspidx >= portRspMax)
4088                         pring->sli.sli3.rspidx = 0;
4089
4090                 if (pring->ringno == LPFC_ELS_RING) {
4091                         lpfc_debugfs_slow_ring_trc(phba,
4092                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
4093                                 *(((uint32_t *) irsp) + 4),
4094                                 *(((uint32_t *) irsp) + 6),
4095                                 *(((uint32_t *) irsp) + 7));
4096                 }
4097
4098                 writel(pring->sli.sli3.rspidx,
4099                         &phba->host_gp[pring->ringno].rspGetInx);
4100
4101                 spin_unlock_irqrestore(&phba->hbalock, iflag);
4102                 /* Handle the response IOCB */
4103                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
4104                 spin_lock_irqsave(&phba->hbalock, iflag);
4105
4106                 /*
4107                  * If the port response put pointer has not been updated, sync
4108                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
4109                  * response put pointer.
4110                  */
4111                 if (pring->sli.sli3.rspidx == portRspPut) {
4112                         portRspPut = le32_to_cpu(pgp->rspPutInx);
4113                 }
4114         } /* while (pring->sli.sli3.rspidx != portRspPut) */
4115
4116         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
4117                 /* At least one response entry has been freed */
4118                 pring->stats.iocb_rsp_full++;
4119                 /* SET RxRE_RSP in Chip Att register */
4120                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4121                 writel(status, phba->CAregaddr);
4122                 readl(phba->CAregaddr); /* flush */
4123         }
4124         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4125                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4126                 pring->stats.iocb_cmd_empty++;
4127
4128                 /* Force update of the local copy of cmdGetInx */
4129                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4130                 lpfc_sli_resume_iocb(phba, pring);
4131
4132                 if ((pring->lpfc_sli_cmd_available))
4133                         (pring->lpfc_sli_cmd_available) (phba, pring);
4134
4135         }
4136
4137         spin_unlock_irqrestore(&phba->hbalock, iflag);
4138         return;
4139 }
4140
4141 /**
4142  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
4143  * @phba: Pointer to HBA context object.
4144  * @pring: Pointer to driver SLI ring object.
4145  * @mask: Host attention register mask for this ring.
4146  *
4147  * This function is called from the worker thread when there is a pending
4148  * ELS response iocb on the driver internal slow-path response iocb worker
4149  * queue. The caller does not hold any lock. The function will remove each
4150  * response iocb from the response worker queue and calls the handle
4151  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4152  **/
4153 static void
4154 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4155                                    struct lpfc_sli_ring *pring, uint32_t mask)
4156 {
4157         struct lpfc_iocbq *irspiocbq;
4158         struct hbq_dmabuf *dmabuf;
4159         struct lpfc_cq_event *cq_event;
4160         unsigned long iflag;
4161         int count = 0;
4162
4163         spin_lock_irqsave(&phba->hbalock, iflag);
4164         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
4165         spin_unlock_irqrestore(&phba->hbalock, iflag);
4166         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4167                 /* Get the response iocb from the head of work queue */
4168                 spin_lock_irqsave(&phba->hbalock, iflag);
4169                 list_remove_head(&phba->sli4_hba.sp_queue_event,
4170                                  cq_event, struct lpfc_cq_event, list);
4171                 spin_unlock_irqrestore(&phba->hbalock, iflag);
4172
4173                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4174                 case CQE_CODE_COMPL_WQE:
4175                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4176                                                  cq_event);
4177                         /* Translate ELS WCQE to response IOCBQ */
4178                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
4179                                                                    irspiocbq);
4180                         if (irspiocbq)
4181                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
4182                                                            irspiocbq);
4183                         count++;
4184                         break;
4185                 case CQE_CODE_RECEIVE:
4186                 case CQE_CODE_RECEIVE_V1:
4187                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
4188                                               cq_event);
4189                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
4190                         count++;
4191                         break;
4192                 default:
4193                         break;
4194                 }
4195
4196                 /* Limit the number of events to 64 to avoid soft lockups */
4197                 if (count == 64)
4198                         break;
4199         }
4200 }
4201
4202 /**
4203  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
4204  * @phba: Pointer to HBA context object.
4205  * @pring: Pointer to driver SLI ring object.
4206  *
4207  * This function aborts all iocbs in the given ring and frees all the iocb
4208  * objects in txq. This function issues an abort iocb for all the iocb commands
4209  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4210  * the return of this function. The caller is not required to hold any locks.
4211  **/
4212 void
4213 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4214 {
4215         LIST_HEAD(completions);
4216         struct lpfc_iocbq *iocb, *next_iocb;
4217
4218         if (pring->ringno == LPFC_ELS_RING) {
4219                 lpfc_fabric_abort_hba(phba);
4220         }
4221
4222         /* Error everything on txq and txcmplq
4223          * First do the txq.
4224          */
4225         if (phba->sli_rev >= LPFC_SLI_REV4) {
4226                 spin_lock_irq(&pring->ring_lock);
4227                 list_splice_init(&pring->txq, &completions);
4228                 pring->txq_cnt = 0;
4229                 spin_unlock_irq(&pring->ring_lock);
4230
4231                 spin_lock_irq(&phba->hbalock);
4232                 /* Next issue ABTS for everything on the txcmplq */
4233                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
4234                         lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
4235                 spin_unlock_irq(&phba->hbalock);
4236         } else {
4237                 spin_lock_irq(&phba->hbalock);
4238                 list_splice_init(&pring->txq, &completions);
4239                 pring->txq_cnt = 0;
4240
4241                 /* Next issue ABTS for everything on the txcmplq */
4242                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
4243                         lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
4244                 spin_unlock_irq(&phba->hbalock);
4245         }
4246         /* Make sure HBA is alive */
4247         lpfc_issue_hb_tmo(phba);
4248
4249         /* Cancel all the IOCBs from the completions list */
4250         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4251                               IOERR_SLI_ABORTED);
4252 }
4253
4254 /**
4255  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4256  * @phba: Pointer to HBA context object.
4257  *
4258  * This function aborts all iocbs in FCP rings and frees all the iocb
4259  * objects in txq. This function issues an abort iocb for all the iocb commands
4260  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4261  * the return of this function. The caller is not required to hold any locks.
4262  **/
4263 void
4264 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4265 {
4266         struct lpfc_sli *psli = &phba->sli;
4267         struct lpfc_sli_ring  *pring;
4268         uint32_t i;
4269
4270         /* Look on all the FCP Rings for the iotag */
4271         if (phba->sli_rev >= LPFC_SLI_REV4) {
4272                 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4273                         pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4274                         lpfc_sli_abort_iocb_ring(phba, pring);
4275                 }
4276         } else {
4277                 pring = &psli->sli3_ring[LPFC_FCP_RING];
4278                 lpfc_sli_abort_iocb_ring(phba, pring);
4279         }
4280 }
4281
4282 /**
4283  * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
4284  * @phba: Pointer to HBA context object.
4285  *
4286  * This function flushes all iocbs in the IO ring and frees all the iocb
4287  * objects in txq and txcmplq. This function will not issue abort iocbs
4288  * for all the iocb commands in txcmplq, they will just be returned with
4289  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4290  * slot has been permanently disabled.
4291  **/
4292 void
4293 lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
4294 {
4295         LIST_HEAD(txq);
4296         LIST_HEAD(txcmplq);
4297         struct lpfc_sli *psli = &phba->sli;
4298         struct lpfc_sli_ring  *pring;
4299         uint32_t i;
4300         struct lpfc_iocbq *piocb, *next_iocb;
4301
4302         spin_lock_irq(&phba->hbalock);
4303         if (phba->hba_flag & HBA_IOQ_FLUSH ||
4304             !phba->sli4_hba.hdwq) {
4305                 spin_unlock_irq(&phba->hbalock);
4306                 return;
4307         }
4308         /* Indicate the I/O queues are flushed */
4309         phba->hba_flag |= HBA_IOQ_FLUSH;
4310         spin_unlock_irq(&phba->hbalock);
4311
4312         /* Look on all the FCP Rings for the iotag */
4313         if (phba->sli_rev >= LPFC_SLI_REV4) {
4314                 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4315                         pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4316
4317                         spin_lock_irq(&pring->ring_lock);
4318                         /* Retrieve everything on txq */
4319                         list_splice_init(&pring->txq, &txq);
4320                         list_for_each_entry_safe(piocb, next_iocb,
4321                                                  &pring->txcmplq, list)
4322                                 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4323                         /* Retrieve everything on the txcmplq */
4324                         list_splice_init(&pring->txcmplq, &txcmplq);
4325                         pring->txq_cnt = 0;
4326                         pring->txcmplq_cnt = 0;
4327                         spin_unlock_irq(&pring->ring_lock);
4328
4329                         /* Flush the txq */
4330                         lpfc_sli_cancel_iocbs(phba, &txq,
4331                                               IOSTAT_LOCAL_REJECT,
4332                                               IOERR_SLI_DOWN);
4333                         /* Flush the txcmpq */
4334                         lpfc_sli_cancel_iocbs(phba, &txcmplq,
4335                                               IOSTAT_LOCAL_REJECT,
4336                                               IOERR_SLI_DOWN);
4337                 }
4338         } else {
4339                 pring = &psli->sli3_ring[LPFC_FCP_RING];
4340
4341                 spin_lock_irq(&phba->hbalock);
4342                 /* Retrieve everything on txq */
4343                 list_splice_init(&pring->txq, &txq);
4344                 list_for_each_entry_safe(piocb, next_iocb,
4345                                          &pring->txcmplq, list)
4346                         piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4347                 /* Retrieve everything on the txcmplq */
4348                 list_splice_init(&pring->txcmplq, &txcmplq);
4349                 pring->txq_cnt = 0;
4350                 pring->txcmplq_cnt = 0;
4351                 spin_unlock_irq(&phba->hbalock);
4352
4353                 /* Flush the txq */
4354                 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4355                                       IOERR_SLI_DOWN);
4356                 /* Flush the txcmpq */
4357                 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4358                                       IOERR_SLI_DOWN);
4359         }
4360 }
4361
4362 /**
4363  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
4364  * @phba: Pointer to HBA context object.
4365  * @mask: Bit mask to be checked.
4366  *
4367  * This function reads the host status register and compares
4368  * with the provided bit mask to check if HBA completed
4369  * the restart. This function will wait in a loop for the
4370  * HBA to complete restart. If the HBA does not restart within
4371  * 15 iterations, the function will reset the HBA again. The
4372  * function returns 1 when HBA fail to restart otherwise returns
4373  * zero.
4374  **/
4375 static int
4376 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
4377 {
4378         uint32_t status;
4379         int i = 0;
4380         int retval = 0;
4381
4382         /* Read the HBA Host Status Register */
4383         if (lpfc_readl(phba->HSregaddr, &status))
4384                 return 1;
4385
4386         phba->hba_flag |= HBA_NEEDS_CFG_PORT;
4387
4388         /*
4389          * Check status register every 100ms for 5 retries, then every
4390          * 500ms for 5, then every 2.5 sec for 5, then reset board and
4391          * every 2.5 sec for 4.
4392          * Break our of the loop if errors occurred during init.
4393          */
4394         while (((status & mask) != mask) &&
4395                !(status & HS_FFERM) &&
4396                i++ < 20) {
4397
4398                 if (i <= 5)
4399                         msleep(10);
4400                 else if (i <= 10)
4401                         msleep(500);
4402                 else
4403                         msleep(2500);
4404
4405                 if (i == 15) {
4406                                 /* Do post */
4407                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4408                         lpfc_sli_brdrestart(phba);
4409                 }
4410                 /* Read the HBA Host Status Register */
4411                 if (lpfc_readl(phba->HSregaddr, &status)) {
4412                         retval = 1;
4413                         break;
4414                 }
4415         }
4416
4417         /* Check to see if any errors occurred during init */
4418         if ((status & HS_FFERM) || (i >= 20)) {
4419                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4420                                 "2751 Adapter failed to restart, "
4421                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4422                                 status,
4423                                 readl(phba->MBslimaddr + 0xa8),
4424                                 readl(phba->MBslimaddr + 0xac));
4425                 phba->link_state = LPFC_HBA_ERROR;
4426                 retval = 1;
4427         }
4428
4429         return retval;
4430 }
4431
4432 /**
4433  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4434  * @phba: Pointer to HBA context object.
4435  * @mask: Bit mask to be checked.
4436  *
4437  * This function checks the host status register to check if HBA is
4438  * ready. This function will wait in a loop for the HBA to be ready
4439  * If the HBA is not ready , the function will will reset the HBA PCI
4440  * function again. The function returns 1 when HBA fail to be ready
4441  * otherwise returns zero.
4442  **/
4443 static int
4444 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4445 {
4446         uint32_t status;
4447         int retval = 0;
4448
4449         /* Read the HBA Host Status Register */
4450         status = lpfc_sli4_post_status_check(phba);
4451
4452         if (status) {
4453                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4454                 lpfc_sli_brdrestart(phba);
4455                 status = lpfc_sli4_post_status_check(phba);
4456         }
4457
4458         /* Check to see if any errors occurred during init */
4459         if (status) {
4460                 phba->link_state = LPFC_HBA_ERROR;
4461                 retval = 1;
4462         } else
4463                 phba->sli4_hba.intr_enable = 0;
4464
4465         return retval;
4466 }
4467
4468 /**
4469  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4470  * @phba: Pointer to HBA context object.
4471  * @mask: Bit mask to be checked.
4472  *
4473  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4474  * from the API jump table function pointer from the lpfc_hba struct.
4475  **/
4476 int
4477 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4478 {
4479         return phba->lpfc_sli_brdready(phba, mask);
4480 }
4481
4482 #define BARRIER_TEST_PATTERN (0xdeadbeef)
4483
4484 /**
4485  * lpfc_reset_barrier - Make HBA ready for HBA reset
4486  * @phba: Pointer to HBA context object.
4487  *
4488  * This function is called before resetting an HBA. This function is called
4489  * with hbalock held and requests HBA to quiesce DMAs before a reset.
4490  **/
4491 void lpfc_reset_barrier(struct lpfc_hba *phba)
4492 {
4493         uint32_t __iomem *resp_buf;
4494         uint32_t __iomem *mbox_buf;
4495         volatile uint32_t mbox;
4496         uint32_t hc_copy, ha_copy, resp_data;
4497         int  i;
4498         uint8_t hdrtype;
4499
4500         lockdep_assert_held(&phba->hbalock);
4501
4502         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4503         if (hdrtype != 0x80 ||
4504             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4505              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4506                 return;
4507
4508         /*
4509          * Tell the other part of the chip to suspend temporarily all
4510          * its DMA activity.
4511          */
4512         resp_buf = phba->MBslimaddr;
4513
4514         /* Disable the error attention */
4515         if (lpfc_readl(phba->HCregaddr, &hc_copy))
4516                 return;
4517         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4518         readl(phba->HCregaddr); /* flush */
4519         phba->link_flag |= LS_IGNORE_ERATT;
4520
4521         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4522                 return;
4523         if (ha_copy & HA_ERATT) {
4524                 /* Clear Chip error bit */
4525                 writel(HA_ERATT, phba->HAregaddr);
4526                 phba->pport->stopped = 1;
4527         }
4528
4529         mbox = 0;
4530         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4531         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4532
4533         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4534         mbox_buf = phba->MBslimaddr;
4535         writel(mbox, mbox_buf);
4536
4537         for (i = 0; i < 50; i++) {
4538                 if (lpfc_readl((resp_buf + 1), &resp_data))
4539                         return;
4540                 if (resp_data != ~(BARRIER_TEST_PATTERN))
4541                         mdelay(1);
4542                 else
4543                         break;
4544         }
4545         resp_data = 0;
4546         if (lpfc_readl((resp_buf + 1), &resp_data))
4547                 return;
4548         if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4549                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4550                     phba->pport->stopped)
4551                         goto restore_hc;
4552                 else
4553                         goto clear_errat;
4554         }
4555
4556         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
4557         resp_data = 0;
4558         for (i = 0; i < 500; i++) {
4559                 if (lpfc_readl(resp_buf, &resp_data))
4560                         return;
4561                 if (resp_data != mbox)
4562                         mdelay(1);
4563                 else
4564                         break;
4565         }
4566
4567 clear_errat:
4568
4569         while (++i < 500) {
4570                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4571                         return;
4572                 if (!(ha_copy & HA_ERATT))
4573                         mdelay(1);
4574                 else
4575                         break;
4576         }
4577
4578         if (readl(phba->HAregaddr) & HA_ERATT) {
4579                 writel(HA_ERATT, phba->HAregaddr);
4580                 phba->pport->stopped = 1;
4581         }
4582
4583 restore_hc:
4584         phba->link_flag &= ~LS_IGNORE_ERATT;
4585         writel(hc_copy, phba->HCregaddr);
4586         readl(phba->HCregaddr); /* flush */
4587 }
4588
4589 /**
4590  * lpfc_sli_brdkill - Issue a kill_board mailbox command
4591  * @phba: Pointer to HBA context object.
4592  *
4593  * This function issues a kill_board mailbox command and waits for
4594  * the error attention interrupt. This function is called for stopping
4595  * the firmware processing. The caller is not required to hold any
4596  * locks. This function calls lpfc_hba_down_post function to free
4597  * any pending commands after the kill. The function will return 1 when it
4598  * fails to kill the board else will return 0.
4599  **/
4600 int
4601 lpfc_sli_brdkill(struct lpfc_hba *phba)
4602 {
4603         struct lpfc_sli *psli;
4604         LPFC_MBOXQ_t *pmb;
4605         uint32_t status;
4606         uint32_t ha_copy;
4607         int retval;
4608         int i = 0;
4609
4610         psli = &phba->sli;
4611
4612         /* Kill HBA */
4613         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4614                         "0329 Kill HBA Data: x%x x%x\n",
4615                         phba->pport->port_state, psli->sli_flag);
4616
4617         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4618         if (!pmb)
4619                 return 1;
4620
4621         /* Disable the error attention */
4622         spin_lock_irq(&phba->hbalock);
4623         if (lpfc_readl(phba->HCregaddr, &status)) {
4624                 spin_unlock_irq(&phba->hbalock);
4625                 mempool_free(pmb, phba->mbox_mem_pool);
4626                 return 1;
4627         }
4628         status &= ~HC_ERINT_ENA;
4629         writel(status, phba->HCregaddr);
4630         readl(phba->HCregaddr); /* flush */
4631         phba->link_flag |= LS_IGNORE_ERATT;
4632         spin_unlock_irq(&phba->hbalock);
4633
4634         lpfc_kill_board(phba, pmb);
4635         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4636         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4637
4638         if (retval != MBX_SUCCESS) {
4639                 if (retval != MBX_BUSY)
4640                         mempool_free(pmb, phba->mbox_mem_pool);
4641                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4642                                 "2752 KILL_BOARD command failed retval %d\n",
4643                                 retval);
4644                 spin_lock_irq(&phba->hbalock);
4645                 phba->link_flag &= ~LS_IGNORE_ERATT;
4646                 spin_unlock_irq(&phba->hbalock);
4647                 return 1;
4648         }
4649
4650         spin_lock_irq(&phba->hbalock);
4651         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4652         spin_unlock_irq(&phba->hbalock);
4653
4654         mempool_free(pmb, phba->mbox_mem_pool);
4655
4656         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4657          * attention every 100ms for 3 seconds. If we don't get ERATT after
4658          * 3 seconds we still set HBA_ERROR state because the status of the
4659          * board is now undefined.
4660          */
4661         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4662                 return 1;
4663         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4664                 mdelay(100);
4665                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4666                         return 1;
4667         }
4668
4669         del_timer_sync(&psli->mbox_tmo);
4670         if (ha_copy & HA_ERATT) {
4671                 writel(HA_ERATT, phba->HAregaddr);
4672                 phba->pport->stopped = 1;
4673         }
4674         spin_lock_irq(&phba->hbalock);
4675         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4676         psli->mbox_active = NULL;
4677         phba->link_flag &= ~LS_IGNORE_ERATT;
4678         spin_unlock_irq(&phba->hbalock);
4679
4680         lpfc_hba_down_post(phba);
4681         phba->link_state = LPFC_HBA_ERROR;
4682
4683         return ha_copy & HA_ERATT ? 0 : 1;
4684 }
4685
4686 /**
4687  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
4688  * @phba: Pointer to HBA context object.
4689  *
4690  * This function resets the HBA by writing HC_INITFF to the control
4691  * register. After the HBA resets, this function resets all the iocb ring
4692  * indices. This function disables PCI layer parity checking during
4693  * the reset.
4694  * This function returns 0 always.
4695  * The caller is not required to hold any locks.
4696  **/
4697 int
4698 lpfc_sli_brdreset(struct lpfc_hba *phba)
4699 {
4700         struct lpfc_sli *psli;
4701         struct lpfc_sli_ring *pring;
4702         uint16_t cfg_value;
4703         int i;
4704
4705         psli = &phba->sli;
4706
4707         /* Reset HBA */
4708         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4709                         "0325 Reset HBA Data: x%x x%x\n",
4710                         (phba->pport) ? phba->pport->port_state : 0,
4711                         psli->sli_flag);
4712
4713         /* perform board reset */
4714         phba->fc_eventTag = 0;
4715         phba->link_events = 0;
4716         phba->hba_flag |= HBA_NEEDS_CFG_PORT;
4717         if (phba->pport) {
4718                 phba->pport->fc_myDID = 0;
4719                 phba->pport->fc_prevDID = 0;
4720         }
4721
4722         /* Turn off parity checking and serr during the physical reset */
4723         if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
4724                 return -EIO;
4725
4726         pci_write_config_word(phba->pcidev, PCI_COMMAND,
4727                               (cfg_value &
4728                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4729
4730         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4731
4732         /* Now toggle INITFF bit in the Host Control Register */
4733         writel(HC_INITFF, phba->HCregaddr);
4734         mdelay(1);
4735         readl(phba->HCregaddr); /* flush */
4736         writel(0, phba->HCregaddr);
4737         readl(phba->HCregaddr); /* flush */
4738
4739         /* Restore PCI cmd register */
4740         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4741
4742         /* Initialize relevant SLI info */
4743         for (i = 0; i < psli->num_rings; i++) {
4744                 pring = &psli->sli3_ring[i];
4745                 pring->flag = 0;
4746                 pring->sli.sli3.rspidx = 0;
4747                 pring->sli.sli3.next_cmdidx  = 0;
4748                 pring->sli.sli3.local_getidx = 0;
4749                 pring->sli.sli3.cmdidx = 0;
4750                 pring->missbufcnt = 0;
4751         }
4752
4753         phba->link_state = LPFC_WARM_START;
4754         return 0;
4755 }
4756
4757 /**
4758  * lpfc_sli4_brdreset - Reset a sli-4 HBA
4759  * @phba: Pointer to HBA context object.
4760  *
4761  * This function resets a SLI4 HBA. This function disables PCI layer parity
4762  * checking during resets the device. The caller is not required to hold
4763  * any locks.
4764  *
4765  * This function returns 0 on success else returns negative error code.
4766  **/
4767 int
4768 lpfc_sli4_brdreset(struct lpfc_hba *phba)
4769 {
4770         struct lpfc_sli *psli = &phba->sli;
4771         uint16_t cfg_value;
4772         int rc = 0;
4773
4774         /* Reset HBA */
4775         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4776                         "0295 Reset HBA Data: x%x x%x x%x\n",
4777                         phba->pport->port_state, psli->sli_flag,
4778                         phba->hba_flag);
4779
4780         /* perform board reset */
4781         phba->fc_eventTag = 0;
4782         phba->link_events = 0;
4783         phba->pport->fc_myDID = 0;
4784         phba->pport->fc_prevDID = 0;
4785
4786         spin_lock_irq(&phba->hbalock);
4787         psli->sli_flag &= ~(LPFC_PROCESS_LA);
4788         phba->fcf.fcf_flag = 0;
4789         spin_unlock_irq(&phba->hbalock);
4790
4791         /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4792         if (phba->hba_flag & HBA_FW_DUMP_OP) {
4793                 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4794                 return rc;
4795         }
4796
4797         /* Now physically reset the device */
4798         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4799                         "0389 Performing PCI function reset!\n");
4800
4801         /* Turn off parity checking and serr during the physical reset */
4802         if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
4803                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4804                                 "3205 PCI read Config failed\n");
4805                 return -EIO;
4806         }
4807
4808         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4809                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4810
4811         /* Perform FCoE PCI function reset before freeing queue memory */
4812         rc = lpfc_pci_function_reset(phba);
4813
4814         /* Restore PCI cmd register */
4815         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4816
4817         return rc;
4818 }
4819
4820 /**
4821  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4822  * @phba: Pointer to HBA context object.
4823  *
4824  * This function is called in the SLI initialization code path to
4825  * restart the HBA. The caller is not required to hold any lock.
4826  * This function writes MBX_RESTART mailbox command to the SLIM and
4827  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4828  * function to free any pending commands. The function enables
4829  * POST only during the first initialization. The function returns zero.
4830  * The function does not guarantee completion of MBX_RESTART mailbox
4831  * command before the return of this function.
4832  **/
4833 static int
4834 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4835 {
4836         MAILBOX_t *mb;
4837         struct lpfc_sli *psli;
4838         volatile uint32_t word0;
4839         void __iomem *to_slim;
4840         uint32_t hba_aer_enabled;
4841
4842         spin_lock_irq(&phba->hbalock);
4843
4844         /* Take PCIe device Advanced Error Reporting (AER) state */
4845         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4846
4847         psli = &phba->sli;
4848
4849         /* Restart HBA */
4850         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4851                         "0337 Restart HBA Data: x%x x%x\n",
4852                         (phba->pport) ? phba->pport->port_state : 0,
4853                         psli->sli_flag);
4854
4855         word0 = 0;
4856         mb = (MAILBOX_t *) &word0;
4857         mb->mbxCommand = MBX_RESTART;
4858         mb->mbxHc = 1;
4859
4860         lpfc_reset_barrier(phba);
4861
4862         to_slim = phba->MBslimaddr;
4863         writel(*(uint32_t *) mb, to_slim);
4864         readl(to_slim); /* flush */
4865
4866         /* Only skip post after fc_ffinit is completed */
4867         if (phba->pport && phba->pport->port_state)
4868                 word0 = 1;      /* This is really setting up word1 */
4869         else
4870                 word0 = 0;      /* This is really setting up word1 */
4871         to_slim = phba->MBslimaddr + sizeof (uint32_t);
4872         writel(*(uint32_t *) mb, to_slim);
4873         readl(to_slim); /* flush */
4874
4875         lpfc_sli_brdreset(phba);
4876         if (phba->pport)
4877                 phba->pport->stopped = 0;
4878         phba->link_state = LPFC_INIT_START;
4879         phba->hba_flag = 0;
4880         spin_unlock_irq(&phba->hbalock);
4881
4882         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4883         psli->stats_start = ktime_get_seconds();
4884
4885         /* Give the INITFF and Post time to settle. */
4886         mdelay(100);
4887
4888         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4889         if (hba_aer_enabled)
4890                 pci_disable_pcie_error_reporting(phba->pcidev);
4891
4892         lpfc_hba_down_post(phba);
4893
4894         return 0;
4895 }
4896
4897 /**
4898  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4899  * @phba: Pointer to HBA context object.
4900  *
4901  * This function is called in the SLI initialization code path to restart
4902  * a SLI4 HBA. The caller is not required to hold any lock.
4903  * At the end of the function, it calls lpfc_hba_down_post function to
4904  * free any pending commands.
4905  **/
4906 static int
4907 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4908 {
4909         struct lpfc_sli *psli = &phba->sli;
4910         uint32_t hba_aer_enabled;
4911         int rc;
4912
4913         /* Restart HBA */
4914         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4915                         "0296 Restart HBA Data: x%x x%x\n",
4916                         phba->pport->port_state, psli->sli_flag);
4917
4918         /* Take PCIe device Advanced Error Reporting (AER) state */
4919         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4920
4921         rc = lpfc_sli4_brdreset(phba);
4922         if (rc) {
4923                 phba->link_state = LPFC_HBA_ERROR;
4924                 goto hba_down_queue;
4925         }
4926
4927         spin_lock_irq(&phba->hbalock);
4928         phba->pport->stopped = 0;
4929         phba->link_state = LPFC_INIT_START;
4930         phba->hba_flag = 0;
4931         spin_unlock_irq(&phba->hbalock);
4932
4933         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4934         psli->stats_start = ktime_get_seconds();
4935
4936         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4937         if (hba_aer_enabled)
4938                 pci_disable_pcie_error_reporting(phba->pcidev);
4939
4940 hba_down_queue:
4941         lpfc_hba_down_post(phba);
4942         lpfc_sli4_queue_destroy(phba);
4943
4944         return rc;
4945 }
4946
4947 /**
4948  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4949  * @phba: Pointer to HBA context object.
4950  *
4951  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4952  * API jump table function pointer from the lpfc_hba struct.
4953 **/
4954 int
4955 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4956 {
4957         return phba->lpfc_sli_brdrestart(phba);
4958 }
4959
4960 /**
4961  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4962  * @phba: Pointer to HBA context object.
4963  *
4964  * This function is called after a HBA restart to wait for successful
4965  * restart of the HBA. Successful restart of the HBA is indicated by
4966  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4967  * iteration, the function will restart the HBA again. The function returns
4968  * zero if HBA successfully restarted else returns negative error code.
4969  **/
4970 int
4971 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4972 {
4973         uint32_t status, i = 0;
4974
4975         /* Read the HBA Host Status Register */
4976         if (lpfc_readl(phba->HSregaddr, &status))
4977                 return -EIO;
4978
4979         /* Check status register to see what current state is */
4980         i = 0;
4981         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4982
4983                 /* Check every 10ms for 10 retries, then every 100ms for 90
4984                  * retries, then every 1 sec for 50 retires for a total of
4985                  * ~60 seconds before reset the board again and check every
4986                  * 1 sec for 50 retries. The up to 60 seconds before the
4987                  * board ready is required by the Falcon FIPS zeroization
4988                  * complete, and any reset the board in between shall cause
4989                  * restart of zeroization, further delay the board ready.
4990                  */
4991                 if (i++ >= 200) {
4992                         /* Adapter failed to init, timeout, status reg
4993                            <status> */
4994                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4995                                         "0436 Adapter failed to init, "
4996                                         "timeout, status reg x%x, "
4997                                         "FW Data: A8 x%x AC x%x\n", status,
4998                                         readl(phba->MBslimaddr + 0xa8),
4999                                         readl(phba->MBslimaddr + 0xac));
5000                         phba->link_state = LPFC_HBA_ERROR;
5001                         return -ETIMEDOUT;
5002                 }
5003
5004                 /* Check to see if any errors occurred during init */
5005                 if (status & HS_FFERM) {
5006                         /* ERROR: During chipset initialization */
5007                         /* Adapter failed to init, chipset, status reg
5008                            <status> */
5009                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5010                                         "0437 Adapter failed to init, "
5011                                         "chipset, status reg x%x, "
5012                                         "FW Data: A8 x%x AC x%x\n", status,
5013                                         readl(phba->MBslimaddr + 0xa8),
5014                                         readl(phba->MBslimaddr + 0xac));
5015                         phba->link_state = LPFC_HBA_ERROR;
5016                         return -EIO;
5017                 }
5018
5019                 if (i <= 10)
5020                         msleep(10);
5021                 else if (i <= 100)
5022                         msleep(100);
5023                 else
5024                         msleep(1000);
5025
5026                 if (i == 150) {
5027                         /* Do post */
5028                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5029                         lpfc_sli_brdrestart(phba);
5030                 }
5031                 /* Read the HBA Host Status Register */
5032                 if (lpfc_readl(phba->HSregaddr, &status))
5033                         return -EIO;
5034         }
5035
5036         /* Check to see if any errors occurred during init */
5037         if (status & HS_FFERM) {
5038                 /* ERROR: During chipset initialization */
5039                 /* Adapter failed to init, chipset, status reg <status> */
5040                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5041                                 "0438 Adapter failed to init, chipset, "
5042                                 "status reg x%x, "
5043                                 "FW Data: A8 x%x AC x%x\n", status,
5044                                 readl(phba->MBslimaddr + 0xa8),
5045                                 readl(phba->MBslimaddr + 0xac));
5046                 phba->link_state = LPFC_HBA_ERROR;
5047                 return -EIO;
5048         }
5049
5050         phba->hba_flag |= HBA_NEEDS_CFG_PORT;
5051
5052         /* Clear all interrupt enable conditions */
5053         writel(0, phba->HCregaddr);
5054         readl(phba->HCregaddr); /* flush */
5055
5056         /* setup host attn register */
5057         writel(0xffffffff, phba->HAregaddr);
5058         readl(phba->HAregaddr); /* flush */
5059         return 0;
5060 }
5061
5062 /**
5063  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
5064  *
5065  * This function calculates and returns the number of HBQs required to be
5066  * configured.
5067  **/
5068 int
5069 lpfc_sli_hbq_count(void)
5070 {
5071         return ARRAY_SIZE(lpfc_hbq_defs);
5072 }
5073
5074 /**
5075  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
5076  *
5077  * This function adds the number of hbq entries in every HBQ to get
5078  * the total number of hbq entries required for the HBA and returns
5079  * the total count.
5080  **/
5081 static int
5082 lpfc_sli_hbq_entry_count(void)
5083 {
5084         int  hbq_count = lpfc_sli_hbq_count();
5085         int  count = 0;
5086         int  i;
5087
5088         for (i = 0; i < hbq_count; ++i)
5089                 count += lpfc_hbq_defs[i]->entry_count;
5090         return count;
5091 }
5092
5093 /**
5094  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
5095  *
5096  * This function calculates amount of memory required for all hbq entries
5097  * to be configured and returns the total memory required.
5098  **/
5099 int
5100 lpfc_sli_hbq_size(void)
5101 {
5102         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
5103 }
5104
5105 /**
5106  * lpfc_sli_hbq_setup - configure and initialize HBQs
5107  * @phba: Pointer to HBA context object.
5108  *
5109  * This function is called during the SLI initialization to configure
5110  * all the HBQs and post buffers to the HBQ. The caller is not
5111  * required to hold any locks. This function will return zero if successful
5112  * else it will return negative error code.
5113  **/
5114 static int
5115 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
5116 {
5117         int  hbq_count = lpfc_sli_hbq_count();
5118         LPFC_MBOXQ_t *pmb;
5119         MAILBOX_t *pmbox;
5120         uint32_t hbqno;
5121         uint32_t hbq_entry_index;
5122
5123                                 /* Get a Mailbox buffer to setup mailbox
5124                                  * commands for HBA initialization
5125                                  */
5126         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5127
5128         if (!pmb)
5129                 return -ENOMEM;
5130
5131         pmbox = &pmb->u.mb;
5132
5133         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
5134         phba->link_state = LPFC_INIT_MBX_CMDS;
5135         phba->hbq_in_use = 1;
5136
5137         hbq_entry_index = 0;
5138         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
5139                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
5140                 phba->hbqs[hbqno].hbqPutIdx      = 0;
5141                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
5142                 phba->hbqs[hbqno].entry_count =
5143                         lpfc_hbq_defs[hbqno]->entry_count;
5144                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
5145                         hbq_entry_index, pmb);
5146                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
5147
5148                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
5149                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
5150                            mbxStatus <status>, ring <num> */
5151
5152                         lpfc_printf_log(phba, KERN_ERR,
5153                                         LOG_SLI | LOG_VPORT,
5154                                         "1805 Adapter failed to init. "
5155                                         "Data: x%x x%x x%x\n",
5156                                         pmbox->mbxCommand,
5157                                         pmbox->mbxStatus, hbqno);
5158
5159                         phba->link_state = LPFC_HBA_ERROR;
5160                         mempool_free(pmb, phba->mbox_mem_pool);
5161                         return -ENXIO;
5162                 }
5163         }
5164         phba->hbq_count = hbq_count;
5165
5166         mempool_free(pmb, phba->mbox_mem_pool);
5167
5168         /* Initially populate or replenish the HBQs */
5169         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5170                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
5171         return 0;
5172 }
5173
5174 /**
5175  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5176  * @phba: Pointer to HBA context object.
5177  *
5178  * This function is called during the SLI initialization to configure
5179  * all the HBQs and post buffers to the HBQ. The caller is not
5180  * required to hold any locks. This function will return zero if successful
5181  * else it will return negative error code.
5182  **/
5183 static int
5184 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5185 {
5186         phba->hbq_in_use = 1;
5187         /**
5188          * Specific case when the MDS diagnostics is enabled and supported.
5189          * The receive buffer count is truncated to manage the incoming
5190          * traffic.
5191          **/
5192         if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5193                 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5194                         lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5195         else
5196                 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5197                         lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
5198         phba->hbq_count = 1;
5199         lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
5200         /* Initially populate or replenish the HBQs */
5201         return 0;
5202 }
5203
5204 /**
5205  * lpfc_sli_config_port - Issue config port mailbox command
5206  * @phba: Pointer to HBA context object.
5207  * @sli_mode: sli mode - 2/3
5208  *
5209  * This function is called by the sli initialization code path
5210  * to issue config_port mailbox command. This function restarts the
5211  * HBA firmware and issues a config_port mailbox command to configure
5212  * the SLI interface in the sli mode specified by sli_mode
5213  * variable. The caller is not required to hold any locks.
5214  * The function returns 0 if successful, else returns negative error
5215  * code.
5216  **/
5217 int
5218 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
5219 {
5220         LPFC_MBOXQ_t *pmb;
5221         uint32_t resetcount = 0, rc = 0, done = 0;
5222
5223         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5224         if (!pmb) {
5225                 phba->link_state = LPFC_HBA_ERROR;
5226                 return -ENOMEM;
5227         }
5228
5229         phba->sli_rev = sli_mode;
5230         while (resetcount < 2 && !done) {
5231                 spin_lock_irq(&phba->hbalock);
5232                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5233                 spin_unlock_irq(&phba->hbalock);
5234                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5235                 lpfc_sli_brdrestart(phba);
5236                 rc = lpfc_sli_chipset_init(phba);
5237                 if (rc)
5238                         break;
5239
5240                 spin_lock_irq(&phba->hbalock);
5241                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5242                 spin_unlock_irq(&phba->hbalock);
5243                 resetcount++;
5244
5245                 /* Call pre CONFIG_PORT mailbox command initialization.  A
5246                  * value of 0 means the call was successful.  Any other
5247                  * nonzero value is a failure, but if ERESTART is returned,
5248                  * the driver may reset the HBA and try again.
5249                  */
5250                 rc = lpfc_config_port_prep(phba);
5251                 if (rc == -ERESTART) {
5252                         phba->link_state = LPFC_LINK_UNKNOWN;
5253                         continue;
5254                 } else if (rc)
5255                         break;
5256
5257                 phba->link_state = LPFC_INIT_MBX_CMDS;
5258                 lpfc_config_port(phba, pmb);
5259                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5260                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5261                                         LPFC_SLI3_HBQ_ENABLED |
5262                                         LPFC_SLI3_CRP_ENABLED |
5263                                         LPFC_SLI3_DSS_ENABLED);
5264                 if (rc != MBX_SUCCESS) {
5265                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5266                                 "0442 Adapter failed to init, mbxCmd x%x "
5267                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
5268                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
5269                         spin_lock_irq(&phba->hbalock);
5270                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
5271                         spin_unlock_irq(&phba->hbalock);
5272                         rc = -ENXIO;
5273                 } else {
5274                         /* Allow asynchronous mailbox command to go through */
5275                         spin_lock_irq(&phba->hbalock);
5276                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5277                         spin_unlock_irq(&phba->hbalock);
5278                         done = 1;
5279
5280                         if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5281                             (pmb->u.mb.un.varCfgPort.gasabt == 0))
5282                                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5283                                         "3110 Port did not grant ASABT\n");
5284                 }
5285         }
5286         if (!done) {
5287                 rc = -EINVAL;
5288                 goto do_prep_failed;
5289         }
5290         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5291                 if (!pmb->u.mb.un.varCfgPort.cMA) {
5292                         rc = -ENXIO;
5293                         goto do_prep_failed;
5294                 }
5295                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
5296                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
5297                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5298                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5299                                 phba->max_vpi : phba->max_vports;
5300
5301                 } else
5302                         phba->max_vpi = 0;
5303                 if (pmb->u.mb.un.varCfgPort.gerbm)
5304                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
5305                 if (pmb->u.mb.un.varCfgPort.gcrp)
5306                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
5307
5308                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5309                 phba->port_gp = phba->mbox->us.s3_pgp.port;
5310
5311                 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5312                         if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5313                                 phba->cfg_enable_bg = 0;
5314                                 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5315                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5316                                                 "0443 Adapter did not grant "
5317                                                 "BlockGuard\n");
5318                         }
5319                 }
5320         } else {
5321                 phba->hbq_get = NULL;
5322                 phba->port_gp = phba->mbox->us.s2.port;
5323                 phba->max_vpi = 0;
5324         }
5325 do_prep_failed:
5326         mempool_free(pmb, phba->mbox_mem_pool);
5327         return rc;
5328 }
5329
5330
5331 /**
5332  * lpfc_sli_hba_setup - SLI initialization function
5333  * @phba: Pointer to HBA context object.
5334  *
5335  * This function is the main SLI initialization function. This function
5336  * is called by the HBA initialization code, HBA reset code and HBA
5337  * error attention handler code. Caller is not required to hold any
5338  * locks. This function issues config_port mailbox command to configure
5339  * the SLI, setup iocb rings and HBQ rings. In the end the function
5340  * calls the config_port_post function to issue init_link mailbox
5341  * command and to start the discovery. The function will return zero
5342  * if successful, else it will return negative error code.
5343  **/
5344 int
5345 lpfc_sli_hba_setup(struct lpfc_hba *phba)
5346 {
5347         uint32_t rc;
5348         int  i;
5349         int longs;
5350
5351         /* Enable ISR already does config_port because of config_msi mbx */
5352         if (phba->hba_flag & HBA_NEEDS_CFG_PORT) {
5353                 rc = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
5354                 if (rc)
5355                         return -EIO;
5356                 phba->hba_flag &= ~HBA_NEEDS_CFG_PORT;
5357         }
5358         phba->fcp_embed_io = 0; /* SLI4 FC support only */
5359
5360         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5361         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5362                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5363                 if (!rc) {
5364                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5365                                         "2709 This device supports "
5366                                         "Advanced Error Reporting (AER)\n");
5367                         spin_lock_irq(&phba->hbalock);
5368                         phba->hba_flag |= HBA_AER_ENABLED;
5369                         spin_unlock_irq(&phba->hbalock);
5370                 } else {
5371                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5372                                         "2708 This device does not support "
5373                                         "Advanced Error Reporting (AER): %d\n",
5374                                         rc);
5375                         phba->cfg_aer_support = 0;
5376                 }
5377         }
5378
5379         if (phba->sli_rev == 3) {
5380                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5381                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
5382         } else {
5383                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5384                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
5385                 phba->sli3_options = 0;
5386         }
5387
5388         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5389                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5390                         phba->sli_rev, phba->max_vpi);
5391         rc = lpfc_sli_ring_map(phba);
5392
5393         if (rc)
5394                 goto lpfc_sli_hba_setup_error;
5395
5396         /* Initialize VPIs. */
5397         if (phba->sli_rev == LPFC_SLI_REV3) {
5398                 /*
5399                  * The VPI bitmask and physical ID array are allocated
5400                  * and initialized once only - at driver load.  A port
5401                  * reset doesn't need to reinitialize this memory.
5402                  */
5403                 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5404                         longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5405                         phba->vpi_bmask = kcalloc(longs,
5406                                                   sizeof(unsigned long),
5407                                                   GFP_KERNEL);
5408                         if (!phba->vpi_bmask) {
5409                                 rc = -ENOMEM;
5410                                 goto lpfc_sli_hba_setup_error;
5411                         }
5412
5413                         phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5414                                                 sizeof(uint16_t),
5415                                                 GFP_KERNEL);
5416                         if (!phba->vpi_ids) {
5417                                 kfree(phba->vpi_bmask);
5418                                 rc = -ENOMEM;
5419                                 goto lpfc_sli_hba_setup_error;
5420                         }
5421                         for (i = 0; i < phba->max_vpi; i++)
5422                                 phba->vpi_ids[i] = i;
5423                 }
5424         }
5425
5426         /* Init HBQs */
5427         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5428                 rc = lpfc_sli_hbq_setup(phba);
5429                 if (rc)
5430                         goto lpfc_sli_hba_setup_error;
5431         }
5432         spin_lock_irq(&phba->hbalock);
5433         phba->sli.sli_flag |= LPFC_PROCESS_LA;
5434         spin_unlock_irq(&phba->hbalock);
5435
5436         rc = lpfc_config_port_post(phba);
5437         if (rc)
5438                 goto lpfc_sli_hba_setup_error;
5439
5440         return rc;
5441
5442 lpfc_sli_hba_setup_error:
5443         phba->link_state = LPFC_HBA_ERROR;
5444         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5445                         "0445 Firmware initialization failed\n");
5446         return rc;
5447 }
5448
5449 /**
5450  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5451  * @phba: Pointer to HBA context object.
5452  *
5453  * This function issue a dump mailbox command to read config region
5454  * 23 and parse the records in the region and populate driver
5455  * data structure.
5456  **/
5457 static int
5458 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
5459 {
5460         LPFC_MBOXQ_t *mboxq;
5461         struct lpfc_dmabuf *mp;
5462         struct lpfc_mqe *mqe;
5463         uint32_t data_length;
5464         int rc;
5465
5466         /* Program the default value of vlan_id and fc_map */
5467         phba->valid_vlan = 0;
5468         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5469         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5470         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5471
5472         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5473         if (!mboxq)
5474                 return -ENOMEM;
5475
5476         mqe = &mboxq->u.mqe;
5477         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5478                 rc = -ENOMEM;
5479                 goto out_free_mboxq;
5480         }
5481
5482         mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
5483         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5484
5485         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5486                         "(%d):2571 Mailbox cmd x%x Status x%x "
5487                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5488                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5489                         "CQ: x%x x%x x%x x%x\n",
5490                         mboxq->vport ? mboxq->vport->vpi : 0,
5491                         bf_get(lpfc_mqe_command, mqe),
5492                         bf_get(lpfc_mqe_status, mqe),
5493                         mqe->un.mb_words[0], mqe->un.mb_words[1],
5494                         mqe->un.mb_words[2], mqe->un.mb_words[3],
5495                         mqe->un.mb_words[4], mqe->un.mb_words[5],
5496                         mqe->un.mb_words[6], mqe->un.mb_words[7],
5497                         mqe->un.mb_words[8], mqe->un.mb_words[9],
5498                         mqe->un.mb_words[10], mqe->un.mb_words[11],
5499                         mqe->un.mb_words[12], mqe->un.mb_words[13],
5500                         mqe->un.mb_words[14], mqe->un.mb_words[15],
5501                         mqe->un.mb_words[16], mqe->un.mb_words[50],
5502                         mboxq->mcqe.word0,
5503                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5504                         mboxq->mcqe.trailer);
5505
5506         if (rc) {
5507                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5508                 kfree(mp);
5509                 rc = -EIO;
5510                 goto out_free_mboxq;
5511         }
5512         data_length = mqe->un.mb_words[5];
5513         if (data_length > DMP_RGN23_SIZE) {
5514                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5515                 kfree(mp);
5516                 rc = -EIO;
5517                 goto out_free_mboxq;
5518         }
5519
5520         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5521         lpfc_mbuf_free(phba, mp->virt, mp->phys);
5522         kfree(mp);
5523         rc = 0;
5524
5525 out_free_mboxq:
5526         mempool_free(mboxq, phba->mbox_mem_pool);
5527         return rc;
5528 }
5529
5530 /**
5531  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5532  * @phba: pointer to lpfc hba data structure.
5533  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5534  * @vpd: pointer to the memory to hold resulting port vpd data.
5535  * @vpd_size: On input, the number of bytes allocated to @vpd.
5536  *            On output, the number of data bytes in @vpd.
5537  *
5538  * This routine executes a READ_REV SLI4 mailbox command.  In
5539  * addition, this routine gets the port vpd data.
5540  *
5541  * Return codes
5542  *      0 - successful
5543  *      -ENOMEM - could not allocated memory.
5544  **/
5545 static int
5546 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5547                     uint8_t *vpd, uint32_t *vpd_size)
5548 {
5549         int rc = 0;
5550         uint32_t dma_size;
5551         struct lpfc_dmabuf *dmabuf;
5552         struct lpfc_mqe *mqe;
5553
5554         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5555         if (!dmabuf)
5556                 return -ENOMEM;
5557
5558         /*
5559          * Get a DMA buffer for the vpd data resulting from the READ_REV
5560          * mailbox command.
5561          */
5562         dma_size = *vpd_size;
5563         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5564                                           &dmabuf->phys, GFP_KERNEL);
5565         if (!dmabuf->virt) {
5566                 kfree(dmabuf);
5567                 return -ENOMEM;
5568         }
5569
5570         /*
5571          * The SLI4 implementation of READ_REV conflicts at word1,
5572          * bits 31:16 and SLI4 adds vpd functionality not present
5573          * in SLI3.  This code corrects the conflicts.
5574          */
5575         lpfc_read_rev(phba, mboxq);
5576         mqe = &mboxq->u.mqe;
5577         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5578         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5579         mqe->un.read_rev.word1 &= 0x0000FFFF;
5580         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5581         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5582
5583         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5584         if (rc) {
5585                 dma_free_coherent(&phba->pcidev->dev, dma_size,
5586                                   dmabuf->virt, dmabuf->phys);
5587                 kfree(dmabuf);
5588                 return -EIO;
5589         }
5590
5591         /*
5592          * The available vpd length cannot be bigger than the
5593          * DMA buffer passed to the port.  Catch the less than
5594          * case and update the caller's size.
5595          */
5596         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5597                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5598
5599         memcpy(vpd, dmabuf->virt, *vpd_size);
5600
5601         dma_free_coherent(&phba->pcidev->dev, dma_size,
5602                           dmabuf->virt, dmabuf->phys);
5603         kfree(dmabuf);
5604         return 0;
5605 }
5606
5607 /**
5608  * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
5609  * @phba: pointer to lpfc hba data structure.
5610  *
5611  * This routine retrieves SLI4 device physical port name this PCI function
5612  * is attached to.
5613  *
5614  * Return codes
5615  *      0 - successful
5616  *      otherwise - failed to retrieve controller attributes
5617  **/
5618 static int
5619 lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
5620 {
5621         LPFC_MBOXQ_t *mboxq;
5622         struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5623         struct lpfc_controller_attribute *cntl_attr;
5624         void *virtaddr = NULL;
5625         uint32_t alloclen, reqlen;
5626         uint32_t shdr_status, shdr_add_status;
5627         union lpfc_sli4_cfg_shdr *shdr;
5628         int rc;
5629
5630         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5631         if (!mboxq)
5632                 return -ENOMEM;
5633
5634         /* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
5635         reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5636         alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5637                         LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5638                         LPFC_SLI4_MBX_NEMBED);
5639
5640         if (alloclen < reqlen) {
5641                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5642                                 "3084 Allocated DMA memory size (%d) is "
5643                                 "less than the requested DMA memory size "
5644                                 "(%d)\n", alloclen, reqlen);
5645                 rc = -ENOMEM;
5646                 goto out_free_mboxq;
5647         }
5648         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5649         virtaddr = mboxq->sge_array->addr[0];
5650         mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5651         shdr = &mbx_cntl_attr->cfg_shdr;
5652         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5653         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5654         if (shdr_status || shdr_add_status || rc) {
5655                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5656                                 "3085 Mailbox x%x (x%x/x%x) failed, "
5657                                 "rc:x%x, status:x%x, add_status:x%x\n",
5658                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5659                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5660                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5661                                 rc, shdr_status, shdr_add_status);
5662                 rc = -ENXIO;
5663                 goto out_free_mboxq;
5664         }
5665
5666         cntl_attr = &mbx_cntl_attr->cntl_attr;
5667         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5668         phba->sli4_hba.lnk_info.lnk_tp =
5669                 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5670         phba->sli4_hba.lnk_info.lnk_no =
5671                 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5672
5673         memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
5674         strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
5675                 sizeof(phba->BIOSVersion));
5676
5677         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5678                         "3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s\n",
5679                         phba->sli4_hba.lnk_info.lnk_tp,
5680                         phba->sli4_hba.lnk_info.lnk_no,
5681                         phba->BIOSVersion);
5682 out_free_mboxq:
5683         if (rc != MBX_TIMEOUT) {
5684                 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5685                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
5686                 else
5687                         mempool_free(mboxq, phba->mbox_mem_pool);
5688         }
5689         return rc;
5690 }
5691
5692 /**
5693  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5694  * @phba: pointer to lpfc hba data structure.
5695  *
5696  * This routine retrieves SLI4 device physical port name this PCI function
5697  * is attached to.
5698  *
5699  * Return codes
5700  *      0 - successful
5701  *      otherwise - failed to retrieve physical port name
5702  **/
5703 static int
5704 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5705 {
5706         LPFC_MBOXQ_t *mboxq;
5707         struct lpfc_mbx_get_port_name *get_port_name;
5708         uint32_t shdr_status, shdr_add_status;
5709         union lpfc_sli4_cfg_shdr *shdr;
5710         char cport_name = 0;
5711         int rc;
5712
5713         /* We assume nothing at this point */
5714         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5715         phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5716
5717         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5718         if (!mboxq)
5719                 return -ENOMEM;
5720         /* obtain link type and link number via READ_CONFIG */
5721         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5722         lpfc_sli4_read_config(phba);
5723         if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5724                 goto retrieve_ppname;
5725
5726         /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5727         rc = lpfc_sli4_get_ctl_attr(phba);
5728         if (rc)
5729                 goto out_free_mboxq;
5730
5731 retrieve_ppname:
5732         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5733                 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5734                 sizeof(struct lpfc_mbx_get_port_name) -
5735                 sizeof(struct lpfc_sli4_cfg_mhdr),
5736                 LPFC_SLI4_MBX_EMBED);
5737         get_port_name = &mboxq->u.mqe.un.get_port_name;
5738         shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5739         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5740         bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5741                 phba->sli4_hba.lnk_info.lnk_tp);
5742         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5743         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5744         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5745         if (shdr_status || shdr_add_status || rc) {
5746                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5747                                 "3087 Mailbox x%x (x%x/x%x) failed: "
5748                                 "rc:x%x, status:x%x, add_status:x%x\n",
5749                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5750                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5751                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5752                                 rc, shdr_status, shdr_add_status);
5753                 rc = -ENXIO;
5754                 goto out_free_mboxq;
5755         }
5756         switch (phba->sli4_hba.lnk_info.lnk_no) {
5757         case LPFC_LINK_NUMBER_0:
5758                 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5759                                 &get_port_name->u.response);
5760                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5761                 break;
5762         case LPFC_LINK_NUMBER_1:
5763                 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5764                                 &get_port_name->u.response);
5765                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5766                 break;
5767         case LPFC_LINK_NUMBER_2:
5768                 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5769                                 &get_port_name->u.response);
5770                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5771                 break;
5772         case LPFC_LINK_NUMBER_3:
5773                 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5774                                 &get_port_name->u.response);
5775                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5776                 break;
5777         default:
5778                 break;
5779         }
5780
5781         if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5782                 phba->Port[0] = cport_name;
5783                 phba->Port[1] = '\0';
5784                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5785                                 "3091 SLI get port name: %s\n", phba->Port);
5786         }
5787
5788 out_free_mboxq:
5789         if (rc != MBX_TIMEOUT) {
5790                 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5791                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
5792                 else
5793                         mempool_free(mboxq, phba->mbox_mem_pool);
5794         }
5795         return rc;
5796 }
5797
5798 /**
5799  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5800  * @phba: pointer to lpfc hba data structure.
5801  *
5802  * This routine is called to explicitly arm the SLI4 device's completion and
5803  * event queues
5804  **/
5805 static void
5806 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5807 {
5808         int qidx;
5809         struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
5810         struct lpfc_sli4_hdw_queue *qp;
5811         struct lpfc_queue *eq;
5812
5813         sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
5814         sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
5815         if (sli4_hba->nvmels_cq)
5816                 sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
5817                                            LPFC_QUEUE_REARM);
5818
5819         if (sli4_hba->hdwq) {
5820                 /* Loop thru all Hardware Queues */
5821                 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
5822                         qp = &sli4_hba->hdwq[qidx];
5823                         /* ARM the corresponding CQ */
5824                         sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
5825                                                 LPFC_QUEUE_REARM);
5826                 }
5827
5828                 /* Loop thru all IRQ vectors */
5829                 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
5830                         eq = sli4_hba->hba_eq_hdl[qidx].eq;
5831                         /* ARM the corresponding EQ */
5832                         sli4_hba->sli4_write_eq_db(phba, eq,
5833                                                    0, LPFC_QUEUE_REARM);
5834                 }
5835         }
5836
5837         if (phba->nvmet_support) {
5838                 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5839                         sli4_hba->sli4_write_cq_db(phba,
5840                                 sli4_hba->nvmet_cqset[qidx], 0,
5841                                 LPFC_QUEUE_REARM);
5842                 }
5843         }
5844 }
5845
5846 /**
5847  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5848  * @phba: Pointer to HBA context object.
5849  * @type: The resource extent type.
5850  * @extnt_count: buffer to hold port available extent count.
5851  * @extnt_size: buffer to hold element count per extent.
5852  *
5853  * This function calls the port and retrievs the number of available
5854  * extents and their size for a particular extent type.
5855  *
5856  * Returns: 0 if successful.  Nonzero otherwise.
5857  **/
5858 int
5859 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5860                                uint16_t *extnt_count, uint16_t *extnt_size)
5861 {
5862         int rc = 0;
5863         uint32_t length;
5864         uint32_t mbox_tmo;
5865         struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5866         LPFC_MBOXQ_t *mbox;
5867
5868         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5869         if (!mbox)
5870                 return -ENOMEM;
5871
5872         /* Find out how many extents are available for this resource type */
5873         length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5874                   sizeof(struct lpfc_sli4_cfg_mhdr));
5875         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5876                          LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5877                          length, LPFC_SLI4_MBX_EMBED);
5878
5879         /* Send an extents count of 0 - the GET doesn't use it. */
5880         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5881                                         LPFC_SLI4_MBX_EMBED);
5882         if (unlikely(rc)) {
5883                 rc = -EIO;
5884                 goto err_exit;
5885         }
5886
5887         if (!phba->sli4_hba.intr_enable)
5888                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5889         else {
5890                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5891                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5892         }
5893         if (unlikely(rc)) {
5894                 rc = -EIO;
5895                 goto err_exit;
5896         }
5897
5898         rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5899         if (bf_get(lpfc_mbox_hdr_status,
5900                    &rsrc_info->header.cfg_shdr.response)) {
5901                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5902                                 "2930 Failed to get resource extents "
5903                                 "Status 0x%x Add'l Status 0x%x\n",
5904                                 bf_get(lpfc_mbox_hdr_status,
5905                                        &rsrc_info->header.cfg_shdr.response),
5906                                 bf_get(lpfc_mbox_hdr_add_status,
5907                                        &rsrc_info->header.cfg_shdr.response));
5908                 rc = -EIO;
5909                 goto err_exit;
5910         }
5911
5912         *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5913                               &rsrc_info->u.rsp);
5914         *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5915                              &rsrc_info->u.rsp);
5916
5917         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5918                         "3162 Retrieved extents type-%d from port: count:%d, "
5919                         "size:%d\n", type, *extnt_count, *extnt_size);
5920
5921 err_exit:
5922         mempool_free(mbox, phba->mbox_mem_pool);
5923         return rc;
5924 }
5925
5926 /**
5927  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5928  * @phba: Pointer to HBA context object.
5929  * @type: The extent type to check.
5930  *
5931  * This function reads the current available extents from the port and checks
5932  * if the extent count or extent size has changed since the last access.
5933  * Callers use this routine post port reset to understand if there is a
5934  * extent reprovisioning requirement.
5935  *
5936  * Returns:
5937  *   -Error: error indicates problem.
5938  *   1: Extent count or size has changed.
5939  *   0: No changes.
5940  **/
5941 static int
5942 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5943 {
5944         uint16_t curr_ext_cnt, rsrc_ext_cnt;
5945         uint16_t size_diff, rsrc_ext_size;
5946         int rc = 0;
5947         struct lpfc_rsrc_blks *rsrc_entry;
5948         struct list_head *rsrc_blk_list = NULL;
5949
5950         size_diff = 0;
5951         curr_ext_cnt = 0;
5952         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5953                                             &rsrc_ext_cnt,
5954                                             &rsrc_ext_size);
5955         if (unlikely(rc))
5956                 return -EIO;
5957
5958         switch (type) {
5959         case LPFC_RSC_TYPE_FCOE_RPI:
5960                 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5961                 break;
5962         case LPFC_RSC_TYPE_FCOE_VPI:
5963                 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5964                 break;
5965         case LPFC_RSC_TYPE_FCOE_XRI:
5966                 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5967                 break;
5968         case LPFC_RSC_TYPE_FCOE_VFI:
5969                 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5970                 break;
5971         default:
5972                 break;
5973         }
5974
5975         list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5976                 curr_ext_cnt++;
5977                 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5978                         size_diff++;
5979         }
5980
5981         if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5982                 rc = 1;
5983
5984         return rc;
5985 }
5986
5987 /**
5988  * lpfc_sli4_cfg_post_extnts -
5989  * @phba: Pointer to HBA context object.
5990  * @extnt_cnt: number of available extents.
5991  * @type: the extent type (rpi, xri, vfi, vpi).
5992  * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5993  * @mbox: pointer to the caller's allocated mailbox structure.
5994  *
5995  * This function executes the extents allocation request.  It also
5996  * takes care of the amount of memory needed to allocate or get the
5997  * allocated extents. It is the caller's responsibility to evaluate
5998  * the response.
5999  *
6000  * Returns:
6001  *   -Error:  Error value describes the condition found.
6002  *   0: if successful
6003  **/
6004 static int
6005 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6006                           uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
6007 {
6008         int rc = 0;
6009         uint32_t req_len;
6010         uint32_t emb_len;
6011         uint32_t alloc_len, mbox_tmo;
6012
6013         /* Calculate the total requested length of the dma memory */
6014         req_len = extnt_cnt * sizeof(uint16_t);
6015
6016         /*
6017          * Calculate the size of an embedded mailbox.  The uint32_t
6018          * accounts for extents-specific word.
6019          */
6020         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6021                 sizeof(uint32_t);
6022
6023         /*
6024          * Presume the allocation and response will fit into an embedded
6025          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6026          */
6027         *emb = LPFC_SLI4_MBX_EMBED;
6028         if (req_len > emb_len) {
6029                 req_len = extnt_cnt * sizeof(uint16_t) +
6030                         sizeof(union lpfc_sli4_cfg_shdr) +
6031                         sizeof(uint32_t);
6032                 *emb = LPFC_SLI4_MBX_NEMBED;
6033         }
6034
6035         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6036                                      LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
6037                                      req_len, *emb);
6038         if (alloc_len < req_len) {
6039                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6040                         "2982 Allocated DMA memory size (x%x) is "
6041                         "less than the requested DMA memory "
6042                         "size (x%x)\n", alloc_len, req_len);
6043                 return -ENOMEM;
6044         }
6045         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6046         if (unlikely(rc))
6047                 return -EIO;
6048
6049         if (!phba->sli4_hba.intr_enable)
6050                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6051         else {
6052                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6053                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6054         }
6055
6056         if (unlikely(rc))
6057                 rc = -EIO;
6058         return rc;
6059 }
6060
6061 /**
6062  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
6063  * @phba: Pointer to HBA context object.
6064  * @type:  The resource extent type to allocate.
6065  *
6066  * This function allocates the number of elements for the specified
6067  * resource type.
6068  **/
6069 static int
6070 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
6071 {
6072         bool emb = false;
6073         uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
6074         uint16_t rsrc_id, rsrc_start, j, k;
6075         uint16_t *ids;
6076         int i, rc;
6077         unsigned long longs;
6078         unsigned long *bmask;
6079         struct lpfc_rsrc_blks *rsrc_blks;
6080         LPFC_MBOXQ_t *mbox;
6081         uint32_t length;
6082         struct lpfc_id_range *id_array = NULL;
6083         void *virtaddr = NULL;
6084         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6085         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6086         struct list_head *ext_blk_list;
6087
6088         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6089                                             &rsrc_cnt,
6090                                             &rsrc_size);
6091         if (unlikely(rc))
6092                 return -EIO;
6093
6094         if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
6095                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6096                         "3009 No available Resource Extents "
6097                         "for resource type 0x%x: Count: 0x%x, "
6098                         "Size 0x%x\n", type, rsrc_cnt,
6099                         rsrc_size);
6100                 return -ENOMEM;
6101         }
6102
6103         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
6104                         "2903 Post resource extents type-0x%x: "
6105                         "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6106
6107         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6108         if (!mbox)
6109                 return -ENOMEM;
6110
6111         rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6112         if (unlikely(rc)) {
6113                 rc = -EIO;
6114                 goto err_exit;
6115         }
6116
6117         /*
6118          * Figure out where the response is located.  Then get local pointers
6119          * to the response data.  The port does not guarantee to respond to
6120          * all extents counts request so update the local variable with the
6121          * allocated count from the port.
6122          */
6123         if (emb == LPFC_SLI4_MBX_EMBED) {
6124                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6125                 id_array = &rsrc_ext->u.rsp.id[0];
6126                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6127         } else {
6128                 virtaddr = mbox->sge_array->addr[0];
6129                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6130                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6131                 id_array = &n_rsrc->id;
6132         }
6133
6134         longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6135         rsrc_id_cnt = rsrc_cnt * rsrc_size;
6136
6137         /*
6138          * Based on the resource size and count, correct the base and max
6139          * resource values.
6140          */
6141         length = sizeof(struct lpfc_rsrc_blks);
6142         switch (type) {
6143         case LPFC_RSC_TYPE_FCOE_RPI:
6144                 phba->sli4_hba.rpi_bmask = kcalloc(longs,
6145                                                    sizeof(unsigned long),
6146                                                    GFP_KERNEL);
6147                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6148                         rc = -ENOMEM;
6149                         goto err_exit;
6150                 }
6151                 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
6152                                                  sizeof(uint16_t),
6153                                                  GFP_KERNEL);
6154                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6155                         kfree(phba->sli4_hba.rpi_bmask);
6156                         rc = -ENOMEM;
6157                         goto err_exit;
6158                 }
6159
6160                 /*
6161                  * The next_rpi was initialized with the maximum available
6162                  * count but the port may allocate a smaller number.  Catch
6163                  * that case and update the next_rpi.
6164                  */
6165                 phba->sli4_hba.next_rpi = rsrc_id_cnt;
6166
6167                 /* Initialize local ptrs for common extent processing later. */
6168                 bmask = phba->sli4_hba.rpi_bmask;
6169                 ids = phba->sli4_hba.rpi_ids;
6170                 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6171                 break;
6172         case LPFC_RSC_TYPE_FCOE_VPI:
6173                 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6174                                           GFP_KERNEL);
6175                 if (unlikely(!phba->vpi_bmask)) {
6176                         rc = -ENOMEM;
6177                         goto err_exit;
6178                 }
6179                 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
6180                                          GFP_KERNEL);
6181                 if (unlikely(!phba->vpi_ids)) {
6182                         kfree(phba->vpi_bmask);
6183                         rc = -ENOMEM;
6184                         goto err_exit;
6185                 }
6186
6187                 /* Initialize local ptrs for common extent processing later. */
6188                 bmask = phba->vpi_bmask;
6189                 ids = phba->vpi_ids;
6190                 ext_blk_list = &phba->lpfc_vpi_blk_list;
6191                 break;
6192         case LPFC_RSC_TYPE_FCOE_XRI:
6193                 phba->sli4_hba.xri_bmask = kcalloc(longs,
6194                                                    sizeof(unsigned long),
6195                                                    GFP_KERNEL);
6196                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6197                         rc = -ENOMEM;
6198                         goto err_exit;
6199                 }
6200                 phba->sli4_hba.max_cfg_param.xri_used = 0;
6201                 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
6202                                                  sizeof(uint16_t),
6203                                                  GFP_KERNEL);
6204                 if (unlikely(!phba->sli4_hba.xri_ids)) {
6205                         kfree(phba->sli4_hba.xri_bmask);
6206                         rc = -ENOMEM;
6207                         goto err_exit;
6208                 }
6209
6210                 /* Initialize local ptrs for common extent processing later. */
6211                 bmask = phba->sli4_hba.xri_bmask;
6212                 ids = phba->sli4_hba.xri_ids;
6213                 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6214                 break;
6215         case LPFC_RSC_TYPE_FCOE_VFI:
6216                 phba->sli4_hba.vfi_bmask = kcalloc(longs,
6217                                                    sizeof(unsigned long),
6218                                                    GFP_KERNEL);
6219                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6220                         rc = -ENOMEM;
6221                         goto err_exit;
6222                 }
6223                 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
6224                                                  sizeof(uint16_t),
6225                                                  GFP_KERNEL);
6226                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6227                         kfree(phba->sli4_hba.vfi_bmask);
6228                         rc = -ENOMEM;
6229                         goto err_exit;
6230                 }
6231
6232                 /* Initialize local ptrs for common extent processing later. */
6233                 bmask = phba->sli4_hba.vfi_bmask;
6234                 ids = phba->sli4_hba.vfi_ids;
6235                 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6236                 break;
6237         default:
6238                 /* Unsupported Opcode.  Fail call. */
6239                 id_array = NULL;
6240                 bmask = NULL;
6241                 ids = NULL;
6242                 ext_blk_list = NULL;
6243                 goto err_exit;
6244         }
6245
6246         /*
6247          * Complete initializing the extent configuration with the
6248          * allocated ids assigned to this function.  The bitmask serves
6249          * as an index into the array and manages the available ids.  The
6250          * array just stores the ids communicated to the port via the wqes.
6251          */
6252         for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6253                 if ((i % 2) == 0)
6254                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6255                                          &id_array[k]);
6256                 else
6257                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6258                                          &id_array[k]);
6259
6260                 rsrc_blks = kzalloc(length, GFP_KERNEL);
6261                 if (unlikely(!rsrc_blks)) {
6262                         rc = -ENOMEM;
6263                         kfree(bmask);
6264                         kfree(ids);
6265                         goto err_exit;
6266                 }
6267                 rsrc_blks->rsrc_start = rsrc_id;
6268                 rsrc_blks->rsrc_size = rsrc_size;
6269                 list_add_tail(&rsrc_blks->list, ext_blk_list);
6270                 rsrc_start = rsrc_id;
6271                 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6272                         phba->sli4_hba.io_xri_start = rsrc_start +
6273                                 lpfc_sli4_get_iocb_cnt(phba);
6274                 }
6275
6276                 while (rsrc_id < (rsrc_start + rsrc_size)) {
6277                         ids[j] = rsrc_id;
6278                         rsrc_id++;
6279                         j++;
6280                 }
6281                 /* Entire word processed.  Get next word.*/
6282                 if ((i % 2) == 1)
6283                         k++;
6284         }
6285  err_exit:
6286         lpfc_sli4_mbox_cmd_free(phba, mbox);
6287         return rc;
6288 }
6289
6290
6291
6292 /**
6293  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6294  * @phba: Pointer to HBA context object.
6295  * @type: the extent's type.
6296  *
6297  * This function deallocates all extents of a particular resource type.
6298  * SLI4 does not allow for deallocating a particular extent range.  It
6299  * is the caller's responsibility to release all kernel memory resources.
6300  **/
6301 static int
6302 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6303 {
6304         int rc;
6305         uint32_t length, mbox_tmo = 0;
6306         LPFC_MBOXQ_t *mbox;
6307         struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6308         struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6309
6310         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6311         if (!mbox)
6312                 return -ENOMEM;
6313
6314         /*
6315          * This function sends an embedded mailbox because it only sends the
6316          * the resource type.  All extents of this type are released by the
6317          * port.
6318          */
6319         length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6320                   sizeof(struct lpfc_sli4_cfg_mhdr));
6321         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6322                          LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6323                          length, LPFC_SLI4_MBX_EMBED);
6324
6325         /* Send an extents count of 0 - the dealloc doesn't use it. */
6326         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6327                                         LPFC_SLI4_MBX_EMBED);
6328         if (unlikely(rc)) {
6329                 rc = -EIO;
6330                 goto out_free_mbox;
6331         }
6332         if (!phba->sli4_hba.intr_enable)
6333                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6334         else {
6335                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6336                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6337         }
6338         if (unlikely(rc)) {
6339                 rc = -EIO;
6340                 goto out_free_mbox;
6341         }
6342
6343         dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6344         if (bf_get(lpfc_mbox_hdr_status,
6345                    &dealloc_rsrc->header.cfg_shdr.response)) {
6346                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6347                                 "2919 Failed to release resource extents "
6348                                 "for type %d - Status 0x%x Add'l Status 0x%x. "
6349                                 "Resource memory not released.\n",
6350                                 type,
6351                                 bf_get(lpfc_mbox_hdr_status,
6352                                     &dealloc_rsrc->header.cfg_shdr.response),
6353                                 bf_get(lpfc_mbox_hdr_add_status,
6354                                     &dealloc_rsrc->header.cfg_shdr.response));
6355                 rc = -EIO;
6356                 goto out_free_mbox;
6357         }
6358
6359         /* Release kernel memory resources for the specific type. */
6360         switch (type) {
6361         case LPFC_RSC_TYPE_FCOE_VPI:
6362                 kfree(phba->vpi_bmask);
6363                 kfree(phba->vpi_ids);
6364                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6365                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6366                                     &phba->lpfc_vpi_blk_list, list) {
6367                         list_del_init(&rsrc_blk->list);
6368                         kfree(rsrc_blk);
6369                 }
6370                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6371                 break;
6372         case LPFC_RSC_TYPE_FCOE_XRI:
6373                 kfree(phba->sli4_hba.xri_bmask);
6374                 kfree(phba->sli4_hba.xri_ids);
6375                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6376                                     &phba->sli4_hba.lpfc_xri_blk_list, list) {
6377                         list_del_init(&rsrc_blk->list);
6378                         kfree(rsrc_blk);
6379                 }
6380                 break;
6381         case LPFC_RSC_TYPE_FCOE_VFI:
6382                 kfree(phba->sli4_hba.vfi_bmask);
6383                 kfree(phba->sli4_hba.vfi_ids);
6384                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6385                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6386                                     &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6387                         list_del_init(&rsrc_blk->list);
6388                         kfree(rsrc_blk);
6389                 }
6390                 break;
6391         case LPFC_RSC_TYPE_FCOE_RPI:
6392                 /* RPI bitmask and physical id array are cleaned up earlier. */
6393                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6394                                     &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6395                         list_del_init(&rsrc_blk->list);
6396                         kfree(rsrc_blk);
6397                 }
6398                 break;
6399         default:
6400                 break;
6401         }
6402
6403         bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6404
6405  out_free_mbox:
6406         mempool_free(mbox, phba->mbox_mem_pool);
6407         return rc;
6408 }
6409
6410 static void
6411 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6412                   uint32_t feature)
6413 {
6414         uint32_t len;
6415
6416         len = sizeof(struct lpfc_mbx_set_feature) -
6417                 sizeof(struct lpfc_sli4_cfg_mhdr);
6418         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6419                          LPFC_MBOX_OPCODE_SET_FEATURES, len,
6420                          LPFC_SLI4_MBX_EMBED);
6421
6422         switch (feature) {
6423         case LPFC_SET_UE_RECOVERY:
6424                 bf_set(lpfc_mbx_set_feature_UER,
6425                        &mbox->u.mqe.un.set_feature, 1);
6426                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6427                 mbox->u.mqe.un.set_feature.param_len = 8;
6428                 break;
6429         case LPFC_SET_MDS_DIAGS:
6430                 bf_set(lpfc_mbx_set_feature_mds,
6431                        &mbox->u.mqe.un.set_feature, 1);
6432                 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
6433                        &mbox->u.mqe.un.set_feature, 1);
6434                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6435                 mbox->u.mqe.un.set_feature.param_len = 8;
6436                 break;
6437         case LPFC_SET_DUAL_DUMP:
6438                 bf_set(lpfc_mbx_set_feature_dd,
6439                        &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6440                 bf_set(lpfc_mbx_set_feature_ddquery,
6441                        &mbox->u.mqe.un.set_feature, 0);
6442                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6443                 mbox->u.mqe.un.set_feature.param_len = 4;
6444                 break;
6445         }
6446
6447         return;
6448 }
6449
6450 /**
6451  * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6452  * @phba: Pointer to HBA context object.
6453  *
6454  * Disable FW logging into host memory on the adapter. To
6455  * be done before reading logs from the host memory.
6456  **/
6457 void
6458 lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6459 {
6460         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6461
6462         spin_lock_irq(&phba->hbalock);
6463         ras_fwlog->state = INACTIVE;
6464         spin_unlock_irq(&phba->hbalock);
6465
6466         /* Disable FW logging to host memory */
6467         writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6468                phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6469
6470         /* Wait 10ms for firmware to stop using DMA buffer */
6471         usleep_range(10 * 1000, 20 * 1000);
6472 }
6473
6474 /**
6475  * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6476  * @phba: Pointer to HBA context object.
6477  *
6478  * This function is called to free memory allocated for RAS FW logging
6479  * support in the driver.
6480  **/
6481 void
6482 lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6483 {
6484         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6485         struct lpfc_dmabuf *dmabuf, *next;
6486
6487         if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6488                 list_for_each_entry_safe(dmabuf, next,
6489                                     &ras_fwlog->fwlog_buff_list,
6490                                     list) {
6491                         list_del(&dmabuf->list);
6492                         dma_free_coherent(&phba->pcidev->dev,
6493                                           LPFC_RAS_MAX_ENTRY_SIZE,
6494                                           dmabuf->virt, dmabuf->phys);
6495                         kfree(dmabuf);
6496                 }
6497         }
6498
6499         if (ras_fwlog->lwpd.virt) {
6500                 dma_free_coherent(&phba->pcidev->dev,
6501                                   sizeof(uint32_t) * 2,
6502                                   ras_fwlog->lwpd.virt,
6503                                   ras_fwlog->lwpd.phys);
6504                 ras_fwlog->lwpd.virt = NULL;
6505         }
6506
6507         spin_lock_irq(&phba->hbalock);
6508         ras_fwlog->state = INACTIVE;
6509         spin_unlock_irq(&phba->hbalock);
6510 }
6511
6512 /**
6513  * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6514  * @phba: Pointer to HBA context object.
6515  * @fwlog_buff_count: Count of buffers to be created.
6516  *
6517  * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6518  * to update FW log is posted to the adapter.
6519  * Buffer count is calculated based on module param ras_fwlog_buffsize
6520  * Size of each buffer posted to FW is 64K.
6521  **/
6522
6523 static int
6524 lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6525                         uint32_t fwlog_buff_count)
6526 {
6527         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6528         struct lpfc_dmabuf *dmabuf;
6529         int rc = 0, i = 0;
6530
6531         /* Initialize List */
6532         INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6533
6534         /* Allocate memory for the LWPD */
6535         ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6536                                             sizeof(uint32_t) * 2,
6537                                             &ras_fwlog->lwpd.phys,
6538                                             GFP_KERNEL);
6539         if (!ras_fwlog->lwpd.virt) {
6540                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6541                                 "6185 LWPD Memory Alloc Failed\n");
6542
6543                 return -ENOMEM;
6544         }
6545
6546         ras_fwlog->fw_buffcount = fwlog_buff_count;
6547         for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6548                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6549                                  GFP_KERNEL);
6550                 if (!dmabuf) {
6551                         rc = -ENOMEM;
6552                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6553                                         "6186 Memory Alloc failed FW logging");
6554                         goto free_mem;
6555                 }
6556
6557                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6558                                                   LPFC_RAS_MAX_ENTRY_SIZE,
6559                                                   &dmabuf->phys, GFP_KERNEL);
6560                 if (!dmabuf->virt) {
6561                         kfree(dmabuf);
6562                         rc = -ENOMEM;
6563                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6564                                         "6187 DMA Alloc Failed FW logging");
6565                         goto free_mem;
6566                 }
6567                 dmabuf->buffer_tag = i;
6568                 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6569         }
6570
6571 free_mem:
6572         if (rc)
6573                 lpfc_sli4_ras_dma_free(phba);
6574
6575         return rc;
6576 }
6577
6578 /**
6579  * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6580  * @phba: pointer to lpfc hba data structure.
6581  * @pmb: pointer to the driver internal queue element for mailbox command.
6582  *
6583  * Completion handler for driver's RAS MBX command to the device.
6584  **/
6585 static void
6586 lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6587 {
6588         MAILBOX_t *mb;
6589         union lpfc_sli4_cfg_shdr *shdr;
6590         uint32_t shdr_status, shdr_add_status;
6591         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6592
6593         mb = &pmb->u.mb;
6594
6595         shdr = (union lpfc_sli4_cfg_shdr *)
6596                 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6597         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6598         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6599
6600         if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
6601                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6602                                 "6188 FW LOG mailbox "
6603                                 "completed with status x%x add_status x%x,"
6604                                 " mbx status x%x\n",
6605                                 shdr_status, shdr_add_status, mb->mbxStatus);
6606
6607                 ras_fwlog->ras_hwsupport = false;
6608                 goto disable_ras;
6609         }
6610
6611         spin_lock_irq(&phba->hbalock);
6612         ras_fwlog->state = ACTIVE;
6613         spin_unlock_irq(&phba->hbalock);
6614         mempool_free(pmb, phba->mbox_mem_pool);
6615
6616         return;
6617
6618 disable_ras:
6619         /* Free RAS DMA memory */
6620         lpfc_sli4_ras_dma_free(phba);
6621         mempool_free(pmb, phba->mbox_mem_pool);
6622 }
6623
6624 /**
6625  * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6626  * @phba: pointer to lpfc hba data structure.
6627  * @fwlog_level: Logging verbosity level.
6628  * @fwlog_enable: Enable/Disable logging.
6629  *
6630  * Initialize memory and post mailbox command to enable FW logging in host
6631  * memory.
6632  **/
6633 int
6634 lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6635                          uint32_t fwlog_level,
6636                          uint32_t fwlog_enable)
6637 {
6638         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6639         struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6640         struct lpfc_dmabuf *dmabuf;
6641         LPFC_MBOXQ_t *mbox;
6642         uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6643         int rc = 0;
6644
6645         spin_lock_irq(&phba->hbalock);
6646         ras_fwlog->state = INACTIVE;
6647         spin_unlock_irq(&phba->hbalock);
6648
6649         fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6650                           phba->cfg_ras_fwlog_buffsize);
6651         fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6652
6653         /*
6654          * If re-enabling FW logging support use earlier allocated
6655          * DMA buffers while posting MBX command.
6656          **/
6657         if (!ras_fwlog->lwpd.virt) {
6658                 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6659                 if (rc) {
6660                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6661                                         "6189 FW Log Memory Allocation Failed");
6662                         return rc;
6663                 }
6664         }
6665
6666         /* Setup Mailbox command */
6667         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6668         if (!mbox) {
6669                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6670                                 "6190 RAS MBX Alloc Failed");
6671                 rc = -ENOMEM;
6672                 goto mem_free;
6673         }
6674
6675         ras_fwlog->fw_loglevel = fwlog_level;
6676         len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6677                 sizeof(struct lpfc_sli4_cfg_mhdr));
6678
6679         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6680                          LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6681                          len, LPFC_SLI4_MBX_EMBED);
6682
6683         mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6684         bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6685                fwlog_enable);
6686         bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6687                ras_fwlog->fw_loglevel);
6688         bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6689                ras_fwlog->fw_buffcount);
6690         bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6691                LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6692
6693         /* Update DMA buffer address */
6694         list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6695                 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6696
6697                 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6698                         putPaddrLow(dmabuf->phys);
6699
6700                 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6701                         putPaddrHigh(dmabuf->phys);
6702         }
6703
6704         /* Update LPWD address */
6705         mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6706         mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6707
6708         spin_lock_irq(&phba->hbalock);
6709         ras_fwlog->state = REG_INPROGRESS;
6710         spin_unlock_irq(&phba->hbalock);
6711         mbox->vport = phba->pport;
6712         mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6713
6714         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6715
6716         if (rc == MBX_NOT_FINISHED) {
6717                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6718                                 "6191 FW-Log Mailbox failed. "
6719                                 "status %d mbxStatus : x%x", rc,
6720                                 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6721                 mempool_free(mbox, phba->mbox_mem_pool);
6722                 rc = -EIO;
6723                 goto mem_free;
6724         } else
6725                 rc = 0;
6726 mem_free:
6727         if (rc)
6728                 lpfc_sli4_ras_dma_free(phba);
6729
6730         return rc;
6731 }
6732
6733 /**
6734  * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6735  * @phba: Pointer to HBA context object.
6736  *
6737  * Check if RAS is supported on the adapter and initialize it.
6738  **/
6739 void
6740 lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6741 {
6742         /* Check RAS FW Log needs to be enabled or not */
6743         if (lpfc_check_fwlog_support(phba))
6744                 return;
6745
6746         lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6747                                  LPFC_RAS_ENABLE_LOGGING);
6748 }
6749
6750 /**
6751  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6752  * @phba: Pointer to HBA context object.
6753  *
6754  * This function allocates all SLI4 resource identifiers.
6755  **/
6756 int
6757 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6758 {
6759         int i, rc, error = 0;
6760         uint16_t count, base;
6761         unsigned long longs;
6762
6763         if (!phba->sli4_hba.rpi_hdrs_in_use)
6764                 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6765         if (phba->sli4_hba.extents_in_use) {
6766                 /*
6767                  * The port supports resource extents. The XRI, VPI, VFI, RPI
6768                  * resource extent count must be read and allocated before
6769                  * provisioning the resource id arrays.
6770                  */
6771                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6772                     LPFC_IDX_RSRC_RDY) {
6773                         /*
6774                          * Extent-based resources are set - the driver could
6775                          * be in a port reset. Figure out if any corrective
6776                          * actions need to be taken.
6777                          */
6778                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6779                                                  LPFC_RSC_TYPE_FCOE_VFI);
6780                         if (rc != 0)
6781                                 error++;
6782                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6783                                                  LPFC_RSC_TYPE_FCOE_VPI);
6784                         if (rc != 0)
6785                                 error++;
6786                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6787                                                  LPFC_RSC_TYPE_FCOE_XRI);
6788                         if (rc != 0)
6789                                 error++;
6790                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6791                                                  LPFC_RSC_TYPE_FCOE_RPI);
6792                         if (rc != 0)
6793                                 error++;
6794
6795                         /*
6796                          * It's possible that the number of resources
6797                          * provided to this port instance changed between
6798                          * resets.  Detect this condition and reallocate
6799                          * resources.  Otherwise, there is no action.
6800                          */
6801                         if (error) {
6802                                 lpfc_printf_log(phba, KERN_INFO,
6803                                                 LOG_MBOX | LOG_INIT,
6804                                                 "2931 Detected extent resource "
6805                                                 "change.  Reallocating all "
6806                                                 "extents.\n");
6807                                 rc = lpfc_sli4_dealloc_extent(phba,
6808                                                  LPFC_RSC_TYPE_FCOE_VFI);
6809                                 rc = lpfc_sli4_dealloc_extent(phba,
6810                                                  LPFC_RSC_TYPE_FCOE_VPI);
6811                                 rc = lpfc_sli4_dealloc_extent(phba,
6812                                                  LPFC_RSC_TYPE_FCOE_XRI);
6813                                 rc = lpfc_sli4_dealloc_extent(phba,
6814                                                  LPFC_RSC_TYPE_FCOE_RPI);
6815                         } else
6816                                 return 0;
6817                 }
6818
6819                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6820                 if (unlikely(rc))
6821                         goto err_exit;
6822
6823                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6824                 if (unlikely(rc))
6825                         goto err_exit;
6826
6827                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6828                 if (unlikely(rc))
6829                         goto err_exit;
6830
6831                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6832                 if (unlikely(rc))
6833                         goto err_exit;
6834                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6835                        LPFC_IDX_RSRC_RDY);
6836                 return rc;
6837         } else {
6838                 /*
6839                  * The port does not support resource extents.  The XRI, VPI,
6840                  * VFI, RPI resource ids were determined from READ_CONFIG.
6841                  * Just allocate the bitmasks and provision the resource id
6842                  * arrays.  If a port reset is active, the resources don't
6843                  * need any action - just exit.
6844                  */
6845                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6846                     LPFC_IDX_RSRC_RDY) {
6847                         lpfc_sli4_dealloc_resource_identifiers(phba);
6848                         lpfc_sli4_remove_rpis(phba);
6849                 }
6850                 /* RPIs. */
6851                 count = phba->sli4_hba.max_cfg_param.max_rpi;
6852                 if (count <= 0) {
6853                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6854                                         "3279 Invalid provisioning of "
6855                                         "rpi:%d\n", count);
6856                         rc = -EINVAL;
6857                         goto err_exit;
6858                 }
6859                 base = phba->sli4_hba.max_cfg_param.rpi_base;
6860                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6861                 phba->sli4_hba.rpi_bmask = kcalloc(longs,
6862                                                    sizeof(unsigned long),
6863                                                    GFP_KERNEL);
6864                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6865                         rc = -ENOMEM;
6866                         goto err_exit;
6867                 }
6868                 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
6869                                                  GFP_KERNEL);
6870                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6871                         rc = -ENOMEM;
6872                         goto free_rpi_bmask;
6873                 }
6874
6875                 for (i = 0; i < count; i++)
6876                         phba->sli4_hba.rpi_ids[i] = base + i;
6877
6878                 /* VPIs. */
6879                 count = phba->sli4_hba.max_cfg_param.max_vpi;
6880                 if (count <= 0) {
6881                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6882                                         "3280 Invalid provisioning of "
6883                                         "vpi:%d\n", count);
6884                         rc = -EINVAL;
6885                         goto free_rpi_ids;
6886                 }
6887                 base = phba->sli4_hba.max_cfg_param.vpi_base;
6888                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6889                 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6890                                           GFP_KERNEL);
6891                 if (unlikely(!phba->vpi_bmask)) {
6892                         rc = -ENOMEM;
6893                         goto free_rpi_ids;
6894                 }
6895                 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
6896                                         GFP_KERNEL);
6897                 if (unlikely(!phba->vpi_ids)) {
6898                         rc = -ENOMEM;
6899                         goto free_vpi_bmask;
6900                 }
6901
6902                 for (i = 0; i < count; i++)
6903                         phba->vpi_ids[i] = base + i;
6904
6905                 /* XRIs. */
6906                 count = phba->sli4_hba.max_cfg_param.max_xri;
6907                 if (count <= 0) {
6908                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6909                                         "3281 Invalid provisioning of "
6910                                         "xri:%d\n", count);
6911                         rc = -EINVAL;
6912                         goto free_vpi_ids;
6913                 }
6914                 base = phba->sli4_hba.max_cfg_param.xri_base;
6915                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6916                 phba->sli4_hba.xri_bmask = kcalloc(longs,
6917                                                    sizeof(unsigned long),
6918                                                    GFP_KERNEL);
6919                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6920                         rc = -ENOMEM;
6921                         goto free_vpi_ids;
6922                 }
6923                 phba->sli4_hba.max_cfg_param.xri_used = 0;
6924                 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
6925                                                  GFP_KERNEL);
6926                 if (unlikely(!phba->sli4_hba.xri_ids)) {
6927                         rc = -ENOMEM;
6928                         goto free_xri_bmask;
6929                 }
6930
6931                 for (i = 0; i < count; i++)
6932                         phba->sli4_hba.xri_ids[i] = base + i;
6933
6934                 /* VFIs. */
6935                 count = phba->sli4_hba.max_cfg_param.max_vfi;
6936                 if (count <= 0) {
6937                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6938                                         "3282 Invalid provisioning of "
6939                                         "vfi:%d\n", count);
6940                         rc = -EINVAL;
6941                         goto free_xri_ids;
6942                 }
6943                 base = phba->sli4_hba.max_cfg_param.vfi_base;
6944                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6945                 phba->sli4_hba.vfi_bmask = kcalloc(longs,
6946                                                    sizeof(unsigned long),
6947                                                    GFP_KERNEL);
6948                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6949                         rc = -ENOMEM;
6950                         goto free_xri_ids;
6951                 }
6952                 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
6953                                                  GFP_KERNEL);
6954                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6955                         rc = -ENOMEM;
6956                         goto free_vfi_bmask;
6957                 }
6958
6959                 for (i = 0; i < count; i++)
6960                         phba->sli4_hba.vfi_ids[i] = base + i;
6961
6962                 /*
6963                  * Mark all resources ready.  An HBA reset doesn't need
6964                  * to reset the initialization.
6965                  */
6966                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6967                        LPFC_IDX_RSRC_RDY);
6968                 return 0;
6969         }
6970
6971  free_vfi_bmask:
6972         kfree(phba->sli4_hba.vfi_bmask);
6973         phba->sli4_hba.vfi_bmask = NULL;
6974  free_xri_ids:
6975         kfree(phba->sli4_hba.xri_ids);
6976         phba->sli4_hba.xri_ids = NULL;
6977  free_xri_bmask:
6978         kfree(phba->sli4_hba.xri_bmask);
6979         phba->sli4_hba.xri_bmask = NULL;
6980  free_vpi_ids:
6981         kfree(phba->vpi_ids);
6982         phba->vpi_ids = NULL;
6983  free_vpi_bmask:
6984         kfree(phba->vpi_bmask);
6985         phba->vpi_bmask = NULL;
6986  free_rpi_ids:
6987         kfree(phba->sli4_hba.rpi_ids);
6988         phba->sli4_hba.rpi_ids = NULL;
6989  free_rpi_bmask:
6990         kfree(phba->sli4_hba.rpi_bmask);
6991         phba->sli4_hba.rpi_bmask = NULL;
6992  err_exit:
6993         return rc;
6994 }
6995
6996 /**
6997  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6998  * @phba: Pointer to HBA context object.
6999  *
7000  * This function allocates the number of elements for the specified
7001  * resource type.
7002  **/
7003 int
7004 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
7005 {
7006         if (phba->sli4_hba.extents_in_use) {
7007                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7008                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7009                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7010                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7011         } else {
7012                 kfree(phba->vpi_bmask);
7013                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
7014                 kfree(phba->vpi_ids);
7015                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7016                 kfree(phba->sli4_hba.xri_bmask);
7017                 kfree(phba->sli4_hba.xri_ids);
7018                 kfree(phba->sli4_hba.vfi_bmask);
7019                 kfree(phba->sli4_hba.vfi_ids);
7020                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7021                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7022         }
7023
7024         return 0;
7025 }
7026
7027 /**
7028  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
7029  * @phba: Pointer to HBA context object.
7030  * @type: The resource extent type.
7031  * @extnt_cnt: buffer to hold port extent count response
7032  * @extnt_size: buffer to hold port extent size response.
7033  *
7034  * This function calls the port to read the host allocated extents
7035  * for a particular type.
7036  **/
7037 int
7038 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
7039                                uint16_t *extnt_cnt, uint16_t *extnt_size)
7040 {
7041         bool emb;
7042         int rc = 0;
7043         uint16_t curr_blks = 0;
7044         uint32_t req_len, emb_len;
7045         uint32_t alloc_len, mbox_tmo;
7046         struct list_head *blk_list_head;
7047         struct lpfc_rsrc_blks *rsrc_blk;
7048         LPFC_MBOXQ_t *mbox;
7049         void *virtaddr = NULL;
7050         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
7051         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
7052         union  lpfc_sli4_cfg_shdr *shdr;
7053
7054         switch (type) {
7055         case LPFC_RSC_TYPE_FCOE_VPI:
7056                 blk_list_head = &phba->lpfc_vpi_blk_list;
7057                 break;
7058         case LPFC_RSC_TYPE_FCOE_XRI:
7059                 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
7060                 break;
7061         case LPFC_RSC_TYPE_FCOE_VFI:
7062                 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
7063                 break;
7064         case LPFC_RSC_TYPE_FCOE_RPI:
7065                 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
7066                 break;
7067         default:
7068                 return -EIO;
7069         }
7070
7071         /* Count the number of extents currently allocatd for this type. */
7072         list_for_each_entry(rsrc_blk, blk_list_head, list) {
7073                 if (curr_blks == 0) {
7074                         /*
7075                          * The GET_ALLOCATED mailbox does not return the size,
7076                          * just the count.  The size should be just the size
7077                          * stored in the current allocated block and all sizes
7078                          * for an extent type are the same so set the return
7079                          * value now.
7080                          */
7081                         *extnt_size = rsrc_blk->rsrc_size;
7082                 }
7083                 curr_blks++;
7084         }
7085
7086         /*
7087          * Calculate the size of an embedded mailbox.  The uint32_t
7088          * accounts for extents-specific word.
7089          */
7090         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
7091                 sizeof(uint32_t);
7092
7093         /*
7094          * Presume the allocation and response will fit into an embedded
7095          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
7096          */
7097         emb = LPFC_SLI4_MBX_EMBED;
7098         req_len = emb_len;
7099         if (req_len > emb_len) {
7100                 req_len = curr_blks * sizeof(uint16_t) +
7101                         sizeof(union lpfc_sli4_cfg_shdr) +
7102                         sizeof(uint32_t);
7103                 emb = LPFC_SLI4_MBX_NEMBED;
7104         }
7105
7106         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7107         if (!mbox)
7108                 return -ENOMEM;
7109         memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
7110
7111         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7112                                      LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
7113                                      req_len, emb);
7114         if (alloc_len < req_len) {
7115                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7116                         "2983 Allocated DMA memory size (x%x) is "
7117                         "less than the requested DMA memory "
7118                         "size (x%x)\n", alloc_len, req_len);
7119                 rc = -ENOMEM;
7120                 goto err_exit;
7121         }
7122         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
7123         if (unlikely(rc)) {
7124                 rc = -EIO;
7125                 goto err_exit;
7126         }
7127
7128         if (!phba->sli4_hba.intr_enable)
7129                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7130         else {
7131                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
7132                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7133         }
7134
7135         if (unlikely(rc)) {
7136                 rc = -EIO;
7137                 goto err_exit;
7138         }
7139
7140         /*
7141          * Figure out where the response is located.  Then get local pointers
7142          * to the response data.  The port does not guarantee to respond to
7143          * all extents counts request so update the local variable with the
7144          * allocated count from the port.
7145          */
7146         if (emb == LPFC_SLI4_MBX_EMBED) {
7147                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7148                 shdr = &rsrc_ext->header.cfg_shdr;
7149                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7150         } else {
7151                 virtaddr = mbox->sge_array->addr[0];
7152                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7153                 shdr = &n_rsrc->cfg_shdr;
7154                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7155         }
7156
7157         if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
7158                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7159                         "2984 Failed to read allocated resources "
7160                         "for type %d - Status 0x%x Add'l Status 0x%x.\n",
7161                         type,
7162                         bf_get(lpfc_mbox_hdr_status, &shdr->response),
7163                         bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7164                 rc = -EIO;
7165                 goto err_exit;
7166         }
7167  err_exit:
7168         lpfc_sli4_mbox_cmd_free(phba, mbox);
7169         return rc;
7170 }
7171
7172 /**
7173  * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
7174  * @phba: pointer to lpfc hba data structure.
7175  * @sgl_list: linked link of sgl buffers to post
7176  * @cnt: number of linked list buffers
7177  *
7178  * This routine walks the list of buffers that have been allocated and
7179  * repost them to the port by using SGL block post. This is needed after a
7180  * pci_function_reset/warm_start or start. It attempts to construct blocks
7181  * of buffer sgls which contains contiguous xris and uses the non-embedded
7182  * SGL block post mailbox commands to post them to the port. For single
7183  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7184  * mailbox command for posting.
7185  *
7186  * Returns: 0 = success, non-zero failure.
7187  **/
7188 static int
7189 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7190                           struct list_head *sgl_list, int cnt)
7191 {
7192         struct lpfc_sglq *sglq_entry = NULL;
7193         struct lpfc_sglq *sglq_entry_next = NULL;
7194         struct lpfc_sglq *sglq_entry_first = NULL;
7195         int status, total_cnt;
7196         int post_cnt = 0, num_posted = 0, block_cnt = 0;
7197         int last_xritag = NO_XRI;
7198         LIST_HEAD(prep_sgl_list);
7199         LIST_HEAD(blck_sgl_list);
7200         LIST_HEAD(allc_sgl_list);
7201         LIST_HEAD(post_sgl_list);
7202         LIST_HEAD(free_sgl_list);
7203
7204         spin_lock_irq(&phba->hbalock);
7205         spin_lock(&phba->sli4_hba.sgl_list_lock);
7206         list_splice_init(sgl_list, &allc_sgl_list);
7207         spin_unlock(&phba->sli4_hba.sgl_list_lock);
7208         spin_unlock_irq(&phba->hbalock);
7209
7210         total_cnt = cnt;
7211         list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7212                                  &allc_sgl_list, list) {
7213                 list_del_init(&sglq_entry->list);
7214                 block_cnt++;
7215                 if ((last_xritag != NO_XRI) &&
7216                     (sglq_entry->sli4_xritag != last_xritag + 1)) {
7217                         /* a hole in xri block, form a sgl posting block */
7218                         list_splice_init(&prep_sgl_list, &blck_sgl_list);
7219                         post_cnt = block_cnt - 1;
7220                         /* prepare list for next posting block */
7221                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
7222                         block_cnt = 1;
7223                 } else {
7224                         /* prepare list for next posting block */
7225                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
7226                         /* enough sgls for non-embed sgl mbox command */
7227                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7228                                 list_splice_init(&prep_sgl_list,
7229                                                  &blck_sgl_list);
7230                                 post_cnt = block_cnt;
7231                                 block_cnt = 0;
7232                         }
7233                 }
7234                 num_posted++;
7235
7236                 /* keep track of last sgl's xritag */
7237                 last_xritag = sglq_entry->sli4_xritag;
7238
7239                 /* end of repost sgl list condition for buffers */
7240                 if (num_posted == total_cnt) {
7241                         if (post_cnt == 0) {
7242                                 list_splice_init(&prep_sgl_list,
7243                                                  &blck_sgl_list);
7244                                 post_cnt = block_cnt;
7245                         } else if (block_cnt == 1) {
7246                                 status = lpfc_sli4_post_sgl(phba,
7247                                                 sglq_entry->phys, 0,
7248                                                 sglq_entry->sli4_xritag);
7249                                 if (!status) {
7250                                         /* successful, put sgl to posted list */
7251                                         list_add_tail(&sglq_entry->list,
7252                                                       &post_sgl_list);
7253                                 } else {
7254                                         /* Failure, put sgl to free list */
7255                                         lpfc_printf_log(phba, KERN_WARNING,
7256                                                 LOG_SLI,
7257                                                 "3159 Failed to post "
7258                                                 "sgl, xritag:x%x\n",
7259                                                 sglq_entry->sli4_xritag);
7260                                         list_add_tail(&sglq_entry->list,
7261                                                       &free_sgl_list);
7262                                         total_cnt--;
7263                                 }
7264                         }
7265                 }
7266
7267                 /* continue until a nembed page worth of sgls */
7268                 if (post_cnt == 0)
7269                         continue;
7270
7271                 /* post the buffer list sgls as a block */
7272                 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7273                                                  post_cnt);
7274
7275                 if (!status) {
7276                         /* success, put sgl list to posted sgl list */
7277                         list_splice_init(&blck_sgl_list, &post_sgl_list);
7278                 } else {
7279                         /* Failure, put sgl list to free sgl list */
7280                         sglq_entry_first = list_first_entry(&blck_sgl_list,
7281                                                             struct lpfc_sglq,
7282                                                             list);
7283                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7284                                         "3160 Failed to post sgl-list, "
7285                                         "xritag:x%x-x%x\n",
7286                                         sglq_entry_first->sli4_xritag,
7287                                         (sglq_entry_first->sli4_xritag +
7288                                          post_cnt - 1));
7289                         list_splice_init(&blck_sgl_list, &free_sgl_list);
7290                         total_cnt -= post_cnt;
7291                 }
7292
7293                 /* don't reset xirtag due to hole in xri block */
7294                 if (block_cnt == 0)
7295                         last_xritag = NO_XRI;
7296
7297                 /* reset sgl post count for next round of posting */
7298                 post_cnt = 0;
7299         }
7300
7301         /* free the sgls failed to post */
7302         lpfc_free_sgl_list(phba, &free_sgl_list);
7303
7304         /* push sgls posted to the available list */
7305         if (!list_empty(&post_sgl_list)) {
7306                 spin_lock_irq(&phba->hbalock);
7307                 spin_lock(&phba->sli4_hba.sgl_list_lock);
7308                 list_splice_init(&post_sgl_list, sgl_list);
7309                 spin_unlock(&phba->sli4_hba.sgl_list_lock);
7310                 spin_unlock_irq(&phba->hbalock);
7311         } else {
7312                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7313                                 "3161 Failure to post sgl to port.\n");
7314                 return -EIO;
7315         }
7316
7317         /* return the number of XRIs actually posted */
7318         return total_cnt;
7319 }
7320
7321 /**
7322  * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
7323  * @phba: pointer to lpfc hba data structure.
7324  *
7325  * This routine walks the list of nvme buffers that have been allocated and
7326  * repost them to the port by using SGL block post. This is needed after a
7327  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7328  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
7329  * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
7330  *
7331  * Returns: 0 = success, non-zero failure.
7332  **/
7333 static int
7334 lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
7335 {
7336         LIST_HEAD(post_nblist);
7337         int num_posted, rc = 0;
7338
7339         /* get all NVME buffers need to repost to a local list */
7340         lpfc_io_buf_flush(phba, &post_nblist);
7341
7342         /* post the list of nvme buffer sgls to port if available */
7343         if (!list_empty(&post_nblist)) {
7344                 num_posted = lpfc_sli4_post_io_sgl_list(
7345                         phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
7346                 /* failed to post any nvme buffer, return error */
7347                 if (num_posted == 0)
7348                         rc = -EIO;
7349         }
7350         return rc;
7351 }
7352
7353 static void
7354 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7355 {
7356         uint32_t len;
7357
7358         len = sizeof(struct lpfc_mbx_set_host_data) -
7359                 sizeof(struct lpfc_sli4_cfg_mhdr);
7360         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7361                          LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7362                          LPFC_SLI4_MBX_EMBED);
7363
7364         mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
7365         mbox->u.mqe.un.set_host_data.param_len =
7366                                         LPFC_HOST_OS_DRIVER_VERSION_SIZE;
7367         snprintf(mbox->u.mqe.un.set_host_data.data,
7368                  LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7369                  "Linux %s v"LPFC_DRIVER_VERSION,
7370                  (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7371 }
7372
7373 int
7374 lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
7375                     struct lpfc_queue *drq, int count, int idx)
7376 {
7377         int rc, i;
7378         struct lpfc_rqe hrqe;
7379         struct lpfc_rqe drqe;
7380         struct lpfc_rqb *rqbp;
7381         unsigned long flags;
7382         struct rqb_dmabuf *rqb_buffer;
7383         LIST_HEAD(rqb_buf_list);
7384
7385         rqbp = hrq->rqbp;
7386         for (i = 0; i < count; i++) {
7387                 spin_lock_irqsave(&phba->hbalock, flags);
7388                 /* IF RQ is already full, don't bother */
7389                 if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
7390                         spin_unlock_irqrestore(&phba->hbalock, flags);
7391                         break;
7392                 }
7393                 spin_unlock_irqrestore(&phba->hbalock, flags);
7394
7395                 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7396                 if (!rqb_buffer)
7397                         break;
7398                 rqb_buffer->hrq = hrq;
7399                 rqb_buffer->drq = drq;
7400                 rqb_buffer->idx = idx;
7401                 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7402         }
7403
7404         spin_lock_irqsave(&phba->hbalock, flags);
7405         while (!list_empty(&rqb_buf_list)) {
7406                 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7407                                  hbuf.list);
7408
7409                 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7410                 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7411                 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7412                 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7413                 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7414                 if (rc < 0) {
7415                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7416                                         "6421 Cannot post to HRQ %d: %x %x %x "
7417                                         "DRQ %x %x\n",
7418                                         hrq->queue_id,
7419                                         hrq->host_index,
7420                                         hrq->hba_index,
7421                                         hrq->entry_count,
7422                                         drq->host_index,
7423                                         drq->hba_index);
7424                         rqbp->rqb_free_buffer(phba, rqb_buffer);
7425                 } else {
7426                         list_add_tail(&rqb_buffer->hbuf.list,
7427                                       &rqbp->rqb_buffer_list);
7428                         rqbp->buffer_count++;
7429                 }
7430         }
7431         spin_unlock_irqrestore(&phba->hbalock, flags);
7432         return 1;
7433 }
7434
7435 /**
7436  * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
7437  * @phba: pointer to lpfc hba data structure.
7438  *
7439  * This routine initializes the per-cq idle_stat to dynamically dictate
7440  * polling decisions.
7441  *
7442  * Return codes:
7443  *   None
7444  **/
7445 static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
7446 {
7447         int i;
7448         struct lpfc_sli4_hdw_queue *hdwq;
7449         struct lpfc_queue *cq;
7450         struct lpfc_idle_stat *idle_stat;
7451         u64 wall;
7452
7453         for_each_present_cpu(i) {
7454                 hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
7455                 cq = hdwq->io_cq;
7456
7457                 /* Skip if we've already handled this cq's primary CPU */
7458                 if (cq->chann != i)
7459                         continue;
7460
7461                 idle_stat = &phba->sli4_hba.idle_stat[i];
7462
7463                 idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
7464                 idle_stat->prev_wall = wall;
7465
7466                 if (phba->nvmet_support)
7467                         cq->poll_mode = LPFC_QUEUE_WORK;
7468                 else
7469                         cq->poll_mode = LPFC_IRQ_POLL;
7470         }
7471
7472         if (!phba->nvmet_support)
7473                 schedule_delayed_work(&phba->idle_stat_delay_work,
7474                                       msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
7475 }
7476
7477 static void lpfc_sli4_dip(struct lpfc_hba *phba)
7478 {
7479         uint32_t if_type;
7480
7481         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7482         if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
7483             if_type == LPFC_SLI_INTF_IF_TYPE_6) {
7484                 struct lpfc_register reg_data;
7485
7486                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7487                                &reg_data.word0))
7488                         return;
7489
7490                 if (bf_get(lpfc_sliport_status_dip, &reg_data))
7491                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7492                                         "2904 Firmware Dump Image Present"
7493                                         " on Adapter");
7494         }
7495 }
7496
7497 /**
7498  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
7499  * @phba: Pointer to HBA context object.
7500  *
7501  * This function is the main SLI4 device initialization PCI function. This
7502  * function is called by the HBA initialization code, HBA reset code and
7503  * HBA error attention handler code. Caller is not required to hold any
7504  * locks.
7505  **/
7506 int
7507 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7508 {
7509         int rc, i, cnt, len, dd;
7510         LPFC_MBOXQ_t *mboxq;
7511         struct lpfc_mqe *mqe;
7512         uint8_t *vpd;
7513         uint32_t vpd_size;
7514         uint32_t ftr_rsp = 0;
7515         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7516         struct lpfc_vport *vport = phba->pport;
7517         struct lpfc_dmabuf *mp;
7518         struct lpfc_rqb *rqbp;
7519
7520         /* Perform a PCI function reset to start from clean */
7521         rc = lpfc_pci_function_reset(phba);
7522         if (unlikely(rc))
7523                 return -ENODEV;
7524
7525         /* Check the HBA Host Status Register for readyness */
7526         rc = lpfc_sli4_post_status_check(phba);
7527         if (unlikely(rc))
7528                 return -ENODEV;
7529         else {
7530                 spin_lock_irq(&phba->hbalock);
7531                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7532                 spin_unlock_irq(&phba->hbalock);
7533         }
7534
7535         lpfc_sli4_dip(phba);
7536
7537         /*
7538          * Allocate a single mailbox container for initializing the
7539          * port.
7540          */
7541         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7542         if (!mboxq)
7543                 return -ENOMEM;
7544
7545         /* Issue READ_REV to collect vpd and FW information. */
7546         vpd_size = SLI4_PAGE_SIZE;
7547         vpd = kzalloc(vpd_size, GFP_KERNEL);
7548         if (!vpd) {
7549                 rc = -ENOMEM;
7550                 goto out_free_mbox;
7551         }
7552
7553         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
7554         if (unlikely(rc)) {
7555                 kfree(vpd);
7556                 goto out_free_mbox;
7557         }
7558
7559         mqe = &mboxq->u.mqe;
7560         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
7561         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
7562                 phba->hba_flag |= HBA_FCOE_MODE;
7563                 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7564         } else {
7565                 phba->hba_flag &= ~HBA_FCOE_MODE;
7566         }
7567
7568         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7569                 LPFC_DCBX_CEE_MODE)
7570                 phba->hba_flag |= HBA_FIP_SUPPORT;
7571         else
7572                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7573
7574         phba->hba_flag &= ~HBA_IOQ_FLUSH;
7575
7576         if (phba->sli_rev != LPFC_SLI_REV4) {
7577                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7578                         "0376 READ_REV Error. SLI Level %d "
7579                         "FCoE enabled %d\n",
7580                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
7581                 rc = -EIO;
7582                 kfree(vpd);
7583                 goto out_free_mbox;
7584         }
7585
7586         /*
7587          * Continue initialization with default values even if driver failed
7588          * to read FCoE param config regions, only read parameters if the
7589          * board is FCoE
7590          */
7591         if (phba->hba_flag & HBA_FCOE_MODE &&
7592             lpfc_sli4_read_fcoe_params(phba))
7593                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7594                         "2570 Failed to read FCoE parameters\n");
7595
7596         /*
7597          * Retrieve sli4 device physical port name, failure of doing it
7598          * is considered as non-fatal.
7599          */
7600         rc = lpfc_sli4_retrieve_pport_name(phba);
7601         if (!rc)
7602                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7603                                 "3080 Successful retrieving SLI4 device "
7604                                 "physical port name: %s.\n", phba->Port);
7605
7606         rc = lpfc_sli4_get_ctl_attr(phba);
7607         if (!rc)
7608                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7609                                 "8351 Successful retrieving SLI4 device "
7610                                 "CTL ATTR\n");
7611
7612         /*
7613          * Evaluate the read rev and vpd data. Populate the driver
7614          * state with the results. If this routine fails, the failure
7615          * is not fatal as the driver will use generic values.
7616          */
7617         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7618         if (unlikely(!rc)) {
7619                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7620                                 "0377 Error %d parsing vpd. "
7621                                 "Using defaults.\n", rc);
7622                 rc = 0;
7623         }
7624         kfree(vpd);
7625
7626         /* Save information as VPD data */
7627         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7628         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
7629
7630         /*
7631          * This is because first G7 ASIC doesn't support the standard
7632          * 0x5a NVME cmd descriptor type/subtype
7633          */
7634         if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7635                         LPFC_SLI_INTF_IF_TYPE_6) &&
7636             (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7637             (phba->vpd.rev.smRev == 0) &&
7638             (phba->cfg_nvme_embed_cmd == 1))
7639                 phba->cfg_nvme_embed_cmd = 0;
7640
7641         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7642         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7643                                          &mqe->un.read_rev);
7644         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7645                                        &mqe->un.read_rev);
7646         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7647                                             &mqe->un.read_rev);
7648         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7649                                            &mqe->un.read_rev);
7650         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7651         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7652         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7653         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7654         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7655         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7656         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7657                         "(%d):0380 READ_REV Status x%x "
7658                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7659                         mboxq->vport ? mboxq->vport->vpi : 0,
7660                         bf_get(lpfc_mqe_status, mqe),
7661                         phba->vpd.rev.opFwName,
7662                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7663                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
7664
7665         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7666             LPFC_SLI_INTF_IF_TYPE_0) {
7667                 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7668                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7669                 if (rc == MBX_SUCCESS) {
7670                         phba->hba_flag |= HBA_RECOVERABLE_UE;
7671                         /* Set 1Sec interval to detect UE */
7672                         phba->eratt_poll_interval = 1;
7673                         phba->sli4_hba.ue_to_sr = bf_get(
7674                                         lpfc_mbx_set_feature_UESR,
7675                                         &mboxq->u.mqe.un.set_feature);
7676                         phba->sli4_hba.ue_to_rp = bf_get(
7677                                         lpfc_mbx_set_feature_UERP,
7678                                         &mboxq->u.mqe.un.set_feature);
7679                 }
7680         }
7681
7682         if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7683                 /* Enable MDS Diagnostics only if the SLI Port supports it */
7684                 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7685                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7686                 if (rc != MBX_SUCCESS)
7687                         phba->mds_diags_support = 0;
7688         }
7689
7690         /*
7691          * Discover the port's supported feature set and match it against the
7692          * hosts requests.
7693          */
7694         lpfc_request_features(phba, mboxq);
7695         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7696         if (unlikely(rc)) {
7697                 rc = -EIO;
7698                 goto out_free_mbox;
7699         }
7700
7701         /*
7702          * The port must support FCP initiator mode as this is the
7703          * only mode running in the host.
7704          */
7705         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7706                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7707                                 "0378 No support for fcpi mode.\n");
7708                 ftr_rsp++;
7709         }
7710
7711         /* Performance Hints are ONLY for FCoE */
7712         if (phba->hba_flag & HBA_FCOE_MODE) {
7713                 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7714                         phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7715                 else
7716                         phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7717         }
7718
7719         /*
7720          * If the port cannot support the host's requested features
7721          * then turn off the global config parameters to disable the
7722          * feature in the driver.  This is not a fatal error.
7723          */
7724         if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7725                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7726                         phba->cfg_enable_bg = 0;
7727                         phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
7728                         ftr_rsp++;
7729                 }
7730         }
7731
7732         if (phba->max_vpi && phba->cfg_enable_npiv &&
7733             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7734                 ftr_rsp++;
7735
7736         if (ftr_rsp) {
7737                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7738                                 "0379 Feature Mismatch Data: x%08x %08x "
7739                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7740                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7741                                 phba->cfg_enable_npiv, phba->max_vpi);
7742                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7743                         phba->cfg_enable_bg = 0;
7744                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7745                         phba->cfg_enable_npiv = 0;
7746         }
7747
7748         /* These SLI3 features are assumed in SLI4 */
7749         spin_lock_irq(&phba->hbalock);
7750         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7751         spin_unlock_irq(&phba->hbalock);
7752
7753         /* Always try to enable dual dump feature if we can */
7754         lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
7755         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7756         dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
7757         if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
7758                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7759                                 "6448 Dual Dump is enabled\n");
7760         else
7761                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
7762                                 "6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
7763                                 "rc:x%x dd:x%x\n",
7764                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7765                                 lpfc_sli_config_mbox_subsys_get(
7766                                         phba, mboxq),
7767                                 lpfc_sli_config_mbox_opcode_get(
7768                                         phba, mboxq),
7769                                 rc, dd);
7770         /*
7771          * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
7772          * calls depends on these resources to complete port setup.
7773          */
7774         rc = lpfc_sli4_alloc_resource_identifiers(phba);
7775         if (rc) {
7776                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7777                                 "2920 Failed to alloc Resource IDs "
7778                                 "rc = x%x\n", rc);
7779                 goto out_free_mbox;
7780         }
7781
7782         lpfc_set_host_data(phba, mboxq);
7783
7784         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7785         if (rc) {
7786                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7787                                 "2134 Failed to set host os driver version %x",
7788                                 rc);
7789         }
7790
7791         /* Read the port's service parameters. */
7792         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7793         if (rc) {
7794                 phba->link_state = LPFC_HBA_ERROR;
7795                 rc = -ENOMEM;
7796                 goto out_free_mbox;
7797         }
7798
7799         mboxq->vport = vport;
7800         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7801         mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
7802         if (rc == MBX_SUCCESS) {
7803                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7804                 rc = 0;
7805         }
7806
7807         /*
7808          * This memory was allocated by the lpfc_read_sparam routine. Release
7809          * it to the mbuf pool.
7810          */
7811         lpfc_mbuf_free(phba, mp->virt, mp->phys);
7812         kfree(mp);
7813         mboxq->ctx_buf = NULL;
7814         if (unlikely(rc)) {
7815                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7816                                 "0382 READ_SPARAM command failed "
7817                                 "status %d, mbxStatus x%x\n",
7818                                 rc, bf_get(lpfc_mqe_status, mqe));
7819                 phba->link_state = LPFC_HBA_ERROR;
7820                 rc = -EIO;
7821                 goto out_free_mbox;
7822         }
7823
7824         lpfc_update_vport_wwn(vport);
7825
7826         /* Update the fc_host data structures with new wwn. */
7827         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7828         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7829
7830         /* Create all the SLI4 queues */
7831         rc = lpfc_sli4_queue_create(phba);
7832         if (rc) {
7833                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7834                                 "3089 Failed to allocate queues\n");
7835                 rc = -ENODEV;
7836                 goto out_free_mbox;
7837         }
7838         /* Set up all the queues to the device */
7839         rc = lpfc_sli4_queue_setup(phba);
7840         if (unlikely(rc)) {
7841                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7842                                 "0381 Error %d during queue setup.\n ", rc);
7843                 goto out_stop_timers;
7844         }
7845         /* Initialize the driver internal SLI layer lists. */
7846         lpfc_sli4_setup(phba);
7847         lpfc_sli4_queue_init(phba);
7848
7849         /* update host els xri-sgl sizes and mappings */
7850         rc = lpfc_sli4_els_sgl_update(phba);
7851         if (unlikely(rc)) {
7852                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7853                                 "1400 Failed to update xri-sgl size and "
7854                                 "mapping: %d\n", rc);
7855                 goto out_destroy_queue;
7856         }
7857
7858         /* register the els sgl pool to the port */
7859         rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7860                                        phba->sli4_hba.els_xri_cnt);
7861         if (unlikely(rc < 0)) {
7862                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7863                                 "0582 Error %d during els sgl post "
7864                                 "operation\n", rc);
7865                 rc = -ENODEV;
7866                 goto out_destroy_queue;
7867         }
7868         phba->sli4_hba.els_xri_cnt = rc;
7869
7870         if (phba->nvmet_support) {
7871                 /* update host nvmet xri-sgl sizes and mappings */
7872                 rc = lpfc_sli4_nvmet_sgl_update(phba);
7873                 if (unlikely(rc)) {
7874                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7875                                         "6308 Failed to update nvmet-sgl size "
7876                                         "and mapping: %d\n", rc);
7877                         goto out_destroy_queue;
7878                 }
7879
7880                 /* register the nvmet sgl pool to the port */
7881                 rc = lpfc_sli4_repost_sgl_list(
7882                         phba,
7883                         &phba->sli4_hba.lpfc_nvmet_sgl_list,
7884                         phba->sli4_hba.nvmet_xri_cnt);
7885                 if (unlikely(rc < 0)) {
7886                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7887                                         "3117 Error %d during nvmet "
7888                                         "sgl post\n", rc);
7889                         rc = -ENODEV;
7890                         goto out_destroy_queue;
7891                 }
7892                 phba->sli4_hba.nvmet_xri_cnt = rc;
7893
7894                 /* We allocate an iocbq for every receive context SGL.
7895                  * The additional allocation is for abort and ls handling.
7896                  */
7897                 cnt = phba->sli4_hba.nvmet_xri_cnt +
7898                         phba->sli4_hba.max_cfg_param.max_xri;
7899         } else {
7900                 /* update host common xri-sgl sizes and mappings */
7901                 rc = lpfc_sli4_io_sgl_update(phba);
7902                 if (unlikely(rc)) {
7903                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7904                                         "6082 Failed to update nvme-sgl size "
7905                                         "and mapping: %d\n", rc);
7906                         goto out_destroy_queue;
7907                 }
7908
7909                 /* register the allocated common sgl pool to the port */
7910                 rc = lpfc_sli4_repost_io_sgl_list(phba);
7911                 if (unlikely(rc)) {
7912                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7913                                         "6116 Error %d during nvme sgl post "
7914                                         "operation\n", rc);
7915                         /* Some NVME buffers were moved to abort nvme list */
7916                         /* A pci function reset will repost them */
7917                         rc = -ENODEV;
7918                         goto out_destroy_queue;
7919                 }
7920                 /* Each lpfc_io_buf job structure has an iocbq element.
7921                  * This cnt provides for abort, els, ct and ls requests.
7922                  */
7923                 cnt = phba->sli4_hba.max_cfg_param.max_xri;
7924         }
7925
7926         if (!phba->sli.iocbq_lookup) {
7927                 /* Initialize and populate the iocb list per host */
7928                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7929                                 "2821 initialize iocb list with %d entries\n",
7930                                 cnt);
7931                 rc = lpfc_init_iocb_list(phba, cnt);
7932                 if (rc) {
7933                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7934                                         "1413 Failed to init iocb list.\n");
7935                         goto out_destroy_queue;
7936                 }
7937         }
7938
7939         if (phba->nvmet_support)
7940                 lpfc_nvmet_create_targetport(phba);
7941
7942         if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
7943                 /* Post initial buffers to all RQs created */
7944                 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7945                         rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7946                         INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7947                         rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7948                         rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
7949                         rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
7950                         rqbp->buffer_count = 0;
7951
7952                         lpfc_post_rq_buffer(
7953                                 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7954                                 phba->sli4_hba.nvmet_mrq_data[i],
7955                                 phba->cfg_nvmet_mrq_post, i);
7956                 }
7957         }
7958
7959         /* Post the rpi header region to the device. */
7960         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7961         if (unlikely(rc)) {
7962                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7963                                 "0393 Error %d during rpi post operation\n",
7964                                 rc);
7965                 rc = -ENODEV;
7966                 goto out_destroy_queue;
7967         }
7968         lpfc_sli4_node_prep(phba);
7969
7970         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
7971                 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
7972                         /*
7973                          * The FC Port needs to register FCFI (index 0)
7974                          */
7975                         lpfc_reg_fcfi(phba, mboxq);
7976                         mboxq->vport = phba->pport;
7977                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7978                         if (rc != MBX_SUCCESS)
7979                                 goto out_unset_queue;
7980                         rc = 0;
7981                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7982                                                 &mboxq->u.mqe.un.reg_fcfi);
7983                 } else {
7984                         /* We are a NVME Target mode with MRQ > 1 */
7985
7986                         /* First register the FCFI */
7987                         lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7988                         mboxq->vport = phba->pport;
7989                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7990                         if (rc != MBX_SUCCESS)
7991                                 goto out_unset_queue;
7992                         rc = 0;
7993                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7994                                                 &mboxq->u.mqe.un.reg_fcfi_mrq);
7995
7996                         /* Next register the MRQs */
7997                         lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7998                         mboxq->vport = phba->pport;
7999                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8000                         if (rc != MBX_SUCCESS)
8001                                 goto out_unset_queue;
8002                         rc = 0;
8003                 }
8004                 /* Check if the port is configured to be disabled */
8005                 lpfc_sli_read_link_ste(phba);
8006         }
8007
8008         /* Don't post more new bufs if repost already recovered
8009          * the nvme sgls.
8010          */
8011         if (phba->nvmet_support == 0) {
8012                 if (phba->sli4_hba.io_xri_cnt == 0) {
8013                         len = lpfc_new_io_buf(
8014                                               phba, phba->sli4_hba.io_xri_max);
8015                         if (len == 0) {
8016                                 rc = -ENOMEM;
8017                                 goto out_unset_queue;
8018                         }
8019
8020                         if (phba->cfg_xri_rebalancing)
8021                                 lpfc_create_multixri_pools(phba);
8022                 }
8023         } else {
8024                 phba->cfg_xri_rebalancing = 0;
8025         }
8026
8027         /* Allow asynchronous mailbox command to go through */
8028         spin_lock_irq(&phba->hbalock);
8029         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8030         spin_unlock_irq(&phba->hbalock);
8031
8032         /* Post receive buffers to the device */
8033         lpfc_sli4_rb_setup(phba);
8034
8035         /* Reset HBA FCF states after HBA reset */
8036         phba->fcf.fcf_flag = 0;
8037         phba->fcf.current_rec.flag = 0;
8038
8039         /* Start the ELS watchdog timer */
8040         mod_timer(&vport->els_tmofunc,
8041                   jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
8042
8043         /* Start heart beat timer */
8044         mod_timer(&phba->hb_tmofunc,
8045                   jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
8046         phba->hba_flag &= ~(HBA_HBEAT_INP | HBA_HBEAT_TMO);
8047         phba->last_completion_time = jiffies;
8048
8049         /* start eq_delay heartbeat */
8050         if (phba->cfg_auto_imax)
8051                 queue_delayed_work(phba->wq, &phba->eq_delay_work,
8052                                    msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
8053
8054         /* start per phba idle_stat_delay heartbeat */
8055         lpfc_init_idle_stat_hb(phba);
8056
8057         /* Start error attention (ERATT) polling timer */
8058         mod_timer(&phba->eratt_poll,
8059                   jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
8060
8061         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
8062         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
8063                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
8064                 if (!rc) {
8065                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8066                                         "2829 This device supports "
8067                                         "Advanced Error Reporting (AER)\n");
8068                         spin_lock_irq(&phba->hbalock);
8069                         phba->hba_flag |= HBA_AER_ENABLED;
8070                         spin_unlock_irq(&phba->hbalock);
8071                 } else {
8072                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8073                                         "2830 This device does not support "
8074                                         "Advanced Error Reporting (AER)\n");
8075                         phba->cfg_aer_support = 0;
8076                 }
8077                 rc = 0;
8078         }
8079
8080         /*
8081          * The port is ready, set the host's link state to LINK_DOWN
8082          * in preparation for link interrupts.
8083          */
8084         spin_lock_irq(&phba->hbalock);
8085         phba->link_state = LPFC_LINK_DOWN;
8086
8087         /* Check if physical ports are trunked */
8088         if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
8089                 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
8090         if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
8091                 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
8092         if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
8093                 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
8094         if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
8095                 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
8096         spin_unlock_irq(&phba->hbalock);
8097
8098         /* Arm the CQs and then EQs on device */
8099         lpfc_sli4_arm_cqeq_intr(phba);
8100
8101         /* Indicate device interrupt mode */
8102         phba->sli4_hba.intr_enable = 1;
8103
8104         if (!(phba->hba_flag & HBA_FCOE_MODE) &&
8105             (phba->hba_flag & LINK_DISABLED)) {
8106                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8107                                 "3103 Adapter Link is disabled.\n");
8108                 lpfc_down_link(phba, mboxq);
8109                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8110                 if (rc != MBX_SUCCESS) {
8111                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8112                                         "3104 Adapter failed to issue "
8113                                         "DOWN_LINK mbox cmd, rc:x%x\n", rc);
8114                         goto out_io_buff_free;
8115                 }
8116         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
8117                 /* don't perform init_link on SLI4 FC port loopback test */
8118                 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
8119                         rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
8120                         if (rc)
8121                                 goto out_io_buff_free;
8122                 }
8123         }
8124         mempool_free(mboxq, phba->mbox_mem_pool);
8125         return rc;
8126 out_io_buff_free:
8127         /* Free allocated IO Buffers */
8128         lpfc_io_free(phba);
8129 out_unset_queue:
8130         /* Unset all the queues set up in this routine when error out */
8131         lpfc_sli4_queue_unset(phba);
8132 out_destroy_queue:
8133         lpfc_free_iocb_list(phba);
8134         lpfc_sli4_queue_destroy(phba);
8135 out_stop_timers:
8136         lpfc_stop_hba_timers(phba);
8137 out_free_mbox:
8138         mempool_free(mboxq, phba->mbox_mem_pool);
8139         return rc;
8140 }
8141
8142 /**
8143  * lpfc_mbox_timeout - Timeout call back function for mbox timer
8144  * @t: Context to fetch pointer to hba structure from.
8145  *
8146  * This is the callback function for mailbox timer. The mailbox
8147  * timer is armed when a new mailbox command is issued and the timer
8148  * is deleted when the mailbox complete. The function is called by
8149  * the kernel timer code when a mailbox does not complete within
8150  * expected time. This function wakes up the worker thread to
8151  * process the mailbox timeout and returns. All the processing is
8152  * done by the worker thread function lpfc_mbox_timeout_handler.
8153  **/
8154 void
8155 lpfc_mbox_timeout(struct timer_list *t)
8156 {
8157         struct lpfc_hba  *phba = from_timer(phba, t, sli.mbox_tmo);
8158         unsigned long iflag;
8159         uint32_t tmo_posted;
8160
8161         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
8162         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
8163         if (!tmo_posted)
8164                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
8165         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
8166
8167         if (!tmo_posted)
8168                 lpfc_worker_wake_up(phba);
8169         return;
8170 }
8171
8172 /**
8173  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
8174  *                                    are pending
8175  * @phba: Pointer to HBA context object.
8176  *
8177  * This function checks if any mailbox completions are present on the mailbox
8178  * completion queue.
8179  **/
8180 static bool
8181 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
8182 {
8183
8184         uint32_t idx;
8185         struct lpfc_queue *mcq;
8186         struct lpfc_mcqe *mcqe;
8187         bool pending_completions = false;
8188         uint8_t qe_valid;
8189
8190         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8191                 return false;
8192
8193         /* Check for completions on mailbox completion queue */
8194
8195         mcq = phba->sli4_hba.mbx_cq;
8196         idx = mcq->hba_index;
8197         qe_valid = mcq->qe_valid;
8198         while (bf_get_le32(lpfc_cqe_valid,
8199                (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
8200                 mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
8201                 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
8202                     (!bf_get_le32(lpfc_trailer_async, mcqe))) {
8203                         pending_completions = true;
8204                         break;
8205                 }
8206                 idx = (idx + 1) % mcq->entry_count;
8207                 if (mcq->hba_index == idx)
8208                         break;
8209
8210                 /* if the index wrapped around, toggle the valid bit */
8211                 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
8212                         qe_valid = (qe_valid) ? 0 : 1;
8213         }
8214         return pending_completions;
8215
8216 }
8217
8218 /**
8219  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
8220  *                                            that were missed.
8221  * @phba: Pointer to HBA context object.
8222  *
8223  * For sli4, it is possible to miss an interrupt. As such mbox completions
8224  * maybe missed causing erroneous mailbox timeouts to occur. This function
8225  * checks to see if mbox completions are on the mailbox completion queue
8226  * and will process all the completions associated with the eq for the
8227  * mailbox completion queue.
8228  **/
8229 static bool
8230 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
8231 {
8232         struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
8233         uint32_t eqidx;
8234         struct lpfc_queue *fpeq = NULL;
8235         struct lpfc_queue *eq;
8236         bool mbox_pending;
8237
8238         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8239                 return false;
8240
8241         /* Find the EQ associated with the mbox CQ */
8242         if (sli4_hba->hdwq) {
8243                 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
8244                         eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
8245                         if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
8246                                 fpeq = eq;
8247                                 break;
8248                         }
8249                 }
8250         }
8251         if (!fpeq)
8252                 return false;
8253
8254         /* Turn off interrupts from this EQ */
8255
8256         sli4_hba->sli4_eq_clr_intr(fpeq);
8257
8258         /* Check to see if a mbox completion is pending */
8259
8260         mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
8261
8262         /*
8263          * If a mbox completion is pending, process all the events on EQ
8264          * associated with the mbox completion queue (this could include
8265          * mailbox commands, async events, els commands, receive queue data
8266          * and fcp commands)
8267          */
8268
8269         if (mbox_pending)
8270                 /* process and rearm the EQ */
8271                 lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
8272         else
8273                 /* Always clear and re-arm the EQ */
8274                 sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
8275
8276         return mbox_pending;
8277
8278 }
8279
8280 /**
8281  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
8282  * @phba: Pointer to HBA context object.
8283  *
8284  * This function is called from worker thread when a mailbox command times out.
8285  * The caller is not required to hold any locks. This function will reset the
8286  * HBA and recover all the pending commands.
8287  **/
8288 void
8289 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
8290 {
8291         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
8292         MAILBOX_t *mb = NULL;
8293
8294         struct lpfc_sli *psli = &phba->sli;
8295
8296         /* If the mailbox completed, process the completion */
8297         lpfc_sli4_process_missed_mbox_completions(phba);
8298
8299         if (!(psli->sli_flag & LPFC_SLI_ACTIVE))
8300                 return;
8301
8302         if (pmbox != NULL)
8303                 mb = &pmbox->u.mb;
8304         /* Check the pmbox pointer first.  There is a race condition
8305          * between the mbox timeout handler getting executed in the
8306          * worklist and the mailbox actually completing. When this
8307          * race condition occurs, the mbox_active will be NULL.
8308          */
8309         spin_lock_irq(&phba->hbalock);
8310         if (pmbox == NULL) {
8311                 lpfc_printf_log(phba, KERN_WARNING,
8312                                 LOG_MBOX | LOG_SLI,
8313                                 "0353 Active Mailbox cleared - mailbox timeout "
8314                                 "exiting\n");
8315                 spin_unlock_irq(&phba->hbalock);
8316                 return;
8317         }
8318
8319         /* Mbox cmd <mbxCommand> timeout */
8320         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8321                         "0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
8322                         mb->mbxCommand,
8323                         phba->pport->port_state,
8324                         phba->sli.sli_flag,
8325                         phba->sli.mbox_active);
8326         spin_unlock_irq(&phba->hbalock);
8327
8328         /* Setting state unknown so lpfc_sli_abort_iocb_ring
8329          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
8330          * it to fail all outstanding SCSI IO.
8331          */
8332         spin_lock_irq(&phba->pport->work_port_lock);
8333         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8334         spin_unlock_irq(&phba->pport->work_port_lock);
8335         spin_lock_irq(&phba->hbalock);
8336         phba->link_state = LPFC_LINK_UNKNOWN;
8337         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
8338         spin_unlock_irq(&phba->hbalock);
8339
8340         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8341                         "0345 Resetting board due to mailbox timeout\n");
8342
8343         /* Reset the HBA device */
8344         lpfc_reset_hba(phba);
8345 }
8346
8347 /**
8348  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
8349  * @phba: Pointer to HBA context object.
8350  * @pmbox: Pointer to mailbox object.
8351  * @flag: Flag indicating how the mailbox need to be processed.
8352  *
8353  * This function is called by discovery code and HBA management code
8354  * to submit a mailbox command to firmware with SLI-3 interface spec. This
8355  * function gets the hbalock to protect the data structures.
8356  * The mailbox command can be submitted in polling mode, in which case
8357  * this function will wait in a polling loop for the completion of the
8358  * mailbox.
8359  * If the mailbox is submitted in no_wait mode (not polling) the
8360  * function will submit the command and returns immediately without waiting
8361  * for the mailbox completion. The no_wait is supported only when HBA
8362  * is in SLI2/SLI3 mode - interrupts are enabled.
8363  * The SLI interface allows only one mailbox pending at a time. If the
8364  * mailbox is issued in polling mode and there is already a mailbox
8365  * pending, then the function will return an error. If the mailbox is issued
8366  * in NO_WAIT mode and there is a mailbox pending already, the function
8367  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
8368  * The sli layer owns the mailbox object until the completion of mailbox
8369  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
8370  * return codes the caller owns the mailbox command after the return of
8371  * the function.
8372  **/
8373 static int
8374 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
8375                        uint32_t flag)
8376 {
8377         MAILBOX_t *mbx;
8378         struct lpfc_sli *psli = &phba->sli;
8379         uint32_t status, evtctr;
8380         uint32_t ha_copy, hc_copy;
8381         int i;
8382         unsigned long timeout;
8383         unsigned long drvr_flag = 0;
8384         uint32_t word0, ldata;
8385         void __iomem *to_slim;
8386         int processing_queue = 0;
8387
8388         spin_lock_irqsave(&phba->hbalock, drvr_flag);
8389         if (!pmbox) {
8390                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8391                 /* processing mbox queue from intr_handler */
8392                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8393                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8394                         return MBX_SUCCESS;
8395                 }
8396                 processing_queue = 1;
8397                 pmbox = lpfc_mbox_get(phba);
8398                 if (!pmbox) {
8399                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8400                         return MBX_SUCCESS;
8401                 }
8402         }
8403
8404         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
8405                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
8406                 if(!pmbox->vport) {
8407                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8408                         lpfc_printf_log(phba, KERN_ERR,
8409                                         LOG_MBOX | LOG_VPORT,
8410                                         "1806 Mbox x%x failed. No vport\n",
8411                                         pmbox->u.mb.mbxCommand);
8412                         dump_stack();
8413                         goto out_not_finished;
8414                 }
8415         }
8416
8417         /* If the PCI channel is in offline state, do not post mbox. */
8418         if (unlikely(pci_channel_offline(phba->pcidev))) {
8419                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8420                 goto out_not_finished;
8421         }
8422
8423         /* If HBA has a deferred error attention, fail the iocb. */
8424         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8425                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8426                 goto out_not_finished;
8427         }
8428
8429         psli = &phba->sli;
8430
8431         mbx = &pmbox->u.mb;
8432         status = MBX_SUCCESS;
8433
8434         if (phba->link_state == LPFC_HBA_ERROR) {
8435                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8436
8437                 /* Mbox command <mbxCommand> cannot issue */
8438                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8439                                 "(%d):0311 Mailbox command x%x cannot "
8440                                 "issue Data: x%x x%x\n",
8441                                 pmbox->vport ? pmbox->vport->vpi : 0,
8442                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
8443                 goto out_not_finished;
8444         }
8445
8446         if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
8447                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8448                         !(hc_copy & HC_MBINT_ENA)) {
8449                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8450                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8451                                 "(%d):2528 Mailbox command x%x cannot "
8452                                 "issue Data: x%x x%x\n",
8453                                 pmbox->vport ? pmbox->vport->vpi : 0,
8454                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
8455                         goto out_not_finished;
8456                 }
8457         }
8458
8459         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8460                 /* Polling for a mbox command when another one is already active
8461                  * is not allowed in SLI. Also, the driver must have established
8462                  * SLI2 mode to queue and process multiple mbox commands.
8463                  */
8464
8465                 if (flag & MBX_POLL) {
8466                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8467
8468                         /* Mbox command <mbxCommand> cannot issue */
8469                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8470                                         "(%d):2529 Mailbox command x%x "
8471                                         "cannot issue Data: x%x x%x\n",
8472                                         pmbox->vport ? pmbox->vport->vpi : 0,
8473                                         pmbox->u.mb.mbxCommand,
8474                                         psli->sli_flag, flag);
8475                         goto out_not_finished;
8476                 }
8477
8478                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
8479                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8480                         /* Mbox command <mbxCommand> cannot issue */
8481                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8482                                         "(%d):2530 Mailbox command x%x "
8483                                         "cannot issue Data: x%x x%x\n",
8484                                         pmbox->vport ? pmbox->vport->vpi : 0,
8485                                         pmbox->u.mb.mbxCommand,
8486                                         psli->sli_flag, flag);
8487                         goto out_not_finished;
8488                 }
8489
8490                 /* Another mailbox command is still being processed, queue this
8491                  * command to be processed later.
8492                  */
8493                 lpfc_mbox_put(phba, pmbox);
8494
8495                 /* Mbox cmd issue - BUSY */
8496                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8497                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
8498                                 "x%x x%x x%x x%x\n",
8499                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
8500                                 mbx->mbxCommand,
8501                                 phba->pport ? phba->pport->port_state : 0xff,
8502                                 psli->sli_flag, flag);
8503
8504                 psli->slistat.mbox_busy++;
8505                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8506
8507                 if (pmbox->vport) {
8508                         lpfc_debugfs_disc_trc(pmbox->vport,
8509                                 LPFC_DISC_TRC_MBOX_VPORT,
8510                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
8511                                 (uint32_t)mbx->mbxCommand,
8512                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8513                 }
8514                 else {
8515                         lpfc_debugfs_disc_trc(phba->pport,
8516                                 LPFC_DISC_TRC_MBOX,
8517                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
8518                                 (uint32_t)mbx->mbxCommand,
8519                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8520                 }
8521
8522                 return MBX_BUSY;
8523         }
8524
8525         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8526
8527         /* If we are not polling, we MUST be in SLI2 mode */
8528         if (flag != MBX_POLL) {
8529                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
8530                     (mbx->mbxCommand != MBX_KILL_BOARD)) {
8531                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8532                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8533                         /* Mbox command <mbxCommand> cannot issue */
8534                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8535                                         "(%d):2531 Mailbox command x%x "
8536                                         "cannot issue Data: x%x x%x\n",
8537                                         pmbox->vport ? pmbox->vport->vpi : 0,
8538                                         pmbox->u.mb.mbxCommand,
8539                                         psli->sli_flag, flag);
8540                         goto out_not_finished;
8541                 }
8542                 /* timeout active mbox command */
8543                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8544                                            1000);
8545                 mod_timer(&psli->mbox_tmo, jiffies + timeout);
8546         }
8547
8548         /* Mailbox cmd <cmd> issue */
8549         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8550                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
8551                         "x%x\n",
8552                         pmbox->vport ? pmbox->vport->vpi : 0,
8553                         mbx->mbxCommand,
8554                         phba->pport ? phba->pport->port_state : 0xff,
8555                         psli->sli_flag, flag);
8556
8557         if (mbx->mbxCommand != MBX_HEARTBEAT) {
8558                 if (pmbox->vport) {
8559                         lpfc_debugfs_disc_trc(pmbox->vport,
8560                                 LPFC_DISC_TRC_MBOX_VPORT,
8561                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8562                                 (uint32_t)mbx->mbxCommand,
8563                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8564                 }
8565                 else {
8566                         lpfc_debugfs_disc_trc(phba->pport,
8567                                 LPFC_DISC_TRC_MBOX,
8568                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
8569                                 (uint32_t)mbx->mbxCommand,
8570                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8571                 }
8572         }
8573
8574         psli->slistat.mbox_cmd++;
8575         evtctr = psli->slistat.mbox_event;
8576
8577         /* next set own bit for the adapter and copy over command word */
8578         mbx->mbxOwner = OWN_CHIP;
8579
8580         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8581                 /* Populate mbox extension offset word. */
8582                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
8583                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
8584                                 = (uint8_t *)phba->mbox_ext
8585                                   - (uint8_t *)phba->mbox;
8586                 }
8587
8588                 /* Copy the mailbox extension data */
8589                 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8590                         lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8591                                               (uint8_t *)phba->mbox_ext,
8592                                               pmbox->in_ext_byte_len);
8593                 }
8594                 /* Copy command data to host SLIM area */
8595                 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
8596         } else {
8597                 /* Populate mbox extension offset word. */
8598                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
8599                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
8600                                 = MAILBOX_HBA_EXT_OFFSET;
8601
8602                 /* Copy the mailbox extension data */
8603                 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
8604                         lpfc_memcpy_to_slim(phba->MBslimaddr +
8605                                 MAILBOX_HBA_EXT_OFFSET,
8606                                 pmbox->ctx_buf, pmbox->in_ext_byte_len);
8607
8608                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
8609                         /* copy command data into host mbox for cmpl */
8610                         lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8611                                               MAILBOX_CMD_SIZE);
8612
8613                 /* First copy mbox command data to HBA SLIM, skip past first
8614                    word */
8615                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
8616                 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
8617                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
8618
8619                 /* Next copy over first word, with mbxOwner set */
8620                 ldata = *((uint32_t *)mbx);
8621                 to_slim = phba->MBslimaddr;
8622                 writel(ldata, to_slim);
8623                 readl(to_slim); /* flush */
8624
8625                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
8626                         /* switch over to host mailbox */
8627                         psli->sli_flag |= LPFC_SLI_ACTIVE;
8628         }
8629
8630         wmb();
8631
8632         switch (flag) {
8633         case MBX_NOWAIT:
8634                 /* Set up reference to mailbox command */
8635                 psli->mbox_active = pmbox;
8636                 /* Interrupt board to do it */
8637                 writel(CA_MBATT, phba->CAregaddr);
8638                 readl(phba->CAregaddr); /* flush */
8639                 /* Don't wait for it to finish, just return */
8640                 break;
8641
8642         case MBX_POLL:
8643                 /* Set up null reference to mailbox command */
8644                 psli->mbox_active = NULL;
8645                 /* Interrupt board to do it */
8646                 writel(CA_MBATT, phba->CAregaddr);
8647                 readl(phba->CAregaddr); /* flush */
8648
8649                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8650                         /* First read mbox status word */
8651                         word0 = *((uint32_t *)phba->mbox);
8652                         word0 = le32_to_cpu(word0);
8653                 } else {
8654                         /* First read mbox status word */
8655                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
8656                                 spin_unlock_irqrestore(&phba->hbalock,
8657                                                        drvr_flag);
8658                                 goto out_not_finished;
8659                         }
8660                 }
8661
8662                 /* Read the HBA Host Attention Register */
8663                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8664                         spin_unlock_irqrestore(&phba->hbalock,
8665                                                        drvr_flag);
8666                         goto out_not_finished;
8667                 }
8668                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8669                                                         1000) + jiffies;
8670                 i = 0;
8671                 /* Wait for command to complete */
8672                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8673                        (!(ha_copy & HA_MBATT) &&
8674                         (phba->link_state > LPFC_WARM_START))) {
8675                         if (time_after(jiffies, timeout)) {
8676                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8677                                 spin_unlock_irqrestore(&phba->hbalock,
8678                                                        drvr_flag);
8679                                 goto out_not_finished;
8680                         }
8681
8682                         /* Check if we took a mbox interrupt while we were
8683                            polling */
8684                         if (((word0 & OWN_CHIP) != OWN_CHIP)
8685                             && (evtctr != psli->slistat.mbox_event))
8686                                 break;
8687
8688                         if (i++ > 10) {
8689                                 spin_unlock_irqrestore(&phba->hbalock,
8690                                                        drvr_flag);
8691                                 msleep(1);
8692                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8693                         }
8694
8695                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8696                                 /* First copy command data */
8697                                 word0 = *((uint32_t *)phba->mbox);
8698                                 word0 = le32_to_cpu(word0);
8699                                 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
8700                                         MAILBOX_t *slimmb;
8701                                         uint32_t slimword0;
8702                                         /* Check real SLIM for any errors */
8703                                         slimword0 = readl(phba->MBslimaddr);
8704                                         slimmb = (MAILBOX_t *) & slimword0;
8705                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8706                                             && slimmb->mbxStatus) {
8707                                                 psli->sli_flag &=
8708                                                     ~LPFC_SLI_ACTIVE;
8709                                                 word0 = slimword0;
8710                                         }
8711                                 }
8712                         } else {
8713                                 /* First copy command data */
8714                                 word0 = readl(phba->MBslimaddr);
8715                         }
8716                         /* Read the HBA Host Attention Register */
8717                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8718                                 spin_unlock_irqrestore(&phba->hbalock,
8719                                                        drvr_flag);
8720                                 goto out_not_finished;
8721                         }
8722                 }
8723
8724                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8725                         /* copy results back to user */
8726                         lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8727                                                 MAILBOX_CMD_SIZE);
8728                         /* Copy the mailbox extension data */
8729                         if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8730                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
8731                                                       pmbox->ctx_buf,
8732                                                       pmbox->out_ext_byte_len);
8733                         }
8734                 } else {
8735                         /* First copy command data */
8736                         lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
8737                                                 MAILBOX_CMD_SIZE);
8738                         /* Copy the mailbox extension data */
8739                         if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8740                                 lpfc_memcpy_from_slim(
8741                                         pmbox->ctx_buf,
8742                                         phba->MBslimaddr +
8743                                         MAILBOX_HBA_EXT_OFFSET,
8744                                         pmbox->out_ext_byte_len);
8745                         }
8746                 }
8747
8748                 writel(HA_MBATT, phba->HAregaddr);
8749                 readl(phba->HAregaddr); /* flush */
8750
8751                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8752                 status = mbx->mbxStatus;
8753         }
8754
8755         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8756         return status;
8757
8758 out_not_finished:
8759         if (processing_queue) {
8760                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
8761                 lpfc_mbox_cmpl_put(phba, pmbox);
8762         }
8763         return MBX_NOT_FINISHED;
8764 }
8765
8766 /**
8767  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8768  * @phba: Pointer to HBA context object.
8769  *
8770  * The function blocks the posting of SLI4 asynchronous mailbox commands from
8771  * the driver internal pending mailbox queue. It will then try to wait out the
8772  * possible outstanding mailbox command before return.
8773  *
8774  * Returns:
8775  *      0 - the outstanding mailbox command completed; otherwise, the wait for
8776  *      the outstanding mailbox command timed out.
8777  **/
8778 static int
8779 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8780 {
8781         struct lpfc_sli *psli = &phba->sli;
8782         int rc = 0;
8783         unsigned long timeout = 0;
8784
8785         /* Mark the asynchronous mailbox command posting as blocked */
8786         spin_lock_irq(&phba->hbalock);
8787         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8788         /* Determine how long we might wait for the active mailbox
8789          * command to be gracefully completed by firmware.
8790          */
8791         if (phba->sli.mbox_active)
8792                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8793                                                 phba->sli.mbox_active) *
8794                                                 1000) + jiffies;
8795         spin_unlock_irq(&phba->hbalock);
8796
8797         /* Make sure the mailbox is really active */
8798         if (timeout)
8799                 lpfc_sli4_process_missed_mbox_completions(phba);
8800
8801         /* Wait for the outstnading mailbox command to complete */
8802         while (phba->sli.mbox_active) {
8803                 /* Check active mailbox complete status every 2ms */
8804                 msleep(2);
8805                 if (time_after(jiffies, timeout)) {
8806                         /* Timeout, marked the outstanding cmd not complete */
8807                         rc = 1;
8808                         break;
8809                 }
8810         }
8811
8812         /* Can not cleanly block async mailbox command, fails it */
8813         if (rc) {
8814                 spin_lock_irq(&phba->hbalock);
8815                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8816                 spin_unlock_irq(&phba->hbalock);
8817         }
8818         return rc;
8819 }
8820
8821 /**
8822  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8823  * @phba: Pointer to HBA context object.
8824  *
8825  * The function unblocks and resume posting of SLI4 asynchronous mailbox
8826  * commands from the driver internal pending mailbox queue. It makes sure
8827  * that there is no outstanding mailbox command before resuming posting
8828  * asynchronous mailbox commands. If, for any reason, there is outstanding
8829  * mailbox command, it will try to wait it out before resuming asynchronous
8830  * mailbox command posting.
8831  **/
8832 static void
8833 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8834 {
8835         struct lpfc_sli *psli = &phba->sli;
8836
8837         spin_lock_irq(&phba->hbalock);
8838         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8839                 /* Asynchronous mailbox posting is not blocked, do nothing */
8840                 spin_unlock_irq(&phba->hbalock);
8841                 return;
8842         }
8843
8844         /* Outstanding synchronous mailbox command is guaranteed to be done,
8845          * successful or timeout, after timing-out the outstanding mailbox
8846          * command shall always be removed, so just unblock posting async
8847          * mailbox command and resume
8848          */
8849         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8850         spin_unlock_irq(&phba->hbalock);
8851
8852         /* wake up worker thread to post asynchronous mailbox command */
8853         lpfc_worker_wake_up(phba);
8854 }
8855
8856 /**
8857  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8858  * @phba: Pointer to HBA context object.
8859  * @mboxq: Pointer to mailbox object.
8860  *
8861  * The function waits for the bootstrap mailbox register ready bit from
8862  * port for twice the regular mailbox command timeout value.
8863  *
8864  *      0 - no timeout on waiting for bootstrap mailbox register ready.
8865  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8866  **/
8867 static int
8868 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8869 {
8870         uint32_t db_ready;
8871         unsigned long timeout;
8872         struct lpfc_register bmbx_reg;
8873
8874         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8875                                    * 1000) + jiffies;
8876
8877         do {
8878                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8879                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8880                 if (!db_ready)
8881                         mdelay(2);
8882
8883                 if (time_after(jiffies, timeout))
8884                         return MBXERR_ERROR;
8885         } while (!db_ready);
8886
8887         return 0;
8888 }
8889
8890 /**
8891  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8892  * @phba: Pointer to HBA context object.
8893  * @mboxq: Pointer to mailbox object.
8894  *
8895  * The function posts a mailbox to the port.  The mailbox is expected
8896  * to be comletely filled in and ready for the port to operate on it.
8897  * This routine executes a synchronous completion operation on the
8898  * mailbox by polling for its completion.
8899  *
8900  * The caller must not be holding any locks when calling this routine.
8901  *
8902  * Returns:
8903  *      MBX_SUCCESS - mailbox posted successfully
8904  *      Any of the MBX error values.
8905  **/
8906 static int
8907 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8908 {
8909         int rc = MBX_SUCCESS;
8910         unsigned long iflag;
8911         uint32_t mcqe_status;
8912         uint32_t mbx_cmnd;
8913         struct lpfc_sli *psli = &phba->sli;
8914         struct lpfc_mqe *mb = &mboxq->u.mqe;
8915         struct lpfc_bmbx_create *mbox_rgn;
8916         struct dma_address *dma_address;
8917
8918         /*
8919          * Only one mailbox can be active to the bootstrap mailbox region
8920          * at a time and there is no queueing provided.
8921          */
8922         spin_lock_irqsave(&phba->hbalock, iflag);
8923         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8924                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8925                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8926                                 "(%d):2532 Mailbox command x%x (x%x/x%x) "
8927                                 "cannot issue Data: x%x x%x\n",
8928                                 mboxq->vport ? mboxq->vport->vpi : 0,
8929                                 mboxq->u.mb.mbxCommand,
8930                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8931                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8932                                 psli->sli_flag, MBX_POLL);
8933                 return MBXERR_ERROR;
8934         }
8935         /* The server grabs the token and owns it until release */
8936         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8937         phba->sli.mbox_active = mboxq;
8938         spin_unlock_irqrestore(&phba->hbalock, iflag);
8939
8940         /* wait for bootstrap mbox register for readyness */
8941         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8942         if (rc)
8943                 goto exit;
8944         /*
8945          * Initialize the bootstrap memory region to avoid stale data areas
8946          * in the mailbox post.  Then copy the caller's mailbox contents to
8947          * the bmbx mailbox region.
8948          */
8949         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8950         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
8951         lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8952                                sizeof(struct lpfc_mqe));
8953
8954         /* Post the high mailbox dma address to the port and wait for ready. */
8955         dma_address = &phba->sli4_hba.bmbx.dma_address;
8956         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8957
8958         /* wait for bootstrap mbox register for hi-address write done */
8959         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8960         if (rc)
8961                 goto exit;
8962
8963         /* Post the low mailbox dma address to the port. */
8964         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
8965
8966         /* wait for bootstrap mbox register for low address write done */
8967         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8968         if (rc)
8969                 goto exit;
8970
8971         /*
8972          * Read the CQ to ensure the mailbox has completed.
8973          * If so, update the mailbox status so that the upper layers
8974          * can complete the request normally.
8975          */
8976         lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8977                                sizeof(struct lpfc_mqe));
8978         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8979         lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8980                                sizeof(struct lpfc_mcqe));
8981         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
8982         /*
8983          * When the CQE status indicates a failure and the mailbox status
8984          * indicates success then copy the CQE status into the mailbox status
8985          * (and prefix it with x4000).
8986          */
8987         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
8988                 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8989                         bf_set(lpfc_mqe_status, mb,
8990                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
8991                 rc = MBXERR_ERROR;
8992         } else
8993                 lpfc_sli4_swap_str(phba, mboxq);
8994
8995         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8996                         "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
8997                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8998                         " x%x x%x CQ: x%x x%x x%x x%x\n",
8999                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
9000                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9001                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9002                         bf_get(lpfc_mqe_status, mb),
9003                         mb->un.mb_words[0], mb->un.mb_words[1],
9004                         mb->un.mb_words[2], mb->un.mb_words[3],
9005                         mb->un.mb_words[4], mb->un.mb_words[5],
9006                         mb->un.mb_words[6], mb->un.mb_words[7],
9007                         mb->un.mb_words[8], mb->un.mb_words[9],
9008                         mb->un.mb_words[10], mb->un.mb_words[11],
9009                         mb->un.mb_words[12], mboxq->mcqe.word0,
9010                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
9011                         mboxq->mcqe.trailer);
9012 exit:
9013         /* We are holding the token, no needed for lock when release */
9014         spin_lock_irqsave(&phba->hbalock, iflag);
9015         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9016         phba->sli.mbox_active = NULL;
9017         spin_unlock_irqrestore(&phba->hbalock, iflag);
9018         return rc;
9019 }
9020
9021 /**
9022  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
9023  * @phba: Pointer to HBA context object.
9024  * @mboxq: Pointer to mailbox object.
9025  * @flag: Flag indicating how the mailbox need to be processed.
9026  *
9027  * This function is called by discovery code and HBA management code to submit
9028  * a mailbox command to firmware with SLI-4 interface spec.
9029  *
9030  * Return codes the caller owns the mailbox command after the return of the
9031  * function.
9032  **/
9033 static int
9034 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
9035                        uint32_t flag)
9036 {
9037         struct lpfc_sli *psli = &phba->sli;
9038         unsigned long iflags;
9039         int rc;
9040
9041         /* dump from issue mailbox command if setup */
9042         lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
9043
9044         rc = lpfc_mbox_dev_check(phba);
9045         if (unlikely(rc)) {
9046                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9047                                 "(%d):2544 Mailbox command x%x (x%x/x%x) "
9048                                 "cannot issue Data: x%x x%x\n",
9049                                 mboxq->vport ? mboxq->vport->vpi : 0,
9050                                 mboxq->u.mb.mbxCommand,
9051                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9052                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9053                                 psli->sli_flag, flag);
9054                 goto out_not_finished;
9055         }
9056
9057         /* Detect polling mode and jump to a handler */
9058         if (!phba->sli4_hba.intr_enable) {
9059                 if (flag == MBX_POLL)
9060                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
9061                 else
9062                         rc = -EIO;
9063                 if (rc != MBX_SUCCESS)
9064                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
9065                                         "(%d):2541 Mailbox command x%x "
9066                                         "(x%x/x%x) failure: "
9067                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
9068                                         "Data: x%x x%x\n,",
9069                                         mboxq->vport ? mboxq->vport->vpi : 0,
9070                                         mboxq->u.mb.mbxCommand,
9071                                         lpfc_sli_config_mbox_subsys_get(phba,
9072                                                                         mboxq),
9073                                         lpfc_sli_config_mbox_opcode_get(phba,
9074                                                                         mboxq),
9075                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
9076                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
9077                                         bf_get(lpfc_mcqe_ext_status,
9078                                                &mboxq->mcqe),
9079                                         psli->sli_flag, flag);
9080                 return rc;
9081         } else if (flag == MBX_POLL) {
9082                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
9083                                 "(%d):2542 Try to issue mailbox command "
9084                                 "x%x (x%x/x%x) synchronously ahead of async "
9085                                 "mailbox command queue: x%x x%x\n",
9086                                 mboxq->vport ? mboxq->vport->vpi : 0,
9087                                 mboxq->u.mb.mbxCommand,
9088                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9089                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9090                                 psli->sli_flag, flag);
9091                 /* Try to block the asynchronous mailbox posting */
9092                 rc = lpfc_sli4_async_mbox_block(phba);
9093                 if (!rc) {
9094                         /* Successfully blocked, now issue sync mbox cmd */
9095                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
9096                         if (rc != MBX_SUCCESS)
9097                                 lpfc_printf_log(phba, KERN_WARNING,
9098                                         LOG_MBOX | LOG_SLI,
9099                                         "(%d):2597 Sync Mailbox command "
9100                                         "x%x (x%x/x%x) failure: "
9101                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
9102                                         "Data: x%x x%x\n,",
9103                                         mboxq->vport ? mboxq->vport->vpi : 0,
9104                                         mboxq->u.mb.mbxCommand,
9105                                         lpfc_sli_config_mbox_subsys_get(phba,
9106                                                                         mboxq),
9107                                         lpfc_sli_config_mbox_opcode_get(phba,
9108                                                                         mboxq),
9109                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
9110                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
9111                                         bf_get(lpfc_mcqe_ext_status,
9112                                                &mboxq->mcqe),
9113                                         psli->sli_flag, flag);
9114                         /* Unblock the async mailbox posting afterward */
9115                         lpfc_sli4_async_mbox_unblock(phba);
9116                 }
9117                 return rc;
9118         }
9119
9120         /* Now, interrupt mode asynchronous mailbox command */
9121         rc = lpfc_mbox_cmd_check(phba, mboxq);
9122         if (rc) {
9123                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9124                                 "(%d):2543 Mailbox command x%x (x%x/x%x) "
9125                                 "cannot issue Data: x%x x%x\n",
9126                                 mboxq->vport ? mboxq->vport->vpi : 0,
9127                                 mboxq->u.mb.mbxCommand,
9128                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9129                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9130                                 psli->sli_flag, flag);
9131                 goto out_not_finished;
9132         }
9133
9134         /* Put the mailbox command to the driver internal FIFO */
9135         psli->slistat.mbox_busy++;
9136         spin_lock_irqsave(&phba->hbalock, iflags);
9137         lpfc_mbox_put(phba, mboxq);
9138         spin_unlock_irqrestore(&phba->hbalock, iflags);
9139         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9140                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
9141                         "x%x (x%x/x%x) x%x x%x x%x\n",
9142                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
9143                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
9144                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9145                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9146                         phba->pport->port_state,
9147                         psli->sli_flag, MBX_NOWAIT);
9148         /* Wake up worker thread to transport mailbox command from head */
9149         lpfc_worker_wake_up(phba);
9150
9151         return MBX_BUSY;
9152
9153 out_not_finished:
9154         return MBX_NOT_FINISHED;
9155 }
9156
9157 /**
9158  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
9159  * @phba: Pointer to HBA context object.
9160  *
9161  * This function is called by worker thread to send a mailbox command to
9162  * SLI4 HBA firmware.
9163  *
9164  **/
9165 int
9166 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
9167 {
9168         struct lpfc_sli *psli = &phba->sli;
9169         LPFC_MBOXQ_t *mboxq;
9170         int rc = MBX_SUCCESS;
9171         unsigned long iflags;
9172         struct lpfc_mqe *mqe;
9173         uint32_t mbx_cmnd;
9174
9175         /* Check interrupt mode before post async mailbox command */
9176         if (unlikely(!phba->sli4_hba.intr_enable))
9177                 return MBX_NOT_FINISHED;
9178
9179         /* Check for mailbox command service token */
9180         spin_lock_irqsave(&phba->hbalock, iflags);
9181         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9182                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9183                 return MBX_NOT_FINISHED;
9184         }
9185         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9186                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9187                 return MBX_NOT_FINISHED;
9188         }
9189         if (unlikely(phba->sli.mbox_active)) {
9190                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9191                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9192                                 "0384 There is pending active mailbox cmd\n");
9193                 return MBX_NOT_FINISHED;
9194         }
9195         /* Take the mailbox command service token */
9196         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9197
9198         /* Get the next mailbox command from head of queue */
9199         mboxq = lpfc_mbox_get(phba);
9200
9201         /* If no more mailbox command waiting for post, we're done */
9202         if (!mboxq) {
9203                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9204                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9205                 return MBX_SUCCESS;
9206         }
9207         phba->sli.mbox_active = mboxq;
9208         spin_unlock_irqrestore(&phba->hbalock, iflags);
9209
9210         /* Check device readiness for posting mailbox command */
9211         rc = lpfc_mbox_dev_check(phba);
9212         if (unlikely(rc))
9213                 /* Driver clean routine will clean up pending mailbox */
9214                 goto out_not_finished;
9215
9216         /* Prepare the mbox command to be posted */
9217         mqe = &mboxq->u.mqe;
9218         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
9219
9220         /* Start timer for the mbox_tmo and log some mailbox post messages */
9221         mod_timer(&psli->mbox_tmo, (jiffies +
9222                   msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
9223
9224         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9225                         "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
9226                         "x%x x%x\n",
9227                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
9228                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9229                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9230                         phba->pport->port_state, psli->sli_flag);
9231
9232         if (mbx_cmnd != MBX_HEARTBEAT) {
9233                 if (mboxq->vport) {
9234                         lpfc_debugfs_disc_trc(mboxq->vport,
9235                                 LPFC_DISC_TRC_MBOX_VPORT,
9236                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
9237                                 mbx_cmnd, mqe->un.mb_words[0],
9238                                 mqe->un.mb_words[1]);
9239                 } else {
9240                         lpfc_debugfs_disc_trc(phba->pport,
9241                                 LPFC_DISC_TRC_MBOX,
9242                                 "MBOX Send: cmd:x%x mb:x%x x%x",
9243                                 mbx_cmnd, mqe->un.mb_words[0],
9244                                 mqe->un.mb_words[1]);
9245                 }
9246         }
9247         psli->slistat.mbox_cmd++;
9248
9249         /* Post the mailbox command to the port */
9250         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
9251         if (rc != MBX_SUCCESS) {
9252                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9253                                 "(%d):2533 Mailbox command x%x (x%x/x%x) "
9254                                 "cannot issue Data: x%x x%x\n",
9255                                 mboxq->vport ? mboxq->vport->vpi : 0,
9256                                 mboxq->u.mb.mbxCommand,
9257                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9258                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9259                                 psli->sli_flag, MBX_NOWAIT);
9260                 goto out_not_finished;
9261         }
9262
9263         return rc;
9264
9265 out_not_finished:
9266         spin_lock_irqsave(&phba->hbalock, iflags);
9267         if (phba->sli.mbox_active) {
9268                 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9269                 __lpfc_mbox_cmpl_put(phba, mboxq);
9270                 /* Release the token */
9271                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9272                 phba->sli.mbox_active = NULL;
9273         }
9274         spin_unlock_irqrestore(&phba->hbalock, iflags);
9275
9276         return MBX_NOT_FINISHED;
9277 }
9278
9279 /**
9280  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
9281  * @phba: Pointer to HBA context object.
9282  * @pmbox: Pointer to mailbox object.
9283  * @flag: Flag indicating how the mailbox need to be processed.
9284  *
9285  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
9286  * the API jump table function pointer from the lpfc_hba struct.
9287  *
9288  * Return codes the caller owns the mailbox command after the return of the
9289  * function.
9290  **/
9291 int
9292 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
9293 {
9294         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
9295 }
9296
9297 /**
9298  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
9299  * @phba: The hba struct for which this call is being executed.
9300  * @dev_grp: The HBA PCI-Device group number.
9301  *
9302  * This routine sets up the mbox interface API function jump table in @phba
9303  * struct.
9304  * Returns: 0 - success, -ENODEV - failure.
9305  **/
9306 int
9307 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9308 {
9309
9310         switch (dev_grp) {
9311         case LPFC_PCI_DEV_LP:
9312                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
9313                 phba->lpfc_sli_handle_slow_ring_event =
9314                                 lpfc_sli_handle_slow_ring_event_s3;
9315                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
9316                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
9317                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
9318                 break;
9319         case LPFC_PCI_DEV_OC:
9320                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
9321                 phba->lpfc_sli_handle_slow_ring_event =
9322                                 lpfc_sli_handle_slow_ring_event_s4;
9323                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
9324                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
9325                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
9326                 break;
9327         default:
9328                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9329                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
9330                                 dev_grp);
9331                 return -ENODEV;
9332         }
9333         return 0;
9334 }
9335
9336 /**
9337  * __lpfc_sli_ringtx_put - Add an iocb to the txq
9338  * @phba: Pointer to HBA context object.
9339  * @pring: Pointer to driver SLI ring object.
9340  * @piocb: Pointer to address of newly added command iocb.
9341  *
9342  * This function is called with hbalock held for SLI3 ports or
9343  * the ring lock held for SLI4 ports to add a command
9344  * iocb to the txq when SLI layer cannot submit the command iocb
9345  * to the ring.
9346  **/
9347 void
9348 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9349                     struct lpfc_iocbq *piocb)
9350 {
9351         if (phba->sli_rev == LPFC_SLI_REV4)
9352                 lockdep_assert_held(&pring->ring_lock);
9353         else
9354                 lockdep_assert_held(&phba->hbalock);
9355         /* Insert the caller's iocb in the txq tail for later processing. */
9356         list_add_tail(&piocb->list, &pring->txq);
9357 }
9358
9359 /**
9360  * lpfc_sli_next_iocb - Get the next iocb in the txq
9361  * @phba: Pointer to HBA context object.
9362  * @pring: Pointer to driver SLI ring object.
9363  * @piocb: Pointer to address of newly added command iocb.
9364  *
9365  * This function is called with hbalock held before a new
9366  * iocb is submitted to the firmware. This function checks
9367  * txq to flush the iocbs in txq to Firmware before
9368  * submitting new iocbs to the Firmware.
9369  * If there are iocbs in the txq which need to be submitted
9370  * to firmware, lpfc_sli_next_iocb returns the first element
9371  * of the txq after dequeuing it from txq.
9372  * If there is no iocb in the txq then the function will return
9373  * *piocb and *piocb is set to NULL. Caller needs to check
9374  * *piocb to find if there are more commands in the txq.
9375  **/
9376 static struct lpfc_iocbq *
9377 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9378                    struct lpfc_iocbq **piocb)
9379 {
9380         struct lpfc_iocbq * nextiocb;
9381
9382         lockdep_assert_held(&phba->hbalock);
9383
9384         nextiocb = lpfc_sli_ringtx_get(phba, pring);
9385         if (!nextiocb) {
9386                 nextiocb = *piocb;
9387                 *piocb = NULL;
9388         }
9389
9390         return nextiocb;
9391 }
9392
9393 /**
9394  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
9395  * @phba: Pointer to HBA context object.
9396  * @ring_number: SLI ring number to issue iocb on.
9397  * @piocb: Pointer to command iocb.
9398  * @flag: Flag indicating if this command can be put into txq.
9399  *
9400  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
9401  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
9402  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
9403  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
9404  * this function allows only iocbs for posting buffers. This function finds
9405  * next available slot in the command ring and posts the command to the
9406  * available slot and writes the port attention register to request HBA start
9407  * processing new iocb. If there is no slot available in the ring and
9408  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9409  * the function returns IOCB_BUSY.
9410  *
9411  * This function is called with hbalock held. The function will return success
9412  * after it successfully submit the iocb to firmware or after adding to the
9413  * txq.
9414  **/
9415 static int
9416 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
9417                     struct lpfc_iocbq *piocb, uint32_t flag)
9418 {
9419         struct lpfc_iocbq *nextiocb;
9420         IOCB_t *iocb;
9421         struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
9422
9423         lockdep_assert_held(&phba->hbalock);
9424
9425         if (piocb->iocb_cmpl && (!piocb->vport) &&
9426            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9427            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
9428                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9429                                 "1807 IOCB x%x failed. No vport\n",
9430                                 piocb->iocb.ulpCommand);
9431                 dump_stack();
9432                 return IOCB_ERROR;
9433         }
9434
9435
9436         /* If the PCI channel is in offline state, do not post iocbs. */
9437         if (unlikely(pci_channel_offline(phba->pcidev)))
9438                 return IOCB_ERROR;
9439
9440         /* If HBA has a deferred error attention, fail the iocb. */
9441         if (unlikely(phba->hba_flag & DEFER_ERATT))
9442                 return IOCB_ERROR;
9443
9444         /*
9445          * We should never get an IOCB if we are in a < LINK_DOWN state
9446          */
9447         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
9448                 return IOCB_ERROR;
9449
9450         /*
9451          * Check to see if we are blocking IOCB processing because of a
9452          * outstanding event.
9453          */
9454         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
9455                 goto iocb_busy;
9456
9457         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
9458                 /*
9459                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
9460                  * can be issued if the link is not up.
9461                  */
9462                 switch (piocb->iocb.ulpCommand) {
9463                 case CMD_GEN_REQUEST64_CR:
9464                 case CMD_GEN_REQUEST64_CX:
9465                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9466                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
9467                                         FC_RCTL_DD_UNSOL_CMD) ||
9468                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9469                                         MENLO_TRANSPORT_TYPE))
9470
9471                                 goto iocb_busy;
9472                         break;
9473                 case CMD_QUE_RING_BUF_CN:
9474                 case CMD_QUE_RING_BUF64_CN:
9475                         /*
9476                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9477                          * completion, iocb_cmpl MUST be 0.
9478                          */
9479                         if (piocb->iocb_cmpl)
9480                                 piocb->iocb_cmpl = NULL;
9481                         fallthrough;
9482                 case CMD_CREATE_XRI_CR:
9483                 case CMD_CLOSE_XRI_CN:
9484                 case CMD_CLOSE_XRI_CX:
9485                         break;
9486                 default:
9487                         goto iocb_busy;
9488                 }
9489
9490         /*
9491          * For FCP commands, we must be in a state where we can process link
9492          * attention events.
9493          */
9494         } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
9495                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
9496                 goto iocb_busy;
9497         }
9498
9499         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9500                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9501                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9502
9503         if (iocb)
9504                 lpfc_sli_update_ring(phba, pring);
9505         else
9506                 lpfc_sli_update_full_ring(phba, pring);
9507
9508         if (!piocb)
9509                 return IOCB_SUCCESS;
9510
9511         goto out_busy;
9512
9513  iocb_busy:
9514         pring->stats.iocb_cmd_delay++;
9515
9516  out_busy:
9517
9518         if (!(flag & SLI_IOCB_RET_IOCB)) {
9519                 __lpfc_sli_ringtx_put(phba, pring, piocb);
9520                 return IOCB_SUCCESS;
9521         }
9522
9523         return IOCB_BUSY;
9524 }
9525
9526 /**
9527  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9528  * @phba: Pointer to HBA context object.
9529  * @piocbq: Pointer to command iocb.
9530  * @sglq: Pointer to the scatter gather queue object.
9531  *
9532  * This routine converts the bpl or bde that is in the IOCB
9533  * to a sgl list for the sli4 hardware. The physical address
9534  * of the bpl/bde is converted back to a virtual address.
9535  * If the IOCB contains a BPL then the list of BDE's is
9536  * converted to sli4_sge's. If the IOCB contains a single
9537  * BDE then it is converted to a single sli_sge.
9538  * The IOCB is still in cpu endianess so the contents of
9539  * the bpl can be used without byte swapping.
9540  *
9541  * Returns valid XRI = Success, NO_XRI = Failure.
9542 **/
9543 static uint16_t
9544 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9545                 struct lpfc_sglq *sglq)
9546 {
9547         uint16_t xritag = NO_XRI;
9548         struct ulp_bde64 *bpl = NULL;
9549         struct ulp_bde64 bde;
9550         struct sli4_sge *sgl  = NULL;
9551         struct lpfc_dmabuf *dmabuf;
9552         IOCB_t *icmd;
9553         int numBdes = 0;
9554         int i = 0;
9555         uint32_t offset = 0; /* accumulated offset in the sg request list */
9556         int inbound = 0; /* number of sg reply entries inbound from firmware */
9557
9558         if (!piocbq || !sglq)
9559                 return xritag;
9560
9561         sgl  = (struct sli4_sge *)sglq->sgl;
9562         icmd = &piocbq->iocb;
9563         if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9564                 return sglq->sli4_xritag;
9565         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9566                 numBdes = icmd->un.genreq64.bdl.bdeSize /
9567                                 sizeof(struct ulp_bde64);
9568                 /* The addrHigh and addrLow fields within the IOCB
9569                  * have not been byteswapped yet so there is no
9570                  * need to swap them back.
9571                  */
9572                 if (piocbq->context3)
9573                         dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9574                 else
9575                         return xritag;
9576
9577                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
9578                 if (!bpl)
9579                         return xritag;
9580
9581                 for (i = 0; i < numBdes; i++) {
9582                         /* Should already be byte swapped. */
9583                         sgl->addr_hi = bpl->addrHigh;
9584                         sgl->addr_lo = bpl->addrLow;
9585
9586                         sgl->word2 = le32_to_cpu(sgl->word2);
9587                         if ((i+1) == numBdes)
9588                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
9589                         else
9590                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
9591                         /* swap the size field back to the cpu so we
9592                          * can assign it to the sgl.
9593                          */
9594                         bde.tus.w = le32_to_cpu(bpl->tus.w);
9595                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
9596                         /* The offsets in the sgl need to be accumulated
9597                          * separately for the request and reply lists.
9598                          * The request is always first, the reply follows.
9599                          */
9600                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9601                                 /* add up the reply sg entries */
9602                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9603                                         inbound++;
9604                                 /* first inbound? reset the offset */
9605                                 if (inbound == 1)
9606                                         offset = 0;
9607                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
9608                                 bf_set(lpfc_sli4_sge_type, sgl,
9609                                         LPFC_SGE_TYPE_DATA);
9610                                 offset += bde.tus.f.bdeSize;
9611                         }
9612                         sgl->word2 = cpu_to_le32(sgl->word2);
9613                         bpl++;
9614                         sgl++;
9615                 }
9616         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9617                         /* The addrHigh and addrLow fields of the BDE have not
9618                          * been byteswapped yet so they need to be swapped
9619                          * before putting them in the sgl.
9620                          */
9621                         sgl->addr_hi =
9622                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9623                         sgl->addr_lo =
9624                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
9625                         sgl->word2 = le32_to_cpu(sgl->word2);
9626                         bf_set(lpfc_sli4_sge_last, sgl, 1);
9627                         sgl->word2 = cpu_to_le32(sgl->word2);
9628                         sgl->sge_len =
9629                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
9630         }
9631         return sglq->sli4_xritag;
9632 }
9633
9634 /**
9635  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9636  * @phba: Pointer to HBA context object.
9637  * @iocbq: Pointer to command iocb.
9638  * @wqe: Pointer to the work queue entry.
9639  *
9640  * This routine converts the iocb command to its Work Queue Entry
9641  * equivalent. The wqe pointer should not have any fields set when
9642  * this routine is called because it will memcpy over them.
9643  * This routine does not set the CQ_ID or the WQEC bits in the
9644  * wqe.
9645  *
9646  * Returns: 0 = Success, IOCB_ERROR = Failure.
9647  **/
9648 static int
9649 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
9650                 union lpfc_wqe128 *wqe)
9651 {
9652         uint32_t xmit_len = 0, total_len = 0;
9653         uint8_t ct = 0;
9654         uint32_t fip;
9655         uint32_t abort_tag;
9656         uint8_t command_type = ELS_COMMAND_NON_FIP;
9657         uint8_t cmnd;
9658         uint16_t xritag;
9659         uint16_t abrt_iotag;
9660         struct lpfc_iocbq *abrtiocbq;
9661         struct ulp_bde64 *bpl = NULL;
9662         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
9663         int numBdes, i;
9664         struct ulp_bde64 bde;
9665         struct lpfc_nodelist *ndlp;
9666         uint32_t *pcmd;
9667         uint32_t if_type;
9668
9669         fip = phba->hba_flag & HBA_FIP_SUPPORT;
9670         /* The fcp commands will set command type */
9671         if (iocbq->iocb_flag &  LPFC_IO_FCP)
9672                 command_type = FCP_COMMAND;
9673         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
9674                 command_type = ELS_COMMAND_FIP;
9675         else
9676                 command_type = ELS_COMMAND_NON_FIP;
9677
9678         if (phba->fcp_embed_io)
9679                 memset(wqe, 0, sizeof(union lpfc_wqe128));
9680         /* Some of the fields are in the right position already */
9681         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
9682         /* The ct field has moved so reset */
9683         wqe->generic.wqe_com.word7 = 0;
9684         wqe->generic.wqe_com.word10 = 0;
9685
9686         abort_tag = (uint32_t) iocbq->iotag;
9687         xritag = iocbq->sli4_xritag;
9688         /* words0-2 bpl convert bde */
9689         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9690                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9691                                 sizeof(struct ulp_bde64);
9692                 bpl  = (struct ulp_bde64 *)
9693                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9694                 if (!bpl)
9695                         return IOCB_ERROR;
9696
9697                 /* Should already be byte swapped. */
9698                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
9699                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
9700                 /* swap the size field back to the cpu so we
9701                  * can assign it to the sgl.
9702                  */
9703                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
9704                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9705                 total_len = 0;
9706                 for (i = 0; i < numBdes; i++) {
9707                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
9708                         total_len += bde.tus.f.bdeSize;
9709                 }
9710         } else
9711                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
9712
9713         iocbq->iocb.ulpIoTag = iocbq->iotag;
9714         cmnd = iocbq->iocb.ulpCommand;
9715
9716         switch (iocbq->iocb.ulpCommand) {
9717         case CMD_ELS_REQUEST64_CR:
9718                 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9719                         ndlp = iocbq->context_un.ndlp;
9720                 else
9721                         ndlp = (struct lpfc_nodelist *)iocbq->context1;
9722                 if (!iocbq->iocb.ulpLe) {
9723                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9724                                 "2007 Only Limited Edition cmd Format"
9725                                 " supported 0x%x\n",
9726                                 iocbq->iocb.ulpCommand);
9727                         return IOCB_ERROR;
9728                 }
9729
9730                 wqe->els_req.payload_len = xmit_len;
9731                 /* Els_reguest64 has a TMO */
9732                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9733                         iocbq->iocb.ulpTimeout);
9734                 /* Need a VF for word 4 set the vf bit*/
9735                 bf_set(els_req64_vf, &wqe->els_req, 0);
9736                 /* And a VFID for word 12 */
9737                 bf_set(els_req64_vfid, &wqe->els_req, 0);
9738                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9739                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9740                        iocbq->iocb.ulpContext);
9741                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9742                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
9743                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
9744                 if (command_type == ELS_COMMAND_FIP)
9745                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9746                                         >> LPFC_FIP_ELS_ID_SHIFT);
9747                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9748                                         iocbq->context2)->virt);
9749                 if_type = bf_get(lpfc_sli_intf_if_type,
9750                                         &phba->sli4_hba.sli_intf);
9751                 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
9752                         if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
9753                                 *pcmd == ELS_CMD_SCR ||
9754                                 *pcmd == ELS_CMD_RDF ||
9755                                 *pcmd == ELS_CMD_RSCN_XMT ||
9756                                 *pcmd == ELS_CMD_FDISC ||
9757                                 *pcmd == ELS_CMD_LOGO ||
9758                                 *pcmd == ELS_CMD_PLOGI)) {
9759                                 bf_set(els_req64_sp, &wqe->els_req, 1);
9760                                 bf_set(els_req64_sid, &wqe->els_req,
9761                                         iocbq->vport->fc_myDID);
9762                                 if ((*pcmd == ELS_CMD_FLOGI) &&
9763                                         !(phba->fc_topology ==
9764                                                 LPFC_TOPOLOGY_LOOP))
9765                                         bf_set(els_req64_sid, &wqe->els_req, 0);
9766                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9767                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9768                                         phba->vpi_ids[iocbq->vport->vpi]);
9769                         } else if (pcmd && iocbq->context1) {
9770                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9771                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9772                                         phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9773                         }
9774                 }
9775                 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9776                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9777                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9778                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9779                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9780                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9781                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9782                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
9783                 wqe->els_req.max_response_payload_len = total_len - xmit_len;
9784                 break;
9785         case CMD_XMIT_SEQUENCE64_CX:
9786                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9787                        iocbq->iocb.un.ulpWord[3]);
9788                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
9789                        iocbq->iocb.unsli3.rcvsli3.ox_id);
9790                 /* The entire sequence is transmitted for this IOCB */
9791                 xmit_len = total_len;
9792                 cmnd = CMD_XMIT_SEQUENCE64_CR;
9793                 if (phba->link_flag & LS_LOOPBACK_MODE)
9794                         bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
9795                 fallthrough;
9796         case CMD_XMIT_SEQUENCE64_CR:
9797                 /* word3 iocb=io_tag32 wqe=reserved */
9798                 wqe->xmit_sequence.rsvd3 = 0;
9799                 /* word4 relative_offset memcpy */
9800                 /* word5 r_ctl/df_ctl memcpy */
9801                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9802                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9803                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9804                        LPFC_WQE_IOD_WRITE);
9805                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9806                        LPFC_WQE_LENLOC_WORD12);
9807                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
9808                 wqe->xmit_sequence.xmit_len = xmit_len;
9809                 command_type = OTHER_COMMAND;
9810                 break;
9811         case CMD_XMIT_BCAST64_CN:
9812                 /* word3 iocb=iotag32 wqe=seq_payload_len */
9813                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
9814                 /* word4 iocb=rsvd wqe=rsvd */
9815                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9816                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
9817                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
9818                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9819                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9820                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9821                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9822                        LPFC_WQE_LENLOC_WORD3);
9823                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
9824                 break;
9825         case CMD_FCP_IWRITE64_CR:
9826                 command_type = FCP_COMMAND_DATA_OUT;
9827                 /* word3 iocb=iotag wqe=payload_offset_len */
9828                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9829                 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9830                        xmit_len + sizeof(struct fcp_rsp));
9831                 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9832                        0);
9833                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9834                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9835                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9836                        iocbq->iocb.ulpFCP2Rcvy);
9837                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9838                 /* Always open the exchange */
9839                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9840                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9841                        LPFC_WQE_LENLOC_WORD4);
9842                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
9843                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
9844                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9845                         bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
9846                         bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9847                         if (iocbq->priority) {
9848                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9849                                        (iocbq->priority << 1));
9850                         } else {
9851                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9852                                        (phba->cfg_XLanePriority << 1));
9853                         }
9854                 }
9855                 /* Note, word 10 is already initialized to 0 */
9856
9857                 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9858                 if (phba->cfg_enable_pbde)
9859                         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9860                 else
9861                         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9862
9863                 if (phba->fcp_embed_io) {
9864                         struct lpfc_io_buf *lpfc_cmd;
9865                         struct sli4_sge *sgl;
9866                         struct fcp_cmnd *fcp_cmnd;
9867                         uint32_t *ptr;
9868
9869                         /* 128 byte wqe support here */
9870
9871                         lpfc_cmd = iocbq->context1;
9872                         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
9873                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9874
9875                         /* Word 0-2 - FCP_CMND */
9876                         wqe->generic.bde.tus.f.bdeFlags =
9877                                 BUFF_TYPE_BDE_IMMED;
9878                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9879                         wqe->generic.bde.addrHigh = 0;
9880                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9881
9882                         bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9883                         bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
9884
9885                         /* Word 22-29  FCP CMND Payload */
9886                         ptr = &wqe->words[22];
9887                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9888                 }
9889                 break;
9890         case CMD_FCP_IREAD64_CR:
9891                 /* word3 iocb=iotag wqe=payload_offset_len */
9892                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9893                 bf_set(payload_offset_len, &wqe->fcp_iread,
9894                        xmit_len + sizeof(struct fcp_rsp));
9895                 bf_set(cmd_buff_len, &wqe->fcp_iread,
9896                        0);
9897                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9898                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9899                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9900                        iocbq->iocb.ulpFCP2Rcvy);
9901                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
9902                 /* Always open the exchange */
9903                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9904                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9905                        LPFC_WQE_LENLOC_WORD4);
9906                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
9907                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
9908                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9909                         bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
9910                         bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9911                         if (iocbq->priority) {
9912                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9913                                        (iocbq->priority << 1));
9914                         } else {
9915                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9916                                        (phba->cfg_XLanePriority << 1));
9917                         }
9918                 }
9919                 /* Note, word 10 is already initialized to 0 */
9920
9921                 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9922                 if (phba->cfg_enable_pbde)
9923                         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9924                 else
9925                         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9926
9927                 if (phba->fcp_embed_io) {
9928                         struct lpfc_io_buf *lpfc_cmd;
9929                         struct sli4_sge *sgl;
9930                         struct fcp_cmnd *fcp_cmnd;
9931                         uint32_t *ptr;
9932
9933                         /* 128 byte wqe support here */
9934
9935                         lpfc_cmd = iocbq->context1;
9936                         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
9937                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9938
9939                         /* Word 0-2 - FCP_CMND */
9940                         wqe->generic.bde.tus.f.bdeFlags =
9941                                 BUFF_TYPE_BDE_IMMED;
9942                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9943                         wqe->generic.bde.addrHigh = 0;
9944                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9945
9946                         bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9947                         bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
9948
9949                         /* Word 22-29  FCP CMND Payload */
9950                         ptr = &wqe->words[22];
9951                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9952                 }
9953                 break;
9954         case CMD_FCP_ICMND64_CR:
9955                 /* word3 iocb=iotag wqe=payload_offset_len */
9956                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9957                 bf_set(payload_offset_len, &wqe->fcp_icmd,
9958                        xmit_len + sizeof(struct fcp_rsp));
9959                 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9960                        0);
9961                 /* word3 iocb=IO_TAG wqe=reserved */
9962                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
9963                 /* Always open the exchange */
9964                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9965                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9966                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9967                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9968                        LPFC_WQE_LENLOC_NONE);
9969                 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9970                        iocbq->iocb.ulpFCP2Rcvy);
9971                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9972                         bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
9973                         bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9974                         if (iocbq->priority) {
9975                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9976                                        (iocbq->priority << 1));
9977                         } else {
9978                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9979                                        (phba->cfg_XLanePriority << 1));
9980                         }
9981                 }
9982                 /* Note, word 10 is already initialized to 0 */
9983
9984                 if (phba->fcp_embed_io) {
9985                         struct lpfc_io_buf *lpfc_cmd;
9986                         struct sli4_sge *sgl;
9987                         struct fcp_cmnd *fcp_cmnd;
9988                         uint32_t *ptr;
9989
9990                         /* 128 byte wqe support here */
9991
9992                         lpfc_cmd = iocbq->context1;
9993                         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
9994                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9995
9996                         /* Word 0-2 - FCP_CMND */
9997                         wqe->generic.bde.tus.f.bdeFlags =
9998                                 BUFF_TYPE_BDE_IMMED;
9999                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10000                         wqe->generic.bde.addrHigh = 0;
10001                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
10002
10003                         bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
10004                         bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
10005
10006                         /* Word 22-29  FCP CMND Payload */
10007                         ptr = &wqe->words[22];
10008                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10009                 }
10010                 break;
10011         case CMD_GEN_REQUEST64_CR:
10012                 /* For this command calculate the xmit length of the
10013                  * request bde.
10014                  */
10015                 xmit_len = 0;
10016                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
10017                         sizeof(struct ulp_bde64);
10018                 for (i = 0; i < numBdes; i++) {
10019                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
10020                         if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
10021                                 break;
10022                         xmit_len += bde.tus.f.bdeSize;
10023                 }
10024                 /* word3 iocb=IO_TAG wqe=request_payload_len */
10025                 wqe->gen_req.request_payload_len = xmit_len;
10026                 /* word4 iocb=parameter wqe=relative_offset memcpy */
10027                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
10028                 /* word6 context tag copied in memcpy */
10029                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
10030                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
10031                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10032                                 "2015 Invalid CT %x command 0x%x\n",
10033                                 ct, iocbq->iocb.ulpCommand);
10034                         return IOCB_ERROR;
10035                 }
10036                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
10037                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
10038                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
10039                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
10040                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
10041                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
10042                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
10043                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
10044                 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
10045                 command_type = OTHER_COMMAND;
10046                 break;
10047         case CMD_XMIT_ELS_RSP64_CX:
10048                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
10049                 /* words0-2 BDE memcpy */
10050                 /* word3 iocb=iotag32 wqe=response_payload_len */
10051                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
10052                 /* word4 */
10053                 wqe->xmit_els_rsp.word4 = 0;
10054                 /* word5 iocb=rsvd wge=did */
10055                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
10056                          iocbq->iocb.un.xseq64.xmit_els_remoteID);
10057
10058                 if_type = bf_get(lpfc_sli_intf_if_type,
10059                                         &phba->sli4_hba.sli_intf);
10060                 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
10061                         if (iocbq->vport->fc_flag & FC_PT2PT) {
10062                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
10063                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
10064                                         iocbq->vport->fc_myDID);
10065                                 if (iocbq->vport->fc_myDID == Fabric_DID) {
10066                                         bf_set(wqe_els_did,
10067                                                 &wqe->xmit_els_rsp.wqe_dest, 0);
10068                                 }
10069                         }
10070                 }
10071                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
10072                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10073                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
10074                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
10075                        iocbq->iocb.unsli3.rcvsli3.ox_id);
10076                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
10077                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
10078                                phba->vpi_ids[iocbq->vport->vpi]);
10079                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
10080                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
10081                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
10082                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
10083                        LPFC_WQE_LENLOC_WORD3);
10084                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
10085                 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
10086                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
10087                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
10088                                         iocbq->context2)->virt);
10089                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
10090                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
10091                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
10092                                         iocbq->vport->fc_myDID);
10093                                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
10094                                 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
10095                                         phba->vpi_ids[phba->pport->vpi]);
10096                 }
10097                 command_type = OTHER_COMMAND;
10098                 break;
10099         case CMD_CLOSE_XRI_CN:
10100         case CMD_ABORT_XRI_CN:
10101         case CMD_ABORT_XRI_CX:
10102                 /* words 0-2 memcpy should be 0 rserved */
10103                 /* port will send abts */
10104                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
10105                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
10106                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
10107                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
10108                 } else
10109                         fip = 0;
10110
10111                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
10112                         /*
10113                          * The link is down, or the command was ELS_FIP
10114                          * so the fw does not need to send abts
10115                          * on the wire.
10116                          */
10117                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
10118                 else
10119                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
10120                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
10121                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
10122                 wqe->abort_cmd.rsrvd5 = 0;
10123                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
10124                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10125                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
10126                 /*
10127                  * The abort handler will send us CMD_ABORT_XRI_CN or
10128                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
10129                  */
10130                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10131                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
10132                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
10133                        LPFC_WQE_LENLOC_NONE);
10134                 cmnd = CMD_ABORT_XRI_CX;
10135                 command_type = OTHER_COMMAND;
10136                 xritag = 0;
10137                 break;
10138         case CMD_XMIT_BLS_RSP64_CX:
10139                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
10140                 /* As BLS ABTS RSP WQE is very different from other WQEs,
10141                  * we re-construct this WQE here based on information in
10142                  * iocbq from scratch.
10143                  */
10144                 memset(wqe, 0, sizeof(*wqe));
10145                 /* OX_ID is invariable to who sent ABTS to CT exchange */
10146                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
10147                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
10148                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
10149                     LPFC_ABTS_UNSOL_INT) {
10150                         /* ABTS sent by initiator to CT exchange, the
10151                          * RX_ID field will be filled with the newly
10152                          * allocated responder XRI.
10153                          */
10154                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10155                                iocbq->sli4_xritag);
10156                 } else {
10157                         /* ABTS sent by responder to CT exchange, the
10158                          * RX_ID field will be filled with the responder
10159                          * RX_ID from ABTS.
10160                          */
10161                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10162                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
10163                 }
10164                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
10165                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
10166
10167                 /* Use CT=VPI */
10168                 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
10169                         ndlp->nlp_DID);
10170                 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
10171                         iocbq->iocb.ulpContext);
10172                 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
10173                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
10174                         phba->vpi_ids[phba->pport->vpi]);
10175                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
10176                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
10177                        LPFC_WQE_LENLOC_NONE);
10178                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
10179                 command_type = OTHER_COMMAND;
10180                 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
10181                         bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
10182                                bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
10183                         bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
10184                                bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
10185                         bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
10186                                bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
10187                 }
10188
10189                 break;
10190         case CMD_SEND_FRAME:
10191                 bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_SEND_FRAME);
10192                 bf_set(wqe_sof, &wqe->generic.wqe_com, 0x2E); /* SOF byte */
10193                 bf_set(wqe_eof, &wqe->generic.wqe_com, 0x41); /* EOF byte */
10194                 bf_set(wqe_lenloc, &wqe->generic.wqe_com, 1);
10195                 bf_set(wqe_xbl, &wqe->generic.wqe_com, 1);
10196                 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10197                 bf_set(wqe_xc, &wqe->generic.wqe_com, 1);
10198                 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, 0xA);
10199                 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10200                 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10201                 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10202                 return 0;
10203         case CMD_XRI_ABORTED_CX:
10204         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
10205         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
10206         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
10207         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
10208         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
10209         default:
10210                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10211                                 "2014 Invalid command 0x%x\n",
10212                                 iocbq->iocb.ulpCommand);
10213                 return IOCB_ERROR;
10214         }
10215
10216         if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
10217                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
10218         else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
10219                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
10220         else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
10221                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
10222         iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
10223                               LPFC_IO_DIF_INSERT);
10224         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10225         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10226         wqe->generic.wqe_com.abort_tag = abort_tag;
10227         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
10228         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
10229         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
10230         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10231         return 0;
10232 }
10233
10234 /**
10235  * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb
10236  * @phba: Pointer to HBA context object.
10237  * @ring_number: SLI ring number to issue wqe on.
10238  * @piocb: Pointer to command iocb.
10239  * @flag: Flag indicating if this command can be put into txq.
10240  *
10241  * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to
10242  * send  an iocb command to an HBA with SLI-4 interface spec.
10243  *
10244  * This function takes the hbalock before invoking the lockless version.
10245  * The function will return success after it successfully submit the wqe to
10246  * firmware or after adding to the txq.
10247  **/
10248 static int
10249 __lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,
10250                            struct lpfc_iocbq *piocb, uint32_t flag)
10251 {
10252         unsigned long iflags;
10253         int rc;
10254
10255         spin_lock_irqsave(&phba->hbalock, iflags);
10256         rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);
10257         spin_unlock_irqrestore(&phba->hbalock, iflags);
10258
10259         return rc;
10260 }
10261
10262 /**
10263  * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe
10264  * @phba: Pointer to HBA context object.
10265  * @ring_number: SLI ring number to issue wqe on.
10266  * @piocb: Pointer to command iocb.
10267  * @flag: Flag indicating if this command can be put into txq.
10268  *
10269  * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue
10270  * an wqe command to an HBA with SLI-4 interface spec.
10271  *
10272  * This function is a lockless version. The function will return success
10273  * after it successfully submit the wqe to firmware or after adding to the
10274  * txq.
10275  **/
10276 static int
10277 __lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,
10278                            struct lpfc_iocbq *piocb, uint32_t flag)
10279 {
10280         int rc;
10281         struct lpfc_io_buf *lpfc_cmd =
10282                 (struct lpfc_io_buf *)piocb->context1;
10283         union lpfc_wqe128 *wqe = &piocb->wqe;
10284         struct sli4_sge *sgl;
10285
10286         /* 128 byte wqe support here */
10287         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
10288
10289         if (phba->fcp_embed_io) {
10290                 struct fcp_cmnd *fcp_cmnd;
10291                 u32 *ptr;
10292
10293                 fcp_cmnd = lpfc_cmd->fcp_cmnd;
10294
10295                 /* Word 0-2 - FCP_CMND */
10296                 wqe->generic.bde.tus.f.bdeFlags =
10297                         BUFF_TYPE_BDE_IMMED;
10298                 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10299                 wqe->generic.bde.addrHigh = 0;
10300                 wqe->generic.bde.addrLow =  88;  /* Word 22 */
10301
10302                 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10303                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10304
10305                 /* Word 22-29  FCP CMND Payload */
10306                 ptr = &wqe->words[22];
10307                 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10308         } else {
10309                 /* Word 0-2 - Inline BDE */
10310                 wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
10311                 wqe->generic.bde.tus.f.bdeSize = sizeof(struct fcp_cmnd);
10312                 wqe->generic.bde.addrHigh = sgl->addr_hi;
10313                 wqe->generic.bde.addrLow =  sgl->addr_lo;
10314
10315                 /* Word 10 */
10316                 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10317                 bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
10318         }
10319
10320         rc = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);
10321         return rc;
10322 }
10323
10324 /**
10325  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
10326  * @phba: Pointer to HBA context object.
10327  * @ring_number: SLI ring number to issue iocb on.
10328  * @piocb: Pointer to command iocb.
10329  * @flag: Flag indicating if this command can be put into txq.
10330  *
10331  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
10332  * an iocb command to an HBA with SLI-4 interface spec.
10333  *
10334  * This function is called with ringlock held. The function will return success
10335  * after it successfully submit the iocb to firmware or after adding to the
10336  * txq.
10337  **/
10338 static int
10339 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
10340                          struct lpfc_iocbq *piocb, uint32_t flag)
10341 {
10342         struct lpfc_sglq *sglq;
10343         union lpfc_wqe128 wqe;
10344         struct lpfc_queue *wq;
10345         struct lpfc_sli_ring *pring;
10346
10347         /* Get the WQ */
10348         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
10349             (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
10350                 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
10351         } else {
10352                 wq = phba->sli4_hba.els_wq;
10353         }
10354
10355         /* Get corresponding ring */
10356         pring = wq->pring;
10357
10358         /*
10359          * The WQE can be either 64 or 128 bytes,
10360          */
10361
10362         lockdep_assert_held(&pring->ring_lock);
10363
10364         if (piocb->sli4_xritag == NO_XRI) {
10365                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10366                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
10367                         sglq = NULL;
10368                 else {
10369                         if (!list_empty(&pring->txq)) {
10370                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
10371                                         __lpfc_sli_ringtx_put(phba,
10372                                                 pring, piocb);
10373                                         return IOCB_SUCCESS;
10374                                 } else {
10375                                         return IOCB_BUSY;
10376                                 }
10377                         } else {
10378                                 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
10379                                 if (!sglq) {
10380                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
10381                                                 __lpfc_sli_ringtx_put(phba,
10382                                                                 pring,
10383                                                                 piocb);
10384                                                 return IOCB_SUCCESS;
10385                                         } else
10386                                                 return IOCB_BUSY;
10387                                 }
10388                         }
10389                 }
10390         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
10391                 /* These IO's already have an XRI and a mapped sgl. */
10392                 sglq = NULL;
10393         }
10394         else {
10395                 /*
10396                  * This is a continuation of a commandi,(CX) so this
10397                  * sglq is on the active list
10398                  */
10399                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
10400                 if (!sglq)
10401                         return IOCB_ERROR;
10402         }
10403
10404         if (sglq) {
10405                 piocb->sli4_lxritag = sglq->sli4_lxritag;
10406                 piocb->sli4_xritag = sglq->sli4_xritag;
10407                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
10408                         return IOCB_ERROR;
10409         }
10410
10411         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
10412                 return IOCB_ERROR;
10413
10414         if (lpfc_sli4_wq_put(wq, &wqe))
10415                 return IOCB_ERROR;
10416         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
10417
10418         return 0;
10419 }
10420
10421 /**
10422  * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o
10423  *
10424  * This routine wraps the actual fcp i/o function for issusing WQE for sli-4
10425  * or IOCB for sli-3  function.
10426  * pointer from the lpfc_hba struct.
10427  *
10428  * Return codes:
10429  * IOCB_ERROR - Error
10430  * IOCB_SUCCESS - Success
10431  * IOCB_BUSY - Busy
10432  **/
10433 int
10434 lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,
10435                       struct lpfc_iocbq *piocb, uint32_t flag)
10436 {
10437         return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);
10438 }
10439
10440 /*
10441  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
10442  *
10443  * This routine wraps the actual lockless version for issusing IOCB function
10444  * pointer from the lpfc_hba struct.
10445  *
10446  * Return codes:
10447  * IOCB_ERROR - Error
10448  * IOCB_SUCCESS - Success
10449  * IOCB_BUSY - Busy
10450  **/
10451 int
10452 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10453                 struct lpfc_iocbq *piocb, uint32_t flag)
10454 {
10455         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10456 }
10457
10458 /**
10459  * lpfc_sli_api_table_setup - Set up sli api function jump table
10460  * @phba: The hba struct for which this call is being executed.
10461  * @dev_grp: The HBA PCI-Device group number.
10462  *
10463  * This routine sets up the SLI interface API function jump table in @phba
10464  * struct.
10465  * Returns: 0 - success, -ENODEV - failure.
10466  **/
10467 int
10468 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
10469 {
10470
10471         switch (dev_grp) {
10472         case LPFC_PCI_DEV_LP:
10473                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
10474                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
10475                 phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;
10476                 break;
10477         case LPFC_PCI_DEV_OC:
10478                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
10479                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
10480                 phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;
10481                 break;
10482         default:
10483                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10484                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
10485                                 dev_grp);
10486                 return -ENODEV;
10487         }
10488         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
10489         return 0;
10490 }
10491
10492 /**
10493  * lpfc_sli4_calc_ring - Calculates which ring to use
10494  * @phba: Pointer to HBA context object.
10495  * @piocb: Pointer to command iocb.
10496  *
10497  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
10498  * hba_wqidx, thus we need to calculate the corresponding ring.
10499  * Since ABORTS must go on the same WQ of the command they are
10500  * aborting, we use command's hba_wqidx.
10501  */
10502 struct lpfc_sli_ring *
10503 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
10504 {
10505         struct lpfc_io_buf *lpfc_cmd;
10506
10507         if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
10508                 if (unlikely(!phba->sli4_hba.hdwq))
10509                         return NULL;
10510                 /*
10511                  * for abort iocb hba_wqidx should already
10512                  * be setup based on what work queue we used.
10513                  */
10514                 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
10515                         lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
10516                         piocb->hba_wqidx = lpfc_cmd->hdwq_no;
10517                 }
10518                 return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
10519         } else {
10520                 if (unlikely(!phba->sli4_hba.els_wq))
10521                         return NULL;
10522                 piocb->hba_wqidx = 0;
10523                 return phba->sli4_hba.els_wq->pring;
10524         }
10525 }
10526
10527 /**
10528  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
10529  * @phba: Pointer to HBA context object.
10530  * @ring_number: Ring number
10531  * @piocb: Pointer to command iocb.
10532  * @flag: Flag indicating if this command can be put into txq.
10533  *
10534  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10535  * function. This function gets the hbalock and calls
10536  * __lpfc_sli_issue_iocb function and will return the error returned
10537  * by __lpfc_sli_issue_iocb function. This wrapper is used by
10538  * functions which do not hold hbalock.
10539  **/
10540 int
10541 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10542                     struct lpfc_iocbq *piocb, uint32_t flag)
10543 {
10544         struct lpfc_sli_ring *pring;
10545         struct lpfc_queue *eq;
10546         unsigned long iflags;
10547         int rc;
10548
10549         if (phba->sli_rev == LPFC_SLI_REV4) {
10550                 eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
10551
10552                 pring = lpfc_sli4_calc_ring(phba, piocb);
10553                 if (unlikely(pring == NULL))
10554                         return IOCB_ERROR;
10555
10556                 spin_lock_irqsave(&pring->ring_lock, iflags);
10557                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10558                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10559
10560                 lpfc_sli4_poll_eq(eq, LPFC_POLL_FASTPATH);
10561         } else {
10562                 /* For now, SLI2/3 will still use hbalock */
10563                 spin_lock_irqsave(&phba->hbalock, iflags);
10564                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10565                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10566         }
10567         return rc;
10568 }
10569
10570 /**
10571  * lpfc_extra_ring_setup - Extra ring setup function
10572  * @phba: Pointer to HBA context object.
10573  *
10574  * This function is called while driver attaches with the
10575  * HBA to setup the extra ring. The extra ring is used
10576  * only when driver needs to support target mode functionality
10577  * or IP over FC functionalities.
10578  *
10579  * This function is called with no lock held. SLI3 only.
10580  **/
10581 static int
10582 lpfc_extra_ring_setup( struct lpfc_hba *phba)
10583 {
10584         struct lpfc_sli *psli;
10585         struct lpfc_sli_ring *pring;
10586
10587         psli = &phba->sli;
10588
10589         /* Adjust cmd/rsp ring iocb entries more evenly */
10590
10591         /* Take some away from the FCP ring */
10592         pring = &psli->sli3_ring[LPFC_FCP_RING];
10593         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10594         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10595         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10596         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10597
10598         /* and give them to the extra ring */
10599         pring = &psli->sli3_ring[LPFC_EXTRA_RING];
10600
10601         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10602         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10603         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10604         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10605
10606         /* Setup default profile for this ring */
10607         pring->iotag_max = 4096;
10608         pring->num_mask = 1;
10609         pring->prt[0].profile = 0;      /* Mask 0 */
10610         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10611         pring->prt[0].type = phba->cfg_multi_ring_type;
10612         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10613         return 0;
10614 }
10615
10616 static void
10617 lpfc_sli_post_recovery_event(struct lpfc_hba *phba,
10618                              struct lpfc_nodelist *ndlp)
10619 {
10620         unsigned long iflags;
10621         struct lpfc_work_evt  *evtp = &ndlp->recovery_evt;
10622
10623         spin_lock_irqsave(&phba->hbalock, iflags);
10624         if (!list_empty(&evtp->evt_listp)) {
10625                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10626                 return;
10627         }
10628
10629         /* Incrementing the reference count until the queued work is done. */
10630         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
10631         if (!evtp->evt_arg1) {
10632                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10633                 return;
10634         }
10635         evtp->evt = LPFC_EVT_RECOVER_PORT;
10636         list_add_tail(&evtp->evt_listp, &phba->work_list);
10637         spin_unlock_irqrestore(&phba->hbalock, iflags);
10638
10639         lpfc_worker_wake_up(phba);
10640 }
10641
10642 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10643  * @phba: Pointer to HBA context object.
10644  * @iocbq: Pointer to iocb object.
10645  *
10646  * The async_event handler calls this routine when it receives
10647  * an ASYNC_STATUS_CN event from the port.  The port generates
10648  * this event when an Abort Sequence request to an rport fails
10649  * twice in succession.  The abort could be originated by the
10650  * driver or by the port.  The ABTS could have been for an ELS
10651  * or FCP IO.  The port only generates this event when an ABTS
10652  * fails to complete after one retry.
10653  */
10654 static void
10655 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10656                           struct lpfc_iocbq *iocbq)
10657 {
10658         struct lpfc_nodelist *ndlp = NULL;
10659         uint16_t rpi = 0, vpi = 0;
10660         struct lpfc_vport *vport = NULL;
10661
10662         /* The rpi in the ulpContext is vport-sensitive. */
10663         vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10664         rpi = iocbq->iocb.ulpContext;
10665
10666         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10667                         "3092 Port generated ABTS async event "
10668                         "on vpi %d rpi %d status 0x%x\n",
10669                         vpi, rpi, iocbq->iocb.ulpStatus);
10670
10671         vport = lpfc_find_vport_by_vpid(phba, vpi);
10672         if (!vport)
10673                 goto err_exit;
10674         ndlp = lpfc_findnode_rpi(vport, rpi);
10675         if (!ndlp)
10676                 goto err_exit;
10677
10678         if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10679                 lpfc_sli_abts_recover_port(vport, ndlp);
10680         return;
10681
10682  err_exit:
10683         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10684                         "3095 Event Context not found, no "
10685                         "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10686                         iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10687                         vpi, rpi);
10688 }
10689
10690 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10691  * @phba: pointer to HBA context object.
10692  * @ndlp: nodelist pointer for the impacted rport.
10693  * @axri: pointer to the wcqe containing the failed exchange.
10694  *
10695  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10696  * port.  The port generates this event when an abort exchange request to an
10697  * rport fails twice in succession with no reply.  The abort could be originated
10698  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
10699  */
10700 void
10701 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10702                            struct lpfc_nodelist *ndlp,
10703                            struct sli4_wcqe_xri_aborted *axri)
10704 {
10705         uint32_t ext_status = 0;
10706
10707         if (!ndlp) {
10708                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10709                                 "3115 Node Context not found, driver "
10710                                 "ignoring abts err event\n");
10711                 return;
10712         }
10713
10714         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10715                         "3116 Port generated FCP XRI ABORT event on "
10716                         "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
10717                         ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
10718                         bf_get(lpfc_wcqe_xa_xri, axri),
10719                         bf_get(lpfc_wcqe_xa_status, axri),
10720                         axri->parameter);
10721
10722         /*
10723          * Catch the ABTS protocol failure case.  Older OCe FW releases returned
10724          * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10725          * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10726          */
10727         ext_status = axri->parameter & IOERR_PARAM_MASK;
10728         if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10729             ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
10730                 lpfc_sli_post_recovery_event(phba, ndlp);
10731 }
10732
10733 /**
10734  * lpfc_sli_async_event_handler - ASYNC iocb handler function
10735  * @phba: Pointer to HBA context object.
10736  * @pring: Pointer to driver SLI ring object.
10737  * @iocbq: Pointer to iocb object.
10738  *
10739  * This function is called by the slow ring event handler
10740  * function when there is an ASYNC event iocb in the ring.
10741  * This function is called with no lock held.
10742  * Currently this function handles only temperature related
10743  * ASYNC events. The function decodes the temperature sensor
10744  * event message and posts events for the management applications.
10745  **/
10746 static void
10747 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10748         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10749 {
10750         IOCB_t *icmd;
10751         uint16_t evt_code;
10752         struct temp_event temp_event_data;
10753         struct Scsi_Host *shost;
10754         uint32_t *iocb_w;
10755
10756         icmd = &iocbq->iocb;
10757         evt_code = icmd->un.asyncstat.evt_code;
10758
10759         switch (evt_code) {
10760         case ASYNC_TEMP_WARN:
10761         case ASYNC_TEMP_SAFE:
10762                 temp_event_data.data = (uint32_t) icmd->ulpContext;
10763                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10764                 if (evt_code == ASYNC_TEMP_WARN) {
10765                         temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10766                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10767                                 "0347 Adapter is very hot, please take "
10768                                 "corrective action. temperature : %d Celsius\n",
10769                                 (uint32_t) icmd->ulpContext);
10770                 } else {
10771                         temp_event_data.event_code = LPFC_NORMAL_TEMP;
10772                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10773                                 "0340 Adapter temperature is OK now. "
10774                                 "temperature : %d Celsius\n",
10775                                 (uint32_t) icmd->ulpContext);
10776                 }
10777
10778                 /* Send temperature change event to applications */
10779                 shost = lpfc_shost_from_vport(phba->pport);
10780                 fc_host_post_vendor_event(shost, fc_get_event_number(),
10781                         sizeof(temp_event_data), (char *) &temp_event_data,
10782                         LPFC_NL_VENDOR_ID);
10783                 break;
10784         case ASYNC_STATUS_CN:
10785                 lpfc_sli_abts_err_handler(phba, iocbq);
10786                 break;
10787         default:
10788                 iocb_w = (uint32_t *) icmd;
10789                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10790                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
10791                         " evt_code 0x%x\n"
10792                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
10793                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
10794                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
10795                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
10796                         pring->ringno, icmd->un.asyncstat.evt_code,
10797                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10798                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10799                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10800                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10801
10802                 break;
10803         }
10804 }
10805
10806
10807 /**
10808  * lpfc_sli4_setup - SLI ring setup function
10809  * @phba: Pointer to HBA context object.
10810  *
10811  * lpfc_sli_setup sets up rings of the SLI interface with
10812  * number of iocbs per ring and iotags. This function is
10813  * called while driver attach to the HBA and before the
10814  * interrupts are enabled. So there is no need for locking.
10815  *
10816  * This function always returns 0.
10817  **/
10818 int
10819 lpfc_sli4_setup(struct lpfc_hba *phba)
10820 {
10821         struct lpfc_sli_ring *pring;
10822
10823         pring = phba->sli4_hba.els_wq->pring;
10824         pring->num_mask = LPFC_MAX_RING_MASK;
10825         pring->prt[0].profile = 0;      /* Mask 0 */
10826         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10827         pring->prt[0].type = FC_TYPE_ELS;
10828         pring->prt[0].lpfc_sli_rcv_unsol_event =
10829             lpfc_els_unsol_event;
10830         pring->prt[1].profile = 0;      /* Mask 1 */
10831         pring->prt[1].rctl = FC_RCTL_ELS_REP;
10832         pring->prt[1].type = FC_TYPE_ELS;
10833         pring->prt[1].lpfc_sli_rcv_unsol_event =
10834             lpfc_els_unsol_event;
10835         pring->prt[2].profile = 0;      /* Mask 2 */
10836         /* NameServer Inquiry */
10837         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10838         /* NameServer */
10839         pring->prt[2].type = FC_TYPE_CT;
10840         pring->prt[2].lpfc_sli_rcv_unsol_event =
10841             lpfc_ct_unsol_event;
10842         pring->prt[3].profile = 0;      /* Mask 3 */
10843         /* NameServer response */
10844         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10845         /* NameServer */
10846         pring->prt[3].type = FC_TYPE_CT;
10847         pring->prt[3].lpfc_sli_rcv_unsol_event =
10848             lpfc_ct_unsol_event;
10849         return 0;
10850 }
10851
10852 /**
10853  * lpfc_sli_setup - SLI ring setup function
10854  * @phba: Pointer to HBA context object.
10855  *
10856  * lpfc_sli_setup sets up rings of the SLI interface with
10857  * number of iocbs per ring and iotags. This function is
10858  * called while driver attach to the HBA and before the
10859  * interrupts are enabled. So there is no need for locking.
10860  *
10861  * This function always returns 0. SLI3 only.
10862  **/
10863 int
10864 lpfc_sli_setup(struct lpfc_hba *phba)
10865 {
10866         int i, totiocbsize = 0;
10867         struct lpfc_sli *psli = &phba->sli;
10868         struct lpfc_sli_ring *pring;
10869
10870         psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
10871         psli->sli_flag = 0;
10872
10873         psli->iocbq_lookup = NULL;
10874         psli->iocbq_lookup_len = 0;
10875         psli->last_iotag = 0;
10876
10877         for (i = 0; i < psli->num_rings; i++) {
10878                 pring = &psli->sli3_ring[i];
10879                 switch (i) {
10880                 case LPFC_FCP_RING:     /* ring 0 - FCP */
10881                         /* numCiocb and numRiocb are used in config_port */
10882                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10883                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10884                         pring->sli.sli3.numCiocb +=
10885                                 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10886                         pring->sli.sli3.numRiocb +=
10887                                 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10888                         pring->sli.sli3.numCiocb +=
10889                                 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10890                         pring->sli.sli3.numRiocb +=
10891                                 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10892                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10893                                                         SLI3_IOCB_CMD_SIZE :
10894                                                         SLI2_IOCB_CMD_SIZE;
10895                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10896                                                         SLI3_IOCB_RSP_SIZE :
10897                                                         SLI2_IOCB_RSP_SIZE;
10898                         pring->iotag_ctr = 0;
10899                         pring->iotag_max =
10900                             (phba->cfg_hba_queue_depth * 2);
10901                         pring->fast_iotag = pring->iotag_max;
10902                         pring->num_mask = 0;
10903                         break;
10904                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
10905                         /* numCiocb and numRiocb are used in config_port */
10906                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10907                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10908                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10909                                                         SLI3_IOCB_CMD_SIZE :
10910                                                         SLI2_IOCB_CMD_SIZE;
10911                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10912                                                         SLI3_IOCB_RSP_SIZE :
10913                                                         SLI2_IOCB_RSP_SIZE;
10914                         pring->iotag_max = phba->cfg_hba_queue_depth;
10915                         pring->num_mask = 0;
10916                         break;
10917                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
10918                         /* numCiocb and numRiocb are used in config_port */
10919                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10920                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10921                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10922                                                         SLI3_IOCB_CMD_SIZE :
10923                                                         SLI2_IOCB_CMD_SIZE;
10924                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10925                                                         SLI3_IOCB_RSP_SIZE :
10926                                                         SLI2_IOCB_RSP_SIZE;
10927                         pring->fast_iotag = 0;
10928                         pring->iotag_ctr = 0;
10929                         pring->iotag_max = 4096;
10930                         pring->lpfc_sli_rcv_async_status =
10931                                 lpfc_sli_async_event_handler;
10932                         pring->num_mask = LPFC_MAX_RING_MASK;
10933                         pring->prt[0].profile = 0;      /* Mask 0 */
10934                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10935                         pring->prt[0].type = FC_TYPE_ELS;
10936                         pring->prt[0].lpfc_sli_rcv_unsol_event =
10937                             lpfc_els_unsol_event;
10938                         pring->prt[1].profile = 0;      /* Mask 1 */
10939                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
10940                         pring->prt[1].type = FC_TYPE_ELS;
10941                         pring->prt[1].lpfc_sli_rcv_unsol_event =
10942                             lpfc_els_unsol_event;
10943                         pring->prt[2].profile = 0;      /* Mask 2 */
10944                         /* NameServer Inquiry */
10945                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10946                         /* NameServer */
10947                         pring->prt[2].type = FC_TYPE_CT;
10948                         pring->prt[2].lpfc_sli_rcv_unsol_event =
10949                             lpfc_ct_unsol_event;
10950                         pring->prt[3].profile = 0;      /* Mask 3 */
10951                         /* NameServer response */
10952                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10953                         /* NameServer */
10954                         pring->prt[3].type = FC_TYPE_CT;
10955                         pring->prt[3].lpfc_sli_rcv_unsol_event =
10956                             lpfc_ct_unsol_event;
10957                         break;
10958                 }
10959                 totiocbsize += (pring->sli.sli3.numCiocb *
10960                         pring->sli.sli3.sizeCiocb) +
10961                         (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
10962         }
10963         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
10964                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
10965                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10966                        "SLI2 SLIM Data: x%x x%lx\n",
10967                        phba->brd_no, totiocbsize,
10968                        (unsigned long) MAX_SLIM_IOCB_SIZE);
10969         }
10970         if (phba->cfg_multi_ring_support == 2)
10971                 lpfc_extra_ring_setup(phba);
10972
10973         return 0;
10974 }
10975
10976 /**
10977  * lpfc_sli4_queue_init - Queue initialization function
10978  * @phba: Pointer to HBA context object.
10979  *
10980  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
10981  * ring. This function also initializes ring indices of each ring.
10982  * This function is called during the initialization of the SLI
10983  * interface of an HBA.
10984  * This function is called with no lock held and always returns
10985  * 1.
10986  **/
10987 void
10988 lpfc_sli4_queue_init(struct lpfc_hba *phba)
10989 {
10990         struct lpfc_sli *psli;
10991         struct lpfc_sli_ring *pring;
10992         int i;
10993
10994         psli = &phba->sli;
10995         spin_lock_irq(&phba->hbalock);
10996         INIT_LIST_HEAD(&psli->mboxq);
10997         INIT_LIST_HEAD(&psli->mboxq_cmpl);
10998         /* Initialize list headers for txq and txcmplq as double linked lists */
10999         for (i = 0; i < phba->cfg_hdw_queue; i++) {
11000                 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
11001                 pring->flag = 0;
11002                 pring->ringno = LPFC_FCP_RING;
11003                 pring->txcmplq_cnt = 0;
11004                 INIT_LIST_HEAD(&pring->txq);
11005                 INIT_LIST_HEAD(&pring->txcmplq);
11006                 INIT_LIST_HEAD(&pring->iocb_continueq);
11007                 spin_lock_init(&pring->ring_lock);
11008         }
11009         pring = phba->sli4_hba.els_wq->pring;
11010         pring->flag = 0;
11011         pring->ringno = LPFC_ELS_RING;
11012         pring->txcmplq_cnt = 0;
11013         INIT_LIST_HEAD(&pring->txq);
11014         INIT_LIST_HEAD(&pring->txcmplq);
11015         INIT_LIST_HEAD(&pring->iocb_continueq);
11016         spin_lock_init(&pring->ring_lock);
11017
11018         if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11019                 pring = phba->sli4_hba.nvmels_wq->pring;
11020                 pring->flag = 0;
11021                 pring->ringno = LPFC_ELS_RING;
11022                 pring->txcmplq_cnt = 0;
11023                 INIT_LIST_HEAD(&pring->txq);
11024                 INIT_LIST_HEAD(&pring->txcmplq);
11025                 INIT_LIST_HEAD(&pring->iocb_continueq);
11026                 spin_lock_init(&pring->ring_lock);
11027         }
11028
11029         spin_unlock_irq(&phba->hbalock);
11030 }
11031
11032 /**
11033  * lpfc_sli_queue_init - Queue initialization function
11034  * @phba: Pointer to HBA context object.
11035  *
11036  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
11037  * ring. This function also initializes ring indices of each ring.
11038  * This function is called during the initialization of the SLI
11039  * interface of an HBA.
11040  * This function is called with no lock held and always returns
11041  * 1.
11042  **/
11043 void
11044 lpfc_sli_queue_init(struct lpfc_hba *phba)
11045 {
11046         struct lpfc_sli *psli;
11047         struct lpfc_sli_ring *pring;
11048         int i;
11049
11050         psli = &phba->sli;
11051         spin_lock_irq(&phba->hbalock);
11052         INIT_LIST_HEAD(&psli->mboxq);
11053         INIT_LIST_HEAD(&psli->mboxq_cmpl);
11054         /* Initialize list headers for txq and txcmplq as double linked lists */
11055         for (i = 0; i < psli->num_rings; i++) {
11056                 pring = &psli->sli3_ring[i];
11057                 pring->ringno = i;
11058                 pring->sli.sli3.next_cmdidx  = 0;
11059                 pring->sli.sli3.local_getidx = 0;
11060                 pring->sli.sli3.cmdidx = 0;
11061                 INIT_LIST_HEAD(&pring->iocb_continueq);
11062                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
11063                 INIT_LIST_HEAD(&pring->postbufq);
11064                 pring->flag = 0;
11065                 INIT_LIST_HEAD(&pring->txq);
11066                 INIT_LIST_HEAD(&pring->txcmplq);
11067                 spin_lock_init(&pring->ring_lock);
11068         }
11069         spin_unlock_irq(&phba->hbalock);
11070 }
11071
11072 /**
11073  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
11074  * @phba: Pointer to HBA context object.
11075  *
11076  * This routine flushes the mailbox command subsystem. It will unconditionally
11077  * flush all the mailbox commands in the three possible stages in the mailbox
11078  * command sub-system: pending mailbox command queue; the outstanding mailbox
11079  * command; and completed mailbox command queue. It is caller's responsibility
11080  * to make sure that the driver is in the proper state to flush the mailbox
11081  * command sub-system. Namely, the posting of mailbox commands into the
11082  * pending mailbox command queue from the various clients must be stopped;
11083  * either the HBA is in a state that it will never works on the outstanding
11084  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
11085  * mailbox command has been completed.
11086  **/
11087 static void
11088 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
11089 {
11090         LIST_HEAD(completions);
11091         struct lpfc_sli *psli = &phba->sli;
11092         LPFC_MBOXQ_t *pmb;
11093         unsigned long iflag;
11094
11095         /* Disable softirqs, including timers from obtaining phba->hbalock */
11096         local_bh_disable();
11097
11098         /* Flush all the mailbox commands in the mbox system */
11099         spin_lock_irqsave(&phba->hbalock, iflag);
11100
11101         /* The pending mailbox command queue */
11102         list_splice_init(&phba->sli.mboxq, &completions);
11103         /* The outstanding active mailbox command */
11104         if (psli->mbox_active) {
11105                 list_add_tail(&psli->mbox_active->list, &completions);
11106                 psli->mbox_active = NULL;
11107                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11108         }
11109         /* The completed mailbox command queue */
11110         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
11111         spin_unlock_irqrestore(&phba->hbalock, iflag);
11112
11113         /* Enable softirqs again, done with phba->hbalock */
11114         local_bh_enable();
11115
11116         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
11117         while (!list_empty(&completions)) {
11118                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
11119                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
11120                 if (pmb->mbox_cmpl)
11121                         pmb->mbox_cmpl(phba, pmb);
11122         }
11123 }
11124
11125 /**
11126  * lpfc_sli_host_down - Vport cleanup function
11127  * @vport: Pointer to virtual port object.
11128  *
11129  * lpfc_sli_host_down is called to clean up the resources
11130  * associated with a vport before destroying virtual
11131  * port data structures.
11132  * This function does following operations:
11133  * - Free discovery resources associated with this virtual
11134  *   port.
11135  * - Free iocbs associated with this virtual port in
11136  *   the txq.
11137  * - Send abort for all iocb commands associated with this
11138  *   vport in txcmplq.
11139  *
11140  * This function is called with no lock held and always returns 1.
11141  **/
11142 int
11143 lpfc_sli_host_down(struct lpfc_vport *vport)
11144 {
11145         LIST_HEAD(completions);
11146         struct lpfc_hba *phba = vport->phba;
11147         struct lpfc_sli *psli = &phba->sli;
11148         struct lpfc_queue *qp = NULL;
11149         struct lpfc_sli_ring *pring;
11150         struct lpfc_iocbq *iocb, *next_iocb;
11151         int i;
11152         unsigned long flags = 0;
11153         uint16_t prev_pring_flag;
11154
11155         lpfc_cleanup_discovery_resources(vport);
11156
11157         spin_lock_irqsave(&phba->hbalock, flags);
11158
11159         /*
11160          * Error everything on the txq since these iocbs
11161          * have not been given to the FW yet.
11162          * Also issue ABTS for everything on the txcmplq
11163          */
11164         if (phba->sli_rev != LPFC_SLI_REV4) {
11165                 for (i = 0; i < psli->num_rings; i++) {
11166                         pring = &psli->sli3_ring[i];
11167                         prev_pring_flag = pring->flag;
11168                         /* Only slow rings */
11169                         if (pring->ringno == LPFC_ELS_RING) {
11170                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11171                                 /* Set the lpfc data pending flag */
11172                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
11173                         }
11174                         list_for_each_entry_safe(iocb, next_iocb,
11175                                                  &pring->txq, list) {
11176                                 if (iocb->vport != vport)
11177                                         continue;
11178                                 list_move_tail(&iocb->list, &completions);
11179                         }
11180                         list_for_each_entry_safe(iocb, next_iocb,
11181                                                  &pring->txcmplq, list) {
11182                                 if (iocb->vport != vport)
11183                                         continue;
11184                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11185                                                            NULL);
11186                         }
11187                         pring->flag = prev_pring_flag;
11188                 }
11189         } else {
11190                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11191                         pring = qp->pring;
11192                         if (!pring)
11193                                 continue;
11194                         if (pring == phba->sli4_hba.els_wq->pring) {
11195                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11196                                 /* Set the lpfc data pending flag */
11197                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
11198                         }
11199                         prev_pring_flag = pring->flag;
11200                         spin_lock(&pring->ring_lock);
11201                         list_for_each_entry_safe(iocb, next_iocb,
11202                                                  &pring->txq, list) {
11203                                 if (iocb->vport != vport)
11204                                         continue;
11205                                 list_move_tail(&iocb->list, &completions);
11206                         }
11207                         spin_unlock(&pring->ring_lock);
11208                         list_for_each_entry_safe(iocb, next_iocb,
11209                                                  &pring->txcmplq, list) {
11210                                 if (iocb->vport != vport)
11211                                         continue;
11212                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11213                                                            NULL);
11214                         }
11215                         pring->flag = prev_pring_flag;
11216                 }
11217         }
11218         spin_unlock_irqrestore(&phba->hbalock, flags);
11219
11220         /* Make sure HBA is alive */
11221         lpfc_issue_hb_tmo(phba);
11222
11223         /* Cancel all the IOCBs from the completions list */
11224         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11225                               IOERR_SLI_DOWN);
11226         return 1;
11227 }
11228
11229 /**
11230  * lpfc_sli_hba_down - Resource cleanup function for the HBA
11231  * @phba: Pointer to HBA context object.
11232  *
11233  * This function cleans up all iocb, buffers, mailbox commands
11234  * while shutting down the HBA. This function is called with no
11235  * lock held and always returns 1.
11236  * This function does the following to cleanup driver resources:
11237  * - Free discovery resources for each virtual port
11238  * - Cleanup any pending fabric iocbs
11239  * - Iterate through the iocb txq and free each entry
11240  *   in the list.
11241  * - Free up any buffer posted to the HBA
11242  * - Free mailbox commands in the mailbox queue.
11243  **/
11244 int
11245 lpfc_sli_hba_down(struct lpfc_hba *phba)
11246 {
11247         LIST_HEAD(completions);
11248         struct lpfc_sli *psli = &phba->sli;
11249         struct lpfc_queue *qp = NULL;
11250         struct lpfc_sli_ring *pring;
11251         struct lpfc_dmabuf *buf_ptr;
11252         unsigned long flags = 0;
11253         int i;
11254
11255         /* Shutdown the mailbox command sub-system */
11256         lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
11257
11258         lpfc_hba_down_prep(phba);
11259
11260         /* Disable softirqs, including timers from obtaining phba->hbalock */
11261         local_bh_disable();
11262
11263         lpfc_fabric_abort_hba(phba);
11264
11265         spin_lock_irqsave(&phba->hbalock, flags);
11266
11267         /*
11268          * Error everything on the txq since these iocbs
11269          * have not been given to the FW yet.
11270          */
11271         if (phba->sli_rev != LPFC_SLI_REV4) {
11272                 for (i = 0; i < psli->num_rings; i++) {
11273                         pring = &psli->sli3_ring[i];
11274                         /* Only slow rings */
11275                         if (pring->ringno == LPFC_ELS_RING) {
11276                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11277                                 /* Set the lpfc data pending flag */
11278                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
11279                         }
11280                         list_splice_init(&pring->txq, &completions);
11281                 }
11282         } else {
11283                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11284                         pring = qp->pring;
11285                         if (!pring)
11286                                 continue;
11287                         spin_lock(&pring->ring_lock);
11288                         list_splice_init(&pring->txq, &completions);
11289                         spin_unlock(&pring->ring_lock);
11290                         if (pring == phba->sli4_hba.els_wq->pring) {
11291                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11292                                 /* Set the lpfc data pending flag */
11293                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
11294                         }
11295                 }
11296         }
11297         spin_unlock_irqrestore(&phba->hbalock, flags);
11298
11299         /* Cancel all the IOCBs from the completions list */
11300         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11301                               IOERR_SLI_DOWN);
11302
11303         spin_lock_irqsave(&phba->hbalock, flags);
11304         list_splice_init(&phba->elsbuf, &completions);
11305         phba->elsbuf_cnt = 0;
11306         phba->elsbuf_prev_cnt = 0;
11307         spin_unlock_irqrestore(&phba->hbalock, flags);
11308
11309         while (!list_empty(&completions)) {
11310                 list_remove_head(&completions, buf_ptr,
11311                         struct lpfc_dmabuf, list);
11312                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
11313                 kfree(buf_ptr);
11314         }
11315
11316         /* Enable softirqs again, done with phba->hbalock */
11317         local_bh_enable();
11318
11319         /* Return any active mbox cmds */
11320         del_timer_sync(&psli->mbox_tmo);
11321
11322         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
11323         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11324         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
11325
11326         return 1;
11327 }
11328
11329 /**
11330  * lpfc_sli_pcimem_bcopy - SLI memory copy function
11331  * @srcp: Source memory pointer.
11332  * @destp: Destination memory pointer.
11333  * @cnt: Number of words required to be copied.
11334  *
11335  * This function is used for copying data between driver memory
11336  * and the SLI memory. This function also changes the endianness
11337  * of each word if native endianness is different from SLI
11338  * endianness. This function can be called with or without
11339  * lock.
11340  **/
11341 void
11342 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
11343 {
11344         uint32_t *src = srcp;
11345         uint32_t *dest = destp;
11346         uint32_t ldata;
11347         int i;
11348
11349         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
11350                 ldata = *src;
11351                 ldata = le32_to_cpu(ldata);
11352                 *dest = ldata;
11353                 src++;
11354                 dest++;
11355         }
11356 }
11357
11358
11359 /**
11360  * lpfc_sli_bemem_bcopy - SLI memory copy function
11361  * @srcp: Source memory pointer.
11362  * @destp: Destination memory pointer.
11363  * @cnt: Number of words required to be copied.
11364  *
11365  * This function is used for copying data between a data structure
11366  * with big endian representation to local endianness.
11367  * This function can be called with or without lock.
11368  **/
11369 void
11370 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
11371 {
11372         uint32_t *src = srcp;
11373         uint32_t *dest = destp;
11374         uint32_t ldata;
11375         int i;
11376
11377         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
11378                 ldata = *src;
11379                 ldata = be32_to_cpu(ldata);
11380                 *dest = ldata;
11381                 src++;
11382                 dest++;
11383         }
11384 }
11385
11386 /**
11387  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
11388  * @phba: Pointer to HBA context object.
11389  * @pring: Pointer to driver SLI ring object.
11390  * @mp: Pointer to driver buffer object.
11391  *
11392  * This function is called with no lock held.
11393  * It always return zero after adding the buffer to the postbufq
11394  * buffer list.
11395  **/
11396 int
11397 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11398                          struct lpfc_dmabuf *mp)
11399 {
11400         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
11401            later */
11402         spin_lock_irq(&phba->hbalock);
11403         list_add_tail(&mp->list, &pring->postbufq);
11404         pring->postbufq_cnt++;
11405         spin_unlock_irq(&phba->hbalock);
11406         return 0;
11407 }
11408
11409 /**
11410  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
11411  * @phba: Pointer to HBA context object.
11412  *
11413  * When HBQ is enabled, buffers are searched based on tags. This function
11414  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
11415  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
11416  * does not conflict with tags of buffer posted for unsolicited events.
11417  * The function returns the allocated tag. The function is called with
11418  * no locks held.
11419  **/
11420 uint32_t
11421 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
11422 {
11423         spin_lock_irq(&phba->hbalock);
11424         phba->buffer_tag_count++;
11425         /*
11426          * Always set the QUE_BUFTAG_BIT to distiguish between
11427          * a tag assigned by HBQ.
11428          */
11429         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
11430         spin_unlock_irq(&phba->hbalock);
11431         return phba->buffer_tag_count;
11432 }
11433
11434 /**
11435  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
11436  * @phba: Pointer to HBA context object.
11437  * @pring: Pointer to driver SLI ring object.
11438  * @tag: Buffer tag.
11439  *
11440  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
11441  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
11442  * iocb is posted to the response ring with the tag of the buffer.
11443  * This function searches the pring->postbufq list using the tag
11444  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
11445  * iocb. If the buffer is found then lpfc_dmabuf object of the
11446  * buffer is returned to the caller else NULL is returned.
11447  * This function is called with no lock held.
11448  **/
11449 struct lpfc_dmabuf *
11450 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11451                         uint32_t tag)
11452 {
11453         struct lpfc_dmabuf *mp, *next_mp;
11454         struct list_head *slp = &pring->postbufq;
11455
11456         /* Search postbufq, from the beginning, looking for a match on tag */
11457         spin_lock_irq(&phba->hbalock);
11458         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
11459                 if (mp->buffer_tag == tag) {
11460                         list_del_init(&mp->list);
11461                         pring->postbufq_cnt--;
11462                         spin_unlock_irq(&phba->hbalock);
11463                         return mp;
11464                 }
11465         }
11466
11467         spin_unlock_irq(&phba->hbalock);
11468         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11469                         "0402 Cannot find virtual addr for buffer tag on "
11470                         "ring %d Data x%lx x%px x%px x%x\n",
11471                         pring->ringno, (unsigned long) tag,
11472                         slp->next, slp->prev, pring->postbufq_cnt);
11473
11474         return NULL;
11475 }
11476
11477 /**
11478  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
11479  * @phba: Pointer to HBA context object.
11480  * @pring: Pointer to driver SLI ring object.
11481  * @phys: DMA address of the buffer.
11482  *
11483  * This function searches the buffer list using the dma_address
11484  * of unsolicited event to find the driver's lpfc_dmabuf object
11485  * corresponding to the dma_address. The function returns the
11486  * lpfc_dmabuf object if a buffer is found else it returns NULL.
11487  * This function is called by the ct and els unsolicited event
11488  * handlers to get the buffer associated with the unsolicited
11489  * event.
11490  *
11491  * This function is called with no lock held.
11492  **/
11493 struct lpfc_dmabuf *
11494 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11495                          dma_addr_t phys)
11496 {
11497         struct lpfc_dmabuf *mp, *next_mp;
11498         struct list_head *slp = &pring->postbufq;
11499
11500         /* Search postbufq, from the beginning, looking for a match on phys */
11501         spin_lock_irq(&phba->hbalock);
11502         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
11503                 if (mp->phys == phys) {
11504                         list_del_init(&mp->list);
11505                         pring->postbufq_cnt--;
11506                         spin_unlock_irq(&phba->hbalock);
11507                         return mp;
11508                 }
11509         }
11510
11511         spin_unlock_irq(&phba->hbalock);
11512         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11513                         "0410 Cannot find virtual addr for mapped buf on "
11514                         "ring %d Data x%llx x%px x%px x%x\n",
11515                         pring->ringno, (unsigned long long)phys,
11516                         slp->next, slp->prev, pring->postbufq_cnt);
11517         return NULL;
11518 }
11519
11520 /**
11521  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
11522  * @phba: Pointer to HBA context object.
11523  * @cmdiocb: Pointer to driver command iocb object.
11524  * @rspiocb: Pointer to driver response iocb object.
11525  *
11526  * This function is the completion handler for the abort iocbs for
11527  * ELS commands. This function is called from the ELS ring event
11528  * handler with no lock held. This function frees memory resources
11529  * associated with the abort iocb.
11530  **/
11531 static void
11532 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11533                         struct lpfc_iocbq *rspiocb)
11534 {
11535         IOCB_t *irsp = &rspiocb->iocb;
11536         uint16_t abort_iotag, abort_context;
11537         struct lpfc_iocbq *abort_iocb = NULL;
11538
11539         if (irsp->ulpStatus) {
11540
11541                 /*
11542                  * Assume that the port already completed and returned, or
11543                  * will return the iocb. Just Log the message.
11544                  */
11545                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11546                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11547
11548                 spin_lock_irq(&phba->hbalock);
11549                 if (phba->sli_rev < LPFC_SLI_REV4) {
11550                         if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11551                             irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11552                             irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11553                                 spin_unlock_irq(&phba->hbalock);
11554                                 goto release_iocb;
11555                         }
11556                         if (abort_iotag != 0 &&
11557                                 abort_iotag <= phba->sli.last_iotag)
11558                                 abort_iocb =
11559                                         phba->sli.iocbq_lookup[abort_iotag];
11560                 } else
11561                         /* For sli4 the abort_tag is the XRI,
11562                          * so the abort routine puts the iotag  of the iocb
11563                          * being aborted in the context field of the abort
11564                          * IOCB.
11565                          */
11566                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
11567
11568                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
11569                                 "0327 Cannot abort els iocb x%px "
11570                                 "with tag %x context %x, abort status %x, "
11571                                 "abort code %x\n",
11572                                 abort_iocb, abort_iotag, abort_context,
11573                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
11574
11575                 spin_unlock_irq(&phba->hbalock);
11576         }
11577 release_iocb:
11578         lpfc_sli_release_iocbq(phba, cmdiocb);
11579         return;
11580 }
11581
11582 /**
11583  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
11584  * @phba: Pointer to HBA context object.
11585  * @cmdiocb: Pointer to driver command iocb object.
11586  * @rspiocb: Pointer to driver response iocb object.
11587  *
11588  * The function is called from SLI ring event handler with no
11589  * lock held. This function is the completion handler for ELS commands
11590  * which are aborted. The function frees memory resources used for
11591  * the aborted ELS commands.
11592  **/
11593 void
11594 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11595                      struct lpfc_iocbq *rspiocb)
11596 {
11597         IOCB_t *irsp = &rspiocb->iocb;
11598
11599         /* ELS cmd tag <ulpIoTag> completes */
11600         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11601                         "0139 Ignoring ELS cmd tag x%x completion Data: "
11602                         "x%x x%x x%x\n",
11603                         irsp->ulpIoTag, irsp->ulpStatus,
11604                         irsp->un.ulpWord[4], irsp->ulpTimeout);
11605         lpfc_nlp_put((struct lpfc_nodelist *)cmdiocb->context1);
11606         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11607                 lpfc_ct_free_iocb(phba, cmdiocb);
11608         else
11609                 lpfc_els_free_iocb(phba, cmdiocb);
11610 }
11611
11612 /**
11613  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11614  * @phba: Pointer to HBA context object.
11615  * @pring: Pointer to driver SLI ring object.
11616  * @cmdiocb: Pointer to driver command iocb object.
11617  * @cmpl: completion function.
11618  *
11619  * This function issues an abort iocb for the provided command iocb. In case
11620  * of unloading, the abort iocb will not be issued to commands on the ELS
11621  * ring. Instead, the callback function shall be changed to those commands
11622  * so that nothing happens when them finishes. This function is called with
11623  * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS
11624  * when the command iocb is an abort request.
11625  *
11626  **/
11627 int
11628 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11629                            struct lpfc_iocbq *cmdiocb, void *cmpl)
11630 {
11631         struct lpfc_vport *vport = cmdiocb->vport;
11632         struct lpfc_iocbq *abtsiocbp;
11633         IOCB_t *icmd = NULL;
11634         IOCB_t *iabt = NULL;
11635         int retval = IOCB_ERROR;
11636         unsigned long iflags;
11637         struct lpfc_nodelist *ndlp;
11638
11639         /*
11640          * There are certain command types we don't want to abort.  And we
11641          * don't want to abort commands that are already in the process of
11642          * being aborted.
11643          */
11644         icmd = &cmdiocb->iocb;
11645         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11646             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11647             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11648                 return IOCB_ABORTING;
11649
11650         if (!pring) {
11651                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11652                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11653                 else
11654                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11655                 return retval;
11656         }
11657
11658         /*
11659          * If we're unloading, don't abort iocb on the ELS ring, but change
11660          * the callback so that nothing happens when it finishes.
11661          */
11662         if ((vport->load_flag & FC_UNLOADING) &&
11663             pring->ringno == LPFC_ELS_RING) {
11664                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11665                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11666                 else
11667                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11668                 return retval;
11669         }
11670
11671         /* issue ABTS for this IOCB based on iotag */
11672         abtsiocbp = __lpfc_sli_get_iocbq(phba);
11673         if (abtsiocbp == NULL)
11674                 return IOCB_NORESOURCE;
11675
11676         /* This signals the response to set the correct status
11677          * before calling the completion handler
11678          */
11679         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11680
11681         iabt = &abtsiocbp->iocb;
11682         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11683         iabt->un.acxri.abortContextTag = icmd->ulpContext;
11684         if (phba->sli_rev == LPFC_SLI_REV4) {
11685                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
11686                 if (pring->ringno == LPFC_ELS_RING)
11687                         iabt->un.acxri.abortContextTag = cmdiocb->iotag;
11688         } else {
11689                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
11690                 if (pring->ringno == LPFC_ELS_RING) {
11691                         ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11692                         iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11693                 }
11694         }
11695         iabt->ulpLe = 1;
11696         iabt->ulpClass = icmd->ulpClass;
11697
11698         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11699         abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
11700         if (cmdiocb->iocb_flag & LPFC_IO_FCP) {
11701                 abtsiocbp->iocb_flag |= LPFC_IO_FCP;
11702                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
11703         }
11704         if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11705                 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
11706
11707         if (phba->link_state >= LPFC_LINK_UP)
11708                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11709         else
11710                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11711
11712         if (cmpl)
11713                 abtsiocbp->iocb_cmpl = cmpl;
11714         else
11715                 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
11716         abtsiocbp->vport = vport;
11717
11718         if (phba->sli_rev == LPFC_SLI_REV4) {
11719                 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11720                 if (unlikely(pring == NULL))
11721                         goto abort_iotag_exit;
11722                 /* Note: both hbalock and ring_lock need to be set here */
11723                 spin_lock_irqsave(&pring->ring_lock, iflags);
11724                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11725                         abtsiocbp, 0);
11726                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11727         } else {
11728                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11729                         abtsiocbp, 0);
11730         }
11731
11732 abort_iotag_exit:
11733
11734         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11735                          "0339 Abort xri x%x, original iotag x%x, "
11736                          "abort cmd iotag x%x retval x%x\n",
11737                          iabt->un.acxri.abortIoTag,
11738                          iabt->un.acxri.abortContextTag,
11739                          abtsiocbp->iotag, retval);
11740
11741         if (retval) {
11742                 cmdiocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11743                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
11744         }
11745
11746         /*
11747          * Caller to this routine should check for IOCB_ERROR
11748          * and handle it properly.  This routine no longer removes
11749          * iocb off txcmplq and call compl in case of IOCB_ERROR.
11750          */
11751         return retval;
11752 }
11753
11754 /**
11755  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11756  * @phba: pointer to lpfc HBA data structure.
11757  *
11758  * This routine will abort all pending and outstanding iocbs to an HBA.
11759  **/
11760 void
11761 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11762 {
11763         struct lpfc_sli *psli = &phba->sli;
11764         struct lpfc_sli_ring *pring;
11765         struct lpfc_queue *qp = NULL;
11766         int i;
11767
11768         if (phba->sli_rev != LPFC_SLI_REV4) {
11769                 for (i = 0; i < psli->num_rings; i++) {
11770                         pring = &psli->sli3_ring[i];
11771                         lpfc_sli_abort_iocb_ring(phba, pring);
11772                 }
11773                 return;
11774         }
11775         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11776                 pring = qp->pring;
11777                 if (!pring)
11778                         continue;
11779                 lpfc_sli_abort_iocb_ring(phba, pring);
11780         }
11781 }
11782
11783 /**
11784  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
11785  * @iocbq: Pointer to driver iocb object.
11786  * @vport: Pointer to driver virtual port object.
11787  * @tgt_id: SCSI ID of the target.
11788  * @lun_id: LUN ID of the scsi device.
11789  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11790  *
11791  * This function acts as an iocb filter for functions which abort or count
11792  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11793  * 0 if the filtering criteria is met for the given iocb and will return
11794  * 1 if the filtering criteria is not met.
11795  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11796  * given iocb is for the SCSI device specified by vport, tgt_id and
11797  * lun_id parameter.
11798  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
11799  * given iocb is for the SCSI target specified by vport and tgt_id
11800  * parameters.
11801  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11802  * given iocb is for the SCSI host associated with the given vport.
11803  * This function is called with no locks held.
11804  **/
11805 static int
11806 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11807                            uint16_t tgt_id, uint64_t lun_id,
11808                            lpfc_ctx_cmd ctx_cmd)
11809 {
11810         struct lpfc_io_buf *lpfc_cmd;
11811         int rc = 1;
11812
11813         if (!iocbq || iocbq->vport != vport)
11814                 return rc;
11815
11816         if (!(iocbq->iocb_flag &  LPFC_IO_FCP) ||
11817             !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
11818                 return rc;
11819
11820         lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
11821
11822         if (lpfc_cmd->pCmd == NULL)
11823                 return rc;
11824
11825         switch (ctx_cmd) {
11826         case LPFC_CTX_LUN:
11827                 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
11828                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11829                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
11830                         rc = 0;
11831                 break;
11832         case LPFC_CTX_TGT:
11833                 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
11834                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
11835                         rc = 0;
11836                 break;
11837         case LPFC_CTX_HOST:
11838                 rc = 0;
11839                 break;
11840         default:
11841                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
11842                         __func__, ctx_cmd);
11843                 break;
11844         }
11845
11846         return rc;
11847 }
11848
11849 /**
11850  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
11851  * @vport: Pointer to virtual port.
11852  * @tgt_id: SCSI ID of the target.
11853  * @lun_id: LUN ID of the scsi device.
11854  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11855  *
11856  * This function returns number of FCP commands pending for the vport.
11857  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11858  * commands pending on the vport associated with SCSI device specified
11859  * by tgt_id and lun_id parameters.
11860  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11861  * commands pending on the vport associated with SCSI target specified
11862  * by tgt_id parameter.
11863  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11864  * commands pending on the vport.
11865  * This function returns the number of iocbs which satisfy the filter.
11866  * This function is called without any lock held.
11867  **/
11868 int
11869 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11870                   lpfc_ctx_cmd ctx_cmd)
11871 {
11872         struct lpfc_hba *phba = vport->phba;
11873         struct lpfc_iocbq *iocbq;
11874         int sum, i;
11875
11876         spin_lock_irq(&phba->hbalock);
11877         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11878                 iocbq = phba->sli.iocbq_lookup[i];
11879
11880                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11881                                                 ctx_cmd) == 0)
11882                         sum++;
11883         }
11884         spin_unlock_irq(&phba->hbalock);
11885
11886         return sum;
11887 }
11888
11889 /**
11890  * lpfc_sli4_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
11891  * @phba: Pointer to HBA context object
11892  * @cmdiocb: Pointer to command iocb object.
11893  * @wcqe: pointer to the complete wcqe
11894  *
11895  * This function is called when an aborted FCP iocb completes. This
11896  * function is called by the ring event handler with no lock held.
11897  * This function frees the iocb. It is called for sli-4 adapters.
11898  **/
11899 void
11900 lpfc_sli4_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11901                          struct lpfc_wcqe_complete *wcqe)
11902 {
11903         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11904                         "3017 ABORT_XRI_CN completing on rpi x%x "
11905                         "original iotag x%x, abort cmd iotag x%x "
11906                         "status 0x%x, reason 0x%x\n",
11907                         cmdiocb->iocb.un.acxri.abortContextTag,
11908                         cmdiocb->iocb.un.acxri.abortIoTag,
11909                         cmdiocb->iotag,
11910                         (bf_get(lpfc_wcqe_c_status, wcqe)
11911                         & LPFC_IOCB_STATUS_MASK),
11912                         wcqe->parameter);
11913         lpfc_sli_release_iocbq(phba, cmdiocb);
11914 }
11915
11916 /**
11917  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
11918  * @phba: Pointer to HBA context object
11919  * @cmdiocb: Pointer to command iocb object.
11920  * @rspiocb: Pointer to response iocb object.
11921  *
11922  * This function is called when an aborted FCP iocb completes. This
11923  * function is called by the ring event handler with no lock held.
11924  * This function frees the iocb.
11925  **/
11926 void
11927 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11928                         struct lpfc_iocbq *rspiocb)
11929 {
11930         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11931                         "3096 ABORT_XRI_CN completing on rpi x%x "
11932                         "original iotag x%x, abort cmd iotag x%x "
11933                         "status 0x%x, reason 0x%x\n",
11934                         cmdiocb->iocb.un.acxri.abortContextTag,
11935                         cmdiocb->iocb.un.acxri.abortIoTag,
11936                         cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11937                         rspiocb->iocb.un.ulpWord[4]);
11938         lpfc_sli_release_iocbq(phba, cmdiocb);
11939         return;
11940 }
11941
11942 /**
11943  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
11944  * @vport: Pointer to virtual port.
11945  * @pring: Pointer to driver SLI ring object.
11946  * @tgt_id: SCSI ID of the target.
11947  * @lun_id: LUN ID of the scsi device.
11948  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11949  *
11950  * This function sends an abort command for every SCSI command
11951  * associated with the given virtual port pending on the ring
11952  * filtered by lpfc_sli_validate_fcp_iocb function.
11953  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11954  * FCP iocbs associated with lun specified by tgt_id and lun_id
11955  * parameters
11956  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11957  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11958  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11959  * FCP iocbs associated with virtual port.
11960  * This function returns number of iocbs it failed to abort.
11961  * This function is called with no locks held.
11962  **/
11963 int
11964 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11965                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
11966 {
11967         struct lpfc_hba *phba = vport->phba;
11968         struct lpfc_iocbq *iocbq;
11969         int errcnt = 0, ret_val = 0;
11970         unsigned long iflags;
11971         int i;
11972
11973         /* all I/Os are in process of being flushed */
11974         if (phba->hba_flag & HBA_IOQ_FLUSH)
11975                 return errcnt;
11976
11977         for (i = 1; i <= phba->sli.last_iotag; i++) {
11978                 iocbq = phba->sli.iocbq_lookup[i];
11979
11980                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11981                                                abort_cmd) != 0)
11982                         continue;
11983
11984                 spin_lock_irqsave(&phba->hbalock, iflags);
11985                 ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,
11986                                                      lpfc_sli_abort_fcp_cmpl);
11987                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11988                 if (ret_val != IOCB_SUCCESS)
11989                         errcnt++;
11990         }
11991
11992         return errcnt;
11993 }
11994
11995 /**
11996  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11997  * @vport: Pointer to virtual port.
11998  * @pring: Pointer to driver SLI ring object.
11999  * @tgt_id: SCSI ID of the target.
12000  * @lun_id: LUN ID of the scsi device.
12001  * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12002  *
12003  * This function sends an abort command for every SCSI command
12004  * associated with the given virtual port pending on the ring
12005  * filtered by lpfc_sli_validate_fcp_iocb function.
12006  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
12007  * FCP iocbs associated with lun specified by tgt_id and lun_id
12008  * parameters
12009  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
12010  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12011  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
12012  * FCP iocbs associated with virtual port.
12013  * This function returns number of iocbs it aborted .
12014  * This function is called with no locks held right after a taskmgmt
12015  * command is sent.
12016  **/
12017 int
12018 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
12019                         uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
12020 {
12021         struct lpfc_hba *phba = vport->phba;
12022         struct lpfc_io_buf *lpfc_cmd;
12023         struct lpfc_iocbq *abtsiocbq;
12024         struct lpfc_nodelist *ndlp;
12025         struct lpfc_iocbq *iocbq;
12026         IOCB_t *icmd;
12027         int sum, i, ret_val;
12028         unsigned long iflags;
12029         struct lpfc_sli_ring *pring_s4 = NULL;
12030
12031         spin_lock_irqsave(&phba->hbalock, iflags);
12032
12033         /* all I/Os are in process of being flushed */
12034         if (phba->hba_flag & HBA_IOQ_FLUSH) {
12035                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12036                 return 0;
12037         }
12038         sum = 0;
12039
12040         for (i = 1; i <= phba->sli.last_iotag; i++) {
12041                 iocbq = phba->sli.iocbq_lookup[i];
12042
12043                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12044                                                cmd) != 0)
12045                         continue;
12046
12047                 /* Guard against IO completion being called at same time */
12048                 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12049                 spin_lock(&lpfc_cmd->buf_lock);
12050
12051                 if (!lpfc_cmd->pCmd) {
12052                         spin_unlock(&lpfc_cmd->buf_lock);
12053                         continue;
12054                 }
12055
12056                 if (phba->sli_rev == LPFC_SLI_REV4) {
12057                         pring_s4 =
12058                             phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
12059                         if (!pring_s4) {
12060                                 spin_unlock(&lpfc_cmd->buf_lock);
12061                                 continue;
12062                         }
12063                         /* Note: both hbalock and ring_lock must be set here */
12064                         spin_lock(&pring_s4->ring_lock);
12065                 }
12066
12067                 /*
12068                  * If the iocbq is already being aborted, don't take a second
12069                  * action, but do count it.
12070                  */
12071                 if ((iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
12072                     !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
12073                         if (phba->sli_rev == LPFC_SLI_REV4)
12074                                 spin_unlock(&pring_s4->ring_lock);
12075                         spin_unlock(&lpfc_cmd->buf_lock);
12076                         continue;
12077                 }
12078
12079                 /* issue ABTS for this IOCB based on iotag */
12080                 abtsiocbq = __lpfc_sli_get_iocbq(phba);
12081                 if (!abtsiocbq) {
12082                         if (phba->sli_rev == LPFC_SLI_REV4)
12083                                 spin_unlock(&pring_s4->ring_lock);
12084                         spin_unlock(&lpfc_cmd->buf_lock);
12085                         continue;
12086                 }
12087
12088                 icmd = &iocbq->iocb;
12089                 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
12090                 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
12091                 if (phba->sli_rev == LPFC_SLI_REV4)
12092                         abtsiocbq->iocb.un.acxri.abortIoTag =
12093                                                          iocbq->sli4_xritag;
12094                 else
12095                         abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
12096                 abtsiocbq->iocb.ulpLe = 1;
12097                 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
12098                 abtsiocbq->vport = vport;
12099
12100                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
12101                 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
12102                 if (iocbq->iocb_flag & LPFC_IO_FCP)
12103                         abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
12104                 if (iocbq->iocb_flag & LPFC_IO_FOF)
12105                         abtsiocbq->iocb_flag |= LPFC_IO_FOF;
12106
12107                 ndlp = lpfc_cmd->rdata->pnode;
12108
12109                 if (lpfc_is_link_up(phba) &&
12110                     (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
12111                         abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
12112                 else
12113                         abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
12114
12115                 /* Setup callback routine and issue the command. */
12116                 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
12117
12118                 /*
12119                  * Indicate the IO is being aborted by the driver and set
12120                  * the caller's flag into the aborted IO.
12121                  */
12122                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
12123
12124                 if (phba->sli_rev == LPFC_SLI_REV4) {
12125                         ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
12126                                                         abtsiocbq, 0);
12127                         spin_unlock(&pring_s4->ring_lock);
12128                 } else {
12129                         ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
12130                                                         abtsiocbq, 0);
12131                 }
12132
12133                 spin_unlock(&lpfc_cmd->buf_lock);
12134
12135                 if (ret_val == IOCB_ERROR)
12136                         __lpfc_sli_release_iocbq(phba, abtsiocbq);
12137                 else
12138                         sum++;
12139         }
12140         spin_unlock_irqrestore(&phba->hbalock, iflags);
12141         return sum;
12142 }
12143
12144 /**
12145  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
12146  * @phba: Pointer to HBA context object.
12147  * @cmdiocbq: Pointer to command iocb.
12148  * @rspiocbq: Pointer to response iocb.
12149  *
12150  * This function is the completion handler for iocbs issued using
12151  * lpfc_sli_issue_iocb_wait function. This function is called by the
12152  * ring event handler function without any lock held. This function
12153  * can be called from both worker thread context and interrupt
12154  * context. This function also can be called from other thread which
12155  * cleans up the SLI layer objects.
12156  * This function copy the contents of the response iocb to the
12157  * response iocb memory object provided by the caller of
12158  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
12159  * sleeps for the iocb completion.
12160  **/
12161 static void
12162 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
12163                         struct lpfc_iocbq *cmdiocbq,
12164                         struct lpfc_iocbq *rspiocbq)
12165 {
12166         wait_queue_head_t *pdone_q;
12167         unsigned long iflags;
12168         struct lpfc_io_buf *lpfc_cmd;
12169
12170         spin_lock_irqsave(&phba->hbalock, iflags);
12171         if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
12172
12173                 /*
12174                  * A time out has occurred for the iocb.  If a time out
12175                  * completion handler has been supplied, call it.  Otherwise,
12176                  * just free the iocbq.
12177                  */
12178
12179                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12180                 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
12181                 cmdiocbq->wait_iocb_cmpl = NULL;
12182                 if (cmdiocbq->iocb_cmpl)
12183                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
12184                 else
12185                         lpfc_sli_release_iocbq(phba, cmdiocbq);
12186                 return;
12187         }
12188
12189         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
12190         if (cmdiocbq->context2 && rspiocbq)
12191                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
12192                        &rspiocbq->iocb, sizeof(IOCB_t));
12193
12194         /* Set the exchange busy flag for task management commands */
12195         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
12196                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
12197                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
12198                         cur_iocbq);
12199                 if (rspiocbq && (rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY))
12200                         lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
12201                 else
12202                         lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
12203         }
12204
12205         pdone_q = cmdiocbq->context_un.wait_queue;
12206         if (pdone_q)
12207                 wake_up(pdone_q);
12208         spin_unlock_irqrestore(&phba->hbalock, iflags);
12209         return;
12210 }
12211
12212 /**
12213  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
12214  * @phba: Pointer to HBA context object..
12215  * @piocbq: Pointer to command iocb.
12216  * @flag: Flag to test.
12217  *
12218  * This routine grabs the hbalock and then test the iocb_flag to
12219  * see if the passed in flag is set.
12220  * Returns:
12221  * 1 if flag is set.
12222  * 0 if flag is not set.
12223  **/
12224 static int
12225 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
12226                  struct lpfc_iocbq *piocbq, uint32_t flag)
12227 {
12228         unsigned long iflags;
12229         int ret;
12230
12231         spin_lock_irqsave(&phba->hbalock, iflags);
12232         ret = piocbq->iocb_flag & flag;
12233         spin_unlock_irqrestore(&phba->hbalock, iflags);
12234         return ret;
12235
12236 }
12237
12238 /**
12239  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
12240  * @phba: Pointer to HBA context object..
12241  * @ring_number: Ring number
12242  * @piocb: Pointer to command iocb.
12243  * @prspiocbq: Pointer to response iocb.
12244  * @timeout: Timeout in number of seconds.
12245  *
12246  * This function issues the iocb to firmware and waits for the
12247  * iocb to complete. The iocb_cmpl field of the shall be used
12248  * to handle iocbs which time out. If the field is NULL, the
12249  * function shall free the iocbq structure.  If more clean up is
12250  * needed, the caller is expected to provide a completion function
12251  * that will provide the needed clean up.  If the iocb command is
12252  * not completed within timeout seconds, the function will either
12253  * free the iocbq structure (if iocb_cmpl == NULL) or execute the
12254  * completion function set in the iocb_cmpl field and then return
12255  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
12256  * resources if this function returns IOCB_TIMEDOUT.
12257  * The function waits for the iocb completion using an
12258  * non-interruptible wait.
12259  * This function will sleep while waiting for iocb completion.
12260  * So, this function should not be called from any context which
12261  * does not allow sleeping. Due to the same reason, this function
12262  * cannot be called with interrupt disabled.
12263  * This function assumes that the iocb completions occur while
12264  * this function sleep. So, this function cannot be called from
12265  * the thread which process iocb completion for this ring.
12266  * This function clears the iocb_flag of the iocb object before
12267  * issuing the iocb and the iocb completion handler sets this
12268  * flag and wakes this thread when the iocb completes.
12269  * The contents of the response iocb will be copied to prspiocbq
12270  * by the completion handler when the command completes.
12271  * This function returns IOCB_SUCCESS when success.
12272  * This function is called with no lock held.
12273  **/
12274 int
12275 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
12276                          uint32_t ring_number,
12277                          struct lpfc_iocbq *piocb,
12278                          struct lpfc_iocbq *prspiocbq,
12279                          uint32_t timeout)
12280 {
12281         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
12282         long timeleft, timeout_req = 0;
12283         int retval = IOCB_SUCCESS;
12284         uint32_t creg_val;
12285         struct lpfc_iocbq *iocb;
12286         int txq_cnt = 0;
12287         int txcmplq_cnt = 0;
12288         struct lpfc_sli_ring *pring;
12289         unsigned long iflags;
12290         bool iocb_completed = true;
12291
12292         if (phba->sli_rev >= LPFC_SLI_REV4)
12293                 pring = lpfc_sli4_calc_ring(phba, piocb);
12294         else
12295                 pring = &phba->sli.sli3_ring[ring_number];
12296         /*
12297          * If the caller has provided a response iocbq buffer, then context2
12298          * is NULL or its an error.
12299          */
12300         if (prspiocbq) {
12301                 if (piocb->context2)
12302                         return IOCB_ERROR;
12303                 piocb->context2 = prspiocbq;
12304         }
12305
12306         piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
12307         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
12308         piocb->context_un.wait_queue = &done_q;
12309         piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
12310
12311         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
12312                 if (lpfc_readl(phba->HCregaddr, &creg_val))
12313                         return IOCB_ERROR;
12314                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
12315                 writel(creg_val, phba->HCregaddr);
12316                 readl(phba->HCregaddr); /* flush */
12317         }
12318
12319         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
12320                                      SLI_IOCB_RET_IOCB);
12321         if (retval == IOCB_SUCCESS) {
12322                 timeout_req = msecs_to_jiffies(timeout * 1000);
12323                 timeleft = wait_event_timeout(done_q,
12324                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
12325                                 timeout_req);
12326                 spin_lock_irqsave(&phba->hbalock, iflags);
12327                 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
12328
12329                         /*
12330                          * IOCB timed out.  Inform the wake iocb wait
12331                          * completion function and set local status
12332                          */
12333
12334                         iocb_completed = false;
12335                         piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
12336                 }
12337                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12338                 if (iocb_completed) {
12339                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12340                                         "0331 IOCB wake signaled\n");
12341                         /* Note: we are not indicating if the IOCB has a success
12342                          * status or not - that's for the caller to check.
12343                          * IOCB_SUCCESS means just that the command was sent and
12344                          * completed. Not that it completed successfully.
12345                          * */
12346                 } else if (timeleft == 0) {
12347                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12348                                         "0338 IOCB wait timeout error - no "
12349                                         "wake response Data x%x\n", timeout);
12350                         retval = IOCB_TIMEDOUT;
12351                 } else {
12352                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12353                                         "0330 IOCB wake NOT set, "
12354                                         "Data x%x x%lx\n",
12355                                         timeout, (timeleft / jiffies));
12356                         retval = IOCB_TIMEDOUT;
12357                 }
12358         } else if (retval == IOCB_BUSY) {
12359                 if (phba->cfg_log_verbose & LOG_SLI) {
12360                         list_for_each_entry(iocb, &pring->txq, list) {
12361                                 txq_cnt++;
12362                         }
12363                         list_for_each_entry(iocb, &pring->txcmplq, list) {
12364                                 txcmplq_cnt++;
12365                         }
12366                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12367                                 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
12368                                 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
12369                 }
12370                 return retval;
12371         } else {
12372                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12373                                 "0332 IOCB wait issue failed, Data x%x\n",
12374                                 retval);
12375                 retval = IOCB_ERROR;
12376         }
12377
12378         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
12379                 if (lpfc_readl(phba->HCregaddr, &creg_val))
12380                         return IOCB_ERROR;
12381                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
12382                 writel(creg_val, phba->HCregaddr);
12383                 readl(phba->HCregaddr); /* flush */
12384         }
12385
12386         if (prspiocbq)
12387                 piocb->context2 = NULL;
12388
12389         piocb->context_un.wait_queue = NULL;
12390         piocb->iocb_cmpl = NULL;
12391         return retval;
12392 }
12393
12394 /**
12395  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
12396  * @phba: Pointer to HBA context object.
12397  * @pmboxq: Pointer to driver mailbox object.
12398  * @timeout: Timeout in number of seconds.
12399  *
12400  * This function issues the mailbox to firmware and waits for the
12401  * mailbox command to complete. If the mailbox command is not
12402  * completed within timeout seconds, it returns MBX_TIMEOUT.
12403  * The function waits for the mailbox completion using an
12404  * interruptible wait. If the thread is woken up due to a
12405  * signal, MBX_TIMEOUT error is returned to the caller. Caller
12406  * should not free the mailbox resources, if this function returns
12407  * MBX_TIMEOUT.
12408  * This function will sleep while waiting for mailbox completion.
12409  * So, this function should not be called from any context which
12410  * does not allow sleeping. Due to the same reason, this function
12411  * cannot be called with interrupt disabled.
12412  * This function assumes that the mailbox completion occurs while
12413  * this function sleep. So, this function cannot be called from
12414  * the worker thread which processes mailbox completion.
12415  * This function is called in the context of HBA management
12416  * applications.
12417  * This function returns MBX_SUCCESS when successful.
12418  * This function is called with no lock held.
12419  **/
12420 int
12421 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
12422                          uint32_t timeout)
12423 {
12424         struct completion mbox_done;
12425         int retval;
12426         unsigned long flag;
12427
12428         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
12429         /* setup wake call as IOCB callback */
12430         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
12431
12432         /* setup context3 field to pass wait_queue pointer to wake function  */
12433         init_completion(&mbox_done);
12434         pmboxq->context3 = &mbox_done;
12435         /* now issue the command */
12436         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
12437         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
12438                 wait_for_completion_timeout(&mbox_done,
12439                                             msecs_to_jiffies(timeout * 1000));
12440
12441                 spin_lock_irqsave(&phba->hbalock, flag);
12442                 pmboxq->context3 = NULL;
12443                 /*
12444                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
12445                  * else do not free the resources.
12446                  */
12447                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
12448                         retval = MBX_SUCCESS;
12449                 } else {
12450                         retval = MBX_TIMEOUT;
12451                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12452                 }
12453                 spin_unlock_irqrestore(&phba->hbalock, flag);
12454         }
12455         return retval;
12456 }
12457
12458 /**
12459  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
12460  * @phba: Pointer to HBA context.
12461  * @mbx_action: Mailbox shutdown options.
12462  *
12463  * This function is called to shutdown the driver's mailbox sub-system.
12464  * It first marks the mailbox sub-system is in a block state to prevent
12465  * the asynchronous mailbox command from issued off the pending mailbox
12466  * command queue. If the mailbox command sub-system shutdown is due to
12467  * HBA error conditions such as EEH or ERATT, this routine shall invoke
12468  * the mailbox sub-system flush routine to forcefully bring down the
12469  * mailbox sub-system. Otherwise, if it is due to normal condition (such
12470  * as with offline or HBA function reset), this routine will wait for the
12471  * outstanding mailbox command to complete before invoking the mailbox
12472  * sub-system flush routine to gracefully bring down mailbox sub-system.
12473  **/
12474 void
12475 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
12476 {
12477         struct lpfc_sli *psli = &phba->sli;
12478         unsigned long timeout;
12479
12480         if (mbx_action == LPFC_MBX_NO_WAIT) {
12481                 /* delay 100ms for port state */
12482                 msleep(100);
12483                 lpfc_sli_mbox_sys_flush(phba);
12484                 return;
12485         }
12486         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
12487
12488         /* Disable softirqs, including timers from obtaining phba->hbalock */
12489         local_bh_disable();
12490
12491         spin_lock_irq(&phba->hbalock);
12492         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
12493
12494         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
12495                 /* Determine how long we might wait for the active mailbox
12496                  * command to be gracefully completed by firmware.
12497                  */
12498                 if (phba->sli.mbox_active)
12499                         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12500                                                 phba->sli.mbox_active) *
12501                                                 1000) + jiffies;
12502                 spin_unlock_irq(&phba->hbalock);
12503
12504                 /* Enable softirqs again, done with phba->hbalock */
12505                 local_bh_enable();
12506
12507                 while (phba->sli.mbox_active) {
12508                         /* Check active mailbox complete status every 2ms */
12509                         msleep(2);
12510                         if (time_after(jiffies, timeout))
12511                                 /* Timeout, let the mailbox flush routine to
12512                                  * forcefully release active mailbox command
12513                                  */
12514                                 break;
12515                 }
12516         } else {
12517                 spin_unlock_irq(&phba->hbalock);
12518
12519                 /* Enable softirqs again, done with phba->hbalock */
12520                 local_bh_enable();
12521         }
12522
12523         lpfc_sli_mbox_sys_flush(phba);
12524 }
12525
12526 /**
12527  * lpfc_sli_eratt_read - read sli-3 error attention events
12528  * @phba: Pointer to HBA context.
12529  *
12530  * This function is called to read the SLI3 device error attention registers
12531  * for possible error attention events. The caller must hold the hostlock
12532  * with spin_lock_irq().
12533  *
12534  * This function returns 1 when there is Error Attention in the Host Attention
12535  * Register and returns 0 otherwise.
12536  **/
12537 static int
12538 lpfc_sli_eratt_read(struct lpfc_hba *phba)
12539 {
12540         uint32_t ha_copy;
12541
12542         /* Read chip Host Attention (HA) register */
12543         if (lpfc_readl(phba->HAregaddr, &ha_copy))
12544                 goto unplug_err;
12545
12546         if (ha_copy & HA_ERATT) {
12547                 /* Read host status register to retrieve error event */
12548                 if (lpfc_sli_read_hs(phba))
12549                         goto unplug_err;
12550
12551                 /* Check if there is a deferred error condition is active */
12552                 if ((HS_FFER1 & phba->work_hs) &&
12553                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
12554                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
12555                         phba->hba_flag |= DEFER_ERATT;
12556                         /* Clear all interrupt enable conditions */
12557                         writel(0, phba->HCregaddr);
12558                         readl(phba->HCregaddr);
12559                 }
12560
12561                 /* Set the driver HA work bitmap */
12562                 phba->work_ha |= HA_ERATT;
12563                 /* Indicate polling handles this ERATT */
12564                 phba->hba_flag |= HBA_ERATT_HANDLED;
12565                 return 1;
12566         }
12567         return 0;
12568
12569 unplug_err:
12570         /* Set the driver HS work bitmap */
12571         phba->work_hs |= UNPLUG_ERR;
12572         /* Set the driver HA work bitmap */
12573         phba->work_ha |= HA_ERATT;
12574         /* Indicate polling handles this ERATT */
12575         phba->hba_flag |= HBA_ERATT_HANDLED;
12576         return 1;
12577 }
12578
12579 /**
12580  * lpfc_sli4_eratt_read - read sli-4 error attention events
12581  * @phba: Pointer to HBA context.
12582  *
12583  * This function is called to read the SLI4 device error attention registers
12584  * for possible error attention events. The caller must hold the hostlock
12585  * with spin_lock_irq().
12586  *
12587  * This function returns 1 when there is Error Attention in the Host Attention
12588  * Register and returns 0 otherwise.
12589  **/
12590 static int
12591 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12592 {
12593         uint32_t uerr_sta_hi, uerr_sta_lo;
12594         uint32_t if_type, portsmphr;
12595         struct lpfc_register portstat_reg;
12596
12597         /*
12598          * For now, use the SLI4 device internal unrecoverable error
12599          * registers for error attention. This can be changed later.
12600          */
12601         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12602         switch (if_type) {
12603         case LPFC_SLI_INTF_IF_TYPE_0:
12604                 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12605                         &uerr_sta_lo) ||
12606                         lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12607                         &uerr_sta_hi)) {
12608                         phba->work_hs |= UNPLUG_ERR;
12609                         phba->work_ha |= HA_ERATT;
12610                         phba->hba_flag |= HBA_ERATT_HANDLED;
12611                         return 1;
12612                 }
12613                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12614                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12615                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12616                                         "1423 HBA Unrecoverable error: "
12617                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12618                                         "ue_mask_lo_reg=0x%x, "
12619                                         "ue_mask_hi_reg=0x%x\n",
12620                                         uerr_sta_lo, uerr_sta_hi,
12621                                         phba->sli4_hba.ue_mask_lo,
12622                                         phba->sli4_hba.ue_mask_hi);
12623                         phba->work_status[0] = uerr_sta_lo;
12624                         phba->work_status[1] = uerr_sta_hi;
12625                         phba->work_ha |= HA_ERATT;
12626                         phba->hba_flag |= HBA_ERATT_HANDLED;
12627                         return 1;
12628                 }
12629                 break;
12630         case LPFC_SLI_INTF_IF_TYPE_2:
12631         case LPFC_SLI_INTF_IF_TYPE_6:
12632                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12633                         &portstat_reg.word0) ||
12634                         lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12635                         &portsmphr)){
12636                         phba->work_hs |= UNPLUG_ERR;
12637                         phba->work_ha |= HA_ERATT;
12638                         phba->hba_flag |= HBA_ERATT_HANDLED;
12639                         return 1;
12640                 }
12641                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12642                         phba->work_status[0] =
12643                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12644                         phba->work_status[1] =
12645                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12646                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12647                                         "2885 Port Status Event: "
12648                                         "port status reg 0x%x, "
12649                                         "port smphr reg 0x%x, "
12650                                         "error 1=0x%x, error 2=0x%x\n",
12651                                         portstat_reg.word0,
12652                                         portsmphr,
12653                                         phba->work_status[0],
12654                                         phba->work_status[1]);
12655                         phba->work_ha |= HA_ERATT;
12656                         phba->hba_flag |= HBA_ERATT_HANDLED;
12657                         return 1;
12658                 }
12659                 break;
12660         case LPFC_SLI_INTF_IF_TYPE_1:
12661         default:
12662                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12663                                 "2886 HBA Error Attention on unsupported "
12664                                 "if type %d.", if_type);
12665                 return 1;
12666         }
12667
12668         return 0;
12669 }
12670
12671 /**
12672  * lpfc_sli_check_eratt - check error attention events
12673  * @phba: Pointer to HBA context.
12674  *
12675  * This function is called from timer soft interrupt context to check HBA's
12676  * error attention register bit for error attention events.
12677  *
12678  * This function returns 1 when there is Error Attention in the Host Attention
12679  * Register and returns 0 otherwise.
12680  **/
12681 int
12682 lpfc_sli_check_eratt(struct lpfc_hba *phba)
12683 {
12684         uint32_t ha_copy;
12685
12686         /* If somebody is waiting to handle an eratt, don't process it
12687          * here. The brdkill function will do this.
12688          */
12689         if (phba->link_flag & LS_IGNORE_ERATT)
12690                 return 0;
12691
12692         /* Check if interrupt handler handles this ERATT */
12693         spin_lock_irq(&phba->hbalock);
12694         if (phba->hba_flag & HBA_ERATT_HANDLED) {
12695                 /* Interrupt handler has handled ERATT */
12696                 spin_unlock_irq(&phba->hbalock);
12697                 return 0;
12698         }
12699
12700         /*
12701          * If there is deferred error attention, do not check for error
12702          * attention
12703          */
12704         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12705                 spin_unlock_irq(&phba->hbalock);
12706                 return 0;
12707         }
12708
12709         /* If PCI channel is offline, don't process it */
12710         if (unlikely(pci_channel_offline(phba->pcidev))) {
12711                 spin_unlock_irq(&phba->hbalock);
12712                 return 0;
12713         }
12714
12715         switch (phba->sli_rev) {
12716         case LPFC_SLI_REV2:
12717         case LPFC_SLI_REV3:
12718                 /* Read chip Host Attention (HA) register */
12719                 ha_copy = lpfc_sli_eratt_read(phba);
12720                 break;
12721         case LPFC_SLI_REV4:
12722                 /* Read device Uncoverable Error (UERR) registers */
12723                 ha_copy = lpfc_sli4_eratt_read(phba);
12724                 break;
12725         default:
12726                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12727                                 "0299 Invalid SLI revision (%d)\n",
12728                                 phba->sli_rev);
12729                 ha_copy = 0;
12730                 break;
12731         }
12732         spin_unlock_irq(&phba->hbalock);
12733
12734         return ha_copy;
12735 }
12736
12737 /**
12738  * lpfc_intr_state_check - Check device state for interrupt handling
12739  * @phba: Pointer to HBA context.
12740  *
12741  * This inline routine checks whether a device or its PCI slot is in a state
12742  * that the interrupt should be handled.
12743  *
12744  * This function returns 0 if the device or the PCI slot is in a state that
12745  * interrupt should be handled, otherwise -EIO.
12746  */
12747 static inline int
12748 lpfc_intr_state_check(struct lpfc_hba *phba)
12749 {
12750         /* If the pci channel is offline, ignore all the interrupts */
12751         if (unlikely(pci_channel_offline(phba->pcidev)))
12752                 return -EIO;
12753
12754         /* Update device level interrupt statistics */
12755         phba->sli.slistat.sli_intr++;
12756
12757         /* Ignore all interrupts during initialization. */
12758         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12759                 return -EIO;
12760
12761         return 0;
12762 }
12763
12764 /**
12765  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
12766  * @irq: Interrupt number.
12767  * @dev_id: The device context pointer.
12768  *
12769  * This function is directly called from the PCI layer as an interrupt
12770  * service routine when device with SLI-3 interface spec is enabled with
12771  * MSI-X multi-message interrupt mode and there are slow-path events in
12772  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12773  * interrupt mode, this function is called as part of the device-level
12774  * interrupt handler. When the PCI slot is in error recovery or the HBA
12775  * is undergoing initialization, the interrupt handler will not process
12776  * the interrupt. The link attention and ELS ring attention events are
12777  * handled by the worker thread. The interrupt handler signals the worker
12778  * thread and returns for these events. This function is called without
12779  * any lock held. It gets the hbalock to access and update SLI data
12780  * structures.
12781  *
12782  * This function returns IRQ_HANDLED when interrupt is handled else it
12783  * returns IRQ_NONE.
12784  **/
12785 irqreturn_t
12786 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
12787 {
12788         struct lpfc_hba  *phba;
12789         uint32_t ha_copy, hc_copy;
12790         uint32_t work_ha_copy;
12791         unsigned long status;
12792         unsigned long iflag;
12793         uint32_t control;
12794
12795         MAILBOX_t *mbox, *pmbox;
12796         struct lpfc_vport *vport;
12797         struct lpfc_nodelist *ndlp;
12798         struct lpfc_dmabuf *mp;
12799         LPFC_MBOXQ_t *pmb;
12800         int rc;
12801
12802         /*
12803          * Get the driver's phba structure from the dev_id and
12804          * assume the HBA is not interrupting.
12805          */
12806         phba = (struct lpfc_hba *)dev_id;
12807
12808         if (unlikely(!phba))
12809                 return IRQ_NONE;
12810
12811         /*
12812          * Stuff needs to be attented to when this function is invoked as an
12813          * individual interrupt handler in MSI-X multi-message interrupt mode
12814          */
12815         if (phba->intr_type == MSIX) {
12816                 /* Check device state for handling interrupt */
12817                 if (lpfc_intr_state_check(phba))
12818                         return IRQ_NONE;
12819                 /* Need to read HA REG for slow-path events */
12820                 spin_lock_irqsave(&phba->hbalock, iflag);
12821                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12822                         goto unplug_error;
12823                 /* If somebody is waiting to handle an eratt don't process it
12824                  * here. The brdkill function will do this.
12825                  */
12826                 if (phba->link_flag & LS_IGNORE_ERATT)
12827                         ha_copy &= ~HA_ERATT;
12828                 /* Check the need for handling ERATT in interrupt handler */
12829                 if (ha_copy & HA_ERATT) {
12830                         if (phba->hba_flag & HBA_ERATT_HANDLED)
12831                                 /* ERATT polling has handled ERATT */
12832                                 ha_copy &= ~HA_ERATT;
12833                         else
12834                                 /* Indicate interrupt handler handles ERATT */
12835                                 phba->hba_flag |= HBA_ERATT_HANDLED;
12836                 }
12837
12838                 /*
12839                  * If there is deferred error attention, do not check for any
12840                  * interrupt.
12841                  */
12842                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12843                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12844                         return IRQ_NONE;
12845                 }
12846
12847                 /* Clear up only attention source related to slow-path */
12848                 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12849                         goto unplug_error;
12850
12851                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12852                         HC_LAINT_ENA | HC_ERINT_ENA),
12853                         phba->HCregaddr);
12854                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12855                         phba->HAregaddr);
12856                 writel(hc_copy, phba->HCregaddr);
12857                 readl(phba->HAregaddr); /* flush */
12858                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12859         } else
12860                 ha_copy = phba->ha_copy;
12861
12862         work_ha_copy = ha_copy & phba->work_ha_mask;
12863
12864         if (work_ha_copy) {
12865                 if (work_ha_copy & HA_LATT) {
12866                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12867                                 /*
12868                                  * Turn off Link Attention interrupts
12869                                  * until CLEAR_LA done
12870                                  */
12871                                 spin_lock_irqsave(&phba->hbalock, iflag);
12872                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
12873                                 if (lpfc_readl(phba->HCregaddr, &control))
12874                                         goto unplug_error;
12875                                 control &= ~HC_LAINT_ENA;
12876                                 writel(control, phba->HCregaddr);
12877                                 readl(phba->HCregaddr); /* flush */
12878                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12879                         }
12880                         else
12881                                 work_ha_copy &= ~HA_LATT;
12882                 }
12883
12884                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
12885                         /*
12886                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12887                          * the only slow ring.
12888                          */
12889                         status = (work_ha_copy &
12890                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
12891                         status >>= (4*LPFC_ELS_RING);
12892                         if (status & HA_RXMASK) {
12893                                 spin_lock_irqsave(&phba->hbalock, iflag);
12894                                 if (lpfc_readl(phba->HCregaddr, &control))
12895                                         goto unplug_error;
12896
12897                                 lpfc_debugfs_slow_ring_trc(phba,
12898                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
12899                                 control, status,
12900                                 (uint32_t)phba->sli.slistat.sli_intr);
12901
12902                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
12903                                         lpfc_debugfs_slow_ring_trc(phba,
12904                                                 "ISR Disable ring:"
12905                                                 "pwork:x%x hawork:x%x wait:x%x",
12906                                                 phba->work_ha, work_ha_copy,
12907                                                 (uint32_t)((unsigned long)
12908                                                 &phba->work_waitq));
12909
12910                                         control &=
12911                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
12912                                         writel(control, phba->HCregaddr);
12913                                         readl(phba->HCregaddr); /* flush */
12914                                 }
12915                                 else {
12916                                         lpfc_debugfs_slow_ring_trc(phba,
12917                                                 "ISR slow ring:   pwork:"
12918                                                 "x%x hawork:x%x wait:x%x",
12919                                                 phba->work_ha, work_ha_copy,
12920                                                 (uint32_t)((unsigned long)
12921                                                 &phba->work_waitq));
12922                                 }
12923                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12924                         }
12925                 }
12926                 spin_lock_irqsave(&phba->hbalock, iflag);
12927                 if (work_ha_copy & HA_ERATT) {
12928                         if (lpfc_sli_read_hs(phba))
12929                                 goto unplug_error;
12930                         /*
12931                          * Check if there is a deferred error condition
12932                          * is active
12933                          */
12934                         if ((HS_FFER1 & phba->work_hs) &&
12935                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
12936                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
12937                                   phba->work_hs)) {
12938                                 phba->hba_flag |= DEFER_ERATT;
12939                                 /* Clear all interrupt enable conditions */
12940                                 writel(0, phba->HCregaddr);
12941                                 readl(phba->HCregaddr);
12942                         }
12943                 }
12944
12945                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
12946                         pmb = phba->sli.mbox_active;
12947                         pmbox = &pmb->u.mb;
12948                         mbox = phba->mbox;
12949                         vport = pmb->vport;
12950
12951                         /* First check out the status word */
12952                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12953                         if (pmbox->mbxOwner != OWN_HOST) {
12954                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12955                                 /*
12956                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
12957                                  * mbxStatus <status>
12958                                  */
12959                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12960                                                 "(%d):0304 Stray Mailbox "
12961                                                 "Interrupt mbxCommand x%x "
12962                                                 "mbxStatus x%x\n",
12963                                                 (vport ? vport->vpi : 0),
12964                                                 pmbox->mbxCommand,
12965                                                 pmbox->mbxStatus);
12966                                 /* clear mailbox attention bit */
12967                                 work_ha_copy &= ~HA_MBATT;
12968                         } else {
12969                                 phba->sli.mbox_active = NULL;
12970                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12971                                 phba->last_completion_time = jiffies;
12972                                 del_timer(&phba->sli.mbox_tmo);
12973                                 if (pmb->mbox_cmpl) {
12974                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
12975                                                         MAILBOX_CMD_SIZE);
12976                                         if (pmb->out_ext_byte_len &&
12977                                                 pmb->ctx_buf)
12978                                                 lpfc_sli_pcimem_bcopy(
12979                                                 phba->mbox_ext,
12980                                                 pmb->ctx_buf,
12981                                                 pmb->out_ext_byte_len);
12982                                 }
12983                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12984                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12985
12986                                         lpfc_debugfs_disc_trc(vport,
12987                                                 LPFC_DISC_TRC_MBOX_VPORT,
12988                                                 "MBOX dflt rpi: : "
12989                                                 "status:x%x rpi:x%x",
12990                                                 (uint32_t)pmbox->mbxStatus,
12991                                                 pmbox->un.varWords[0], 0);
12992
12993                                         if (!pmbox->mbxStatus) {
12994                                                 mp = (struct lpfc_dmabuf *)
12995                                                         (pmb->ctx_buf);
12996                                                 ndlp = (struct lpfc_nodelist *)
12997                                                         pmb->ctx_ndlp;
12998
12999                                                 /* Reg_LOGIN of dflt RPI was
13000                                                  * successful. new lets get
13001                                                  * rid of the RPI using the
13002                                                  * same mbox buffer.
13003                                                  */
13004                                                 lpfc_unreg_login(phba,
13005                                                         vport->vpi,
13006                                                         pmbox->un.varWords[0],
13007                                                         pmb);
13008                                                 pmb->mbox_cmpl =
13009                                                         lpfc_mbx_cmpl_dflt_rpi;
13010                                                 pmb->ctx_buf = mp;
13011                                                 pmb->ctx_ndlp = ndlp;
13012                                                 pmb->vport = vport;
13013                                                 rc = lpfc_sli_issue_mbox(phba,
13014                                                                 pmb,
13015                                                                 MBX_NOWAIT);
13016                                                 if (rc != MBX_BUSY)
13017                                                         lpfc_printf_log(phba,
13018                                                         KERN_ERR,
13019                                                         LOG_TRACE_EVENT,
13020                                                         "0350 rc should have"
13021                                                         "been MBX_BUSY\n");
13022                                                 if (rc != MBX_NOT_FINISHED)
13023                                                         goto send_current_mbox;
13024                                         }
13025                                 }
13026                                 spin_lock_irqsave(
13027                                                 &phba->pport->work_port_lock,
13028                                                 iflag);
13029                                 phba->pport->work_port_events &=
13030                                         ~WORKER_MBOX_TMO;
13031                                 spin_unlock_irqrestore(
13032                                                 &phba->pport->work_port_lock,
13033                                                 iflag);
13034
13035                                 /* Do NOT queue MBX_HEARTBEAT to the worker
13036                                  * thread for processing.
13037                                  */
13038                                 if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13039                                         /* Process mbox now */
13040                                         phba->sli.mbox_active = NULL;
13041                                         phba->sli.sli_flag &=
13042                                                 ~LPFC_SLI_MBOX_ACTIVE;
13043                                         if (pmb->mbox_cmpl)
13044                                                 pmb->mbox_cmpl(phba, pmb);
13045                                 } else {
13046                                         /* Queue to worker thread to process */
13047                                         lpfc_mbox_cmpl_put(phba, pmb);
13048                                 }
13049                         }
13050                 } else
13051                         spin_unlock_irqrestore(&phba->hbalock, iflag);
13052
13053                 if ((work_ha_copy & HA_MBATT) &&
13054                     (phba->sli.mbox_active == NULL)) {
13055 send_current_mbox:
13056                         /* Process next mailbox command if there is one */
13057                         do {
13058                                 rc = lpfc_sli_issue_mbox(phba, NULL,
13059                                                          MBX_NOWAIT);
13060                         } while (rc == MBX_NOT_FINISHED);
13061                         if (rc != MBX_SUCCESS)
13062                                 lpfc_printf_log(phba, KERN_ERR,
13063                                                 LOG_TRACE_EVENT,
13064                                                 "0349 rc should be "
13065                                                 "MBX_SUCCESS\n");
13066                 }
13067
13068                 spin_lock_irqsave(&phba->hbalock, iflag);
13069                 phba->work_ha |= work_ha_copy;
13070                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13071                 lpfc_worker_wake_up(phba);
13072         }
13073         return IRQ_HANDLED;
13074 unplug_error:
13075         spin_unlock_irqrestore(&phba->hbalock, iflag);
13076         return IRQ_HANDLED;
13077
13078 } /* lpfc_sli_sp_intr_handler */
13079
13080 /**
13081  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
13082  * @irq: Interrupt number.
13083  * @dev_id: The device context pointer.
13084  *
13085  * This function is directly called from the PCI layer as an interrupt
13086  * service routine when device with SLI-3 interface spec is enabled with
13087  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13088  * ring event in the HBA. However, when the device is enabled with either
13089  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13090  * device-level interrupt handler. When the PCI slot is in error recovery
13091  * or the HBA is undergoing initialization, the interrupt handler will not
13092  * process the interrupt. The SCSI FCP fast-path ring event are handled in
13093  * the intrrupt context. This function is called without any lock held.
13094  * It gets the hbalock to access and update SLI data structures.
13095  *
13096  * This function returns IRQ_HANDLED when interrupt is handled else it
13097  * returns IRQ_NONE.
13098  **/
13099 irqreturn_t
13100 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
13101 {
13102         struct lpfc_hba  *phba;
13103         uint32_t ha_copy;
13104         unsigned long status;
13105         unsigned long iflag;
13106         struct lpfc_sli_ring *pring;
13107
13108         /* Get the driver's phba structure from the dev_id and
13109          * assume the HBA is not interrupting.
13110          */
13111         phba = (struct lpfc_hba *) dev_id;
13112
13113         if (unlikely(!phba))
13114                 return IRQ_NONE;
13115
13116         /*
13117          * Stuff needs to be attented to when this function is invoked as an
13118          * individual interrupt handler in MSI-X multi-message interrupt mode
13119          */
13120         if (phba->intr_type == MSIX) {
13121                 /* Check device state for handling interrupt */
13122                 if (lpfc_intr_state_check(phba))
13123                         return IRQ_NONE;
13124                 /* Need to read HA REG for FCP ring and other ring events */
13125                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
13126                         return IRQ_HANDLED;
13127                 /* Clear up only attention source related to fast-path */
13128                 spin_lock_irqsave(&phba->hbalock, iflag);
13129                 /*
13130                  * If there is deferred error attention, do not check for
13131                  * any interrupt.
13132                  */
13133                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13134                         spin_unlock_irqrestore(&phba->hbalock, iflag);
13135                         return IRQ_NONE;
13136                 }
13137                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
13138                         phba->HAregaddr);
13139                 readl(phba->HAregaddr); /* flush */
13140                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13141         } else
13142                 ha_copy = phba->ha_copy;
13143
13144         /*
13145          * Process all events on FCP ring. Take the optimized path for FCP IO.
13146          */
13147         ha_copy &= ~(phba->work_ha_mask);
13148
13149         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13150         status >>= (4*LPFC_FCP_RING);
13151         pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
13152         if (status & HA_RXMASK)
13153                 lpfc_sli_handle_fast_ring_event(phba, pring, status);
13154
13155         if (phba->cfg_multi_ring_support == 2) {
13156                 /*
13157                  * Process all events on extra ring. Take the optimized path
13158                  * for extra ring IO.
13159                  */
13160                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
13161                 status >>= (4*LPFC_EXTRA_RING);
13162                 if (status & HA_RXMASK) {
13163                         lpfc_sli_handle_fast_ring_event(phba,
13164                                         &phba->sli.sli3_ring[LPFC_EXTRA_RING],
13165                                         status);
13166                 }
13167         }
13168         return IRQ_HANDLED;
13169 }  /* lpfc_sli_fp_intr_handler */
13170
13171 /**
13172  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
13173  * @irq: Interrupt number.
13174  * @dev_id: The device context pointer.
13175  *
13176  * This function is the HBA device-level interrupt handler to device with
13177  * SLI-3 interface spec, called from the PCI layer when either MSI or
13178  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
13179  * requires driver attention. This function invokes the slow-path interrupt
13180  * attention handling function and fast-path interrupt attention handling
13181  * function in turn to process the relevant HBA attention events. This
13182  * function is called without any lock held. It gets the hbalock to access
13183  * and update SLI data structures.
13184  *
13185  * This function returns IRQ_HANDLED when interrupt is handled, else it
13186  * returns IRQ_NONE.
13187  **/
13188 irqreturn_t
13189 lpfc_sli_intr_handler(int irq, void *dev_id)
13190 {
13191         struct lpfc_hba  *phba;
13192         irqreturn_t sp_irq_rc, fp_irq_rc;
13193         unsigned long status1, status2;
13194         uint32_t hc_copy;
13195
13196         /*
13197          * Get the driver's phba structure from the dev_id and
13198          * assume the HBA is not interrupting.
13199          */
13200         phba = (struct lpfc_hba *) dev_id;
13201
13202         if (unlikely(!phba))
13203                 return IRQ_NONE;
13204
13205         /* Check device state for handling interrupt */
13206         if (lpfc_intr_state_check(phba))
13207                 return IRQ_NONE;
13208
13209         spin_lock(&phba->hbalock);
13210         if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
13211                 spin_unlock(&phba->hbalock);
13212                 return IRQ_HANDLED;
13213         }
13214
13215         if (unlikely(!phba->ha_copy)) {
13216                 spin_unlock(&phba->hbalock);
13217                 return IRQ_NONE;
13218         } else if (phba->ha_copy & HA_ERATT) {
13219                 if (phba->hba_flag & HBA_ERATT_HANDLED)
13220                         /* ERATT polling has handled ERATT */
13221                         phba->ha_copy &= ~HA_ERATT;
13222                 else
13223                         /* Indicate interrupt handler handles ERATT */
13224                         phba->hba_flag |= HBA_ERATT_HANDLED;
13225         }
13226
13227         /*
13228          * If there is deferred error attention, do not check for any interrupt.
13229          */
13230         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13231                 spin_unlock(&phba->hbalock);
13232                 return IRQ_NONE;
13233         }
13234
13235         /* Clear attention sources except link and error attentions */
13236         if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
13237                 spin_unlock(&phba->hbalock);
13238                 return IRQ_HANDLED;
13239         }
13240         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
13241                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
13242                 phba->HCregaddr);
13243         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
13244         writel(hc_copy, phba->HCregaddr);
13245         readl(phba->HAregaddr); /* flush */
13246         spin_unlock(&phba->hbalock);
13247
13248         /*
13249          * Invokes slow-path host attention interrupt handling as appropriate.
13250          */
13251
13252         /* status of events with mailbox and link attention */
13253         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
13254
13255         /* status of events with ELS ring */
13256         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
13257         status2 >>= (4*LPFC_ELS_RING);
13258
13259         if (status1 || (status2 & HA_RXMASK))
13260                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
13261         else
13262                 sp_irq_rc = IRQ_NONE;
13263
13264         /*
13265          * Invoke fast-path host attention interrupt handling as appropriate.
13266          */
13267
13268         /* status of events with FCP ring */
13269         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13270         status1 >>= (4*LPFC_FCP_RING);
13271
13272         /* status of events with extra ring */
13273         if (phba->cfg_multi_ring_support == 2) {
13274                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
13275                 status2 >>= (4*LPFC_EXTRA_RING);
13276         } else
13277                 status2 = 0;
13278
13279         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
13280                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
13281         else
13282                 fp_irq_rc = IRQ_NONE;
13283
13284         /* Return device-level interrupt handling status */
13285         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
13286 }  /* lpfc_sli_intr_handler */
13287
13288 /**
13289  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
13290  * @phba: pointer to lpfc hba data structure.
13291  *
13292  * This routine is invoked by the worker thread to process all the pending
13293  * SLI4 els abort xri events.
13294  **/
13295 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
13296 {
13297         struct lpfc_cq_event *cq_event;
13298         unsigned long iflags;
13299
13300         /* First, declare the els xri abort event has been handled */
13301         spin_lock_irqsave(&phba->hbalock, iflags);
13302         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
13303         spin_unlock_irqrestore(&phba->hbalock, iflags);
13304
13305         /* Now, handle all the els xri abort events */
13306         spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
13307         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
13308                 /* Get the first event from the head of the event queue */
13309                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
13310                                  cq_event, struct lpfc_cq_event, list);
13311                 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
13312                                        iflags);
13313                 /* Notify aborted XRI for ELS work queue */
13314                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
13315
13316                 /* Free the event processed back to the free pool */
13317                 lpfc_sli4_cq_event_release(phba, cq_event);
13318                 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
13319                                   iflags);
13320         }
13321         spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
13322 }
13323
13324 /**
13325  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
13326  * @phba: pointer to lpfc hba data structure
13327  * @pIocbIn: pointer to the rspiocbq
13328  * @pIocbOut: pointer to the cmdiocbq
13329  * @wcqe: pointer to the complete wcqe
13330  *
13331  * This routine transfers the fields of a command iocbq to a response iocbq
13332  * by copying all the IOCB fields from command iocbq and transferring the
13333  * completion status information from the complete wcqe.
13334  **/
13335 static void
13336 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
13337                               struct lpfc_iocbq *pIocbIn,
13338                               struct lpfc_iocbq *pIocbOut,
13339                               struct lpfc_wcqe_complete *wcqe)
13340 {
13341         int numBdes, i;
13342         unsigned long iflags;
13343         uint32_t status, max_response;
13344         struct lpfc_dmabuf *dmabuf;
13345         struct ulp_bde64 *bpl, bde;
13346         size_t offset = offsetof(struct lpfc_iocbq, iocb);
13347
13348         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
13349                sizeof(struct lpfc_iocbq) - offset);
13350         /* Map WCQE parameters into irspiocb parameters */
13351         status = bf_get(lpfc_wcqe_c_status, wcqe);
13352         pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
13353         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
13354                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
13355                         pIocbIn->iocb.un.fcpi.fcpi_parm =
13356                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
13357                                         wcqe->total_data_placed;
13358                 else
13359                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
13360         else {
13361                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
13362                 switch (pIocbOut->iocb.ulpCommand) {
13363                 case CMD_ELS_REQUEST64_CR:
13364                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
13365                         bpl  = (struct ulp_bde64 *)dmabuf->virt;
13366                         bde.tus.w = le32_to_cpu(bpl[1].tus.w);
13367                         max_response = bde.tus.f.bdeSize;
13368                         break;
13369                 case CMD_GEN_REQUEST64_CR:
13370                         max_response = 0;
13371                         if (!pIocbOut->context3)
13372                                 break;
13373                         numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
13374                                         sizeof(struct ulp_bde64);
13375                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
13376                         bpl = (struct ulp_bde64 *)dmabuf->virt;
13377                         for (i = 0; i < numBdes; i++) {
13378                                 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
13379                                 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
13380                                         max_response += bde.tus.f.bdeSize;
13381                         }
13382                         break;
13383                 default:
13384                         max_response = wcqe->total_data_placed;
13385                         break;
13386                 }
13387                 if (max_response < wcqe->total_data_placed)
13388                         pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
13389                 else
13390                         pIocbIn->iocb.un.genreq64.bdl.bdeSize =
13391                                 wcqe->total_data_placed;
13392         }
13393
13394         /* Convert BG errors for completion status */
13395         if (status == CQE_STATUS_DI_ERROR) {
13396                 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
13397
13398                 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
13399                         pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
13400                 else
13401                         pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
13402
13403                 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
13404                 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
13405                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13406                                 BGS_GUARD_ERR_MASK;
13407                 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
13408                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13409                                 BGS_APPTAG_ERR_MASK;
13410                 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
13411                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13412                                 BGS_REFTAG_ERR_MASK;
13413
13414                 /* Check to see if there was any good data before the error */
13415                 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
13416                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13417                                 BGS_HI_WATER_MARK_PRESENT_MASK;
13418                         pIocbIn->iocb.unsli3.sli3_bg.bghm =
13419                                 wcqe->total_data_placed;
13420                 }
13421
13422                 /*
13423                 * Set ALL the error bits to indicate we don't know what
13424                 * type of error it is.
13425                 */
13426                 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
13427                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13428                                 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
13429                                 BGS_GUARD_ERR_MASK);
13430         }
13431
13432         /* Pick up HBA exchange busy condition */
13433         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
13434                 spin_lock_irqsave(&phba->hbalock, iflags);
13435                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
13436                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13437         }
13438 }
13439
13440 /**
13441  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
13442  * @phba: Pointer to HBA context object.
13443  * @irspiocbq: Pointer to work-queue completion queue entry.
13444  *
13445  * This routine handles an ELS work-queue completion event and construct
13446  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
13447  * discovery engine to handle.
13448  *
13449  * Return: Pointer to the receive IOCBQ, NULL otherwise.
13450  **/
13451 static struct lpfc_iocbq *
13452 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
13453                                struct lpfc_iocbq *irspiocbq)
13454 {
13455         struct lpfc_sli_ring *pring;
13456         struct lpfc_iocbq *cmdiocbq;
13457         struct lpfc_wcqe_complete *wcqe;
13458         unsigned long iflags;
13459
13460         pring = lpfc_phba_elsring(phba);
13461         if (unlikely(!pring))
13462                 return NULL;
13463
13464         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
13465         pring->stats.iocb_event++;
13466         /* Look up the ELS command IOCB and create pseudo response IOCB */
13467         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13468                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13469         if (unlikely(!cmdiocbq)) {
13470                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13471                                 "0386 ELS complete with no corresponding "
13472                                 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
13473                                 wcqe->word0, wcqe->total_data_placed,
13474                                 wcqe->parameter, wcqe->word3);
13475                 lpfc_sli_release_iocbq(phba, irspiocbq);
13476                 return NULL;
13477         }
13478
13479         spin_lock_irqsave(&pring->ring_lock, iflags);
13480         /* Put the iocb back on the txcmplq */
13481         lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13482         spin_unlock_irqrestore(&pring->ring_lock, iflags);
13483
13484         /* Fake the irspiocbq and copy necessary response information */
13485         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
13486
13487         return irspiocbq;
13488 }
13489
13490 inline struct lpfc_cq_event *
13491 lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13492 {
13493         struct lpfc_cq_event *cq_event;
13494
13495         /* Allocate a new internal CQ_EVENT entry */
13496         cq_event = lpfc_sli4_cq_event_alloc(phba);
13497         if (!cq_event) {
13498                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13499                                 "0602 Failed to alloc CQ_EVENT entry\n");
13500                 return NULL;
13501         }
13502
13503         /* Move the CQE into the event */
13504         memcpy(&cq_event->cqe, entry, size);
13505         return cq_event;
13506 }
13507
13508 /**
13509  * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
13510  * @phba: Pointer to HBA context object.
13511  * @mcqe: Pointer to mailbox completion queue entry.
13512  *
13513  * This routine process a mailbox completion queue entry with asynchronous
13514  * event.
13515  *
13516  * Return: true if work posted to worker thread, otherwise false.
13517  **/
13518 static bool
13519 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13520 {
13521         struct lpfc_cq_event *cq_event;
13522         unsigned long iflags;
13523
13524         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13525                         "0392 Async Event: word0:x%x, word1:x%x, "
13526                         "word2:x%x, word3:x%x\n", mcqe->word0,
13527                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13528
13529         cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13530         if (!cq_event)
13531                 return false;
13532
13533         spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
13534         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13535         spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
13536
13537         /* Set the async event flag */
13538         spin_lock_irqsave(&phba->hbalock, iflags);
13539         phba->hba_flag |= ASYNC_EVENT;
13540         spin_unlock_irqrestore(&phba->hbalock, iflags);
13541
13542         return true;
13543 }
13544
13545 /**
13546  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13547  * @phba: Pointer to HBA context object.
13548  * @mcqe: Pointer to mailbox completion queue entry.
13549  *
13550  * This routine process a mailbox completion queue entry with mailbox
13551  * completion event.
13552  *
13553  * Return: true if work posted to worker thread, otherwise false.
13554  **/
13555 static bool
13556 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13557 {
13558         uint32_t mcqe_status;
13559         MAILBOX_t *mbox, *pmbox;
13560         struct lpfc_mqe *mqe;
13561         struct lpfc_vport *vport;
13562         struct lpfc_nodelist *ndlp;
13563         struct lpfc_dmabuf *mp;
13564         unsigned long iflags;
13565         LPFC_MBOXQ_t *pmb;
13566         bool workposted = false;
13567         int rc;
13568
13569         /* If not a mailbox complete MCQE, out by checking mailbox consume */
13570         if (!bf_get(lpfc_trailer_completed, mcqe))
13571                 goto out_no_mqe_complete;
13572
13573         /* Get the reference to the active mbox command */
13574         spin_lock_irqsave(&phba->hbalock, iflags);
13575         pmb = phba->sli.mbox_active;
13576         if (unlikely(!pmb)) {
13577                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13578                                 "1832 No pending MBOX command to handle\n");
13579                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13580                 goto out_no_mqe_complete;
13581         }
13582         spin_unlock_irqrestore(&phba->hbalock, iflags);
13583         mqe = &pmb->u.mqe;
13584         pmbox = (MAILBOX_t *)&pmb->u.mqe;
13585         mbox = phba->mbox;
13586         vport = pmb->vport;
13587
13588         /* Reset heartbeat timer */
13589         phba->last_completion_time = jiffies;
13590         del_timer(&phba->sli.mbox_tmo);
13591
13592         /* Move mbox data to caller's mailbox region, do endian swapping */
13593         if (pmb->mbox_cmpl && mbox)
13594                 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
13595
13596         /*
13597          * For mcqe errors, conditionally move a modified error code to
13598          * the mbox so that the error will not be missed.
13599          */
13600         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13601         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13602                 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13603                         bf_set(lpfc_mqe_status, mqe,
13604                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
13605         }
13606         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13607                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13608                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13609                                       "MBOX dflt rpi: status:x%x rpi:x%x",
13610                                       mcqe_status,
13611                                       pmbox->un.varWords[0], 0);
13612                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
13613                         mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13614                         ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
13615                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
13616                          * RID of the PPI using the same mbox buffer.
13617                          */
13618                         lpfc_unreg_login(phba, vport->vpi,
13619                                          pmbox->un.varWords[0], pmb);
13620                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
13621                         pmb->ctx_buf = mp;
13622
13623                         /* No reference taken here.  This is a default
13624                          * RPI reg/immediate unreg cycle. The reference was
13625                          * taken in the reg rpi path and is released when
13626                          * this mailbox completes.
13627                          */
13628                         pmb->ctx_ndlp = ndlp;
13629                         pmb->vport = vport;
13630                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13631                         if (rc != MBX_BUSY)
13632                                 lpfc_printf_log(phba, KERN_ERR,
13633                                                 LOG_TRACE_EVENT,
13634                                                 "0385 rc should "
13635                                                 "have been MBX_BUSY\n");
13636                         if (rc != MBX_NOT_FINISHED)
13637                                 goto send_current_mbox;
13638                 }
13639         }
13640         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13641         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13642         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13643
13644         /* Do NOT queue MBX_HEARTBEAT to the worker thread for processing. */
13645         if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13646                 spin_lock_irqsave(&phba->hbalock, iflags);
13647                 /* Release the mailbox command posting token */
13648                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13649                 phba->sli.mbox_active = NULL;
13650                 if (bf_get(lpfc_trailer_consumed, mcqe))
13651                         lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13652                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13653
13654                 /* Post the next mbox command, if there is one */
13655                 lpfc_sli4_post_async_mbox(phba);
13656
13657                 /* Process cmpl now */
13658                 if (pmb->mbox_cmpl)
13659                         pmb->mbox_cmpl(phba, pmb);
13660                 return false;
13661         }
13662
13663         /* There is mailbox completion work to queue to the worker thread */
13664         spin_lock_irqsave(&phba->hbalock, iflags);
13665         __lpfc_mbox_cmpl_put(phba, pmb);
13666         phba->work_ha |= HA_MBATT;
13667         spin_unlock_irqrestore(&phba->hbalock, iflags);
13668         workposted = true;
13669
13670 send_current_mbox:
13671         spin_lock_irqsave(&phba->hbalock, iflags);
13672         /* Release the mailbox command posting token */
13673         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13674         /* Setting active mailbox pointer need to be in sync to flag clear */
13675         phba->sli.mbox_active = NULL;
13676         if (bf_get(lpfc_trailer_consumed, mcqe))
13677                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13678         spin_unlock_irqrestore(&phba->hbalock, iflags);
13679         /* Wake up worker thread to post the next pending mailbox command */
13680         lpfc_worker_wake_up(phba);
13681         return workposted;
13682
13683 out_no_mqe_complete:
13684         spin_lock_irqsave(&phba->hbalock, iflags);
13685         if (bf_get(lpfc_trailer_consumed, mcqe))
13686                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13687         spin_unlock_irqrestore(&phba->hbalock, iflags);
13688         return false;
13689 }
13690
13691 /**
13692  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13693  * @phba: Pointer to HBA context object.
13694  * @cq: Pointer to associated CQ
13695  * @cqe: Pointer to mailbox completion queue entry.
13696  *
13697  * This routine process a mailbox completion queue entry, it invokes the
13698  * proper mailbox complete handling or asynchronous event handling routine
13699  * according to the MCQE's async bit.
13700  *
13701  * Return: true if work posted to worker thread, otherwise false.
13702  **/
13703 static bool
13704 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13705                          struct lpfc_cqe *cqe)
13706 {
13707         struct lpfc_mcqe mcqe;
13708         bool workposted;
13709
13710         cq->CQ_mbox++;
13711
13712         /* Copy the mailbox MCQE and convert endian order as needed */
13713         lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
13714
13715         /* Invoke the proper event handling routine */
13716         if (!bf_get(lpfc_trailer_async, &mcqe))
13717                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13718         else
13719                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13720         return workposted;
13721 }
13722
13723 /**
13724  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13725  * @phba: Pointer to HBA context object.
13726  * @cq: Pointer to associated CQ
13727  * @wcqe: Pointer to work-queue completion queue entry.
13728  *
13729  * This routine handles an ELS work-queue completion event.
13730  *
13731  * Return: true if work posted to worker thread, otherwise false.
13732  **/
13733 static bool
13734 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13735                              struct lpfc_wcqe_complete *wcqe)
13736 {
13737         struct lpfc_iocbq *irspiocbq;
13738         unsigned long iflags;
13739         struct lpfc_sli_ring *pring = cq->pring;
13740         int txq_cnt = 0;
13741         int txcmplq_cnt = 0;
13742
13743         /* Check for response status */
13744         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13745                 /* Log the error status */
13746                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13747                                 "0357 ELS CQE error: status=x%x: "
13748                                 "CQE: %08x %08x %08x %08x\n",
13749                                 bf_get(lpfc_wcqe_c_status, wcqe),
13750                                 wcqe->word0, wcqe->total_data_placed,
13751                                 wcqe->parameter, wcqe->word3);
13752         }
13753
13754         /* Get an irspiocbq for later ELS response processing use */
13755         irspiocbq = lpfc_sli_get_iocbq(phba);
13756         if (!irspiocbq) {
13757                 if (!list_empty(&pring->txq))
13758                         txq_cnt++;
13759                 if (!list_empty(&pring->txcmplq))
13760                         txcmplq_cnt++;
13761                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13762                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13763                         "els_txcmplq_cnt=%d\n",
13764                         txq_cnt, phba->iocb_cnt,
13765                         txcmplq_cnt);
13766                 return false;
13767         }
13768
13769         /* Save off the slow-path queue event for work thread to process */
13770         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
13771         spin_lock_irqsave(&phba->hbalock, iflags);
13772         list_add_tail(&irspiocbq->cq_event.list,
13773                       &phba->sli4_hba.sp_queue_event);
13774         phba->hba_flag |= HBA_SP_QUEUE_EVT;
13775         spin_unlock_irqrestore(&phba->hbalock, iflags);
13776
13777         return true;
13778 }
13779
13780 /**
13781  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13782  * @phba: Pointer to HBA context object.
13783  * @wcqe: Pointer to work-queue completion queue entry.
13784  *
13785  * This routine handles slow-path WQ entry consumed event by invoking the
13786  * proper WQ release routine to the slow-path WQ.
13787  **/
13788 static void
13789 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13790                              struct lpfc_wcqe_release *wcqe)
13791 {
13792         /* sanity check on queue memory */
13793         if (unlikely(!phba->sli4_hba.els_wq))
13794                 return;
13795         /* Check for the slow-path ELS work queue */
13796         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13797                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13798                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13799         else
13800                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13801                                 "2579 Slow-path wqe consume event carries "
13802                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13803                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13804                                 phba->sli4_hba.els_wq->queue_id);
13805 }
13806
13807 /**
13808  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13809  * @phba: Pointer to HBA context object.
13810  * @cq: Pointer to a WQ completion queue.
13811  * @wcqe: Pointer to work-queue completion queue entry.
13812  *
13813  * This routine handles an XRI abort event.
13814  *
13815  * Return: true if work posted to worker thread, otherwise false.
13816  **/
13817 static bool
13818 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13819                                    struct lpfc_queue *cq,
13820                                    struct sli4_wcqe_xri_aborted *wcqe)
13821 {
13822         bool workposted = false;
13823         struct lpfc_cq_event *cq_event;
13824         unsigned long iflags;
13825
13826         switch (cq->subtype) {
13827         case LPFC_IO:
13828                 lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
13829                 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
13830                         /* Notify aborted XRI for NVME work queue */
13831                         if (phba->nvmet_support)
13832                                 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13833                 }
13834                 workposted = false;
13835                 break;
13836         case LPFC_NVME_LS: /* NVME LS uses ELS resources */
13837         case LPFC_ELS:
13838                 cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));
13839                 if (!cq_event) {
13840                         workposted = false;
13841                         break;
13842                 }
13843                 cq_event->hdwq = cq->hdwq;
13844                 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
13845                                   iflags);
13846                 list_add_tail(&cq_event->list,
13847                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13848                 /* Set the els xri abort event flag */
13849                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13850                 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
13851                                        iflags);
13852                 workposted = true;
13853                 break;
13854         default:
13855                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13856                                 "0603 Invalid CQ subtype %d: "
13857                                 "%08x %08x %08x %08x\n",
13858                                 cq->subtype, wcqe->word0, wcqe->parameter,
13859                                 wcqe->word2, wcqe->word3);
13860                 workposted = false;
13861                 break;
13862         }
13863         return workposted;
13864 }
13865
13866 #define FC_RCTL_MDS_DIAGS       0xF4
13867
13868 /**
13869  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
13870  * @phba: Pointer to HBA context object.
13871  * @rcqe: Pointer to receive-queue completion queue entry.
13872  *
13873  * This routine process a receive-queue completion queue entry.
13874  *
13875  * Return: true if work posted to worker thread, otherwise false.
13876  **/
13877 static bool
13878 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13879 {
13880         bool workposted = false;
13881         struct fc_frame_header *fc_hdr;
13882         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13883         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
13884         struct lpfc_nvmet_tgtport *tgtp;
13885         struct hbq_dmabuf *dma_buf;
13886         uint32_t status, rq_id;
13887         unsigned long iflags;
13888
13889         /* sanity check on queue memory */
13890         if (unlikely(!hrq) || unlikely(!drq))
13891                 return workposted;
13892
13893         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13894                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13895         else
13896                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13897         if (rq_id != hrq->queue_id)
13898                 goto out;
13899
13900         status = bf_get(lpfc_rcqe_status, rcqe);
13901         switch (status) {
13902         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13903                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13904                                 "2537 Receive Frame Truncated!!\n");
13905                 fallthrough;
13906         case FC_STATUS_RQ_SUCCESS:
13907                 spin_lock_irqsave(&phba->hbalock, iflags);
13908                 lpfc_sli4_rq_release(hrq, drq);
13909                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13910                 if (!dma_buf) {
13911                         hrq->RQ_no_buf_found++;
13912                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13913                         goto out;
13914                 }
13915                 hrq->RQ_rcv_buf++;
13916                 hrq->RQ_buf_posted--;
13917                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
13918
13919                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13920
13921                 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
13922                     fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
13923                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13924                         /* Handle MDS Loopback frames */
13925                         if  (!(phba->pport->load_flag & FC_UNLOADING))
13926                                 lpfc_sli4_handle_mds_loopback(phba->pport,
13927                                                               dma_buf);
13928                         else
13929                                 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13930                         break;
13931                 }
13932
13933                 /* save off the frame for the work thread to process */
13934                 list_add_tail(&dma_buf->cq_event.list,
13935                               &phba->sli4_hba.sp_queue_event);
13936                 /* Frame received */
13937                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
13938                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13939                 workposted = true;
13940                 break;
13941         case FC_STATUS_INSUFF_BUF_FRM_DISC:
13942                 if (phba->nvmet_support) {
13943                         tgtp = phba->targetport->private;
13944                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13945                                         "6402 RQE Error x%x, posted %d err_cnt "
13946                                         "%d: %x %x %x\n",
13947                                         status, hrq->RQ_buf_posted,
13948                                         hrq->RQ_no_posted_buf,
13949                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
13950                                         atomic_read(&tgtp->rcv_fcp_cmd_out),
13951                                         atomic_read(&tgtp->xmt_fcp_release));
13952                 }
13953                 fallthrough;
13954
13955         case FC_STATUS_INSUFF_BUF_NEED_BUF:
13956                 hrq->RQ_no_posted_buf++;
13957                 /* Post more buffers if possible */
13958                 spin_lock_irqsave(&phba->hbalock, iflags);
13959                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13960                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13961                 workposted = true;
13962                 break;
13963         }
13964 out:
13965         return workposted;
13966 }
13967
13968 /**
13969  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13970  * @phba: Pointer to HBA context object.
13971  * @cq: Pointer to the completion queue.
13972  * @cqe: Pointer to a completion queue entry.
13973  *
13974  * This routine process a slow-path work-queue or receive queue completion queue
13975  * entry.
13976  *
13977  * Return: true if work posted to worker thread, otherwise false.
13978  **/
13979 static bool
13980 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13981                          struct lpfc_cqe *cqe)
13982 {
13983         struct lpfc_cqe cqevt;
13984         bool workposted = false;
13985
13986         /* Copy the work queue CQE and convert endian order if needed */
13987         lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
13988
13989         /* Check and process for different type of WCQE and dispatch */
13990         switch (bf_get(lpfc_cqe_code, &cqevt)) {
13991         case CQE_CODE_COMPL_WQE:
13992                 /* Process the WQ/RQ complete event */
13993                 phba->last_completion_time = jiffies;
13994                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
13995                                 (struct lpfc_wcqe_complete *)&cqevt);
13996                 break;
13997         case CQE_CODE_RELEASE_WQE:
13998                 /* Process the WQ release event */
13999                 lpfc_sli4_sp_handle_rel_wcqe(phba,
14000                                 (struct lpfc_wcqe_release *)&cqevt);
14001                 break;
14002         case CQE_CODE_XRI_ABORTED:
14003                 /* Process the WQ XRI abort event */
14004                 phba->last_completion_time = jiffies;
14005                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14006                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
14007                 break;
14008         case CQE_CODE_RECEIVE:
14009         case CQE_CODE_RECEIVE_V1:
14010                 /* Process the RQ event */
14011                 phba->last_completion_time = jiffies;
14012                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
14013                                 (struct lpfc_rcqe *)&cqevt);
14014                 break;
14015         default:
14016                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14017                                 "0388 Not a valid WCQE code: x%x\n",
14018                                 bf_get(lpfc_cqe_code, &cqevt));
14019                 break;
14020         }
14021         return workposted;
14022 }
14023
14024 /**
14025  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
14026  * @phba: Pointer to HBA context object.
14027  * @eqe: Pointer to fast-path event queue entry.
14028  * @speq: Pointer to slow-path event queue.
14029  *
14030  * This routine process a event queue entry from the slow-path event queue.
14031  * It will check the MajorCode and MinorCode to determine this is for a
14032  * completion event on a completion queue, if not, an error shall be logged
14033  * and just return. Otherwise, it will get to the corresponding completion
14034  * queue and process all the entries on that completion queue, rearm the
14035  * completion queue, and then return.
14036  *
14037  **/
14038 static void
14039 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14040         struct lpfc_queue *speq)
14041 {
14042         struct lpfc_queue *cq = NULL, *childq;
14043         uint16_t cqid;
14044         int ret = 0;
14045
14046         /* Get the reference to the corresponding CQ */
14047         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14048
14049         list_for_each_entry(childq, &speq->child_list, list) {
14050                 if (childq->queue_id == cqid) {
14051                         cq = childq;
14052                         break;
14053                 }
14054         }
14055         if (unlikely(!cq)) {
14056                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
14057                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14058                                         "0365 Slow-path CQ identifier "
14059                                         "(%d) does not exist\n", cqid);
14060                 return;
14061         }
14062
14063         /* Save EQ associated with this CQ */
14064         cq->assoc_qp = speq;
14065
14066         if (is_kdump_kernel())
14067                 ret = queue_work(phba->wq, &cq->spwork);
14068         else
14069                 ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
14070
14071         if (!ret)
14072                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14073                                 "0390 Cannot schedule queue work "
14074                                 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14075                                 cqid, cq->queue_id, raw_smp_processor_id());
14076 }
14077
14078 /**
14079  * __lpfc_sli4_process_cq - Process elements of a CQ
14080  * @phba: Pointer to HBA context object.
14081  * @cq: Pointer to CQ to be processed
14082  * @handler: Routine to process each cqe
14083  * @delay: Pointer to usdelay to set in case of rescheduling of the handler
14084  * @poll_mode: Polling mode we were called from
14085  *
14086  * This routine processes completion queue entries in a CQ. While a valid
14087  * queue element is found, the handler is called. During processing checks
14088  * are made for periodic doorbell writes to let the hardware know of
14089  * element consumption.
14090  *
14091  * If the max limit on cqes to process is hit, or there are no more valid
14092  * entries, the loop stops. If we processed a sufficient number of elements,
14093  * meaning there is sufficient load, rather than rearming and generating
14094  * another interrupt, a cq rescheduling delay will be set. A delay of 0
14095  * indicates no rescheduling.
14096  *
14097  * Returns True if work scheduled, False otherwise.
14098  **/
14099 static bool
14100 __lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
14101         bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
14102                         struct lpfc_cqe *), unsigned long *delay,
14103                         enum lpfc_poll_mode poll_mode)
14104 {
14105         struct lpfc_cqe *cqe;
14106         bool workposted = false;
14107         int count = 0, consumed = 0;
14108         bool arm = true;
14109
14110         /* default - no reschedule */
14111         *delay = 0;
14112
14113         if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
14114                 goto rearm_and_exit;
14115
14116         /* Process all the entries to the CQ */
14117         cq->q_flag = 0;
14118         cqe = lpfc_sli4_cq_get(cq);
14119         while (cqe) {
14120                 workposted |= handler(phba, cq, cqe);
14121                 __lpfc_sli4_consume_cqe(phba, cq, cqe);
14122
14123                 consumed++;
14124                 if (!(++count % cq->max_proc_limit))
14125                         break;
14126
14127                 if (!(count % cq->notify_interval)) {
14128                         phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14129                                                 LPFC_QUEUE_NOARM);
14130                         consumed = 0;
14131                         cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
14132                 }
14133
14134                 if (count == LPFC_NVMET_CQ_NOTIFY)
14135                         cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
14136
14137                 cqe = lpfc_sli4_cq_get(cq);
14138         }
14139         if (count >= phba->cfg_cq_poll_threshold) {
14140                 *delay = 1;
14141                 arm = false;
14142         }
14143
14144         /* Note: complete the irq_poll softirq before rearming CQ */
14145         if (poll_mode == LPFC_IRQ_POLL)
14146                 irq_poll_complete(&cq->iop);
14147
14148         /* Track the max number of CQEs processed in 1 EQ */
14149         if (count > cq->CQ_max_cqe)
14150                 cq->CQ_max_cqe = count;
14151
14152         cq->assoc_qp->EQ_cqe_cnt += count;
14153
14154         /* Catch the no cq entry condition */
14155         if (unlikely(count == 0))
14156                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14157                                 "0369 No entry from completion queue "
14158                                 "qid=%d\n", cq->queue_id);
14159
14160         xchg(&cq->queue_claimed, 0);
14161
14162 rearm_and_exit:
14163         phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14164                         arm ?  LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
14165
14166         return workposted;
14167 }
14168
14169 /**
14170  * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
14171  * @cq: pointer to CQ to process
14172  *
14173  * This routine calls the cq processing routine with a handler specific
14174  * to the type of queue bound to it.
14175  *
14176  * The CQ routine returns two values: the first is the calling status,
14177  * which indicates whether work was queued to the  background discovery
14178  * thread. If true, the routine should wakeup the discovery thread;
14179  * the second is the delay parameter. If non-zero, rather than rearming
14180  * the CQ and yet another interrupt, the CQ handler should be queued so
14181  * that it is processed in a subsequent polling action. The value of
14182  * the delay indicates when to reschedule it.
14183  **/
14184 static void
14185 __lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
14186 {
14187         struct lpfc_hba *phba = cq->phba;
14188         unsigned long delay;
14189         bool workposted = false;
14190         int ret = 0;
14191
14192         /* Process and rearm the CQ */
14193         switch (cq->type) {
14194         case LPFC_MCQ:
14195                 workposted |= __lpfc_sli4_process_cq(phba, cq,
14196                                                 lpfc_sli4_sp_handle_mcqe,
14197                                                 &delay, LPFC_QUEUE_WORK);
14198                 break;
14199         case LPFC_WCQ:
14200                 if (cq->subtype == LPFC_IO)
14201                         workposted |= __lpfc_sli4_process_cq(phba, cq,
14202                                                 lpfc_sli4_fp_handle_cqe,
14203                                                 &delay, LPFC_QUEUE_WORK);
14204                 else
14205                         workposted |= __lpfc_sli4_process_cq(phba, cq,
14206                                                 lpfc_sli4_sp_handle_cqe,
14207                                                 &delay, LPFC_QUEUE_WORK);
14208                 break;
14209         default:
14210                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14211                                 "0370 Invalid completion queue type (%d)\n",
14212                                 cq->type);
14213                 return;
14214         }
14215
14216         if (delay) {
14217                 if (is_kdump_kernel())
14218                         ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
14219                                                 delay);
14220                 else
14221                         ret = queue_delayed_work_on(cq->chann, phba->wq,
14222                                                 &cq->sched_spwork, delay);
14223                 if (!ret)
14224                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14225                                 "0394 Cannot schedule queue work "
14226                                 "for cqid=%d on CPU %d\n",
14227                                 cq->queue_id, cq->chann);
14228         }
14229
14230         /* wake up worker thread if there are works to be done */
14231         if (workposted)
14232                 lpfc_worker_wake_up(phba);
14233 }
14234
14235 /**
14236  * lpfc_sli4_sp_process_cq - slow-path work handler when started by
14237  *   interrupt
14238  * @work: pointer to work element
14239  *
14240  * translates from the work handler and calls the slow-path handler.
14241  **/
14242 static void
14243 lpfc_sli4_sp_process_cq(struct work_struct *work)
14244 {
14245         struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
14246
14247         __lpfc_sli4_sp_process_cq(cq);
14248 }
14249
14250 /**
14251  * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
14252  * @work: pointer to work element
14253  *
14254  * translates from the work handler and calls the slow-path handler.
14255  **/
14256 static void
14257 lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
14258 {
14259         struct lpfc_queue *cq = container_of(to_delayed_work(work),
14260                                         struct lpfc_queue, sched_spwork);
14261
14262         __lpfc_sli4_sp_process_cq(cq);
14263 }
14264
14265 /**
14266  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
14267  * @phba: Pointer to HBA context object.
14268  * @cq: Pointer to associated CQ
14269  * @wcqe: Pointer to work-queue completion queue entry.
14270  *
14271  * This routine process a fast-path work queue completion entry from fast-path
14272  * event queue for FCP command response completion.
14273  **/
14274 static void
14275 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14276                              struct lpfc_wcqe_complete *wcqe)
14277 {
14278         struct lpfc_sli_ring *pring = cq->pring;
14279         struct lpfc_iocbq *cmdiocbq;
14280         struct lpfc_iocbq irspiocbq;
14281         unsigned long iflags;
14282
14283         /* Check for response status */
14284         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14285                 /* If resource errors reported from HBA, reduce queue
14286                  * depth of the SCSI device.
14287                  */
14288                 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
14289                      IOSTAT_LOCAL_REJECT)) &&
14290                     ((wcqe->parameter & IOERR_PARAM_MASK) ==
14291                      IOERR_NO_RESOURCES))
14292                         phba->lpfc_rampdown_queue_depth(phba);
14293
14294                 /* Log the cmpl status */
14295                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14296                                 "0373 FCP CQE cmpl: status=x%x: "
14297                                 "CQE: %08x %08x %08x %08x\n",
14298                                 bf_get(lpfc_wcqe_c_status, wcqe),
14299                                 wcqe->word0, wcqe->total_data_placed,
14300                                 wcqe->parameter, wcqe->word3);
14301         }
14302
14303         /* Look up the FCP command IOCB and create pseudo response IOCB */
14304         spin_lock_irqsave(&pring->ring_lock, iflags);
14305         pring->stats.iocb_event++;
14306         spin_unlock_irqrestore(&pring->ring_lock, iflags);
14307         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14308                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
14309         if (unlikely(!cmdiocbq)) {
14310                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14311                                 "0374 FCP complete with no corresponding "
14312                                 "cmdiocb: iotag (%d)\n",
14313                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
14314                 return;
14315         }
14316 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
14317         cmdiocbq->isr_timestamp = cq->isr_timestamp;
14318 #endif
14319         if (cmdiocbq->iocb_cmpl == NULL) {
14320                 if (cmdiocbq->wqe_cmpl) {
14321                         /* For FCP the flag is cleared in wqe_cmpl */
14322                         if (!(cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
14323                             cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
14324                                 spin_lock_irqsave(&phba->hbalock, iflags);
14325                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
14326                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
14327                         }
14328
14329                         /* Pass the cmd_iocb and the wcqe to the upper layer */
14330                         (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
14331                         return;
14332                 }
14333                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14334                                 "0375 FCP cmdiocb not callback function "
14335                                 "iotag: (%d)\n",
14336                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
14337                 return;
14338         }
14339
14340         /* Only SLI4 non-IO commands stil use IOCB */
14341         /* Fake the irspiocb and copy necessary response information */
14342         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
14343
14344         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
14345                 spin_lock_irqsave(&phba->hbalock, iflags);
14346                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
14347                 spin_unlock_irqrestore(&phba->hbalock, iflags);
14348         }
14349
14350         /* Pass the cmd_iocb and the rsp state to the upper layer */
14351         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
14352 }
14353
14354 /**
14355  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
14356  * @phba: Pointer to HBA context object.
14357  * @cq: Pointer to completion queue.
14358  * @wcqe: Pointer to work-queue completion queue entry.
14359  *
14360  * This routine handles an fast-path WQ entry consumed event by invoking the
14361  * proper WQ release routine to the slow-path WQ.
14362  **/
14363 static void
14364 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14365                              struct lpfc_wcqe_release *wcqe)
14366 {
14367         struct lpfc_queue *childwq;
14368         bool wqid_matched = false;
14369         uint16_t hba_wqid;
14370
14371         /* Check for fast-path FCP work queue release */
14372         hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
14373         list_for_each_entry(childwq, &cq->child_list, list) {
14374                 if (childwq->queue_id == hba_wqid) {
14375                         lpfc_sli4_wq_release(childwq,
14376                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
14377                         if (childwq->q_flag & HBA_NVMET_WQFULL)
14378                                 lpfc_nvmet_wqfull_process(phba, childwq);
14379                         wqid_matched = true;
14380                         break;
14381                 }
14382         }
14383         /* Report warning log message if no match found */
14384         if (wqid_matched != true)
14385                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14386                                 "2580 Fast-path wqe consume event carries "
14387                                 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
14388 }
14389
14390 /**
14391  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
14392  * @phba: Pointer to HBA context object.
14393  * @cq: Pointer to completion queue.
14394  * @rcqe: Pointer to receive-queue completion queue entry.
14395  *
14396  * This routine process a receive-queue completion queue entry.
14397  *
14398  * Return: true if work posted to worker thread, otherwise false.
14399  **/
14400 static bool
14401 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14402                             struct lpfc_rcqe *rcqe)
14403 {
14404         bool workposted = false;
14405         struct lpfc_queue *hrq;
14406         struct lpfc_queue *drq;
14407         struct rqb_dmabuf *dma_buf;
14408         struct fc_frame_header *fc_hdr;
14409         struct lpfc_nvmet_tgtport *tgtp;
14410         uint32_t status, rq_id;
14411         unsigned long iflags;
14412         uint32_t fctl, idx;
14413
14414         if ((phba->nvmet_support == 0) ||
14415             (phba->sli4_hba.nvmet_cqset == NULL))
14416                 return workposted;
14417
14418         idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
14419         hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
14420         drq = phba->sli4_hba.nvmet_mrq_data[idx];
14421
14422         /* sanity check on queue memory */
14423         if (unlikely(!hrq) || unlikely(!drq))
14424                 return workposted;
14425
14426         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14427                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14428         else
14429                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14430
14431         if ((phba->nvmet_support == 0) ||
14432             (rq_id != hrq->queue_id))
14433                 return workposted;
14434
14435         status = bf_get(lpfc_rcqe_status, rcqe);
14436         switch (status) {
14437         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
14438                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14439                                 "6126 Receive Frame Truncated!!\n");
14440                 fallthrough;
14441         case FC_STATUS_RQ_SUCCESS:
14442                 spin_lock_irqsave(&phba->hbalock, iflags);
14443                 lpfc_sli4_rq_release(hrq, drq);
14444                 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
14445                 if (!dma_buf) {
14446                         hrq->RQ_no_buf_found++;
14447                         spin_unlock_irqrestore(&phba->hbalock, iflags);
14448                         goto out;
14449                 }
14450                 spin_unlock_irqrestore(&phba->hbalock, iflags);
14451                 hrq->RQ_rcv_buf++;
14452                 hrq->RQ_buf_posted--;
14453                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14454
14455                 /* Just some basic sanity checks on FCP Command frame */
14456                 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
14457                         fc_hdr->fh_f_ctl[1] << 8 |
14458                         fc_hdr->fh_f_ctl[2]);
14459                 if (((fctl &
14460                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
14461                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
14462                     (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
14463                         goto drop;
14464
14465                 if (fc_hdr->fh_type == FC_TYPE_FCP) {
14466                         dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
14467                         lpfc_nvmet_unsol_fcp_event(
14468                                 phba, idx, dma_buf, cq->isr_timestamp,
14469                                 cq->q_flag & HBA_NVMET_CQ_NOTIFY);
14470                         return false;
14471                 }
14472 drop:
14473                 lpfc_rq_buf_free(phba, &dma_buf->hbuf);
14474                 break;
14475         case FC_STATUS_INSUFF_BUF_FRM_DISC:
14476                 if (phba->nvmet_support) {
14477                         tgtp = phba->targetport->private;
14478                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14479                                         "6401 RQE Error x%x, posted %d err_cnt "
14480                                         "%d: %x %x %x\n",
14481                                         status, hrq->RQ_buf_posted,
14482                                         hrq->RQ_no_posted_buf,
14483                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
14484                                         atomic_read(&tgtp->rcv_fcp_cmd_out),
14485                                         atomic_read(&tgtp->xmt_fcp_release));
14486                 }
14487                 fallthrough;
14488
14489         case FC_STATUS_INSUFF_BUF_NEED_BUF:
14490                 hrq->RQ_no_posted_buf++;
14491                 /* Post more buffers if possible */
14492                 break;
14493         }
14494 out:
14495         return workposted;
14496 }
14497
14498 /**
14499  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
14500  * @phba: adapter with cq
14501  * @cq: Pointer to the completion queue.
14502  * @cqe: Pointer to fast-path completion queue entry.
14503  *
14504  * This routine process a fast-path work queue completion entry from fast-path
14505  * event queue for FCP command response completion.
14506  *
14507  * Return: true if work posted to worker thread, otherwise false.
14508  **/
14509 static bool
14510 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14511                          struct lpfc_cqe *cqe)
14512 {
14513         struct lpfc_wcqe_release wcqe;
14514         bool workposted = false;
14515
14516         /* Copy the work queue CQE and convert endian order if needed */
14517         lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
14518
14519         /* Check and process for different type of WCQE and dispatch */
14520         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
14521         case CQE_CODE_COMPL_WQE:
14522         case CQE_CODE_NVME_ERSP:
14523                 cq->CQ_wq++;
14524                 /* Process the WQ complete event */
14525                 phba->last_completion_time = jiffies;
14526                 if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
14527                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
14528                                 (struct lpfc_wcqe_complete *)&wcqe);
14529                 break;
14530         case CQE_CODE_RELEASE_WQE:
14531                 cq->CQ_release_wqe++;
14532                 /* Process the WQ release event */
14533                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
14534                                 (struct lpfc_wcqe_release *)&wcqe);
14535                 break;
14536         case CQE_CODE_XRI_ABORTED:
14537                 cq->CQ_xri_aborted++;
14538                 /* Process the WQ XRI abort event */
14539                 phba->last_completion_time = jiffies;
14540                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14541                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
14542                 break;
14543         case CQE_CODE_RECEIVE_V1:
14544         case CQE_CODE_RECEIVE:
14545                 phba->last_completion_time = jiffies;
14546                 if (cq->subtype == LPFC_NVMET) {
14547                         workposted = lpfc_sli4_nvmet_handle_rcqe(
14548                                 phba, cq, (struct lpfc_rcqe *)&wcqe);
14549                 }
14550                 break;
14551         default:
14552                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14553                                 "0144 Not a valid CQE code: x%x\n",
14554                                 bf_get(lpfc_wcqe_c_code, &wcqe));
14555                 break;
14556         }
14557         return workposted;
14558 }
14559
14560 /**
14561  * lpfc_sli4_sched_cq_work - Schedules cq work
14562  * @phba: Pointer to HBA context object.
14563  * @cq: Pointer to CQ
14564  * @cqid: CQ ID
14565  *
14566  * This routine checks the poll mode of the CQ corresponding to
14567  * cq->chann, then either schedules a softirq or queue_work to complete
14568  * cq work.
14569  *
14570  * queue_work path is taken if in NVMET mode, or if poll_mode is in
14571  * LPFC_QUEUE_WORK mode.  Otherwise, softirq path is taken.
14572  *
14573  **/
14574 static void lpfc_sli4_sched_cq_work(struct lpfc_hba *phba,
14575                                     struct lpfc_queue *cq, uint16_t cqid)
14576 {
14577         int ret = 0;
14578
14579         switch (cq->poll_mode) {
14580         case LPFC_IRQ_POLL:
14581                 irq_poll_sched(&cq->iop);
14582                 break;
14583         case LPFC_QUEUE_WORK:
14584         default:
14585                 if (is_kdump_kernel())
14586                         ret = queue_work(phba->wq, &cq->irqwork);
14587                 else
14588                         ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
14589                 if (!ret)
14590                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14591                                         "0383 Cannot schedule queue work "
14592                                         "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14593                                         cqid, cq->queue_id,
14594                                         raw_smp_processor_id());
14595         }
14596 }
14597
14598 /**
14599  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
14600  * @phba: Pointer to HBA context object.
14601  * @eq: Pointer to the queue structure.
14602  * @eqe: Pointer to fast-path event queue entry.
14603  *
14604  * This routine process a event queue entry from the fast-path event queue.
14605  * It will check the MajorCode and MinorCode to determine this is for a
14606  * completion event on a completion queue, if not, an error shall be logged
14607  * and just return. Otherwise, it will get to the corresponding completion
14608  * queue and process all the entries on the completion queue, rearm the
14609  * completion queue, and then return.
14610  **/
14611 static void
14612 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
14613                          struct lpfc_eqe *eqe)
14614 {
14615         struct lpfc_queue *cq = NULL;
14616         uint32_t qidx = eq->hdwq;
14617         uint16_t cqid, id;
14618
14619         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
14620                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14621                                 "0366 Not a valid completion "
14622                                 "event: majorcode=x%x, minorcode=x%x\n",
14623                                 bf_get_le32(lpfc_eqe_major_code, eqe),
14624                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
14625                 return;
14626         }
14627
14628         /* Get the reference to the corresponding CQ */
14629         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14630
14631         /* Use the fast lookup method first */
14632         if (cqid <= phba->sli4_hba.cq_max) {
14633                 cq = phba->sli4_hba.cq_lookup[cqid];
14634                 if (cq)
14635                         goto  work_cq;
14636         }
14637
14638         /* Next check for NVMET completion */
14639         if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14640                 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14641                 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14642                         /* Process NVMET unsol rcv */
14643                         cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14644                         goto  process_cq;
14645                 }
14646         }
14647
14648         if (phba->sli4_hba.nvmels_cq &&
14649             (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14650                 /* Process NVME unsol rcv */
14651                 cq = phba->sli4_hba.nvmels_cq;
14652         }
14653
14654         /* Otherwise this is a Slow path event */
14655         if (cq == NULL) {
14656                 lpfc_sli4_sp_handle_eqe(phba, eqe,
14657                                         phba->sli4_hba.hdwq[qidx].hba_eq);
14658                 return;
14659         }
14660
14661 process_cq:
14662         if (unlikely(cqid != cq->queue_id)) {
14663                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14664                                 "0368 Miss-matched fast-path completion "
14665                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14666                                 cqid, cq->queue_id);
14667                 return;
14668         }
14669
14670 work_cq:
14671 #if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
14672         if (phba->ktime_on)
14673                 cq->isr_timestamp = ktime_get_ns();
14674         else
14675                 cq->isr_timestamp = 0;
14676 #endif
14677         lpfc_sli4_sched_cq_work(phba, cq, cqid);
14678 }
14679
14680 /**
14681  * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14682  * @cq: Pointer to CQ to be processed
14683  * @poll_mode: Enum lpfc_poll_state to determine poll mode
14684  *
14685  * This routine calls the cq processing routine with the handler for
14686  * fast path CQEs.
14687  *
14688  * The CQ routine returns two values: the first is the calling status,
14689  * which indicates whether work was queued to the  background discovery
14690  * thread. If true, the routine should wakeup the discovery thread;
14691  * the second is the delay parameter. If non-zero, rather than rearming
14692  * the CQ and yet another interrupt, the CQ handler should be queued so
14693  * that it is processed in a subsequent polling action. The value of
14694  * the delay indicates when to reschedule it.
14695  **/
14696 static void
14697 __lpfc_sli4_hba_process_cq(struct lpfc_queue *cq,
14698                            enum lpfc_poll_mode poll_mode)
14699 {
14700         struct lpfc_hba *phba = cq->phba;
14701         unsigned long delay;
14702         bool workposted = false;
14703         int ret = 0;
14704
14705         /* process and rearm the CQ */
14706         workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
14707                                              &delay, poll_mode);
14708
14709         if (delay) {
14710                 if (is_kdump_kernel())
14711                         ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
14712                                                 delay);
14713                 else
14714                         ret = queue_delayed_work_on(cq->chann, phba->wq,
14715                                                 &cq->sched_irqwork, delay);
14716                 if (!ret)
14717                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14718                                         "0367 Cannot schedule queue work "
14719                                         "for cqid=%d on CPU %d\n",
14720                                         cq->queue_id, cq->chann);
14721         }
14722
14723         /* wake up worker thread if there are works to be done */
14724         if (workposted)
14725                 lpfc_worker_wake_up(phba);
14726 }
14727
14728 /**
14729  * lpfc_sli4_hba_process_cq - fast-path work handler when started by
14730  *   interrupt
14731  * @work: pointer to work element
14732  *
14733  * translates from the work handler and calls the fast-path handler.
14734  **/
14735 static void
14736 lpfc_sli4_hba_process_cq(struct work_struct *work)
14737 {
14738         struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
14739
14740         __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
14741 }
14742
14743 /**
14744  * lpfc_sli4_hba_process_cq - fast-path work handler when started by timer
14745  * @work: pointer to work element
14746  *
14747  * translates from the work handler and calls the fast-path handler.
14748  **/
14749 static void
14750 lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
14751 {
14752         struct lpfc_queue *cq = container_of(to_delayed_work(work),
14753                                         struct lpfc_queue, sched_irqwork);
14754
14755         __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
14756 }
14757
14758 /**
14759  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
14760  * @irq: Interrupt number.
14761  * @dev_id: The device context pointer.
14762  *
14763  * This function is directly called from the PCI layer as an interrupt
14764  * service routine when device with SLI-4 interface spec is enabled with
14765  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14766  * ring event in the HBA. However, when the device is enabled with either
14767  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14768  * device-level interrupt handler. When the PCI slot is in error recovery
14769  * or the HBA is undergoing initialization, the interrupt handler will not
14770  * process the interrupt. The SCSI FCP fast-path ring event are handled in
14771  * the intrrupt context. This function is called without any lock held.
14772  * It gets the hbalock to access and update SLI data structures. Note that,
14773  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14774  * equal to that of FCP CQ index.
14775  *
14776  * The link attention and ELS ring attention events are handled
14777  * by the worker thread. The interrupt handler signals the worker thread
14778  * and returns for these events. This function is called without any lock
14779  * held. It gets the hbalock to access and update SLI data structures.
14780  *
14781  * This function returns IRQ_HANDLED when interrupt is handled else it
14782  * returns IRQ_NONE.
14783  **/
14784 irqreturn_t
14785 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
14786 {
14787         struct lpfc_hba *phba;
14788         struct lpfc_hba_eq_hdl *hba_eq_hdl;
14789         struct lpfc_queue *fpeq;
14790         unsigned long iflag;
14791         int ecount = 0;
14792         int hba_eqidx;
14793         struct lpfc_eq_intr_info *eqi;
14794
14795         /* Get the driver's phba structure from the dev_id */
14796         hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14797         phba = hba_eq_hdl->phba;
14798         hba_eqidx = hba_eq_hdl->idx;
14799
14800         if (unlikely(!phba))
14801                 return IRQ_NONE;
14802         if (unlikely(!phba->sli4_hba.hdwq))
14803                 return IRQ_NONE;
14804
14805         /* Get to the EQ struct associated with this vector */
14806         fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
14807         if (unlikely(!fpeq))
14808                 return IRQ_NONE;
14809
14810         /* Check device state for handling interrupt */
14811         if (unlikely(lpfc_intr_state_check(phba))) {
14812                 /* Check again for link_state with lock held */
14813                 spin_lock_irqsave(&phba->hbalock, iflag);
14814                 if (phba->link_state < LPFC_LINK_DOWN)
14815                         /* Flush, clear interrupt, and rearm the EQ */
14816                         lpfc_sli4_eqcq_flush(phba, fpeq);
14817                 spin_unlock_irqrestore(&phba->hbalock, iflag);
14818                 return IRQ_NONE;
14819         }
14820
14821         eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
14822         eqi->icnt++;
14823
14824         fpeq->last_cpu = raw_smp_processor_id();
14825
14826         if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
14827             fpeq->q_flag & HBA_EQ_DELAY_CHK &&
14828             phba->cfg_auto_imax &&
14829             fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
14830             phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
14831                 lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
14832
14833         /* process and rearm the EQ */
14834         ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
14835
14836         if (unlikely(ecount == 0)) {
14837                 fpeq->EQ_no_entry++;
14838                 if (phba->intr_type == MSIX)
14839                         /* MSI-X treated interrupt served as no EQ share INT */
14840                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14841                                         "0358 MSI-X interrupt with no EQE\n");
14842                 else
14843                         /* Non MSI-X treated on interrupt as EQ share INT */
14844                         return IRQ_NONE;
14845         }
14846
14847         return IRQ_HANDLED;
14848 } /* lpfc_sli4_fp_intr_handler */
14849
14850 /**
14851  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14852  * @irq: Interrupt number.
14853  * @dev_id: The device context pointer.
14854  *
14855  * This function is the device-level interrupt handler to device with SLI-4
14856  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14857  * interrupt mode is enabled and there is an event in the HBA which requires
14858  * driver attention. This function invokes the slow-path interrupt attention
14859  * handling function and fast-path interrupt attention handling function in
14860  * turn to process the relevant HBA attention events. This function is called
14861  * without any lock held. It gets the hbalock to access and update SLI data
14862  * structures.
14863  *
14864  * This function returns IRQ_HANDLED when interrupt is handled, else it
14865  * returns IRQ_NONE.
14866  **/
14867 irqreturn_t
14868 lpfc_sli4_intr_handler(int irq, void *dev_id)
14869 {
14870         struct lpfc_hba  *phba;
14871         irqreturn_t hba_irq_rc;
14872         bool hba_handled = false;
14873         int qidx;
14874
14875         /* Get the driver's phba structure from the dev_id */
14876         phba = (struct lpfc_hba *)dev_id;
14877
14878         if (unlikely(!phba))
14879                 return IRQ_NONE;
14880
14881         /*
14882          * Invoke fast-path host attention interrupt handling as appropriate.
14883          */
14884         for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
14885                 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
14886                                         &phba->sli4_hba.hba_eq_hdl[qidx]);
14887                 if (hba_irq_rc == IRQ_HANDLED)
14888                         hba_handled |= true;
14889         }
14890
14891         return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
14892 } /* lpfc_sli4_intr_handler */
14893
14894 void lpfc_sli4_poll_hbtimer(struct timer_list *t)
14895 {
14896         struct lpfc_hba *phba = from_timer(phba, t, cpuhp_poll_timer);
14897         struct lpfc_queue *eq;
14898         int i = 0;
14899
14900         rcu_read_lock();
14901
14902         list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
14903                 i += lpfc_sli4_poll_eq(eq, LPFC_POLL_SLOWPATH);
14904         if (!list_empty(&phba->poll_list))
14905                 mod_timer(&phba->cpuhp_poll_timer,
14906                           jiffies + msecs_to_jiffies(LPFC_POLL_HB));
14907
14908         rcu_read_unlock();
14909 }
14910
14911 inline int lpfc_sli4_poll_eq(struct lpfc_queue *eq, uint8_t path)
14912 {
14913         struct lpfc_hba *phba = eq->phba;
14914         int i = 0;
14915
14916         /*
14917          * Unlocking an irq is one of the entry point to check
14918          * for re-schedule, but we are good for io submission
14919          * path as midlayer does a get_cpu to glue us in. Flush
14920          * out the invalidate queue so we can see the updated
14921          * value for flag.
14922          */
14923         smp_rmb();
14924
14925         if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
14926                 /* We will not likely get the completion for the caller
14927                  * during this iteration but i guess that's fine.
14928                  * Future io's coming on this eq should be able to
14929                  * pick it up.  As for the case of single io's, they
14930                  * will be handled through a sched from polling timer
14931                  * function which is currently triggered every 1msec.
14932                  */
14933                 i = lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM);
14934
14935         return i;
14936 }
14937
14938 static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
14939 {
14940         struct lpfc_hba *phba = eq->phba;
14941
14942         /* kickstart slowpath processing if needed */
14943         if (list_empty(&phba->poll_list))
14944                 mod_timer(&phba->cpuhp_poll_timer,
14945                           jiffies + msecs_to_jiffies(LPFC_POLL_HB));
14946
14947         list_add_rcu(&eq->_poll_list, &phba->poll_list);
14948         synchronize_rcu();
14949 }
14950
14951 static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
14952 {
14953         struct lpfc_hba *phba = eq->phba;
14954
14955         /* Disable slowpath processing for this eq.  Kick start the eq
14956          * by RE-ARMING the eq's ASAP
14957          */
14958         list_del_rcu(&eq->_poll_list);
14959         synchronize_rcu();
14960
14961         if (list_empty(&phba->poll_list))
14962                 del_timer_sync(&phba->cpuhp_poll_timer);
14963 }
14964
14965 void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
14966 {
14967         struct lpfc_queue *eq, *next;
14968
14969         list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
14970                 list_del(&eq->_poll_list);
14971
14972         INIT_LIST_HEAD(&phba->poll_list);
14973         synchronize_rcu();
14974 }
14975
14976 static inline void
14977 __lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
14978 {
14979         if (mode == eq->mode)
14980                 return;
14981         /*
14982          * currently this function is only called during a hotplug
14983          * event and the cpu on which this function is executing
14984          * is going offline.  By now the hotplug has instructed
14985          * the scheduler to remove this cpu from cpu active mask.
14986          * So we don't need to work about being put aside by the
14987          * scheduler for a high priority process.  Yes, the inte-
14988          * rrupts could come but they are known to retire ASAP.
14989          */
14990
14991         /* Disable polling in the fastpath */
14992         WRITE_ONCE(eq->mode, mode);
14993         /* flush out the store buffer */
14994         smp_wmb();
14995
14996         /*
14997          * Add this eq to the polling list and start polling. For
14998          * a grace period both interrupt handler and poller will
14999          * try to process the eq _but_ that's fine.  We have a
15000          * synchronization mechanism in place (queue_claimed) to
15001          * deal with it.  This is just a draining phase for int-
15002          * errupt handler (not eq's) as we have guranteed through
15003          * barrier that all the CPUs have seen the new CQ_POLLED
15004          * state. which will effectively disable the REARMING of
15005          * the EQ.  The whole idea is eq's die off eventually as
15006          * we are not rearming EQ's anymore.
15007          */
15008         mode ? lpfc_sli4_add_to_poll_list(eq) :
15009                lpfc_sli4_remove_from_poll_list(eq);
15010 }
15011
15012 void lpfc_sli4_start_polling(struct lpfc_queue *eq)
15013 {
15014         __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
15015 }
15016
15017 void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
15018 {
15019         struct lpfc_hba *phba = eq->phba;
15020
15021         __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
15022
15023         /* Kick start for the pending io's in h/w.
15024          * Once we switch back to interrupt processing on a eq
15025          * the io path completion will only arm eq's when it
15026          * receives a completion.  But since eq's are in disa-
15027          * rmed state it doesn't receive a completion.  This
15028          * creates a deadlock scenaro.
15029          */
15030         phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
15031 }
15032
15033 /**
15034  * lpfc_sli4_queue_free - free a queue structure and associated memory
15035  * @queue: The queue structure to free.
15036  *
15037  * This function frees a queue structure and the DMAable memory used for
15038  * the host resident queue. This function must be called after destroying the
15039  * queue on the HBA.
15040  **/
15041 void
15042 lpfc_sli4_queue_free(struct lpfc_queue *queue)
15043 {
15044         struct lpfc_dmabuf *dmabuf;
15045
15046         if (!queue)
15047                 return;
15048
15049         if (!list_empty(&queue->wq_list))
15050                 list_del(&queue->wq_list);
15051
15052         while (!list_empty(&queue->page_list)) {
15053                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
15054                                  list);
15055                 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
15056                                   dmabuf->virt, dmabuf->phys);
15057                 kfree(dmabuf);
15058         }
15059         if (queue->rqbp) {
15060                 lpfc_free_rq_buffer(queue->phba, queue);
15061                 kfree(queue->rqbp);
15062         }
15063
15064         if (!list_empty(&queue->cpu_list))
15065                 list_del(&queue->cpu_list);
15066
15067         kfree(queue);
15068         return;
15069 }
15070
15071 /**
15072  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
15073  * @phba: The HBA that this queue is being created on.
15074  * @page_size: The size of a queue page
15075  * @entry_size: The size of each queue entry for this queue.
15076  * @entry_count: The number of entries that this queue will handle.
15077  * @cpu: The cpu that will primarily utilize this queue.
15078  *
15079  * This function allocates a queue structure and the DMAable memory used for
15080  * the host resident queue. This function must be called before creating the
15081  * queue on the HBA.
15082  **/
15083 struct lpfc_queue *
15084 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
15085                       uint32_t entry_size, uint32_t entry_count, int cpu)
15086 {
15087         struct lpfc_queue *queue;
15088         struct lpfc_dmabuf *dmabuf;
15089         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15090         uint16_t x, pgcnt;
15091
15092         if (!phba->sli4_hba.pc_sli4_params.supported)
15093                 hw_page_size = page_size;
15094
15095         pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
15096
15097         /* If needed, Adjust page count to match the max the adapter supports */
15098         if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
15099                 pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
15100
15101         queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
15102                              GFP_KERNEL, cpu_to_node(cpu));
15103         if (!queue)
15104                 return NULL;
15105
15106         INIT_LIST_HEAD(&queue->list);
15107         INIT_LIST_HEAD(&queue->_poll_list);
15108         INIT_LIST_HEAD(&queue->wq_list);
15109         INIT_LIST_HEAD(&queue->wqfull_list);
15110         INIT_LIST_HEAD(&queue->page_list);
15111         INIT_LIST_HEAD(&queue->child_list);
15112         INIT_LIST_HEAD(&queue->cpu_list);
15113
15114         /* Set queue parameters now.  If the system cannot provide memory
15115          * resources, the free routine needs to know what was allocated.
15116          */
15117         queue->page_count = pgcnt;
15118         queue->q_pgs = (void **)&queue[1];
15119         queue->entry_cnt_per_pg = hw_page_size / entry_size;
15120         queue->entry_size = entry_size;
15121         queue->entry_count = entry_count;
15122         queue->page_size = hw_page_size;
15123         queue->phba = phba;
15124
15125         for (x = 0; x < queue->page_count; x++) {
15126                 dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
15127                                       dev_to_node(&phba->pcidev->dev));
15128                 if (!dmabuf)
15129                         goto out_fail;
15130                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
15131                                                   hw_page_size, &dmabuf->phys,
15132                                                   GFP_KERNEL);
15133                 if (!dmabuf->virt) {
15134                         kfree(dmabuf);
15135                         goto out_fail;
15136                 }
15137                 dmabuf->buffer_tag = x;
15138                 list_add_tail(&dmabuf->list, &queue->page_list);
15139                 /* use lpfc_sli4_qe to index a paritcular entry in this page */
15140                 queue->q_pgs[x] = dmabuf->virt;
15141         }
15142         INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
15143         INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
15144         INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
15145         INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
15146
15147         /* notify_interval will be set during q creation */
15148
15149         return queue;
15150 out_fail:
15151         lpfc_sli4_queue_free(queue);
15152         return NULL;
15153 }
15154
15155 /**
15156  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
15157  * @phba: HBA structure that indicates port to create a queue on.
15158  * @pci_barset: PCI BAR set flag.
15159  *
15160  * This function shall perform iomap of the specified PCI BAR address to host
15161  * memory address if not already done so and return it. The returned host
15162  * memory address can be NULL.
15163  */
15164 static void __iomem *
15165 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
15166 {
15167         if (!phba->pcidev)
15168                 return NULL;
15169
15170         switch (pci_barset) {
15171         case WQ_PCI_BAR_0_AND_1:
15172                 return phba->pci_bar0_memmap_p;
15173         case WQ_PCI_BAR_2_AND_3:
15174                 return phba->pci_bar2_memmap_p;
15175         case WQ_PCI_BAR_4_AND_5:
15176                 return phba->pci_bar4_memmap_p;
15177         default:
15178                 break;
15179         }
15180         return NULL;
15181 }
15182
15183 /**
15184  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
15185  * @phba: HBA structure that EQs are on.
15186  * @startq: The starting EQ index to modify
15187  * @numq: The number of EQs (consecutive indexes) to modify
15188  * @usdelay: amount of delay
15189  *
15190  * This function revises the EQ delay on 1 or more EQs. The EQ delay
15191  * is set either by writing to a register (if supported by the SLI Port)
15192  * or by mailbox command. The mailbox command allows several EQs to be
15193  * updated at once.
15194  *
15195  * The @phba struct is used to send a mailbox command to HBA. The @startq
15196  * is used to get the starting EQ index to change. The @numq value is
15197  * used to specify how many consecutive EQ indexes, starting at EQ index,
15198  * are to be changed. This function is asynchronous and will wait for any
15199  * mailbox commands to finish before returning.
15200  *
15201  * On success this function will return a zero. If unable to allocate
15202  * enough memory this function will return -ENOMEM. If a mailbox command
15203  * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
15204  * have had their delay multipler changed.
15205  **/
15206 void
15207 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
15208                          uint32_t numq, uint32_t usdelay)
15209 {
15210         struct lpfc_mbx_modify_eq_delay *eq_delay;
15211         LPFC_MBOXQ_t *mbox;
15212         struct lpfc_queue *eq;
15213         int cnt = 0, rc, length;
15214         uint32_t shdr_status, shdr_add_status;
15215         uint32_t dmult;
15216         int qidx;
15217         union lpfc_sli4_cfg_shdr *shdr;
15218
15219         if (startq >= phba->cfg_irq_chann)
15220                 return;
15221
15222         if (usdelay > 0xFFFF) {
15223                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
15224                                 "6429 usdelay %d too large. Scaled down to "
15225                                 "0xFFFF.\n", usdelay);
15226                 usdelay = 0xFFFF;
15227         }
15228
15229         /* set values by EQ_DELAY register if supported */
15230         if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
15231                 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
15232                         eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
15233                         if (!eq)
15234                                 continue;
15235
15236                         lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
15237
15238                         if (++cnt >= numq)
15239                                 break;
15240                 }
15241                 return;
15242         }
15243
15244         /* Otherwise, set values by mailbox cmd */
15245
15246         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15247         if (!mbox) {
15248                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15249                                 "6428 Failed allocating mailbox cmd buffer."
15250                                 " EQ delay was not set.\n");
15251                 return;
15252         }
15253         length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
15254                   sizeof(struct lpfc_sli4_cfg_mhdr));
15255         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15256                          LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
15257                          length, LPFC_SLI4_MBX_EMBED);
15258         eq_delay = &mbox->u.mqe.un.eq_delay;
15259
15260         /* Calculate delay multiper from maximum interrupt per second */
15261         dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
15262         if (dmult)
15263                 dmult--;
15264         if (dmult > LPFC_DMULT_MAX)
15265                 dmult = LPFC_DMULT_MAX;
15266
15267         for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
15268                 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
15269                 if (!eq)
15270                         continue;
15271                 eq->q_mode = usdelay;
15272                 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
15273                 eq_delay->u.request.eq[cnt].phase = 0;
15274                 eq_delay->u.request.eq[cnt].delay_multi = dmult;
15275
15276                 if (++cnt >= numq)
15277                         break;
15278         }
15279         eq_delay->u.request.num_eq = cnt;
15280
15281         mbox->vport = phba->pport;
15282         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15283         mbox->ctx_buf = NULL;
15284         mbox->ctx_ndlp = NULL;
15285         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15286         shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
15287         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15288         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15289         if (shdr_status || shdr_add_status || rc) {
15290                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15291                                 "2512 MODIFY_EQ_DELAY mailbox failed with "
15292                                 "status x%x add_status x%x, mbx status x%x\n",
15293                                 shdr_status, shdr_add_status, rc);
15294         }
15295         mempool_free(mbox, phba->mbox_mem_pool);
15296         return;
15297 }
15298
15299 /**
15300  * lpfc_eq_create - Create an Event Queue on the HBA
15301  * @phba: HBA structure that indicates port to create a queue on.
15302  * @eq: The queue structure to use to create the event queue.
15303  * @imax: The maximum interrupt per second limit.
15304  *
15305  * This function creates an event queue, as detailed in @eq, on a port,
15306  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
15307  *
15308  * The @phba struct is used to send mailbox command to HBA. The @eq struct
15309  * is used to get the entry count and entry size that are necessary to
15310  * determine the number of pages to allocate and use for this queue. This
15311  * function will send the EQ_CREATE mailbox command to the HBA to setup the
15312  * event queue. This function is asynchronous and will wait for the mailbox
15313  * command to finish before continuing.
15314  *
15315  * On success this function will return a zero. If unable to allocate enough
15316  * memory this function will return -ENOMEM. If the queue create mailbox command
15317  * fails this function will return -ENXIO.
15318  **/
15319 int
15320 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
15321 {
15322         struct lpfc_mbx_eq_create *eq_create;
15323         LPFC_MBOXQ_t *mbox;
15324         int rc, length, status = 0;
15325         struct lpfc_dmabuf *dmabuf;
15326         uint32_t shdr_status, shdr_add_status;
15327         union lpfc_sli4_cfg_shdr *shdr;
15328         uint16_t dmult;
15329         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15330
15331         /* sanity check on queue memory */
15332         if (!eq)
15333                 return -ENODEV;
15334         if (!phba->sli4_hba.pc_sli4_params.supported)
15335                 hw_page_size = SLI4_PAGE_SIZE;
15336
15337         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15338         if (!mbox)
15339                 return -ENOMEM;
15340         length = (sizeof(struct lpfc_mbx_eq_create) -
15341                   sizeof(struct lpfc_sli4_cfg_mhdr));
15342         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15343                          LPFC_MBOX_OPCODE_EQ_CREATE,
15344                          length, LPFC_SLI4_MBX_EMBED);
15345         eq_create = &mbox->u.mqe.un.eq_create;
15346         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
15347         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
15348                eq->page_count);
15349         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
15350                LPFC_EQE_SIZE);
15351         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
15352
15353         /* Use version 2 of CREATE_EQ if eqav is set */
15354         if (phba->sli4_hba.pc_sli4_params.eqav) {
15355                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15356                        LPFC_Q_CREATE_VERSION_2);
15357                 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
15358                        phba->sli4_hba.pc_sli4_params.eqav);
15359         }
15360
15361         /* don't setup delay multiplier using EQ_CREATE */
15362         dmult = 0;
15363         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
15364                dmult);
15365         switch (eq->entry_count) {
15366         default:
15367                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15368                                 "0360 Unsupported EQ count. (%d)\n",
15369                                 eq->entry_count);
15370                 if (eq->entry_count < 256) {
15371                         status = -EINVAL;
15372                         goto out;
15373                 }
15374                 fallthrough;    /* otherwise default to smallest count */
15375         case 256:
15376                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15377                        LPFC_EQ_CNT_256);
15378                 break;
15379         case 512:
15380                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15381                        LPFC_EQ_CNT_512);
15382                 break;
15383         case 1024:
15384                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15385                        LPFC_EQ_CNT_1024);
15386                 break;
15387         case 2048:
15388                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15389                        LPFC_EQ_CNT_2048);
15390                 break;
15391         case 4096:
15392                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15393                        LPFC_EQ_CNT_4096);
15394                 break;
15395         }
15396         list_for_each_entry(dmabuf, &eq->page_list, list) {
15397                 memset(dmabuf->virt, 0, hw_page_size);
15398                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15399                                         putPaddrLow(dmabuf->phys);
15400                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15401                                         putPaddrHigh(dmabuf->phys);
15402         }
15403         mbox->vport = phba->pport;
15404         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15405         mbox->ctx_buf = NULL;
15406         mbox->ctx_ndlp = NULL;
15407         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15408         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15409         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15410         if (shdr_status || shdr_add_status || rc) {
15411                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15412                                 "2500 EQ_CREATE mailbox failed with "
15413                                 "status x%x add_status x%x, mbx status x%x\n",
15414                                 shdr_status, shdr_add_status, rc);
15415                 status = -ENXIO;
15416         }
15417         eq->type = LPFC_EQ;
15418         eq->subtype = LPFC_NONE;
15419         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
15420         if (eq->queue_id == 0xFFFF)
15421                 status = -ENXIO;
15422         eq->host_index = 0;
15423         eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
15424         eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
15425 out:
15426         mempool_free(mbox, phba->mbox_mem_pool);
15427         return status;
15428 }
15429
15430 static int lpfc_cq_poll_hdler(struct irq_poll *iop, int budget)
15431 {
15432         struct lpfc_queue *cq = container_of(iop, struct lpfc_queue, iop);
15433
15434         __lpfc_sli4_hba_process_cq(cq, LPFC_IRQ_POLL);
15435
15436         return 1;
15437 }
15438
15439 /**
15440  * lpfc_cq_create - Create a Completion Queue on the HBA
15441  * @phba: HBA structure that indicates port to create a queue on.
15442  * @cq: The queue structure to use to create the completion queue.
15443  * @eq: The event queue to bind this completion queue to.
15444  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
15445  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
15446  *
15447  * This function creates a completion queue, as detailed in @wq, on a port,
15448  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
15449  *
15450  * The @phba struct is used to send mailbox command to HBA. The @cq struct
15451  * is used to get the entry count and entry size that are necessary to
15452  * determine the number of pages to allocate and use for this queue. The @eq
15453  * is used to indicate which event queue to bind this completion queue to. This
15454  * function will send the CQ_CREATE mailbox command to the HBA to setup the
15455  * completion queue. This function is asynchronous and will wait for the mailbox
15456  * command to finish before continuing.
15457  *
15458  * On success this function will return a zero. If unable to allocate enough
15459  * memory this function will return -ENOMEM. If the queue create mailbox command
15460  * fails this function will return -ENXIO.
15461  **/
15462 int
15463 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
15464                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
15465 {
15466         struct lpfc_mbx_cq_create *cq_create;
15467         struct lpfc_dmabuf *dmabuf;
15468         LPFC_MBOXQ_t *mbox;
15469         int rc, length, status = 0;
15470         uint32_t shdr_status, shdr_add_status;
15471         union lpfc_sli4_cfg_shdr *shdr;
15472
15473         /* sanity check on queue memory */
15474         if (!cq || !eq)
15475                 return -ENODEV;
15476
15477         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15478         if (!mbox)
15479                 return -ENOMEM;
15480         length = (sizeof(struct lpfc_mbx_cq_create) -
15481                   sizeof(struct lpfc_sli4_cfg_mhdr));
15482         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15483                          LPFC_MBOX_OPCODE_CQ_CREATE,
15484                          length, LPFC_SLI4_MBX_EMBED);
15485         cq_create = &mbox->u.mqe.un.cq_create;
15486         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
15487         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
15488                     cq->page_count);
15489         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
15490         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
15491         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15492                phba->sli4_hba.pc_sli4_params.cqv);
15493         if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
15494                 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
15495                        (cq->page_size / SLI4_PAGE_SIZE));
15496                 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
15497                        eq->queue_id);
15498                 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
15499                        phba->sli4_hba.pc_sli4_params.cqav);
15500         } else {
15501                 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
15502                        eq->queue_id);
15503         }
15504         switch (cq->entry_count) {
15505         case 2048:
15506         case 4096:
15507                 if (phba->sli4_hba.pc_sli4_params.cqv ==
15508                     LPFC_Q_CREATE_VERSION_2) {
15509                         cq_create->u.request.context.lpfc_cq_context_count =
15510                                 cq->entry_count;
15511                         bf_set(lpfc_cq_context_count,
15512                                &cq_create->u.request.context,
15513                                LPFC_CQ_CNT_WORD7);
15514                         break;
15515                 }
15516                 fallthrough;
15517         default:
15518                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15519                                 "0361 Unsupported CQ count: "
15520                                 "entry cnt %d sz %d pg cnt %d\n",
15521                                 cq->entry_count, cq->entry_size,
15522                                 cq->page_count);
15523                 if (cq->entry_count < 256) {
15524                         status = -EINVAL;
15525                         goto out;
15526                 }
15527                 fallthrough;    /* otherwise default to smallest count */
15528         case 256:
15529                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15530                        LPFC_CQ_CNT_256);
15531                 break;
15532         case 512:
15533                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15534                        LPFC_CQ_CNT_512);
15535                 break;
15536         case 1024:
15537                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15538                        LPFC_CQ_CNT_1024);
15539                 break;
15540         }
15541         list_for_each_entry(dmabuf, &cq->page_list, list) {
15542                 memset(dmabuf->virt, 0, cq->page_size);
15543                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15544                                         putPaddrLow(dmabuf->phys);
15545                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15546                                         putPaddrHigh(dmabuf->phys);
15547         }
15548         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15549
15550         /* The IOCTL status is embedded in the mailbox subheader. */
15551         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15552         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15553         if (shdr_status || shdr_add_status || rc) {
15554                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15555                                 "2501 CQ_CREATE mailbox failed with "
15556                                 "status x%x add_status x%x, mbx status x%x\n",
15557                                 shdr_status, shdr_add_status, rc);
15558                 status = -ENXIO;
15559                 goto out;
15560         }
15561         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
15562         if (cq->queue_id == 0xFFFF) {
15563                 status = -ENXIO;
15564                 goto out;
15565         }
15566         /* link the cq onto the parent eq child list */
15567         list_add_tail(&cq->list, &eq->child_list);
15568         /* Set up completion queue's type and subtype */
15569         cq->type = type;
15570         cq->subtype = subtype;
15571         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
15572         cq->assoc_qid = eq->queue_id;
15573         cq->assoc_qp = eq;
15574         cq->host_index = 0;
15575         cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15576         cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
15577
15578         if (cq->queue_id > phba->sli4_hba.cq_max)
15579                 phba->sli4_hba.cq_max = cq->queue_id;
15580
15581         irq_poll_init(&cq->iop, LPFC_IRQ_POLL_WEIGHT, lpfc_cq_poll_hdler);
15582 out:
15583         mempool_free(mbox, phba->mbox_mem_pool);
15584         return status;
15585 }
15586
15587 /**
15588  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
15589  * @phba: HBA structure that indicates port to create a queue on.
15590  * @cqp: The queue structure array to use to create the completion queues.
15591  * @hdwq: The hardware queue array  with the EQ to bind completion queues to.
15592  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
15593  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
15594  *
15595  * This function creates a set of  completion queue, s to support MRQ
15596  * as detailed in @cqp, on a port,
15597  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
15598  *
15599  * The @phba struct is used to send mailbox command to HBA. The @cq struct
15600  * is used to get the entry count and entry size that are necessary to
15601  * determine the number of pages to allocate and use for this queue. The @eq
15602  * is used to indicate which event queue to bind this completion queue to. This
15603  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
15604  * completion queue. This function is asynchronous and will wait for the mailbox
15605  * command to finish before continuing.
15606  *
15607  * On success this function will return a zero. If unable to allocate enough
15608  * memory this function will return -ENOMEM. If the queue create mailbox command
15609  * fails this function will return -ENXIO.
15610  **/
15611 int
15612 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
15613                    struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
15614                    uint32_t subtype)
15615 {
15616         struct lpfc_queue *cq;
15617         struct lpfc_queue *eq;
15618         struct lpfc_mbx_cq_create_set *cq_set;
15619         struct lpfc_dmabuf *dmabuf;
15620         LPFC_MBOXQ_t *mbox;
15621         int rc, length, alloclen, status = 0;
15622         int cnt, idx, numcq, page_idx = 0;
15623         uint32_t shdr_status, shdr_add_status;
15624         union lpfc_sli4_cfg_shdr *shdr;
15625         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15626
15627         /* sanity check on queue memory */
15628         numcq = phba->cfg_nvmet_mrq;
15629         if (!cqp || !hdwq || !numcq)
15630                 return -ENODEV;
15631
15632         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15633         if (!mbox)
15634                 return -ENOMEM;
15635
15636         length = sizeof(struct lpfc_mbx_cq_create_set);
15637         length += ((numcq * cqp[0]->page_count) *
15638                    sizeof(struct dma_address));
15639         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15640                         LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
15641                         LPFC_SLI4_MBX_NEMBED);
15642         if (alloclen < length) {
15643                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15644                                 "3098 Allocated DMA memory size (%d) is "
15645                                 "less than the requested DMA memory size "
15646                                 "(%d)\n", alloclen, length);
15647                 status = -ENOMEM;
15648                 goto out;
15649         }
15650         cq_set = mbox->sge_array->addr[0];
15651         shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
15652         bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
15653
15654         for (idx = 0; idx < numcq; idx++) {
15655                 cq = cqp[idx];
15656                 eq = hdwq[idx].hba_eq;
15657                 if (!cq || !eq) {
15658                         status = -ENOMEM;
15659                         goto out;
15660                 }
15661                 if (!phba->sli4_hba.pc_sli4_params.supported)
15662                         hw_page_size = cq->page_size;
15663
15664                 switch (idx) {
15665                 case 0:
15666                         bf_set(lpfc_mbx_cq_create_set_page_size,
15667                                &cq_set->u.request,
15668                                (hw_page_size / SLI4_PAGE_SIZE));
15669                         bf_set(lpfc_mbx_cq_create_set_num_pages,
15670                                &cq_set->u.request, cq->page_count);
15671                         bf_set(lpfc_mbx_cq_create_set_evt,
15672                                &cq_set->u.request, 1);
15673                         bf_set(lpfc_mbx_cq_create_set_valid,
15674                                &cq_set->u.request, 1);
15675                         bf_set(lpfc_mbx_cq_create_set_cqe_size,
15676                                &cq_set->u.request, 0);
15677                         bf_set(lpfc_mbx_cq_create_set_num_cq,
15678                                &cq_set->u.request, numcq);
15679                         bf_set(lpfc_mbx_cq_create_set_autovalid,
15680                                &cq_set->u.request,
15681                                phba->sli4_hba.pc_sli4_params.cqav);
15682                         switch (cq->entry_count) {
15683                         case 2048:
15684                         case 4096:
15685                                 if (phba->sli4_hba.pc_sli4_params.cqv ==
15686                                     LPFC_Q_CREATE_VERSION_2) {
15687                                         bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15688                                                &cq_set->u.request,
15689                                                 cq->entry_count);
15690                                         bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15691                                                &cq_set->u.request,
15692                                                LPFC_CQ_CNT_WORD7);
15693                                         break;
15694                                 }
15695                                 fallthrough;
15696                         default:
15697                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15698                                                 "3118 Bad CQ count. (%d)\n",
15699                                                 cq->entry_count);
15700                                 if (cq->entry_count < 256) {
15701                                         status = -EINVAL;
15702                                         goto out;
15703                                 }
15704                                 fallthrough;    /* otherwise default to smallest */
15705                         case 256:
15706                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15707                                        &cq_set->u.request, LPFC_CQ_CNT_256);
15708                                 break;
15709                         case 512:
15710                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15711                                        &cq_set->u.request, LPFC_CQ_CNT_512);
15712                                 break;
15713                         case 1024:
15714                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15715                                        &cq_set->u.request, LPFC_CQ_CNT_1024);
15716                                 break;
15717                         }
15718                         bf_set(lpfc_mbx_cq_create_set_eq_id0,
15719                                &cq_set->u.request, eq->queue_id);
15720                         break;
15721                 case 1:
15722                         bf_set(lpfc_mbx_cq_create_set_eq_id1,
15723                                &cq_set->u.request, eq->queue_id);
15724                         break;
15725                 case 2:
15726                         bf_set(lpfc_mbx_cq_create_set_eq_id2,
15727                                &cq_set->u.request, eq->queue_id);
15728                         break;
15729                 case 3:
15730                         bf_set(lpfc_mbx_cq_create_set_eq_id3,
15731                                &cq_set->u.request, eq->queue_id);
15732                         break;
15733                 case 4:
15734                         bf_set(lpfc_mbx_cq_create_set_eq_id4,
15735                                &cq_set->u.request, eq->queue_id);
15736                         break;
15737                 case 5:
15738                         bf_set(lpfc_mbx_cq_create_set_eq_id5,
15739                                &cq_set->u.request, eq->queue_id);
15740                         break;
15741                 case 6:
15742                         bf_set(lpfc_mbx_cq_create_set_eq_id6,
15743                                &cq_set->u.request, eq->queue_id);
15744                         break;
15745                 case 7:
15746                         bf_set(lpfc_mbx_cq_create_set_eq_id7,
15747                                &cq_set->u.request, eq->queue_id);
15748                         break;
15749                 case 8:
15750                         bf_set(lpfc_mbx_cq_create_set_eq_id8,
15751                                &cq_set->u.request, eq->queue_id);
15752                         break;
15753                 case 9:
15754                         bf_set(lpfc_mbx_cq_create_set_eq_id9,
15755                                &cq_set->u.request, eq->queue_id);
15756                         break;
15757                 case 10:
15758                         bf_set(lpfc_mbx_cq_create_set_eq_id10,
15759                                &cq_set->u.request, eq->queue_id);
15760                         break;
15761                 case 11:
15762                         bf_set(lpfc_mbx_cq_create_set_eq_id11,
15763                                &cq_set->u.request, eq->queue_id);
15764                         break;
15765                 case 12:
15766                         bf_set(lpfc_mbx_cq_create_set_eq_id12,
15767                                &cq_set->u.request, eq->queue_id);
15768                         break;
15769                 case 13:
15770                         bf_set(lpfc_mbx_cq_create_set_eq_id13,
15771                                &cq_set->u.request, eq->queue_id);
15772                         break;
15773                 case 14:
15774                         bf_set(lpfc_mbx_cq_create_set_eq_id14,
15775                                &cq_set->u.request, eq->queue_id);
15776                         break;
15777                 case 15:
15778                         bf_set(lpfc_mbx_cq_create_set_eq_id15,
15779                                &cq_set->u.request, eq->queue_id);
15780                         break;
15781                 }
15782
15783                 /* link the cq onto the parent eq child list */
15784                 list_add_tail(&cq->list, &eq->child_list);
15785                 /* Set up completion queue's type and subtype */
15786                 cq->type = type;
15787                 cq->subtype = subtype;
15788                 cq->assoc_qid = eq->queue_id;
15789                 cq->assoc_qp = eq;
15790                 cq->host_index = 0;
15791                 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15792                 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
15793                                          cq->entry_count);
15794                 cq->chann = idx;
15795
15796                 rc = 0;
15797                 list_for_each_entry(dmabuf, &cq->page_list, list) {
15798                         memset(dmabuf->virt, 0, hw_page_size);
15799                         cnt = page_idx + dmabuf->buffer_tag;
15800                         cq_set->u.request.page[cnt].addr_lo =
15801                                         putPaddrLow(dmabuf->phys);
15802                         cq_set->u.request.page[cnt].addr_hi =
15803                                         putPaddrHigh(dmabuf->phys);
15804                         rc++;
15805                 }
15806                 page_idx += rc;
15807         }
15808
15809         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15810
15811         /* The IOCTL status is embedded in the mailbox subheader. */
15812         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15813         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15814         if (shdr_status || shdr_add_status || rc) {
15815                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15816                                 "3119 CQ_CREATE_SET mailbox failed with "
15817                                 "status x%x add_status x%x, mbx status x%x\n",
15818                                 shdr_status, shdr_add_status, rc);
15819                 status = -ENXIO;
15820                 goto out;
15821         }
15822         rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15823         if (rc == 0xFFFF) {
15824                 status = -ENXIO;
15825                 goto out;
15826         }
15827
15828         for (idx = 0; idx < numcq; idx++) {
15829                 cq = cqp[idx];
15830                 cq->queue_id = rc + idx;
15831                 if (cq->queue_id > phba->sli4_hba.cq_max)
15832                         phba->sli4_hba.cq_max = cq->queue_id;
15833         }
15834
15835 out:
15836         lpfc_sli4_mbox_cmd_free(phba, mbox);
15837         return status;
15838 }
15839
15840 /**
15841  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
15842  * @phba: HBA structure that indicates port to create a queue on.
15843  * @mq: The queue structure to use to create the mailbox queue.
15844  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15845  * @cq: The completion queue to associate with this cq.
15846  *
15847  * This function provides failback (fb) functionality when the
15848  * mq_create_ext fails on older FW generations.  It's purpose is identical
15849  * to mq_create_ext otherwise.
15850  *
15851  * This routine cannot fail as all attributes were previously accessed and
15852  * initialized in mq_create_ext.
15853  **/
15854 static void
15855 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15856                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
15857 {
15858         struct lpfc_mbx_mq_create *mq_create;
15859         struct lpfc_dmabuf *dmabuf;
15860         int length;
15861
15862         length = (sizeof(struct lpfc_mbx_mq_create) -
15863                   sizeof(struct lpfc_sli4_cfg_mhdr));
15864         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15865                          LPFC_MBOX_OPCODE_MQ_CREATE,
15866                          length, LPFC_SLI4_MBX_EMBED);
15867         mq_create = &mbox->u.mqe.un.mq_create;
15868         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
15869                mq->page_count);
15870         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
15871                cq->queue_id);
15872         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15873         switch (mq->entry_count) {
15874         case 16:
15875                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15876                        LPFC_MQ_RING_SIZE_16);
15877                 break;
15878         case 32:
15879                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15880                        LPFC_MQ_RING_SIZE_32);
15881                 break;
15882         case 64:
15883                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15884                        LPFC_MQ_RING_SIZE_64);
15885                 break;
15886         case 128:
15887                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15888                        LPFC_MQ_RING_SIZE_128);
15889                 break;
15890         }
15891         list_for_each_entry(dmabuf, &mq->page_list, list) {
15892                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15893                         putPaddrLow(dmabuf->phys);
15894                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15895                         putPaddrHigh(dmabuf->phys);
15896         }
15897 }
15898
15899 /**
15900  * lpfc_mq_create - Create a mailbox Queue on the HBA
15901  * @phba: HBA structure that indicates port to create a queue on.
15902  * @mq: The queue structure to use to create the mailbox queue.
15903  * @cq: The completion queue to associate with this cq.
15904  * @subtype: The queue's subtype.
15905  *
15906  * This function creates a mailbox queue, as detailed in @mq, on a port,
15907  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15908  *
15909  * The @phba struct is used to send mailbox command to HBA. The @cq struct
15910  * is used to get the entry count and entry size that are necessary to
15911  * determine the number of pages to allocate and use for this queue. This
15912  * function will send the MQ_CREATE mailbox command to the HBA to setup the
15913  * mailbox queue. This function is asynchronous and will wait for the mailbox
15914  * command to finish before continuing.
15915  *
15916  * On success this function will return a zero. If unable to allocate enough
15917  * memory this function will return -ENOMEM. If the queue create mailbox command
15918  * fails this function will return -ENXIO.
15919  **/
15920 int32_t
15921 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15922                struct lpfc_queue *cq, uint32_t subtype)
15923 {
15924         struct lpfc_mbx_mq_create *mq_create;
15925         struct lpfc_mbx_mq_create_ext *mq_create_ext;
15926         struct lpfc_dmabuf *dmabuf;
15927         LPFC_MBOXQ_t *mbox;
15928         int rc, length, status = 0;
15929         uint32_t shdr_status, shdr_add_status;
15930         union lpfc_sli4_cfg_shdr *shdr;
15931         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15932
15933         /* sanity check on queue memory */
15934         if (!mq || !cq)
15935                 return -ENODEV;
15936         if (!phba->sli4_hba.pc_sli4_params.supported)
15937                 hw_page_size = SLI4_PAGE_SIZE;
15938
15939         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15940         if (!mbox)
15941                 return -ENOMEM;
15942         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15943                   sizeof(struct lpfc_sli4_cfg_mhdr));
15944         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15945                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15946                          length, LPFC_SLI4_MBX_EMBED);
15947
15948         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
15949         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
15950         bf_set(lpfc_mbx_mq_create_ext_num_pages,
15951                &mq_create_ext->u.request, mq->page_count);
15952         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15953                &mq_create_ext->u.request, 1);
15954         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
15955                &mq_create_ext->u.request, 1);
15956         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15957                &mq_create_ext->u.request, 1);
15958         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15959                &mq_create_ext->u.request, 1);
15960         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15961                &mq_create_ext->u.request, 1);
15962         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
15963         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15964                phba->sli4_hba.pc_sli4_params.mqv);
15965         if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15966                 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15967                        cq->queue_id);
15968         else
15969                 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15970                        cq->queue_id);
15971         switch (mq->entry_count) {
15972         default:
15973                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15974                                 "0362 Unsupported MQ count. (%d)\n",
15975                                 mq->entry_count);
15976                 if (mq->entry_count < 16) {
15977                         status = -EINVAL;
15978                         goto out;
15979                 }
15980                 fallthrough;    /* otherwise default to smallest count */
15981         case 16:
15982                 bf_set(lpfc_mq_context_ring_size,
15983                        &mq_create_ext->u.request.context,
15984                        LPFC_MQ_RING_SIZE_16);
15985                 break;
15986         case 32:
15987                 bf_set(lpfc_mq_context_ring_size,
15988                        &mq_create_ext->u.request.context,
15989                        LPFC_MQ_RING_SIZE_32);
15990                 break;
15991         case 64:
15992                 bf_set(lpfc_mq_context_ring_size,
15993                        &mq_create_ext->u.request.context,
15994                        LPFC_MQ_RING_SIZE_64);
15995                 break;
15996         case 128:
15997                 bf_set(lpfc_mq_context_ring_size,
15998                        &mq_create_ext->u.request.context,
15999                        LPFC_MQ_RING_SIZE_128);
16000                 break;
16001         }
16002         list_for_each_entry(dmabuf, &mq->page_list, list) {
16003                 memset(dmabuf->virt, 0, hw_page_size);
16004                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
16005                                         putPaddrLow(dmabuf->phys);
16006                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
16007                                         putPaddrHigh(dmabuf->phys);
16008         }
16009         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16010         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16011                               &mq_create_ext->u.response);
16012         if (rc != MBX_SUCCESS) {
16013                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16014                                 "2795 MQ_CREATE_EXT failed with "
16015                                 "status x%x. Failback to MQ_CREATE.\n",
16016                                 rc);
16017                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
16018                 mq_create = &mbox->u.mqe.un.mq_create;
16019                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16020                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
16021                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16022                                       &mq_create->u.response);
16023         }
16024
16025         /* The IOCTL status is embedded in the mailbox subheader. */
16026         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16027         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16028         if (shdr_status || shdr_add_status || rc) {
16029                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16030                                 "2502 MQ_CREATE mailbox failed with "
16031                                 "status x%x add_status x%x, mbx status x%x\n",
16032                                 shdr_status, shdr_add_status, rc);
16033                 status = -ENXIO;
16034                 goto out;
16035         }
16036         if (mq->queue_id == 0xFFFF) {
16037                 status = -ENXIO;
16038                 goto out;
16039         }
16040         mq->type = LPFC_MQ;
16041         mq->assoc_qid = cq->queue_id;
16042         mq->subtype = subtype;
16043         mq->host_index = 0;
16044         mq->hba_index = 0;
16045
16046         /* link the mq onto the parent cq child list */
16047         list_add_tail(&mq->list, &cq->child_list);
16048 out:
16049         mempool_free(mbox, phba->mbox_mem_pool);
16050         return status;
16051 }
16052
16053 /**
16054  * lpfc_wq_create - Create a Work Queue on the HBA
16055  * @phba: HBA structure that indicates port to create a queue on.
16056  * @wq: The queue structure to use to create the work queue.
16057  * @cq: The completion queue to bind this work queue to.
16058  * @subtype: The subtype of the work queue indicating its functionality.
16059  *
16060  * This function creates a work queue, as detailed in @wq, on a port, described
16061  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
16062  *
16063  * The @phba struct is used to send mailbox command to HBA. The @wq struct
16064  * is used to get the entry count and entry size that are necessary to
16065  * determine the number of pages to allocate and use for this queue. The @cq
16066  * is used to indicate which completion queue to bind this work queue to. This
16067  * function will send the WQ_CREATE mailbox command to the HBA to setup the
16068  * work queue. This function is asynchronous and will wait for the mailbox
16069  * command to finish before continuing.
16070  *
16071  * On success this function will return a zero. If unable to allocate enough
16072  * memory this function will return -ENOMEM. If the queue create mailbox command
16073  * fails this function will return -ENXIO.
16074  **/
16075 int
16076 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
16077                struct lpfc_queue *cq, uint32_t subtype)
16078 {
16079         struct lpfc_mbx_wq_create *wq_create;
16080         struct lpfc_dmabuf *dmabuf;
16081         LPFC_MBOXQ_t *mbox;
16082         int rc, length, status = 0;
16083         uint32_t shdr_status, shdr_add_status;
16084         union lpfc_sli4_cfg_shdr *shdr;
16085         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16086         struct dma_address *page;
16087         void __iomem *bar_memmap_p;
16088         uint32_t db_offset;
16089         uint16_t pci_barset;
16090         uint8_t dpp_barset;
16091         uint32_t dpp_offset;
16092         uint8_t wq_create_version;
16093 #ifdef CONFIG_X86
16094         unsigned long pg_addr;
16095 #endif
16096
16097         /* sanity check on queue memory */
16098         if (!wq || !cq)
16099                 return -ENODEV;
16100         if (!phba->sli4_hba.pc_sli4_params.supported)
16101                 hw_page_size = wq->page_size;
16102
16103         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16104         if (!mbox)
16105                 return -ENOMEM;
16106         length = (sizeof(struct lpfc_mbx_wq_create) -
16107                   sizeof(struct lpfc_sli4_cfg_mhdr));
16108         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16109                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
16110                          length, LPFC_SLI4_MBX_EMBED);
16111         wq_create = &mbox->u.mqe.un.wq_create;
16112         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
16113         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
16114                     wq->page_count);
16115         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
16116                     cq->queue_id);
16117
16118         /* wqv is the earliest version supported, NOT the latest */
16119         bf_set(lpfc_mbox_hdr_version, &shdr->request,
16120                phba->sli4_hba.pc_sli4_params.wqv);
16121
16122         if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
16123             (wq->page_size > SLI4_PAGE_SIZE))
16124                 wq_create_version = LPFC_Q_CREATE_VERSION_1;
16125         else
16126                 wq_create_version = LPFC_Q_CREATE_VERSION_0;
16127
16128         switch (wq_create_version) {
16129         case LPFC_Q_CREATE_VERSION_1:
16130                 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
16131                        wq->entry_count);
16132                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16133                        LPFC_Q_CREATE_VERSION_1);
16134
16135                 switch (wq->entry_size) {
16136                 default:
16137                 case 64:
16138                         bf_set(lpfc_mbx_wq_create_wqe_size,
16139                                &wq_create->u.request_1,
16140                                LPFC_WQ_WQE_SIZE_64);
16141                         break;
16142                 case 128:
16143                         bf_set(lpfc_mbx_wq_create_wqe_size,
16144                                &wq_create->u.request_1,
16145                                LPFC_WQ_WQE_SIZE_128);
16146                         break;
16147                 }
16148                 /* Request DPP by default */
16149                 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
16150                 bf_set(lpfc_mbx_wq_create_page_size,
16151                        &wq_create->u.request_1,
16152                        (wq->page_size / SLI4_PAGE_SIZE));
16153                 page = wq_create->u.request_1.page;
16154                 break;
16155         default:
16156                 page = wq_create->u.request.page;
16157                 break;
16158         }
16159
16160         list_for_each_entry(dmabuf, &wq->page_list, list) {
16161                 memset(dmabuf->virt, 0, hw_page_size);
16162                 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
16163                 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
16164         }
16165
16166         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16167                 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
16168
16169         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16170         /* The IOCTL status is embedded in the mailbox subheader. */
16171         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16172         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16173         if (shdr_status || shdr_add_status || rc) {
16174                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16175                                 "2503 WQ_CREATE mailbox failed with "
16176                                 "status x%x add_status x%x, mbx status x%x\n",
16177                                 shdr_status, shdr_add_status, rc);
16178                 status = -ENXIO;
16179                 goto out;
16180         }
16181
16182         if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
16183                 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
16184                                         &wq_create->u.response);
16185         else
16186                 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
16187                                         &wq_create->u.response_1);
16188
16189         if (wq->queue_id == 0xFFFF) {
16190                 status = -ENXIO;
16191                 goto out;
16192         }
16193
16194         wq->db_format = LPFC_DB_LIST_FORMAT;
16195         if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
16196                 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16197                         wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
16198                                                &wq_create->u.response);
16199                         if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
16200                             (wq->db_format != LPFC_DB_RING_FORMAT)) {
16201                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16202                                                 "3265 WQ[%d] doorbell format "
16203                                                 "not supported: x%x\n",
16204                                                 wq->queue_id, wq->db_format);
16205                                 status = -EINVAL;
16206                                 goto out;
16207                         }
16208                         pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
16209                                             &wq_create->u.response);
16210                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16211                                                                    pci_barset);
16212                         if (!bar_memmap_p) {
16213                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16214                                                 "3263 WQ[%d] failed to memmap "
16215                                                 "pci barset:x%x\n",
16216                                                 wq->queue_id, pci_barset);
16217                                 status = -ENOMEM;
16218                                 goto out;
16219                         }
16220                         db_offset = wq_create->u.response.doorbell_offset;
16221                         if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
16222                             (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
16223                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16224                                                 "3252 WQ[%d] doorbell offset "
16225                                                 "not supported: x%x\n",
16226                                                 wq->queue_id, db_offset);
16227                                 status = -EINVAL;
16228                                 goto out;
16229                         }
16230                         wq->db_regaddr = bar_memmap_p + db_offset;
16231                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16232                                         "3264 WQ[%d]: barset:x%x, offset:x%x, "
16233                                         "format:x%x\n", wq->queue_id,
16234                                         pci_barset, db_offset, wq->db_format);
16235                 } else
16236                         wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
16237         } else {
16238                 /* Check if DPP was honored by the firmware */
16239                 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
16240                                     &wq_create->u.response_1);
16241                 if (wq->dpp_enable) {
16242                         pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
16243                                             &wq_create->u.response_1);
16244                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16245                                                                    pci_barset);
16246                         if (!bar_memmap_p) {
16247                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16248                                                 "3267 WQ[%d] failed to memmap "
16249                                                 "pci barset:x%x\n",
16250                                                 wq->queue_id, pci_barset);
16251                                 status = -ENOMEM;
16252                                 goto out;
16253                         }
16254                         db_offset = wq_create->u.response_1.doorbell_offset;
16255                         wq->db_regaddr = bar_memmap_p + db_offset;
16256                         wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
16257                                             &wq_create->u.response_1);
16258                         dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
16259                                             &wq_create->u.response_1);
16260                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16261                                                                    dpp_barset);
16262                         if (!bar_memmap_p) {
16263                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16264                                                 "3268 WQ[%d] failed to memmap "
16265                                                 "pci barset:x%x\n",
16266                                                 wq->queue_id, dpp_barset);
16267                                 status = -ENOMEM;
16268                                 goto out;
16269                         }
16270                         dpp_offset = wq_create->u.response_1.dpp_offset;
16271                         wq->dpp_regaddr = bar_memmap_p + dpp_offset;
16272                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16273                                         "3271 WQ[%d]: barset:x%x, offset:x%x, "
16274                                         "dpp_id:x%x dpp_barset:x%x "
16275                                         "dpp_offset:x%x\n",
16276                                         wq->queue_id, pci_barset, db_offset,
16277                                         wq->dpp_id, dpp_barset, dpp_offset);
16278
16279 #ifdef CONFIG_X86
16280                         /* Enable combined writes for DPP aperture */
16281                         pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
16282                         rc = set_memory_wc(pg_addr, 1);
16283                         if (rc) {
16284                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16285                                         "3272 Cannot setup Combined "
16286                                         "Write on WQ[%d] - disable DPP\n",
16287                                         wq->queue_id);
16288                                 phba->cfg_enable_dpp = 0;
16289                         }
16290 #else
16291                         phba->cfg_enable_dpp = 0;
16292 #endif
16293                 } else
16294                         wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
16295         }
16296         wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
16297         if (wq->pring == NULL) {
16298                 status = -ENOMEM;
16299                 goto out;
16300         }
16301         wq->type = LPFC_WQ;
16302         wq->assoc_qid = cq->queue_id;
16303         wq->subtype = subtype;
16304         wq->host_index = 0;
16305         wq->hba_index = 0;
16306         wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
16307
16308         /* link the wq onto the parent cq child list */
16309         list_add_tail(&wq->list, &cq->child_list);
16310 out:
16311         mempool_free(mbox, phba->mbox_mem_pool);
16312         return status;
16313 }
16314
16315 /**
16316  * lpfc_rq_create - Create a Receive Queue on the HBA
16317  * @phba: HBA structure that indicates port to create a queue on.
16318  * @hrq: The queue structure to use to create the header receive queue.
16319  * @drq: The queue structure to use to create the data receive queue.
16320  * @cq: The completion queue to bind this work queue to.
16321  * @subtype: The subtype of the work queue indicating its functionality.
16322  *
16323  * This function creates a receive buffer queue pair , as detailed in @hrq and
16324  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
16325  * to the HBA.
16326  *
16327  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
16328  * struct is used to get the entry count that is necessary to determine the
16329  * number of pages to use for this queue. The @cq is used to indicate which
16330  * completion queue to bind received buffers that are posted to these queues to.
16331  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
16332  * receive queue pair. This function is asynchronous and will wait for the
16333  * mailbox command to finish before continuing.
16334  *
16335  * On success this function will return a zero. If unable to allocate enough
16336  * memory this function will return -ENOMEM. If the queue create mailbox command
16337  * fails this function will return -ENXIO.
16338  **/
16339 int
16340 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16341                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
16342 {
16343         struct lpfc_mbx_rq_create *rq_create;
16344         struct lpfc_dmabuf *dmabuf;
16345         LPFC_MBOXQ_t *mbox;
16346         int rc, length, status = 0;
16347         uint32_t shdr_status, shdr_add_status;
16348         union lpfc_sli4_cfg_shdr *shdr;
16349         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16350         void __iomem *bar_memmap_p;
16351         uint32_t db_offset;
16352         uint16_t pci_barset;
16353
16354         /* sanity check on queue memory */
16355         if (!hrq || !drq || !cq)
16356                 return -ENODEV;
16357         if (!phba->sli4_hba.pc_sli4_params.supported)
16358                 hw_page_size = SLI4_PAGE_SIZE;
16359
16360         if (hrq->entry_count != drq->entry_count)
16361                 return -EINVAL;
16362         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16363         if (!mbox)
16364                 return -ENOMEM;
16365         length = (sizeof(struct lpfc_mbx_rq_create) -
16366                   sizeof(struct lpfc_sli4_cfg_mhdr));
16367         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16368                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
16369                          length, LPFC_SLI4_MBX_EMBED);
16370         rq_create = &mbox->u.mqe.un.rq_create;
16371         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
16372         bf_set(lpfc_mbox_hdr_version, &shdr->request,
16373                phba->sli4_hba.pc_sli4_params.rqv);
16374         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
16375                 bf_set(lpfc_rq_context_rqe_count_1,
16376                        &rq_create->u.request.context,
16377                        hrq->entry_count);
16378                 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
16379                 bf_set(lpfc_rq_context_rqe_size,
16380                        &rq_create->u.request.context,
16381                        LPFC_RQE_SIZE_8);
16382                 bf_set(lpfc_rq_context_page_size,
16383                        &rq_create->u.request.context,
16384                        LPFC_RQ_PAGE_SIZE_4096);
16385         } else {
16386                 switch (hrq->entry_count) {
16387                 default:
16388                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16389                                         "2535 Unsupported RQ count. (%d)\n",
16390                                         hrq->entry_count);
16391                         if (hrq->entry_count < 512) {
16392                                 status = -EINVAL;
16393                                 goto out;
16394                         }
16395                         fallthrough;    /* otherwise default to smallest count */
16396                 case 512:
16397                         bf_set(lpfc_rq_context_rqe_count,
16398                                &rq_create->u.request.context,
16399                                LPFC_RQ_RING_SIZE_512);
16400                         break;
16401                 case 1024:
16402                         bf_set(lpfc_rq_context_rqe_count,
16403                                &rq_create->u.request.context,
16404                                LPFC_RQ_RING_SIZE_1024);
16405                         break;
16406                 case 2048:
16407                         bf_set(lpfc_rq_context_rqe_count,
16408                                &rq_create->u.request.context,
16409                                LPFC_RQ_RING_SIZE_2048);
16410                         break;
16411                 case 4096:
16412                         bf_set(lpfc_rq_context_rqe_count,
16413                                &rq_create->u.request.context,
16414                                LPFC_RQ_RING_SIZE_4096);
16415                         break;
16416                 }
16417                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
16418                        LPFC_HDR_BUF_SIZE);
16419         }
16420         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
16421                cq->queue_id);
16422         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
16423                hrq->page_count);
16424         list_for_each_entry(dmabuf, &hrq->page_list, list) {
16425                 memset(dmabuf->virt, 0, hw_page_size);
16426                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16427                                         putPaddrLow(dmabuf->phys);
16428                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16429                                         putPaddrHigh(dmabuf->phys);
16430         }
16431         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16432                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
16433
16434         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16435         /* The IOCTL status is embedded in the mailbox subheader. */
16436         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16437         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16438         if (shdr_status || shdr_add_status || rc) {
16439                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16440                                 "2504 RQ_CREATE mailbox failed with "
16441                                 "status x%x add_status x%x, mbx status x%x\n",
16442                                 shdr_status, shdr_add_status, rc);
16443                 status = -ENXIO;
16444                 goto out;
16445         }
16446         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16447         if (hrq->queue_id == 0xFFFF) {
16448                 status = -ENXIO;
16449                 goto out;
16450         }
16451
16452         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16453                 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
16454                                         &rq_create->u.response);
16455                 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
16456                     (hrq->db_format != LPFC_DB_RING_FORMAT)) {
16457                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16458                                         "3262 RQ [%d] doorbell format not "
16459                                         "supported: x%x\n", hrq->queue_id,
16460                                         hrq->db_format);
16461                         status = -EINVAL;
16462                         goto out;
16463                 }
16464
16465                 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
16466                                     &rq_create->u.response);
16467                 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
16468                 if (!bar_memmap_p) {
16469                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16470                                         "3269 RQ[%d] failed to memmap pci "
16471                                         "barset:x%x\n", hrq->queue_id,
16472                                         pci_barset);
16473                         status = -ENOMEM;
16474                         goto out;
16475                 }
16476
16477                 db_offset = rq_create->u.response.doorbell_offset;
16478                 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
16479                     (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
16480                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16481                                         "3270 RQ[%d] doorbell offset not "
16482                                         "supported: x%x\n", hrq->queue_id,
16483                                         db_offset);
16484                         status = -EINVAL;
16485                         goto out;
16486                 }
16487                 hrq->db_regaddr = bar_memmap_p + db_offset;
16488                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16489                                 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
16490                                 "format:x%x\n", hrq->queue_id, pci_barset,
16491                                 db_offset, hrq->db_format);
16492         } else {
16493                 hrq->db_format = LPFC_DB_RING_FORMAT;
16494                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16495         }
16496         hrq->type = LPFC_HRQ;
16497         hrq->assoc_qid = cq->queue_id;
16498         hrq->subtype = subtype;
16499         hrq->host_index = 0;
16500         hrq->hba_index = 0;
16501         hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
16502
16503         /* now create the data queue */
16504         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16505                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
16506                          length, LPFC_SLI4_MBX_EMBED);
16507         bf_set(lpfc_mbox_hdr_version, &shdr->request,
16508                phba->sli4_hba.pc_sli4_params.rqv);
16509         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
16510                 bf_set(lpfc_rq_context_rqe_count_1,
16511                        &rq_create->u.request.context, hrq->entry_count);
16512                 if (subtype == LPFC_NVMET)
16513                         rq_create->u.request.context.buffer_size =
16514                                 LPFC_NVMET_DATA_BUF_SIZE;
16515                 else
16516                         rq_create->u.request.context.buffer_size =
16517                                 LPFC_DATA_BUF_SIZE;
16518                 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
16519                        LPFC_RQE_SIZE_8);
16520                 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
16521                        (PAGE_SIZE/SLI4_PAGE_SIZE));
16522         } else {
16523                 switch (drq->entry_count) {
16524                 default:
16525                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16526                                         "2536 Unsupported RQ count. (%d)\n",
16527                                         drq->entry_count);
16528                         if (drq->entry_count < 512) {
16529                                 status = -EINVAL;
16530                                 goto out;
16531                         }
16532                         fallthrough;    /* otherwise default to smallest count */
16533                 case 512:
16534                         bf_set(lpfc_rq_context_rqe_count,
16535                                &rq_create->u.request.context,
16536                                LPFC_RQ_RING_SIZE_512);
16537                         break;
16538                 case 1024:
16539                         bf_set(lpfc_rq_context_rqe_count,
16540                                &rq_create->u.request.context,
16541                                LPFC_RQ_RING_SIZE_1024);
16542                         break;
16543                 case 2048:
16544                         bf_set(lpfc_rq_context_rqe_count,
16545                                &rq_create->u.request.context,
16546                                LPFC_RQ_RING_SIZE_2048);
16547                         break;
16548                 case 4096:
16549                         bf_set(lpfc_rq_context_rqe_count,
16550                                &rq_create->u.request.context,
16551                                LPFC_RQ_RING_SIZE_4096);
16552                         break;
16553                 }
16554                 if (subtype == LPFC_NVMET)
16555                         bf_set(lpfc_rq_context_buf_size,
16556                                &rq_create->u.request.context,
16557                                LPFC_NVMET_DATA_BUF_SIZE);
16558                 else
16559                         bf_set(lpfc_rq_context_buf_size,
16560                                &rq_create->u.request.context,
16561                                LPFC_DATA_BUF_SIZE);
16562         }
16563         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
16564                cq->queue_id);
16565         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
16566                drq->page_count);
16567         list_for_each_entry(dmabuf, &drq->page_list, list) {
16568                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16569                                         putPaddrLow(dmabuf->phys);
16570                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16571                                         putPaddrHigh(dmabuf->phys);
16572         }
16573         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16574                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
16575         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16576         /* The IOCTL status is embedded in the mailbox subheader. */
16577         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
16578         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16579         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16580         if (shdr_status || shdr_add_status || rc) {
16581                 status = -ENXIO;
16582                 goto out;
16583         }
16584         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16585         if (drq->queue_id == 0xFFFF) {
16586                 status = -ENXIO;
16587                 goto out;
16588         }
16589         drq->type = LPFC_DRQ;
16590         drq->assoc_qid = cq->queue_id;
16591         drq->subtype = subtype;
16592         drq->host_index = 0;
16593         drq->hba_index = 0;
16594         drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
16595
16596         /* link the header and data RQs onto the parent cq child list */
16597         list_add_tail(&hrq->list, &cq->child_list);
16598         list_add_tail(&drq->list, &cq->child_list);
16599
16600 out:
16601         mempool_free(mbox, phba->mbox_mem_pool);
16602         return status;
16603 }
16604
16605 /**
16606  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
16607  * @phba: HBA structure that indicates port to create a queue on.
16608  * @hrqp: The queue structure array to use to create the header receive queues.
16609  * @drqp: The queue structure array to use to create the data receive queues.
16610  * @cqp: The completion queue array to bind these receive queues to.
16611  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16612  *
16613  * This function creates a receive buffer queue pair , as detailed in @hrq and
16614  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
16615  * to the HBA.
16616  *
16617  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
16618  * struct is used to get the entry count that is necessary to determine the
16619  * number of pages to use for this queue. The @cq is used to indicate which
16620  * completion queue to bind received buffers that are posted to these queues to.
16621  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
16622  * receive queue pair. This function is asynchronous and will wait for the
16623  * mailbox command to finish before continuing.
16624  *
16625  * On success this function will return a zero. If unable to allocate enough
16626  * memory this function will return -ENOMEM. If the queue create mailbox command
16627  * fails this function will return -ENXIO.
16628  **/
16629 int
16630 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
16631                 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
16632                 uint32_t subtype)
16633 {
16634         struct lpfc_queue *hrq, *drq, *cq;
16635         struct lpfc_mbx_rq_create_v2 *rq_create;
16636         struct lpfc_dmabuf *dmabuf;
16637         LPFC_MBOXQ_t *mbox;
16638         int rc, length, alloclen, status = 0;
16639         int cnt, idx, numrq, page_idx = 0;
16640         uint32_t shdr_status, shdr_add_status;
16641         union lpfc_sli4_cfg_shdr *shdr;
16642         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16643
16644         numrq = phba->cfg_nvmet_mrq;
16645         /* sanity check on array memory */
16646         if (!hrqp || !drqp || !cqp || !numrq)
16647                 return -ENODEV;
16648         if (!phba->sli4_hba.pc_sli4_params.supported)
16649                 hw_page_size = SLI4_PAGE_SIZE;
16650
16651         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16652         if (!mbox)
16653                 return -ENOMEM;
16654
16655         length = sizeof(struct lpfc_mbx_rq_create_v2);
16656         length += ((2 * numrq * hrqp[0]->page_count) *
16657                    sizeof(struct dma_address));
16658
16659         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16660                                     LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
16661                                     LPFC_SLI4_MBX_NEMBED);
16662         if (alloclen < length) {
16663                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16664                                 "3099 Allocated DMA memory size (%d) is "
16665                                 "less than the requested DMA memory size "
16666                                 "(%d)\n", alloclen, length);
16667                 status = -ENOMEM;
16668                 goto out;
16669         }
16670
16671
16672
16673         rq_create = mbox->sge_array->addr[0];
16674         shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
16675
16676         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
16677         cnt = 0;
16678
16679         for (idx = 0; idx < numrq; idx++) {
16680                 hrq = hrqp[idx];
16681                 drq = drqp[idx];
16682                 cq  = cqp[idx];
16683
16684                 /* sanity check on queue memory */
16685                 if (!hrq || !drq || !cq) {
16686                         status = -ENODEV;
16687                         goto out;
16688                 }
16689
16690                 if (hrq->entry_count != drq->entry_count) {
16691                         status = -EINVAL;
16692                         goto out;
16693                 }
16694
16695                 if (idx == 0) {
16696                         bf_set(lpfc_mbx_rq_create_num_pages,
16697                                &rq_create->u.request,
16698                                hrq->page_count);
16699                         bf_set(lpfc_mbx_rq_create_rq_cnt,
16700                                &rq_create->u.request, (numrq * 2));
16701                         bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
16702                                1);
16703                         bf_set(lpfc_rq_context_base_cq,
16704                                &rq_create->u.request.context,
16705                                cq->queue_id);
16706                         bf_set(lpfc_rq_context_data_size,
16707                                &rq_create->u.request.context,
16708                                LPFC_NVMET_DATA_BUF_SIZE);
16709                         bf_set(lpfc_rq_context_hdr_size,
16710                                &rq_create->u.request.context,
16711                                LPFC_HDR_BUF_SIZE);
16712                         bf_set(lpfc_rq_context_rqe_count_1,
16713                                &rq_create->u.request.context,
16714                                hrq->entry_count);
16715                         bf_set(lpfc_rq_context_rqe_size,
16716                                &rq_create->u.request.context,
16717                                LPFC_RQE_SIZE_8);
16718                         bf_set(lpfc_rq_context_page_size,
16719                                &rq_create->u.request.context,
16720                                (PAGE_SIZE/SLI4_PAGE_SIZE));
16721                 }
16722                 rc = 0;
16723                 list_for_each_entry(dmabuf, &hrq->page_list, list) {
16724                         memset(dmabuf->virt, 0, hw_page_size);
16725                         cnt = page_idx + dmabuf->buffer_tag;
16726                         rq_create->u.request.page[cnt].addr_lo =
16727                                         putPaddrLow(dmabuf->phys);
16728                         rq_create->u.request.page[cnt].addr_hi =
16729                                         putPaddrHigh(dmabuf->phys);
16730                         rc++;
16731                 }
16732                 page_idx += rc;
16733
16734                 rc = 0;
16735                 list_for_each_entry(dmabuf, &drq->page_list, list) {
16736                         memset(dmabuf->virt, 0, hw_page_size);
16737                         cnt = page_idx + dmabuf->buffer_tag;
16738                         rq_create->u.request.page[cnt].addr_lo =
16739                                         putPaddrLow(dmabuf->phys);
16740                         rq_create->u.request.page[cnt].addr_hi =
16741                                         putPaddrHigh(dmabuf->phys);
16742                         rc++;
16743                 }
16744                 page_idx += rc;
16745
16746                 hrq->db_format = LPFC_DB_RING_FORMAT;
16747                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16748                 hrq->type = LPFC_HRQ;
16749                 hrq->assoc_qid = cq->queue_id;
16750                 hrq->subtype = subtype;
16751                 hrq->host_index = 0;
16752                 hrq->hba_index = 0;
16753                 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
16754
16755                 drq->db_format = LPFC_DB_RING_FORMAT;
16756                 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16757                 drq->type = LPFC_DRQ;
16758                 drq->assoc_qid = cq->queue_id;
16759                 drq->subtype = subtype;
16760                 drq->host_index = 0;
16761                 drq->hba_index = 0;
16762                 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
16763
16764                 list_add_tail(&hrq->list, &cq->child_list);
16765                 list_add_tail(&drq->list, &cq->child_list);
16766         }
16767
16768         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16769         /* The IOCTL status is embedded in the mailbox subheader. */
16770         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16771         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16772         if (shdr_status || shdr_add_status || rc) {
16773                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16774                                 "3120 RQ_CREATE mailbox failed with "
16775                                 "status x%x add_status x%x, mbx status x%x\n",
16776                                 shdr_status, shdr_add_status, rc);
16777                 status = -ENXIO;
16778                 goto out;
16779         }
16780         rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16781         if (rc == 0xFFFF) {
16782                 status = -ENXIO;
16783                 goto out;
16784         }
16785
16786         /* Initialize all RQs with associated queue id */
16787         for (idx = 0; idx < numrq; idx++) {
16788                 hrq = hrqp[idx];
16789                 hrq->queue_id = rc + (2 * idx);
16790                 drq = drqp[idx];
16791                 drq->queue_id = rc + (2 * idx) + 1;
16792         }
16793
16794 out:
16795         lpfc_sli4_mbox_cmd_free(phba, mbox);
16796         return status;
16797 }
16798
16799 /**
16800  * lpfc_eq_destroy - Destroy an event Queue on the HBA
16801  * @phba: HBA structure that indicates port to destroy a queue on.
16802  * @eq: The queue structure associated with the queue to destroy.
16803  *
16804  * This function destroys a queue, as detailed in @eq by sending an mailbox
16805  * command, specific to the type of queue, to the HBA.
16806  *
16807  * The @eq struct is used to get the queue ID of the queue to destroy.
16808  *
16809  * On success this function will return a zero. If the queue destroy mailbox
16810  * command fails this function will return -ENXIO.
16811  **/
16812 int
16813 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16814 {
16815         LPFC_MBOXQ_t *mbox;
16816         int rc, length, status = 0;
16817         uint32_t shdr_status, shdr_add_status;
16818         union lpfc_sli4_cfg_shdr *shdr;
16819
16820         /* sanity check on queue memory */
16821         if (!eq)
16822                 return -ENODEV;
16823
16824         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16825         if (!mbox)
16826                 return -ENOMEM;
16827         length = (sizeof(struct lpfc_mbx_eq_destroy) -
16828                   sizeof(struct lpfc_sli4_cfg_mhdr));
16829         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16830                          LPFC_MBOX_OPCODE_EQ_DESTROY,
16831                          length, LPFC_SLI4_MBX_EMBED);
16832         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16833                eq->queue_id);
16834         mbox->vport = eq->phba->pport;
16835         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16836
16837         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16838         /* The IOCTL status is embedded in the mailbox subheader. */
16839         shdr = (union lpfc_sli4_cfg_shdr *)
16840                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16841         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16842         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16843         if (shdr_status || shdr_add_status || rc) {
16844                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16845                                 "2505 EQ_DESTROY mailbox failed with "
16846                                 "status x%x add_status x%x, mbx status x%x\n",
16847                                 shdr_status, shdr_add_status, rc);
16848                 status = -ENXIO;
16849         }
16850
16851         /* Remove eq from any list */
16852         list_del_init(&eq->list);
16853         mempool_free(mbox, eq->phba->mbox_mem_pool);
16854         return status;
16855 }
16856
16857 /**
16858  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16859  * @phba: HBA structure that indicates port to destroy a queue on.
16860  * @cq: The queue structure associated with the queue to destroy.
16861  *
16862  * This function destroys a queue, as detailed in @cq by sending an mailbox
16863  * command, specific to the type of queue, to the HBA.
16864  *
16865  * The @cq struct is used to get the queue ID of the queue to destroy.
16866  *
16867  * On success this function will return a zero. If the queue destroy mailbox
16868  * command fails this function will return -ENXIO.
16869  **/
16870 int
16871 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16872 {
16873         LPFC_MBOXQ_t *mbox;
16874         int rc, length, status = 0;
16875         uint32_t shdr_status, shdr_add_status;
16876         union lpfc_sli4_cfg_shdr *shdr;
16877
16878         /* sanity check on queue memory */
16879         if (!cq)
16880                 return -ENODEV;
16881         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16882         if (!mbox)
16883                 return -ENOMEM;
16884         length = (sizeof(struct lpfc_mbx_cq_destroy) -
16885                   sizeof(struct lpfc_sli4_cfg_mhdr));
16886         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16887                          LPFC_MBOX_OPCODE_CQ_DESTROY,
16888                          length, LPFC_SLI4_MBX_EMBED);
16889         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16890                cq->queue_id);
16891         mbox->vport = cq->phba->pport;
16892         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16893         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16894         /* The IOCTL status is embedded in the mailbox subheader. */
16895         shdr = (union lpfc_sli4_cfg_shdr *)
16896                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16897         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16898         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16899         if (shdr_status || shdr_add_status || rc) {
16900                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16901                                 "2506 CQ_DESTROY mailbox failed with "
16902                                 "status x%x add_status x%x, mbx status x%x\n",
16903                                 shdr_status, shdr_add_status, rc);
16904                 status = -ENXIO;
16905         }
16906         /* Remove cq from any list */
16907         list_del_init(&cq->list);
16908         mempool_free(mbox, cq->phba->mbox_mem_pool);
16909         return status;
16910 }
16911
16912 /**
16913  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16914  * @phba: HBA structure that indicates port to destroy a queue on.
16915  * @mq: The queue structure associated with the queue to destroy.
16916  *
16917  * This function destroys a queue, as detailed in @mq by sending an mailbox
16918  * command, specific to the type of queue, to the HBA.
16919  *
16920  * The @mq struct is used to get the queue ID of the queue to destroy.
16921  *
16922  * On success this function will return a zero. If the queue destroy mailbox
16923  * command fails this function will return -ENXIO.
16924  **/
16925 int
16926 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16927 {
16928         LPFC_MBOXQ_t *mbox;
16929         int rc, length, status = 0;
16930         uint32_t shdr_status, shdr_add_status;
16931         union lpfc_sli4_cfg_shdr *shdr;
16932
16933         /* sanity check on queue memory */
16934         if (!mq)
16935                 return -ENODEV;
16936         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16937         if (!mbox)
16938                 return -ENOMEM;
16939         length = (sizeof(struct lpfc_mbx_mq_destroy) -
16940                   sizeof(struct lpfc_sli4_cfg_mhdr));
16941         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16942                          LPFC_MBOX_OPCODE_MQ_DESTROY,
16943                          length, LPFC_SLI4_MBX_EMBED);
16944         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16945                mq->queue_id);
16946         mbox->vport = mq->phba->pport;
16947         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16948         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16949         /* The IOCTL status is embedded in the mailbox subheader. */
16950         shdr = (union lpfc_sli4_cfg_shdr *)
16951                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16952         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16953         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16954         if (shdr_status || shdr_add_status || rc) {
16955                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16956                                 "2507 MQ_DESTROY mailbox failed with "
16957                                 "status x%x add_status x%x, mbx status x%x\n",
16958                                 shdr_status, shdr_add_status, rc);
16959                 status = -ENXIO;
16960         }
16961         /* Remove mq from any list */
16962         list_del_init(&mq->list);
16963         mempool_free(mbox, mq->phba->mbox_mem_pool);
16964         return status;
16965 }
16966
16967 /**
16968  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16969  * @phba: HBA structure that indicates port to destroy a queue on.
16970  * @wq: The queue structure associated with the queue to destroy.
16971  *
16972  * This function destroys a queue, as detailed in @wq by sending an mailbox
16973  * command, specific to the type of queue, to the HBA.
16974  *
16975  * The @wq struct is used to get the queue ID of the queue to destroy.
16976  *
16977  * On success this function will return a zero. If the queue destroy mailbox
16978  * command fails this function will return -ENXIO.
16979  **/
16980 int
16981 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16982 {
16983         LPFC_MBOXQ_t *mbox;
16984         int rc, length, status = 0;
16985         uint32_t shdr_status, shdr_add_status;
16986         union lpfc_sli4_cfg_shdr *shdr;
16987
16988         /* sanity check on queue memory */
16989         if (!wq)
16990                 return -ENODEV;
16991         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16992         if (!mbox)
16993                 return -ENOMEM;
16994         length = (sizeof(struct lpfc_mbx_wq_destroy) -
16995                   sizeof(struct lpfc_sli4_cfg_mhdr));
16996         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16997                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16998                          length, LPFC_SLI4_MBX_EMBED);
16999         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
17000                wq->queue_id);
17001         mbox->vport = wq->phba->pport;
17002         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17003         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
17004         shdr = (union lpfc_sli4_cfg_shdr *)
17005                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
17006         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17007         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17008         if (shdr_status || shdr_add_status || rc) {
17009                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17010                                 "2508 WQ_DESTROY mailbox failed with "
17011                                 "status x%x add_status x%x, mbx status x%x\n",
17012                                 shdr_status, shdr_add_status, rc);
17013                 status = -ENXIO;
17014         }
17015         /* Remove wq from any list */
17016         list_del_init(&wq->list);
17017         kfree(wq->pring);
17018         wq->pring = NULL;
17019         mempool_free(mbox, wq->phba->mbox_mem_pool);
17020         return status;
17021 }
17022
17023 /**
17024  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
17025  * @phba: HBA structure that indicates port to destroy a queue on.
17026  * @hrq: The queue structure associated with the queue to destroy.
17027  * @drq: The queue structure associated with the queue to destroy.
17028  *
17029  * This function destroys a queue, as detailed in @rq by sending an mailbox
17030  * command, specific to the type of queue, to the HBA.
17031  *
17032  * The @rq struct is used to get the queue ID of the queue to destroy.
17033  *
17034  * On success this function will return a zero. If the queue destroy mailbox
17035  * command fails this function will return -ENXIO.
17036  **/
17037 int
17038 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17039                 struct lpfc_queue *drq)
17040 {
17041         LPFC_MBOXQ_t *mbox;
17042         int rc, length, status = 0;
17043         uint32_t shdr_status, shdr_add_status;
17044         union lpfc_sli4_cfg_shdr *shdr;
17045
17046         /* sanity check on queue memory */
17047         if (!hrq || !drq)
17048                 return -ENODEV;
17049         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
17050         if (!mbox)
17051                 return -ENOMEM;
17052         length = (sizeof(struct lpfc_mbx_rq_destroy) -
17053                   sizeof(struct lpfc_sli4_cfg_mhdr));
17054         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17055                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
17056                          length, LPFC_SLI4_MBX_EMBED);
17057         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17058                hrq->queue_id);
17059         mbox->vport = hrq->phba->pport;
17060         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17061         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
17062         /* The IOCTL status is embedded in the mailbox subheader. */
17063         shdr = (union lpfc_sli4_cfg_shdr *)
17064                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17065         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17066         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17067         if (shdr_status || shdr_add_status || rc) {
17068                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17069                                 "2509 RQ_DESTROY mailbox failed with "
17070                                 "status x%x add_status x%x, mbx status x%x\n",
17071                                 shdr_status, shdr_add_status, rc);
17072                 if (rc != MBX_TIMEOUT)
17073                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
17074                 return -ENXIO;
17075         }
17076         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17077                drq->queue_id);
17078         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
17079         shdr = (union lpfc_sli4_cfg_shdr *)
17080                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17081         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17082         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17083         if (shdr_status || shdr_add_status || rc) {
17084                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17085                                 "2510 RQ_DESTROY mailbox failed with "
17086                                 "status x%x add_status x%x, mbx status x%x\n",
17087                                 shdr_status, shdr_add_status, rc);
17088                 status = -ENXIO;
17089         }
17090         list_del_init(&hrq->list);
17091         list_del_init(&drq->list);
17092         mempool_free(mbox, hrq->phba->mbox_mem_pool);
17093         return status;
17094 }
17095
17096 /**
17097  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
17098  * @phba: The virtual port for which this call being executed.
17099  * @pdma_phys_addr0: Physical address of the 1st SGL page.
17100  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
17101  * @xritag: the xritag that ties this io to the SGL pages.
17102  *
17103  * This routine will post the sgl pages for the IO that has the xritag
17104  * that is in the iocbq structure. The xritag is assigned during iocbq
17105  * creation and persists for as long as the driver is loaded.
17106  * if the caller has fewer than 256 scatter gather segments to map then
17107  * pdma_phys_addr1 should be 0.
17108  * If the caller needs to map more than 256 scatter gather segment then
17109  * pdma_phys_addr1 should be a valid physical address.
17110  * physical address for SGLs must be 64 byte aligned.
17111  * If you are going to map 2 SGL's then the first one must have 256 entries
17112  * the second sgl can have between 1 and 256 entries.
17113  *
17114  * Return codes:
17115  *      0 - Success
17116  *      -ENXIO, -ENOMEM - Failure
17117  **/
17118 int
17119 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
17120                 dma_addr_t pdma_phys_addr0,
17121                 dma_addr_t pdma_phys_addr1,
17122                 uint16_t xritag)
17123 {
17124         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
17125         LPFC_MBOXQ_t *mbox;
17126         int rc;
17127         uint32_t shdr_status, shdr_add_status;
17128         uint32_t mbox_tmo;
17129         union lpfc_sli4_cfg_shdr *shdr;
17130
17131         if (xritag == NO_XRI) {
17132                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17133                                 "0364 Invalid param:\n");
17134                 return -EINVAL;
17135         }
17136
17137         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17138         if (!mbox)
17139                 return -ENOMEM;
17140
17141         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17142                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17143                         sizeof(struct lpfc_mbx_post_sgl_pages) -
17144                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17145
17146         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
17147                                 &mbox->u.mqe.un.post_sgl_pages;
17148         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
17149         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
17150
17151         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
17152                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
17153         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
17154                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
17155
17156         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
17157                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
17158         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
17159                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
17160         if (!phba->sli4_hba.intr_enable)
17161                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17162         else {
17163                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
17164                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17165         }
17166         /* The IOCTL status is embedded in the mailbox subheader. */
17167         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
17168         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17169         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17170         if (rc != MBX_TIMEOUT)
17171                 mempool_free(mbox, phba->mbox_mem_pool);
17172         if (shdr_status || shdr_add_status || rc) {
17173                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17174                                 "2511 POST_SGL mailbox failed with "
17175                                 "status x%x add_status x%x, mbx status x%x\n",
17176                                 shdr_status, shdr_add_status, rc);
17177         }
17178         return 0;
17179 }
17180
17181 /**
17182  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
17183  * @phba: pointer to lpfc hba data structure.
17184  *
17185  * This routine is invoked to post rpi header templates to the
17186  * HBA consistent with the SLI-4 interface spec.  This routine
17187  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17188  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17189  *
17190  * Returns
17191  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
17192  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
17193  **/
17194 static uint16_t
17195 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
17196 {
17197         unsigned long xri;
17198
17199         /*
17200          * Fetch the next logical xri.  Because this index is logical,
17201          * the driver starts at 0 each time.
17202          */
17203         spin_lock_irq(&phba->hbalock);
17204         xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
17205                                  phba->sli4_hba.max_cfg_param.max_xri, 0);
17206         if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
17207                 spin_unlock_irq(&phba->hbalock);
17208                 return NO_XRI;
17209         } else {
17210                 set_bit(xri, phba->sli4_hba.xri_bmask);
17211                 phba->sli4_hba.max_cfg_param.xri_used++;
17212         }
17213         spin_unlock_irq(&phba->hbalock);
17214         return xri;
17215 }
17216
17217 /**
17218  * lpfc_sli4_free_xri - Release an xri for reuse.
17219  * @phba: pointer to lpfc hba data structure.
17220  * @xri: xri to release.
17221  *
17222  * This routine is invoked to release an xri to the pool of
17223  * available rpis maintained by the driver.
17224  **/
17225 static void
17226 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
17227 {
17228         if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
17229                 phba->sli4_hba.max_cfg_param.xri_used--;
17230         }
17231 }
17232
17233 /**
17234  * lpfc_sli4_free_xri - Release an xri for reuse.
17235  * @phba: pointer to lpfc hba data structure.
17236  * @xri: xri to release.
17237  *
17238  * This routine is invoked to release an xri to the pool of
17239  * available rpis maintained by the driver.
17240  **/
17241 void
17242 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
17243 {
17244         spin_lock_irq(&phba->hbalock);
17245         __lpfc_sli4_free_xri(phba, xri);
17246         spin_unlock_irq(&phba->hbalock);
17247 }
17248
17249 /**
17250  * lpfc_sli4_next_xritag - Get an xritag for the io
17251  * @phba: Pointer to HBA context object.
17252  *
17253  * This function gets an xritag for the iocb. If there is no unused xritag
17254  * it will return 0xffff.
17255  * The function returns the allocated xritag if successful, else returns zero.
17256  * Zero is not a valid xritag.
17257  * The caller is not required to hold any lock.
17258  **/
17259 uint16_t
17260 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
17261 {
17262         uint16_t xri_index;
17263
17264         xri_index = lpfc_sli4_alloc_xri(phba);
17265         if (xri_index == NO_XRI)
17266                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17267                                 "2004 Failed to allocate XRI.last XRITAG is %d"
17268                                 " Max XRI is %d, Used XRI is %d\n",
17269                                 xri_index,
17270                                 phba->sli4_hba.max_cfg_param.max_xri,
17271                                 phba->sli4_hba.max_cfg_param.xri_used);
17272         return xri_index;
17273 }
17274
17275 /**
17276  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
17277  * @phba: pointer to lpfc hba data structure.
17278  * @post_sgl_list: pointer to els sgl entry list.
17279  * @post_cnt: number of els sgl entries on the list.
17280  *
17281  * This routine is invoked to post a block of driver's sgl pages to the
17282  * HBA using non-embedded mailbox command. No Lock is held. This routine
17283  * is only called when the driver is loading and after all IO has been
17284  * stopped.
17285  **/
17286 static int
17287 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
17288                             struct list_head *post_sgl_list,
17289                             int post_cnt)
17290 {
17291         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
17292         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
17293         struct sgl_page_pairs *sgl_pg_pairs;
17294         void *viraddr;
17295         LPFC_MBOXQ_t *mbox;
17296         uint32_t reqlen, alloclen, pg_pairs;
17297         uint32_t mbox_tmo;
17298         uint16_t xritag_start = 0;
17299         int rc = 0;
17300         uint32_t shdr_status, shdr_add_status;
17301         union lpfc_sli4_cfg_shdr *shdr;
17302
17303         reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
17304                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
17305         if (reqlen > SLI4_PAGE_SIZE) {
17306                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17307                                 "2559 Block sgl registration required DMA "
17308                                 "size (%d) great than a page\n", reqlen);
17309                 return -ENOMEM;
17310         }
17311
17312         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17313         if (!mbox)
17314                 return -ENOMEM;
17315
17316         /* Allocate DMA memory and set up the non-embedded mailbox command */
17317         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17318                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
17319                          LPFC_SLI4_MBX_NEMBED);
17320
17321         if (alloclen < reqlen) {
17322                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17323                                 "0285 Allocated DMA memory size (%d) is "
17324                                 "less than the requested DMA memory "
17325                                 "size (%d)\n", alloclen, reqlen);
17326                 lpfc_sli4_mbox_cmd_free(phba, mbox);
17327                 return -ENOMEM;
17328         }
17329         /* Set up the SGL pages in the non-embedded DMA pages */
17330         viraddr = mbox->sge_array->addr[0];
17331         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
17332         sgl_pg_pairs = &sgl->sgl_pg_pairs;
17333
17334         pg_pairs = 0;
17335         list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
17336                 /* Set up the sge entry */
17337                 sgl_pg_pairs->sgl_pg0_addr_lo =
17338                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
17339                 sgl_pg_pairs->sgl_pg0_addr_hi =
17340                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
17341                 sgl_pg_pairs->sgl_pg1_addr_lo =
17342                                 cpu_to_le32(putPaddrLow(0));
17343                 sgl_pg_pairs->sgl_pg1_addr_hi =
17344                                 cpu_to_le32(putPaddrHigh(0));
17345
17346                 /* Keep the first xritag on the list */
17347                 if (pg_pairs == 0)
17348                         xritag_start = sglq_entry->sli4_xritag;
17349                 sgl_pg_pairs++;
17350                 pg_pairs++;
17351         }
17352
17353         /* Complete initialization and perform endian conversion. */
17354         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
17355         bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
17356         sgl->word0 = cpu_to_le32(sgl->word0);
17357
17358         if (!phba->sli4_hba.intr_enable)
17359                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17360         else {
17361                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
17362                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17363         }
17364         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
17365         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17366         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17367         if (rc != MBX_TIMEOUT)
17368                 lpfc_sli4_mbox_cmd_free(phba, mbox);
17369         if (shdr_status || shdr_add_status || rc) {
17370                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17371                                 "2513 POST_SGL_BLOCK mailbox command failed "
17372                                 "status x%x add_status x%x mbx status x%x\n",
17373                                 shdr_status, shdr_add_status, rc);
17374                 rc = -ENXIO;
17375         }
17376         return rc;
17377 }
17378
17379 /**
17380  * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
17381  * @phba: pointer to lpfc hba data structure.
17382  * @nblist: pointer to nvme buffer list.
17383  * @count: number of scsi buffers on the list.
17384  *
17385  * This routine is invoked to post a block of @count scsi sgl pages from a
17386  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
17387  * No Lock is held.
17388  *
17389  **/
17390 static int
17391 lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
17392                             int count)
17393 {
17394         struct lpfc_io_buf *lpfc_ncmd;
17395         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
17396         struct sgl_page_pairs *sgl_pg_pairs;
17397         void *viraddr;
17398         LPFC_MBOXQ_t *mbox;
17399         uint32_t reqlen, alloclen, pg_pairs;
17400         uint32_t mbox_tmo;
17401         uint16_t xritag_start = 0;
17402         int rc = 0;
17403         uint32_t shdr_status, shdr_add_status;
17404         dma_addr_t pdma_phys_bpl1;
17405         union lpfc_sli4_cfg_shdr *shdr;
17406
17407         /* Calculate the requested length of the dma memory */
17408         reqlen = count * sizeof(struct sgl_page_pairs) +
17409                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
17410         if (reqlen > SLI4_PAGE_SIZE) {
17411                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
17412                                 "6118 Block sgl registration required DMA "
17413                                 "size (%d) great than a page\n", reqlen);
17414                 return -ENOMEM;
17415         }
17416         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17417         if (!mbox) {
17418                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17419                                 "6119 Failed to allocate mbox cmd memory\n");
17420                 return -ENOMEM;
17421         }
17422
17423         /* Allocate DMA memory and set up the non-embedded mailbox command */
17424         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17425                                     LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17426                                     reqlen, LPFC_SLI4_MBX_NEMBED);
17427
17428         if (alloclen < reqlen) {
17429                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17430                                 "6120 Allocated DMA memory size (%d) is "
17431                                 "less than the requested DMA memory "
17432                                 "size (%d)\n", alloclen, reqlen);
17433                 lpfc_sli4_mbox_cmd_free(phba, mbox);
17434                 return -ENOMEM;
17435         }
17436
17437         /* Get the first SGE entry from the non-embedded DMA memory */
17438         viraddr = mbox->sge_array->addr[0];
17439
17440         /* Set up the SGL pages in the non-embedded DMA pages */
17441         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
17442         sgl_pg_pairs = &sgl->sgl_pg_pairs;
17443
17444         pg_pairs = 0;
17445         list_for_each_entry(lpfc_ncmd, nblist, list) {
17446                 /* Set up the sge entry */
17447                 sgl_pg_pairs->sgl_pg0_addr_lo =
17448                         cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
17449                 sgl_pg_pairs->sgl_pg0_addr_hi =
17450                         cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
17451                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
17452                         pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
17453                                                 SGL_PAGE_SIZE;
17454                 else
17455                         pdma_phys_bpl1 = 0;
17456                 sgl_pg_pairs->sgl_pg1_addr_lo =
17457                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
17458                 sgl_pg_pairs->sgl_pg1_addr_hi =
17459                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
17460                 /* Keep the first xritag on the list */
17461                 if (pg_pairs == 0)
17462                         xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
17463                 sgl_pg_pairs++;
17464                 pg_pairs++;
17465         }
17466         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
17467         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
17468         /* Perform endian conversion if necessary */
17469         sgl->word0 = cpu_to_le32(sgl->word0);
17470
17471         if (!phba->sli4_hba.intr_enable) {
17472                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17473         } else {
17474                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
17475                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17476         }
17477         shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
17478         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17479         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17480         if (rc != MBX_TIMEOUT)
17481                 lpfc_sli4_mbox_cmd_free(phba, mbox);
17482         if (shdr_status || shdr_add_status || rc) {
17483                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17484                                 "6125 POST_SGL_BLOCK mailbox command failed "
17485                                 "status x%x add_status x%x mbx status x%x\n",
17486                                 shdr_status, shdr_add_status, rc);
17487                 rc = -ENXIO;
17488         }
17489         return rc;
17490 }
17491
17492 /**
17493  * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
17494  * @phba: pointer to lpfc hba data structure.
17495  * @post_nblist: pointer to the nvme buffer list.
17496  * @sb_count: number of nvme buffers.
17497  *
17498  * This routine walks a list of nvme buffers that was passed in. It attempts
17499  * to construct blocks of nvme buffer sgls which contains contiguous xris and
17500  * uses the non-embedded SGL block post mailbox commands to post to the port.
17501  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
17502  * embedded SGL post mailbox command for posting. The @post_nblist passed in
17503  * must be local list, thus no lock is needed when manipulate the list.
17504  *
17505  * Returns: 0 = failure, non-zero number of successfully posted buffers.
17506  **/
17507 int
17508 lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
17509                            struct list_head *post_nblist, int sb_count)
17510 {
17511         struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
17512         int status, sgl_size;
17513         int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
17514         dma_addr_t pdma_phys_sgl1;
17515         int last_xritag = NO_XRI;
17516         int cur_xritag;
17517         LIST_HEAD(prep_nblist);
17518         LIST_HEAD(blck_nblist);
17519         LIST_HEAD(nvme_nblist);
17520
17521         /* sanity check */
17522         if (sb_count <= 0)
17523                 return -EINVAL;
17524
17525         sgl_size = phba->cfg_sg_dma_buf_size;
17526         list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
17527                 list_del_init(&lpfc_ncmd->list);
17528                 block_cnt++;
17529                 if ((last_xritag != NO_XRI) &&
17530                     (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
17531                         /* a hole in xri block, form a sgl posting block */
17532                         list_splice_init(&prep_nblist, &blck_nblist);
17533                         post_cnt = block_cnt - 1;
17534                         /* prepare list for next posting block */
17535                         list_add_tail(&lpfc_ncmd->list, &prep_nblist);
17536                         block_cnt = 1;
17537                 } else {
17538                         /* prepare list for next posting block */
17539                         list_add_tail(&lpfc_ncmd->list, &prep_nblist);
17540                         /* enough sgls for non-embed sgl mbox command */
17541                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
17542                                 list_splice_init(&prep_nblist, &blck_nblist);
17543                                 post_cnt = block_cnt;
17544                                 block_cnt = 0;
17545                         }
17546                 }
17547                 num_posting++;
17548                 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
17549
17550                 /* end of repost sgl list condition for NVME buffers */
17551                 if (num_posting == sb_count) {
17552                         if (post_cnt == 0) {
17553                                 /* last sgl posting block */
17554                                 list_splice_init(&prep_nblist, &blck_nblist);
17555                                 post_cnt = block_cnt;
17556                         } else if (block_cnt == 1) {
17557                                 /* last single sgl with non-contiguous xri */
17558                                 if (sgl_size > SGL_PAGE_SIZE)
17559                                         pdma_phys_sgl1 =
17560                                                 lpfc_ncmd->dma_phys_sgl +
17561                                                 SGL_PAGE_SIZE;
17562                                 else
17563                                         pdma_phys_sgl1 = 0;
17564                                 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
17565                                 status = lpfc_sli4_post_sgl(
17566                                                 phba, lpfc_ncmd->dma_phys_sgl,
17567                                                 pdma_phys_sgl1, cur_xritag);
17568                                 if (status) {
17569                                         /* Post error.  Buffer unavailable. */
17570                                         lpfc_ncmd->flags |=
17571                                                 LPFC_SBUF_NOT_POSTED;
17572                                 } else {
17573                                         /* Post success. Bffer available. */
17574                                         lpfc_ncmd->flags &=
17575                                                 ~LPFC_SBUF_NOT_POSTED;
17576                                         lpfc_ncmd->status = IOSTAT_SUCCESS;
17577                                         num_posted++;
17578                                 }
17579                                 /* success, put on NVME buffer sgl list */
17580                                 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
17581                         }
17582                 }
17583
17584                 /* continue until a nembed page worth of sgls */
17585                 if (post_cnt == 0)
17586                         continue;
17587
17588                 /* post block of NVME buffer list sgls */
17589                 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
17590                                                      post_cnt);
17591
17592                 /* don't reset xirtag due to hole in xri block */
17593                 if (block_cnt == 0)
17594                         last_xritag = NO_XRI;
17595
17596                 /* reset NVME buffer post count for next round of posting */
17597                 post_cnt = 0;
17598
17599                 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
17600                 while (!list_empty(&blck_nblist)) {
17601                         list_remove_head(&blck_nblist, lpfc_ncmd,
17602                                          struct lpfc_io_buf, list);
17603                         if (status) {
17604                                 /* Post error.  Mark buffer unavailable. */
17605                                 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
17606                         } else {
17607                                 /* Post success, Mark buffer available. */
17608                                 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
17609                                 lpfc_ncmd->status = IOSTAT_SUCCESS;
17610                                 num_posted++;
17611                         }
17612                         list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
17613                 }
17614         }
17615         /* Push NVME buffers with sgl posted to the available list */
17616         lpfc_io_buf_replenish(phba, &nvme_nblist);
17617
17618         return num_posted;
17619 }
17620
17621 /**
17622  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
17623  * @phba: pointer to lpfc_hba struct that the frame was received on
17624  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17625  *
17626  * This function checks the fields in the @fc_hdr to see if the FC frame is a
17627  * valid type of frame that the LPFC driver will handle. This function will
17628  * return a zero if the frame is a valid frame or a non zero value when the
17629  * frame does not pass the check.
17630  **/
17631 static int
17632 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
17633 {
17634         /*  make rctl_names static to save stack space */
17635         struct fc_vft_header *fc_vft_hdr;
17636         uint32_t *header = (uint32_t *) fc_hdr;
17637
17638 #define FC_RCTL_MDS_DIAGS       0xF4
17639
17640         switch (fc_hdr->fh_r_ctl) {
17641         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
17642         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
17643         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
17644         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
17645         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
17646         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
17647         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
17648         case FC_RCTL_DD_CMD_STATUS:     /* command status */
17649         case FC_RCTL_ELS_REQ:   /* extended link services request */
17650         case FC_RCTL_ELS_REP:   /* extended link services reply */
17651         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
17652         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
17653         case FC_RCTL_BA_NOP:    /* basic link service NOP */
17654         case FC_RCTL_BA_ABTS:   /* basic link service abort */
17655         case FC_RCTL_BA_RMC:    /* remove connection */
17656         case FC_RCTL_BA_ACC:    /* basic accept */
17657         case FC_RCTL_BA_RJT:    /* basic reject */
17658         case FC_RCTL_BA_PRMT:
17659         case FC_RCTL_ACK_1:     /* acknowledge_1 */
17660         case FC_RCTL_ACK_0:     /* acknowledge_0 */
17661         case FC_RCTL_P_RJT:     /* port reject */
17662         case FC_RCTL_F_RJT:     /* fabric reject */
17663         case FC_RCTL_P_BSY:     /* port busy */
17664         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
17665         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
17666         case FC_RCTL_LCR:       /* link credit reset */
17667         case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
17668         case FC_RCTL_END:       /* end */
17669                 break;
17670         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
17671                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17672                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
17673                 return lpfc_fc_frame_check(phba, fc_hdr);
17674         default:
17675                 goto drop;
17676         }
17677
17678         switch (fc_hdr->fh_type) {
17679         case FC_TYPE_BLS:
17680         case FC_TYPE_ELS:
17681         case FC_TYPE_FCP:
17682         case FC_TYPE_CT:
17683         case FC_TYPE_NVME:
17684                 break;
17685         case FC_TYPE_IP:
17686         case FC_TYPE_ILS:
17687         default:
17688                 goto drop;
17689         }
17690
17691         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
17692                         "2538 Received frame rctl:x%x, type:x%x, "
17693                         "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
17694                         fc_hdr->fh_r_ctl, fc_hdr->fh_type,
17695                         be32_to_cpu(header[0]), be32_to_cpu(header[1]),
17696                         be32_to_cpu(header[2]), be32_to_cpu(header[3]),
17697                         be32_to_cpu(header[4]), be32_to_cpu(header[5]),
17698                         be32_to_cpu(header[6]));
17699         return 0;
17700 drop:
17701         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
17702                         "2539 Dropped frame rctl:x%x type:x%x\n",
17703                         fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17704         return 1;
17705 }
17706
17707 /**
17708  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
17709  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17710  *
17711  * This function processes the FC header to retrieve the VFI from the VF
17712  * header, if one exists. This function will return the VFI if one exists
17713  * or 0 if no VSAN Header exists.
17714  **/
17715 static uint32_t
17716 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
17717 {
17718         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17719
17720         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
17721                 return 0;
17722         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
17723 }
17724
17725 /**
17726  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
17727  * @phba: Pointer to the HBA structure to search for the vport on
17728  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17729  * @fcfi: The FC Fabric ID that the frame came from
17730  * @did: Destination ID to match against
17731  *
17732  * This function searches the @phba for a vport that matches the content of the
17733  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
17734  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
17735  * returns the matching vport pointer or NULL if unable to match frame to a
17736  * vport.
17737  **/
17738 static struct lpfc_vport *
17739 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
17740                        uint16_t fcfi, uint32_t did)
17741 {
17742         struct lpfc_vport **vports;
17743         struct lpfc_vport *vport = NULL;
17744         int i;
17745
17746         if (did == Fabric_DID)
17747                 return phba->pport;
17748         if ((phba->pport->fc_flag & FC_PT2PT) &&
17749                 !(phba->link_state == LPFC_HBA_READY))
17750                 return phba->pport;
17751
17752         vports = lpfc_create_vport_work_array(phba);
17753         if (vports != NULL) {
17754                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17755                         if (phba->fcf.fcfi == fcfi &&
17756                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
17757                             vports[i]->fc_myDID == did) {
17758                                 vport = vports[i];
17759                                 break;
17760                         }
17761                 }
17762         }
17763         lpfc_destroy_vport_work_array(phba, vports);
17764         return vport;
17765 }
17766
17767 /**
17768  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
17769  * @vport: The vport to work on.
17770  *
17771  * This function updates the receive sequence time stamp for this vport. The
17772  * receive sequence time stamp indicates the time that the last frame of the
17773  * the sequence that has been idle for the longest amount of time was received.
17774  * the driver uses this time stamp to indicate if any received sequences have
17775  * timed out.
17776  **/
17777 static void
17778 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17779 {
17780         struct lpfc_dmabuf *h_buf;
17781         struct hbq_dmabuf *dmabuf = NULL;
17782
17783         /* get the oldest sequence on the rcv list */
17784         h_buf = list_get_first(&vport->rcv_buffer_list,
17785                                struct lpfc_dmabuf, list);
17786         if (!h_buf)
17787                 return;
17788         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17789         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17790 }
17791
17792 /**
17793  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17794  * @vport: The vport that the received sequences were sent to.
17795  *
17796  * This function cleans up all outstanding received sequences. This is called
17797  * by the driver when a link event or user action invalidates all the received
17798  * sequences.
17799  **/
17800 void
17801 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17802 {
17803         struct lpfc_dmabuf *h_buf, *hnext;
17804         struct lpfc_dmabuf *d_buf, *dnext;
17805         struct hbq_dmabuf *dmabuf = NULL;
17806
17807         /* start with the oldest sequence on the rcv list */
17808         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17809                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17810                 list_del_init(&dmabuf->hbuf.list);
17811                 list_for_each_entry_safe(d_buf, dnext,
17812                                          &dmabuf->dbuf.list, list) {
17813                         list_del_init(&d_buf->list);
17814                         lpfc_in_buf_free(vport->phba, d_buf);
17815                 }
17816                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17817         }
17818 }
17819
17820 /**
17821  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17822  * @vport: The vport that the received sequences were sent to.
17823  *
17824  * This function determines whether any received sequences have timed out by
17825  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17826  * indicates that there is at least one timed out sequence this routine will
17827  * go through the received sequences one at a time from most inactive to most
17828  * active to determine which ones need to be cleaned up. Once it has determined
17829  * that a sequence needs to be cleaned up it will simply free up the resources
17830  * without sending an abort.
17831  **/
17832 void
17833 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17834 {
17835         struct lpfc_dmabuf *h_buf, *hnext;
17836         struct lpfc_dmabuf *d_buf, *dnext;
17837         struct hbq_dmabuf *dmabuf = NULL;
17838         unsigned long timeout;
17839         int abort_count = 0;
17840
17841         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17842                    vport->rcv_buffer_time_stamp);
17843         if (list_empty(&vport->rcv_buffer_list) ||
17844             time_before(jiffies, timeout))
17845                 return;
17846         /* start with the oldest sequence on the rcv list */
17847         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17848                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17849                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17850                            dmabuf->time_stamp);
17851                 if (time_before(jiffies, timeout))
17852                         break;
17853                 abort_count++;
17854                 list_del_init(&dmabuf->hbuf.list);
17855                 list_for_each_entry_safe(d_buf, dnext,
17856                                          &dmabuf->dbuf.list, list) {
17857                         list_del_init(&d_buf->list);
17858                         lpfc_in_buf_free(vport->phba, d_buf);
17859                 }
17860                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17861         }
17862         if (abort_count)
17863                 lpfc_update_rcv_time_stamp(vport);
17864 }
17865
17866 /**
17867  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17868  * @vport: pointer to a vitural port
17869  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17870  *
17871  * This function searches through the existing incomplete sequences that have
17872  * been sent to this @vport. If the frame matches one of the incomplete
17873  * sequences then the dbuf in the @dmabuf is added to the list of frames that
17874  * make up that sequence. If no sequence is found that matches this frame then
17875  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17876  * This function returns a pointer to the first dmabuf in the sequence list that
17877  * the frame was linked to.
17878  **/
17879 static struct hbq_dmabuf *
17880 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17881 {
17882         struct fc_frame_header *new_hdr;
17883         struct fc_frame_header *temp_hdr;
17884         struct lpfc_dmabuf *d_buf;
17885         struct lpfc_dmabuf *h_buf;
17886         struct hbq_dmabuf *seq_dmabuf = NULL;
17887         struct hbq_dmabuf *temp_dmabuf = NULL;
17888         uint8_t found = 0;
17889
17890         INIT_LIST_HEAD(&dmabuf->dbuf.list);
17891         dmabuf->time_stamp = jiffies;
17892         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17893
17894         /* Use the hdr_buf to find the sequence that this frame belongs to */
17895         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17896                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17897                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17898                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17899                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17900                         continue;
17901                 /* found a pending sequence that matches this frame */
17902                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17903                 break;
17904         }
17905         if (!seq_dmabuf) {
17906                 /*
17907                  * This indicates first frame received for this sequence.
17908                  * Queue the buffer on the vport's rcv_buffer_list.
17909                  */
17910                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17911                 lpfc_update_rcv_time_stamp(vport);
17912                 return dmabuf;
17913         }
17914         temp_hdr = seq_dmabuf->hbuf.virt;
17915         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17916                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
17917                 list_del_init(&seq_dmabuf->hbuf.list);
17918                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17919                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17920                 lpfc_update_rcv_time_stamp(vport);
17921                 return dmabuf;
17922         }
17923         /* move this sequence to the tail to indicate a young sequence */
17924         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17925         seq_dmabuf->time_stamp = jiffies;
17926         lpfc_update_rcv_time_stamp(vport);
17927         if (list_empty(&seq_dmabuf->dbuf.list)) {
17928                 temp_hdr = dmabuf->hbuf.virt;
17929                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17930                 return seq_dmabuf;
17931         }
17932         /* find the correct place in the sequence to insert this frame */
17933         d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17934         while (!found) {
17935                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17936                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17937                 /*
17938                  * If the frame's sequence count is greater than the frame on
17939                  * the list then insert the frame right after this frame
17940                  */
17941                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17942                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
17943                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
17944                         found = 1;
17945                         break;
17946                 }
17947
17948                 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17949                         break;
17950                 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
17951         }
17952
17953         if (found)
17954                 return seq_dmabuf;
17955         return NULL;
17956 }
17957
17958 /**
17959  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17960  * @vport: pointer to a vitural port
17961  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17962  *
17963  * This function tries to abort from the partially assembed sequence, described
17964  * by the information from basic abbort @dmabuf. It checks to see whether such
17965  * partially assembled sequence held by the driver. If so, it shall free up all
17966  * the frames from the partially assembled sequence.
17967  *
17968  * Return
17969  * true  -- if there is matching partially assembled sequence present and all
17970  *          the frames freed with the sequence;
17971  * false -- if there is no matching partially assembled sequence present so
17972  *          nothing got aborted in the lower layer driver
17973  **/
17974 static bool
17975 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17976                             struct hbq_dmabuf *dmabuf)
17977 {
17978         struct fc_frame_header *new_hdr;
17979         struct fc_frame_header *temp_hdr;
17980         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17981         struct hbq_dmabuf *seq_dmabuf = NULL;
17982
17983         /* Use the hdr_buf to find the sequence that matches this frame */
17984         INIT_LIST_HEAD(&dmabuf->dbuf.list);
17985         INIT_LIST_HEAD(&dmabuf->hbuf.list);
17986         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17987         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17988                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17989                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17990                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17991                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17992                         continue;
17993                 /* found a pending sequence that matches this frame */
17994                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17995                 break;
17996         }
17997
17998         /* Free up all the frames from the partially assembled sequence */
17999         if (seq_dmabuf) {
18000                 list_for_each_entry_safe(d_buf, n_buf,
18001                                          &seq_dmabuf->dbuf.list, list) {
18002                         list_del_init(&d_buf->list);
18003                         lpfc_in_buf_free(vport->phba, d_buf);
18004                 }
18005                 return true;
18006         }
18007         return false;
18008 }
18009
18010 /**
18011  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
18012  * @vport: pointer to a vitural port
18013  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18014  *
18015  * This function tries to abort from the assembed sequence from upper level
18016  * protocol, described by the information from basic abbort @dmabuf. It
18017  * checks to see whether such pending context exists at upper level protocol.
18018  * If so, it shall clean up the pending context.
18019  *
18020  * Return
18021  * true  -- if there is matching pending context of the sequence cleaned
18022  *          at ulp;
18023  * false -- if there is no matching pending context of the sequence present
18024  *          at ulp.
18025  **/
18026 static bool
18027 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18028 {
18029         struct lpfc_hba *phba = vport->phba;
18030         int handled;
18031
18032         /* Accepting abort at ulp with SLI4 only */
18033         if (phba->sli_rev < LPFC_SLI_REV4)
18034                 return false;
18035
18036         /* Register all caring upper level protocols to attend abort */
18037         handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
18038         if (handled)
18039                 return true;
18040
18041         return false;
18042 }
18043
18044 /**
18045  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
18046  * @phba: Pointer to HBA context object.
18047  * @cmd_iocbq: pointer to the command iocbq structure.
18048  * @rsp_iocbq: pointer to the response iocbq structure.
18049  *
18050  * This function handles the sequence abort response iocb command complete
18051  * event. It properly releases the memory allocated to the sequence abort
18052  * accept iocb.
18053  **/
18054 static void
18055 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
18056                              struct lpfc_iocbq *cmd_iocbq,
18057                              struct lpfc_iocbq *rsp_iocbq)
18058 {
18059         struct lpfc_nodelist *ndlp;
18060
18061         if (cmd_iocbq) {
18062                 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
18063                 lpfc_nlp_put(ndlp);
18064                 lpfc_nlp_not_used(ndlp);
18065                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
18066         }
18067
18068         /* Failure means BLS ABORT RSP did not get delivered to remote node*/
18069         if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
18070                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18071                         "3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
18072                         rsp_iocbq->iocb.ulpStatus,
18073                         rsp_iocbq->iocb.un.ulpWord[4]);
18074 }
18075
18076 /**
18077  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
18078  * @phba: Pointer to HBA context object.
18079  * @xri: xri id in transaction.
18080  *
18081  * This function validates the xri maps to the known range of XRIs allocated an
18082  * used by the driver.
18083  **/
18084 uint16_t
18085 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
18086                       uint16_t xri)
18087 {
18088         uint16_t i;
18089
18090         for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
18091                 if (xri == phba->sli4_hba.xri_ids[i])
18092                         return i;
18093         }
18094         return NO_XRI;
18095 }
18096
18097 /**
18098  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
18099  * @vport: pointer to a vitural port.
18100  * @fc_hdr: pointer to a FC frame header.
18101  * @aborted: was the partially assembled receive sequence successfully aborted
18102  *
18103  * This function sends a basic response to a previous unsol sequence abort
18104  * event after aborting the sequence handling.
18105  **/
18106 void
18107 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
18108                         struct fc_frame_header *fc_hdr, bool aborted)
18109 {
18110         struct lpfc_hba *phba = vport->phba;
18111         struct lpfc_iocbq *ctiocb = NULL;
18112         struct lpfc_nodelist *ndlp;
18113         uint16_t oxid, rxid, xri, lxri;
18114         uint32_t sid, fctl;
18115         IOCB_t *icmd;
18116         int rc;
18117
18118         if (!lpfc_is_link_up(phba))
18119                 return;
18120
18121         sid = sli4_sid_from_fc_hdr(fc_hdr);
18122         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
18123         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
18124
18125         ndlp = lpfc_findnode_did(vport, sid);
18126         if (!ndlp) {
18127                 ndlp = lpfc_nlp_init(vport, sid);
18128                 if (!ndlp) {
18129                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
18130                                          "1268 Failed to allocate ndlp for "
18131                                          "oxid:x%x SID:x%x\n", oxid, sid);
18132                         return;
18133                 }
18134                 /* Put ndlp onto pport node list */
18135                 lpfc_enqueue_node(vport, ndlp);
18136         }
18137
18138         /* Allocate buffer for rsp iocb */
18139         ctiocb = lpfc_sli_get_iocbq(phba);
18140         if (!ctiocb)
18141                 return;
18142
18143         /* Extract the F_CTL field from FC_HDR */
18144         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
18145
18146         icmd = &ctiocb->iocb;
18147         icmd->un.xseq64.bdl.bdeSize = 0;
18148         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
18149         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
18150         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
18151         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
18152
18153         /* Fill in the rest of iocb fields */
18154         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
18155         icmd->ulpBdeCount = 0;
18156         icmd->ulpLe = 1;
18157         icmd->ulpClass = CLASS3;
18158         icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
18159         ctiocb->context1 = lpfc_nlp_get(ndlp);
18160         if (!ctiocb->context1) {
18161                 lpfc_sli_release_iocbq(phba, ctiocb);
18162                 return;
18163         }
18164
18165         ctiocb->vport = phba->pport;
18166         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
18167         ctiocb->sli4_lxritag = NO_XRI;
18168         ctiocb->sli4_xritag = NO_XRI;
18169
18170         if (fctl & FC_FC_EX_CTX)
18171                 /* Exchange responder sent the abort so we
18172                  * own the oxid.
18173                  */
18174                 xri = oxid;
18175         else
18176                 xri = rxid;
18177         lxri = lpfc_sli4_xri_inrange(phba, xri);
18178         if (lxri != NO_XRI)
18179                 lpfc_set_rrq_active(phba, ndlp, lxri,
18180                         (xri == oxid) ? rxid : oxid, 0);
18181         /* For BA_ABTS from exchange responder, if the logical xri with
18182          * the oxid maps to the FCP XRI range, the port no longer has
18183          * that exchange context, send a BLS_RJT. Override the IOCB for
18184          * a BA_RJT.
18185          */
18186         if ((fctl & FC_FC_EX_CTX) &&
18187             (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
18188                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
18189                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
18190                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
18191                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
18192         }
18193
18194         /* If BA_ABTS failed to abort a partially assembled receive sequence,
18195          * the driver no longer has that exchange, send a BLS_RJT. Override
18196          * the IOCB for a BA_RJT.
18197          */
18198         if (aborted == false) {
18199                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
18200                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
18201                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
18202                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
18203         }
18204
18205         if (fctl & FC_FC_EX_CTX) {
18206                 /* ABTS sent by responder to CT exchange, construction
18207                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
18208                  * field and RX_ID from ABTS for RX_ID field.
18209                  */
18210                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
18211         } else {
18212                 /* ABTS sent by initiator to CT exchange, construction
18213                  * of BA_ACC will need to allocate a new XRI as for the
18214                  * XRI_TAG field.
18215                  */
18216                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
18217         }
18218         bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
18219         bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
18220
18221         /* Xmit CT abts response on exchange <xid> */
18222         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
18223                          "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
18224                          icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
18225
18226         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
18227         if (rc == IOCB_ERROR) {
18228                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
18229                                  "2925 Failed to issue CT ABTS RSP x%x on "
18230                                  "xri x%x, Data x%x\n",
18231                                  icmd->un.xseq64.w5.hcsw.Rctl, oxid,
18232                                  phba->link_state);
18233                 lpfc_nlp_put(ndlp);
18234                 ctiocb->context1 = NULL;
18235                 lpfc_sli_release_iocbq(phba, ctiocb);
18236         }
18237 }
18238
18239 /**
18240  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
18241  * @vport: Pointer to the vport on which this sequence was received
18242  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18243  *
18244  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
18245  * receive sequence is only partially assembed by the driver, it shall abort
18246  * the partially assembled frames for the sequence. Otherwise, if the
18247  * unsolicited receive sequence has been completely assembled and passed to
18248  * the Upper Layer Protocol (ULP), it then mark the per oxid status for the
18249  * unsolicited sequence has been aborted. After that, it will issue a basic
18250  * accept to accept the abort.
18251  **/
18252 static void
18253 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
18254                              struct hbq_dmabuf *dmabuf)
18255 {
18256         struct lpfc_hba *phba = vport->phba;
18257         struct fc_frame_header fc_hdr;
18258         uint32_t fctl;
18259         bool aborted;
18260
18261         /* Make a copy of fc_hdr before the dmabuf being released */
18262         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
18263         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
18264
18265         if (fctl & FC_FC_EX_CTX) {
18266                 /* ABTS by responder to exchange, no cleanup needed */
18267                 aborted = true;
18268         } else {
18269                 /* ABTS by initiator to exchange, need to do cleanup */
18270                 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
18271                 if (aborted == false)
18272                         aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
18273         }
18274         lpfc_in_buf_free(phba, &dmabuf->dbuf);
18275
18276         if (phba->nvmet_support) {
18277                 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
18278                 return;
18279         }
18280
18281         /* Respond with BA_ACC or BA_RJT accordingly */
18282         lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
18283 }
18284
18285 /**
18286  * lpfc_seq_complete - Indicates if a sequence is complete
18287  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18288  *
18289  * This function checks the sequence, starting with the frame described by
18290  * @dmabuf, to see if all the frames associated with this sequence are present.
18291  * the frames associated with this sequence are linked to the @dmabuf using the
18292  * dbuf list. This function looks for two major things. 1) That the first frame
18293  * has a sequence count of zero. 2) There is a frame with last frame of sequence
18294  * set. 3) That there are no holes in the sequence count. The function will
18295  * return 1 when the sequence is complete, otherwise it will return 0.
18296  **/
18297 static int
18298 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
18299 {
18300         struct fc_frame_header *hdr;
18301         struct lpfc_dmabuf *d_buf;
18302         struct hbq_dmabuf *seq_dmabuf;
18303         uint32_t fctl;
18304         int seq_count = 0;
18305
18306         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18307         /* make sure first fame of sequence has a sequence count of zero */
18308         if (hdr->fh_seq_cnt != seq_count)
18309                 return 0;
18310         fctl = (hdr->fh_f_ctl[0] << 16 |
18311                 hdr->fh_f_ctl[1] << 8 |
18312                 hdr->fh_f_ctl[2]);
18313         /* If last frame of sequence we can return success. */
18314         if (fctl & FC_FC_END_SEQ)
18315                 return 1;
18316         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
18317                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18318                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18319                 /* If there is a hole in the sequence count then fail. */
18320                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
18321                         return 0;
18322                 fctl = (hdr->fh_f_ctl[0] << 16 |
18323                         hdr->fh_f_ctl[1] << 8 |
18324                         hdr->fh_f_ctl[2]);
18325                 /* If last frame of sequence we can return success. */
18326                 if (fctl & FC_FC_END_SEQ)
18327                         return 1;
18328         }
18329         return 0;
18330 }
18331
18332 /**
18333  * lpfc_prep_seq - Prep sequence for ULP processing
18334  * @vport: Pointer to the vport on which this sequence was received
18335  * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence
18336  *
18337  * This function takes a sequence, described by a list of frames, and creates
18338  * a list of iocbq structures to describe the sequence. This iocbq list will be
18339  * used to issue to the generic unsolicited sequence handler. This routine
18340  * returns a pointer to the first iocbq in the list. If the function is unable
18341  * to allocate an iocbq then it throw out the received frames that were not
18342  * able to be described and return a pointer to the first iocbq. If unable to
18343  * allocate any iocbqs (including the first) this function will return NULL.
18344  **/
18345 static struct lpfc_iocbq *
18346 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
18347 {
18348         struct hbq_dmabuf *hbq_buf;
18349         struct lpfc_dmabuf *d_buf, *n_buf;
18350         struct lpfc_iocbq *first_iocbq, *iocbq;
18351         struct fc_frame_header *fc_hdr;
18352         uint32_t sid;
18353         uint32_t len, tot_len;
18354         struct ulp_bde64 *pbde;
18355
18356         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18357         /* remove from receive buffer list */
18358         list_del_init(&seq_dmabuf->hbuf.list);
18359         lpfc_update_rcv_time_stamp(vport);
18360         /* get the Remote Port's SID */
18361         sid = sli4_sid_from_fc_hdr(fc_hdr);
18362         tot_len = 0;
18363         /* Get an iocbq struct to fill in. */
18364         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
18365         if (first_iocbq) {
18366                 /* Initialize the first IOCB. */
18367                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
18368                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
18369                 first_iocbq->vport = vport;
18370
18371                 /* Check FC Header to see what TYPE of frame we are rcv'ing */
18372                 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
18373                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
18374                         first_iocbq->iocb.un.rcvels.parmRo =
18375                                 sli4_did_from_fc_hdr(fc_hdr);
18376                         first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
18377                 } else
18378                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
18379                 first_iocbq->iocb.ulpContext = NO_XRI;
18380                 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
18381                         be16_to_cpu(fc_hdr->fh_ox_id);
18382                 /* iocbq is prepped for internal consumption.  Physical vpi. */
18383                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
18384                         vport->phba->vpi_ids[vport->vpi];
18385                 /* put the first buffer into the first IOCBq */
18386                 tot_len = bf_get(lpfc_rcqe_length,
18387                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
18388
18389                 first_iocbq->context2 = &seq_dmabuf->dbuf;
18390                 first_iocbq->context3 = NULL;
18391                 first_iocbq->iocb.ulpBdeCount = 1;
18392                 if (tot_len > LPFC_DATA_BUF_SIZE)
18393                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
18394                                                         LPFC_DATA_BUF_SIZE;
18395                 else
18396                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
18397
18398                 first_iocbq->iocb.un.rcvels.remoteID = sid;
18399
18400                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
18401         }
18402         iocbq = first_iocbq;
18403         /*
18404          * Each IOCBq can have two Buffers assigned, so go through the list
18405          * of buffers for this sequence and save two buffers in each IOCBq
18406          */
18407         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
18408                 if (!iocbq) {
18409                         lpfc_in_buf_free(vport->phba, d_buf);
18410                         continue;
18411                 }
18412                 if (!iocbq->context3) {
18413                         iocbq->context3 = d_buf;
18414                         iocbq->iocb.ulpBdeCount++;
18415                         /* We need to get the size out of the right CQE */
18416                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18417                         len = bf_get(lpfc_rcqe_length,
18418                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
18419                         pbde = (struct ulp_bde64 *)
18420                                         &iocbq->iocb.unsli3.sli3Words[4];
18421                         if (len > LPFC_DATA_BUF_SIZE)
18422                                 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
18423                         else
18424                                 pbde->tus.f.bdeSize = len;
18425
18426                         iocbq->iocb.unsli3.rcvsli3.acc_len += len;
18427                         tot_len += len;
18428                 } else {
18429                         iocbq = lpfc_sli_get_iocbq(vport->phba);
18430                         if (!iocbq) {
18431                                 if (first_iocbq) {
18432                                         first_iocbq->iocb.ulpStatus =
18433                                                         IOSTAT_FCP_RSP_ERROR;
18434                                         first_iocbq->iocb.un.ulpWord[4] =
18435                                                         IOERR_NO_RESOURCES;
18436                                 }
18437                                 lpfc_in_buf_free(vport->phba, d_buf);
18438                                 continue;
18439                         }
18440                         /* We need to get the size out of the right CQE */
18441                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18442                         len = bf_get(lpfc_rcqe_length,
18443                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
18444                         iocbq->context2 = d_buf;
18445                         iocbq->context3 = NULL;
18446                         iocbq->iocb.ulpBdeCount = 1;
18447                         if (len > LPFC_DATA_BUF_SIZE)
18448                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
18449                                                         LPFC_DATA_BUF_SIZE;
18450                         else
18451                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
18452
18453                         tot_len += len;
18454                         iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
18455
18456                         iocbq->iocb.un.rcvels.remoteID = sid;
18457                         list_add_tail(&iocbq->list, &first_iocbq->list);
18458                 }
18459         }
18460         /* Free the sequence's header buffer */
18461         if (!first_iocbq)
18462                 lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
18463
18464         return first_iocbq;
18465 }
18466
18467 static void
18468 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
18469                           struct hbq_dmabuf *seq_dmabuf)
18470 {
18471         struct fc_frame_header *fc_hdr;
18472         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
18473         struct lpfc_hba *phba = vport->phba;
18474
18475         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18476         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
18477         if (!iocbq) {
18478                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18479                                 "2707 Ring %d handler: Failed to allocate "
18480                                 "iocb Rctl x%x Type x%x received\n",
18481                                 LPFC_ELS_RING,
18482                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18483                 return;
18484         }
18485         if (!lpfc_complete_unsol_iocb(phba,
18486                                       phba->sli4_hba.els_wq->pring,
18487                                       iocbq, fc_hdr->fh_r_ctl,
18488                                       fc_hdr->fh_type))
18489                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18490                                 "2540 Ring %d handler: unexpected Rctl "
18491                                 "x%x Type x%x received\n",
18492                                 LPFC_ELS_RING,
18493                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18494
18495         /* Free iocb created in lpfc_prep_seq */
18496         list_for_each_entry_safe(curr_iocb, next_iocb,
18497                 &iocbq->list, list) {
18498                 list_del_init(&curr_iocb->list);
18499                 lpfc_sli_release_iocbq(phba, curr_iocb);
18500         }
18501         lpfc_sli_release_iocbq(phba, iocbq);
18502 }
18503
18504 static void
18505 lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
18506                             struct lpfc_iocbq *rspiocb)
18507 {
18508         struct lpfc_dmabuf *pcmd = cmdiocb->context2;
18509
18510         if (pcmd && pcmd->virt)
18511                 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
18512         kfree(pcmd);
18513         lpfc_sli_release_iocbq(phba, cmdiocb);
18514         lpfc_drain_txq(phba);
18515 }
18516
18517 static void
18518 lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
18519                               struct hbq_dmabuf *dmabuf)
18520 {
18521         struct fc_frame_header *fc_hdr;
18522         struct lpfc_hba *phba = vport->phba;
18523         struct lpfc_iocbq *iocbq = NULL;
18524         union  lpfc_wqe *wqe;
18525         struct lpfc_dmabuf *pcmd = NULL;
18526         uint32_t frame_len;
18527         int rc;
18528         unsigned long iflags;
18529
18530         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18531         frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
18532
18533         /* Send the received frame back */
18534         iocbq = lpfc_sli_get_iocbq(phba);
18535         if (!iocbq) {
18536                 /* Queue cq event and wakeup worker thread to process it */
18537                 spin_lock_irqsave(&phba->hbalock, iflags);
18538                 list_add_tail(&dmabuf->cq_event.list,
18539                               &phba->sli4_hba.sp_queue_event);
18540                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
18541                 spin_unlock_irqrestore(&phba->hbalock, iflags);
18542                 lpfc_worker_wake_up(phba);
18543                 return;
18544         }
18545
18546         /* Allocate buffer for command payload */
18547         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
18548         if (pcmd)
18549                 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
18550                                             &pcmd->phys);
18551         if (!pcmd || !pcmd->virt)
18552                 goto exit;
18553
18554         INIT_LIST_HEAD(&pcmd->list);
18555
18556         /* copyin the payload */
18557         memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
18558
18559         /* fill in BDE's for command */
18560         iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
18561         iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
18562         iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
18563         iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
18564
18565         iocbq->context2 = pcmd;
18566         iocbq->vport = vport;
18567         iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
18568         iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
18569
18570         /*
18571          * Setup rest of the iocb as though it were a WQE
18572          * Build the SEND_FRAME WQE
18573          */
18574         wqe = (union lpfc_wqe *)&iocbq->iocb;
18575
18576         wqe->send_frame.frame_len = frame_len;
18577         wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
18578         wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
18579         wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
18580         wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
18581         wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
18582         wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
18583
18584         iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
18585         iocbq->iocb.ulpLe = 1;
18586         iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
18587         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
18588         if (rc == IOCB_ERROR)
18589                 goto exit;
18590
18591         lpfc_in_buf_free(phba, &dmabuf->dbuf);
18592         return;
18593
18594 exit:
18595         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18596                         "2023 Unable to process MDS loopback frame\n");
18597         if (pcmd && pcmd->virt)
18598                 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
18599         kfree(pcmd);
18600         if (iocbq)
18601                 lpfc_sli_release_iocbq(phba, iocbq);
18602         lpfc_in_buf_free(phba, &dmabuf->dbuf);
18603 }
18604
18605 /**
18606  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
18607  * @phba: Pointer to HBA context object.
18608  * @dmabuf: Pointer to a dmabuf that describes the FC sequence.
18609  *
18610  * This function is called with no lock held. This function processes all
18611  * the received buffers and gives it to upper layers when a received buffer
18612  * indicates that it is the final frame in the sequence. The interrupt
18613  * service routine processes received buffers at interrupt contexts.
18614  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
18615  * appropriate receive function when the final frame in a sequence is received.
18616  **/
18617 void
18618 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
18619                                  struct hbq_dmabuf *dmabuf)
18620 {
18621         struct hbq_dmabuf *seq_dmabuf;
18622         struct fc_frame_header *fc_hdr;
18623         struct lpfc_vport *vport;
18624         uint32_t fcfi;
18625         uint32_t did;
18626
18627         /* Process each received buffer */
18628         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18629
18630         if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
18631             fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
18632                 vport = phba->pport;
18633                 /* Handle MDS Loopback frames */
18634                 if  (!(phba->pport->load_flag & FC_UNLOADING))
18635                         lpfc_sli4_handle_mds_loopback(vport, dmabuf);
18636                 else
18637                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
18638                 return;
18639         }
18640
18641         /* check to see if this a valid type of frame */
18642         if (lpfc_fc_frame_check(phba, fc_hdr)) {
18643                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18644                 return;
18645         }
18646
18647         if ((bf_get(lpfc_cqe_code,
18648                     &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
18649                 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
18650                               &dmabuf->cq_event.cqe.rcqe_cmpl);
18651         else
18652                 fcfi = bf_get(lpfc_rcqe_fcf_id,
18653                               &dmabuf->cq_event.cqe.rcqe_cmpl);
18654
18655         if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
18656                 vport = phba->pport;
18657                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18658                                 "2023 MDS Loopback %d bytes\n",
18659                                 bf_get(lpfc_rcqe_length,
18660                                        &dmabuf->cq_event.cqe.rcqe_cmpl));
18661                 /* Handle MDS Loopback frames */
18662                 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
18663                 return;
18664         }
18665
18666         /* d_id this frame is directed to */
18667         did = sli4_did_from_fc_hdr(fc_hdr);
18668
18669         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
18670         if (!vport) {
18671                 /* throw out the frame */
18672                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18673                 return;
18674         }
18675
18676         /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
18677         if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
18678                 (did != Fabric_DID)) {
18679                 /*
18680                  * Throw out the frame if we are not pt2pt.
18681                  * The pt2pt protocol allows for discovery frames
18682                  * to be received without a registered VPI.
18683                  */
18684                 if (!(vport->fc_flag & FC_PT2PT) ||
18685                         (phba->link_state == LPFC_HBA_READY)) {
18686                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
18687                         return;
18688                 }
18689         }
18690
18691         /* Handle the basic abort sequence (BA_ABTS) event */
18692         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
18693                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
18694                 return;
18695         }
18696
18697         /* Link this frame */
18698         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
18699         if (!seq_dmabuf) {
18700                 /* unable to add frame to vport - throw it out */
18701                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18702                 return;
18703         }
18704         /* If not last frame in sequence continue processing frames. */
18705         if (!lpfc_seq_complete(seq_dmabuf))
18706                 return;
18707
18708         /* Send the complete sequence to the upper layer protocol */
18709         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
18710 }
18711
18712 /**
18713  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
18714  * @phba: pointer to lpfc hba data structure.
18715  *
18716  * This routine is invoked to post rpi header templates to the
18717  * HBA consistent with the SLI-4 interface spec.  This routine
18718  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18719  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
18720  *
18721  * This routine does not require any locks.  It's usage is expected
18722  * to be driver load or reset recovery when the driver is
18723  * sequential.
18724  *
18725  * Return codes
18726  *      0 - successful
18727  *      -EIO - The mailbox failed to complete successfully.
18728  *      When this error occurs, the driver is not guaranteed
18729  *      to have any rpi regions posted to the device and
18730  *      must either attempt to repost the regions or take a
18731  *      fatal error.
18732  **/
18733 int
18734 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
18735 {
18736         struct lpfc_rpi_hdr *rpi_page;
18737         uint32_t rc = 0;
18738         uint16_t lrpi = 0;
18739
18740         /* SLI4 ports that support extents do not require RPI headers. */
18741         if (!phba->sli4_hba.rpi_hdrs_in_use)
18742                 goto exit;
18743         if (phba->sli4_hba.extents_in_use)
18744                 return -EIO;
18745
18746         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
18747                 /*
18748                  * Assign the rpi headers a physical rpi only if the driver
18749                  * has not initialized those resources.  A port reset only
18750                  * needs the headers posted.
18751                  */
18752                 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
18753                     LPFC_RPI_RSRC_RDY)
18754                         rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
18755
18756                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
18757                 if (rc != MBX_SUCCESS) {
18758                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18759                                         "2008 Error %d posting all rpi "
18760                                         "headers\n", rc);
18761                         rc = -EIO;
18762                         break;
18763                 }
18764         }
18765
18766  exit:
18767         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
18768                LPFC_RPI_RSRC_RDY);
18769         return rc;
18770 }
18771
18772 /**
18773  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
18774  * @phba: pointer to lpfc hba data structure.
18775  * @rpi_page:  pointer to the rpi memory region.
18776  *
18777  * This routine is invoked to post a single rpi header to the
18778  * HBA consistent with the SLI-4 interface spec.  This memory region
18779  * maps up to 64 rpi context regions.
18780  *
18781  * Return codes
18782  *      0 - successful
18783  *      -ENOMEM - No available memory
18784  *      -EIO - The mailbox failed to complete successfully.
18785  **/
18786 int
18787 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18788 {
18789         LPFC_MBOXQ_t *mboxq;
18790         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18791         uint32_t rc = 0;
18792         uint32_t shdr_status, shdr_add_status;
18793         union lpfc_sli4_cfg_shdr *shdr;
18794
18795         /* SLI4 ports that support extents do not require RPI headers. */
18796         if (!phba->sli4_hba.rpi_hdrs_in_use)
18797                 return rc;
18798         if (phba->sli4_hba.extents_in_use)
18799                 return -EIO;
18800
18801         /* The port is notified of the header region via a mailbox command. */
18802         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18803         if (!mboxq) {
18804                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18805                                 "2001 Unable to allocate memory for issuing "
18806                                 "SLI_CONFIG_SPECIAL mailbox command\n");
18807                 return -ENOMEM;
18808         }
18809
18810         /* Post all rpi memory regions to the port. */
18811         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
18812         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18813                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18814                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
18815                          sizeof(struct lpfc_sli4_cfg_mhdr),
18816                          LPFC_SLI4_MBX_EMBED);
18817
18818
18819         /* Post the physical rpi to the port for this rpi header. */
18820         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18821                rpi_page->start_rpi);
18822         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18823                hdr_tmpl, rpi_page->page_count);
18824
18825         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18826         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
18827         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18828         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18829         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18830         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18831         if (rc != MBX_TIMEOUT)
18832                 mempool_free(mboxq, phba->mbox_mem_pool);
18833         if (shdr_status || shdr_add_status || rc) {
18834                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18835                                 "2514 POST_RPI_HDR mailbox failed with "
18836                                 "status x%x add_status x%x, mbx status x%x\n",
18837                                 shdr_status, shdr_add_status, rc);
18838                 rc = -ENXIO;
18839         } else {
18840                 /*
18841                  * The next_rpi stores the next logical module-64 rpi value used
18842                  * to post physical rpis in subsequent rpi postings.
18843                  */
18844                 spin_lock_irq(&phba->hbalock);
18845                 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18846                 spin_unlock_irq(&phba->hbalock);
18847         }
18848         return rc;
18849 }
18850
18851 /**
18852  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18853  * @phba: pointer to lpfc hba data structure.
18854  *
18855  * This routine is invoked to post rpi header templates to the
18856  * HBA consistent with the SLI-4 interface spec.  This routine
18857  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18858  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
18859  *
18860  * Returns
18861  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
18862  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
18863  **/
18864 int
18865 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18866 {
18867         unsigned long rpi;
18868         uint16_t max_rpi, rpi_limit;
18869         uint16_t rpi_remaining, lrpi = 0;
18870         struct lpfc_rpi_hdr *rpi_hdr;
18871         unsigned long iflag;
18872
18873         /*
18874          * Fetch the next logical rpi.  Because this index is logical,
18875          * the  driver starts at 0 each time.
18876          */
18877         spin_lock_irqsave(&phba->hbalock, iflag);
18878         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18879         rpi_limit = phba->sli4_hba.next_rpi;
18880
18881         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18882         if (rpi >= rpi_limit)
18883                 rpi = LPFC_RPI_ALLOC_ERROR;
18884         else {
18885                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18886                 phba->sli4_hba.max_cfg_param.rpi_used++;
18887                 phba->sli4_hba.rpi_count++;
18888         }
18889         lpfc_printf_log(phba, KERN_INFO,
18890                         LOG_NODE | LOG_DISCOVERY,
18891                         "0001 Allocated rpi:x%x max:x%x lim:x%x\n",
18892                         (int) rpi, max_rpi, rpi_limit);
18893
18894         /*
18895          * Don't try to allocate more rpi header regions if the device limit
18896          * has been exhausted.
18897          */
18898         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18899             (phba->sli4_hba.rpi_count >= max_rpi)) {
18900                 spin_unlock_irqrestore(&phba->hbalock, iflag);
18901                 return rpi;
18902         }
18903
18904         /*
18905          * RPI header postings are not required for SLI4 ports capable of
18906          * extents.
18907          */
18908         if (!phba->sli4_hba.rpi_hdrs_in_use) {
18909                 spin_unlock_irqrestore(&phba->hbalock, iflag);
18910                 return rpi;
18911         }
18912
18913         /*
18914          * If the driver is running low on rpi resources, allocate another
18915          * page now.  Note that the next_rpi value is used because
18916          * it represents how many are actually in use whereas max_rpi notes
18917          * how many are supported max by the device.
18918          */
18919         rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
18920         spin_unlock_irqrestore(&phba->hbalock, iflag);
18921         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18922                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18923                 if (!rpi_hdr) {
18924                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18925                                         "2002 Error Could not grow rpi "
18926                                         "count\n");
18927                 } else {
18928                         lrpi = rpi_hdr->start_rpi;
18929                         rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
18930                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18931                 }
18932         }
18933
18934         return rpi;
18935 }
18936
18937 /**
18938  * lpfc_sli4_free_rpi - Release an rpi for reuse.
18939  * @phba: pointer to lpfc hba data structure.
18940  * @rpi: rpi to free
18941  *
18942  * This routine is invoked to release an rpi to the pool of
18943  * available rpis maintained by the driver.
18944  **/
18945 static void
18946 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18947 {
18948         /*
18949          * if the rpi value indicates a prior unreg has already
18950          * been done, skip the unreg.
18951          */
18952         if (rpi == LPFC_RPI_ALLOC_ERROR)
18953                 return;
18954
18955         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18956                 phba->sli4_hba.rpi_count--;
18957                 phba->sli4_hba.max_cfg_param.rpi_used--;
18958         } else {
18959                 lpfc_printf_log(phba, KERN_INFO,
18960                                 LOG_NODE | LOG_DISCOVERY,
18961                                 "2016 rpi %x not inuse\n",
18962                                 rpi);
18963         }
18964 }
18965
18966 /**
18967  * lpfc_sli4_free_rpi - Release an rpi for reuse.
18968  * @phba: pointer to lpfc hba data structure.
18969  * @rpi: rpi to free
18970  *
18971  * This routine is invoked to release an rpi to the pool of
18972  * available rpis maintained by the driver.
18973  **/
18974 void
18975 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18976 {
18977         spin_lock_irq(&phba->hbalock);
18978         __lpfc_sli4_free_rpi(phba, rpi);
18979         spin_unlock_irq(&phba->hbalock);
18980 }
18981
18982 /**
18983  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18984  * @phba: pointer to lpfc hba data structure.
18985  *
18986  * This routine is invoked to remove the memory region that
18987  * provided rpi via a bitmask.
18988  **/
18989 void
18990 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18991 {
18992         kfree(phba->sli4_hba.rpi_bmask);
18993         kfree(phba->sli4_hba.rpi_ids);
18994         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
18995 }
18996
18997 /**
18998  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18999  * @ndlp: pointer to lpfc nodelist data structure.
19000  * @cmpl: completion call-back.
19001  * @arg: data to load as MBox 'caller buffer information'
19002  *
19003  * This routine is invoked to remove the memory region that
19004  * provided rpi via a bitmask.
19005  **/
19006 int
19007 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
19008         void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
19009 {
19010         LPFC_MBOXQ_t *mboxq;
19011         struct lpfc_hba *phba = ndlp->phba;
19012         int rc;
19013
19014         /* The port is notified of the header region via a mailbox command. */
19015         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19016         if (!mboxq)
19017                 return -ENOMEM;
19018
19019         /* Post all rpi memory regions to the port. */
19020         lpfc_resume_rpi(mboxq, ndlp);
19021         if (cmpl) {
19022                 mboxq->mbox_cmpl = cmpl;
19023                 mboxq->ctx_buf = arg;
19024                 mboxq->ctx_ndlp = ndlp;
19025         } else
19026                 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19027         mboxq->vport = ndlp->vport;
19028         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19029         if (rc == MBX_NOT_FINISHED) {
19030                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19031                                 "2010 Resume RPI Mailbox failed "
19032                                 "status %d, mbxStatus x%x\n", rc,
19033                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19034                 mempool_free(mboxq, phba->mbox_mem_pool);
19035                 return -EIO;
19036         }
19037         return 0;
19038 }
19039
19040 /**
19041  * lpfc_sli4_init_vpi - Initialize a vpi with the port
19042  * @vport: Pointer to the vport for which the vpi is being initialized
19043  *
19044  * This routine is invoked to activate a vpi with the port.
19045  *
19046  * Returns:
19047  *    0 success
19048  *    -Evalue otherwise
19049  **/
19050 int
19051 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
19052 {
19053         LPFC_MBOXQ_t *mboxq;
19054         int rc = 0;
19055         int retval = MBX_SUCCESS;
19056         uint32_t mbox_tmo;
19057         struct lpfc_hba *phba = vport->phba;
19058         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19059         if (!mboxq)
19060                 return -ENOMEM;
19061         lpfc_init_vpi(phba, mboxq, vport->vpi);
19062         mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
19063         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
19064         if (rc != MBX_SUCCESS) {
19065                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19066                                 "2022 INIT VPI Mailbox failed "
19067                                 "status %d, mbxStatus x%x\n", rc,
19068                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19069                 retval = -EIO;
19070         }
19071         if (rc != MBX_TIMEOUT)
19072                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
19073
19074         return retval;
19075 }
19076
19077 /**
19078  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
19079  * @phba: pointer to lpfc hba data structure.
19080  * @mboxq: Pointer to mailbox object.
19081  *
19082  * This routine is invoked to manually add a single FCF record. The caller
19083  * must pass a completely initialized FCF_Record.  This routine takes
19084  * care of the nonembedded mailbox operations.
19085  **/
19086 static void
19087 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
19088 {
19089         void *virt_addr;
19090         union lpfc_sli4_cfg_shdr *shdr;
19091         uint32_t shdr_status, shdr_add_status;
19092
19093         virt_addr = mboxq->sge_array->addr[0];
19094         /* The IOCTL status is embedded in the mailbox subheader. */
19095         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
19096         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19097         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19098
19099         if ((shdr_status || shdr_add_status) &&
19100                 (shdr_status != STATUS_FCF_IN_USE))
19101                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19102                         "2558 ADD_FCF_RECORD mailbox failed with "
19103                         "status x%x add_status x%x\n",
19104                         shdr_status, shdr_add_status);
19105
19106         lpfc_sli4_mbox_cmd_free(phba, mboxq);
19107 }
19108
19109 /**
19110  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
19111  * @phba: pointer to lpfc hba data structure.
19112  * @fcf_record:  pointer to the initialized fcf record to add.
19113  *
19114  * This routine is invoked to manually add a single FCF record. The caller
19115  * must pass a completely initialized FCF_Record.  This routine takes
19116  * care of the nonembedded mailbox operations.
19117  **/
19118 int
19119 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
19120 {
19121         int rc = 0;
19122         LPFC_MBOXQ_t *mboxq;
19123         uint8_t *bytep;
19124         void *virt_addr;
19125         struct lpfc_mbx_sge sge;
19126         uint32_t alloc_len, req_len;
19127         uint32_t fcfindex;
19128
19129         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19130         if (!mboxq) {
19131                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19132                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
19133                 return -ENOMEM;
19134         }
19135
19136         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
19137                   sizeof(uint32_t);
19138
19139         /* Allocate DMA memory and set up the non-embedded mailbox command */
19140         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19141                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
19142                                      req_len, LPFC_SLI4_MBX_NEMBED);
19143         if (alloc_len < req_len) {
19144                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19145                         "2523 Allocated DMA memory size (x%x) is "
19146                         "less than the requested DMA memory "
19147                         "size (x%x)\n", alloc_len, req_len);
19148                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19149                 return -ENOMEM;
19150         }
19151
19152         /*
19153          * Get the first SGE entry from the non-embedded DMA memory.  This
19154          * routine only uses a single SGE.
19155          */
19156         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
19157         virt_addr = mboxq->sge_array->addr[0];
19158         /*
19159          * Configure the FCF record for FCFI 0.  This is the driver's
19160          * hardcoded default and gets used in nonFIP mode.
19161          */
19162         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
19163         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
19164         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
19165
19166         /*
19167          * Copy the fcf_index and the FCF Record Data. The data starts after
19168          * the FCoE header plus word10. The data copy needs to be endian
19169          * correct.
19170          */
19171         bytep += sizeof(uint32_t);
19172         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
19173         mboxq->vport = phba->pport;
19174         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
19175         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19176         if (rc == MBX_NOT_FINISHED) {
19177                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19178                         "2515 ADD_FCF_RECORD mailbox failed with "
19179                         "status 0x%x\n", rc);
19180                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19181                 rc = -EIO;
19182         } else
19183                 rc = 0;
19184
19185         return rc;
19186 }
19187
19188 /**
19189  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
19190  * @phba: pointer to lpfc hba data structure.
19191  * @fcf_record:  pointer to the fcf record to write the default data.
19192  * @fcf_index: FCF table entry index.
19193  *
19194  * This routine is invoked to build the driver's default FCF record.  The
19195  * values used are hardcoded.  This routine handles memory initialization.
19196  *
19197  **/
19198 void
19199 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
19200                                 struct fcf_record *fcf_record,
19201                                 uint16_t fcf_index)
19202 {
19203         memset(fcf_record, 0, sizeof(struct fcf_record));
19204         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
19205         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
19206         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
19207         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
19208         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
19209         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
19210         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
19211         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
19212         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
19213         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
19214         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
19215         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
19216         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
19217         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
19218         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
19219         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
19220                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
19221         /* Set the VLAN bit map */
19222         if (phba->valid_vlan) {
19223                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
19224                         = 1 << (phba->vlan_id % 8);
19225         }
19226 }
19227
19228 /**
19229  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
19230  * @phba: pointer to lpfc hba data structure.
19231  * @fcf_index: FCF table entry offset.
19232  *
19233  * This routine is invoked to scan the entire FCF table by reading FCF
19234  * record and processing it one at a time starting from the @fcf_index
19235  * for initial FCF discovery or fast FCF failover rediscovery.
19236  *
19237  * Return 0 if the mailbox command is submitted successfully, none 0
19238  * otherwise.
19239  **/
19240 int
19241 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
19242 {
19243         int rc = 0, error;
19244         LPFC_MBOXQ_t *mboxq;
19245
19246         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
19247         phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
19248         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19249         if (!mboxq) {
19250                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19251                                 "2000 Failed to allocate mbox for "
19252                                 "READ_FCF cmd\n");
19253                 error = -ENOMEM;
19254                 goto fail_fcf_scan;
19255         }
19256         /* Construct the read FCF record mailbox command */
19257         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
19258         if (rc) {
19259                 error = -EINVAL;
19260                 goto fail_fcf_scan;
19261         }
19262         /* Issue the mailbox command asynchronously */
19263         mboxq->vport = phba->pport;
19264         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
19265
19266         spin_lock_irq(&phba->hbalock);
19267         phba->hba_flag |= FCF_TS_INPROG;
19268         spin_unlock_irq(&phba->hbalock);
19269
19270         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19271         if (rc == MBX_NOT_FINISHED)
19272                 error = -EIO;
19273         else {
19274                 /* Reset eligible FCF count for new scan */
19275                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
19276                         phba->fcf.eligible_fcf_cnt = 0;
19277                 error = 0;
19278         }
19279 fail_fcf_scan:
19280         if (error) {
19281                 if (mboxq)
19282                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
19283                 /* FCF scan failed, clear FCF_TS_INPROG flag */
19284                 spin_lock_irq(&phba->hbalock);
19285                 phba->hba_flag &= ~FCF_TS_INPROG;
19286                 spin_unlock_irq(&phba->hbalock);
19287         }
19288         return error;
19289 }
19290
19291 /**
19292  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
19293  * @phba: pointer to lpfc hba data structure.
19294  * @fcf_index: FCF table entry offset.
19295  *
19296  * This routine is invoked to read an FCF record indicated by @fcf_index
19297  * and to use it for FLOGI roundrobin FCF failover.
19298  *
19299  * Return 0 if the mailbox command is submitted successfully, none 0
19300  * otherwise.
19301  **/
19302 int
19303 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
19304 {
19305         int rc = 0, error;
19306         LPFC_MBOXQ_t *mboxq;
19307
19308         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19309         if (!mboxq) {
19310                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
19311                                 "2763 Failed to allocate mbox for "
19312                                 "READ_FCF cmd\n");
19313                 error = -ENOMEM;
19314                 goto fail_fcf_read;
19315         }
19316         /* Construct the read FCF record mailbox command */
19317         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
19318         if (rc) {
19319                 error = -EINVAL;
19320                 goto fail_fcf_read;
19321         }
19322         /* Issue the mailbox command asynchronously */
19323         mboxq->vport = phba->pport;
19324         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
19325         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19326         if (rc == MBX_NOT_FINISHED)
19327                 error = -EIO;
19328         else
19329                 error = 0;
19330
19331 fail_fcf_read:
19332         if (error && mboxq)
19333                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19334         return error;
19335 }
19336
19337 /**
19338  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
19339  * @phba: pointer to lpfc hba data structure.
19340  * @fcf_index: FCF table entry offset.
19341  *
19342  * This routine is invoked to read an FCF record indicated by @fcf_index to
19343  * determine whether it's eligible for FLOGI roundrobin failover list.
19344  *
19345  * Return 0 if the mailbox command is submitted successfully, none 0
19346  * otherwise.
19347  **/
19348 int
19349 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
19350 {
19351         int rc = 0, error;
19352         LPFC_MBOXQ_t *mboxq;
19353
19354         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19355         if (!mboxq) {
19356                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
19357                                 "2758 Failed to allocate mbox for "
19358                                 "READ_FCF cmd\n");
19359                                 error = -ENOMEM;
19360                                 goto fail_fcf_read;
19361         }
19362         /* Construct the read FCF record mailbox command */
19363         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
19364         if (rc) {
19365                 error = -EINVAL;
19366                 goto fail_fcf_read;
19367         }
19368         /* Issue the mailbox command asynchronously */
19369         mboxq->vport = phba->pport;
19370         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
19371         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19372         if (rc == MBX_NOT_FINISHED)
19373                 error = -EIO;
19374         else
19375                 error = 0;
19376
19377 fail_fcf_read:
19378         if (error && mboxq)
19379                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19380         return error;
19381 }
19382
19383 /**
19384  * lpfc_check_next_fcf_pri_level
19385  * @phba: pointer to the lpfc_hba struct for this port.
19386  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
19387  * routine when the rr_bmask is empty. The FCF indecies are put into the
19388  * rr_bmask based on their priority level. Starting from the highest priority
19389  * to the lowest. The most likely FCF candidate will be in the highest
19390  * priority group. When this routine is called it searches the fcf_pri list for
19391  * next lowest priority group and repopulates the rr_bmask with only those
19392  * fcf_indexes.
19393  * returns:
19394  * 1=success 0=failure
19395  **/
19396 static int
19397 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
19398 {
19399         uint16_t next_fcf_pri;
19400         uint16_t last_index;
19401         struct lpfc_fcf_pri *fcf_pri;
19402         int rc;
19403         int ret = 0;
19404
19405         last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
19406                         LPFC_SLI4_FCF_TBL_INDX_MAX);
19407         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19408                         "3060 Last IDX %d\n", last_index);
19409
19410         /* Verify the priority list has 2 or more entries */
19411         spin_lock_irq(&phba->hbalock);
19412         if (list_empty(&phba->fcf.fcf_pri_list) ||
19413             list_is_singular(&phba->fcf.fcf_pri_list)) {
19414                 spin_unlock_irq(&phba->hbalock);
19415                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
19416                         "3061 Last IDX %d\n", last_index);
19417                 return 0; /* Empty rr list */
19418         }
19419         spin_unlock_irq(&phba->hbalock);
19420
19421         next_fcf_pri = 0;
19422         /*
19423          * Clear the rr_bmask and set all of the bits that are at this
19424          * priority.
19425          */
19426         memset(phba->fcf.fcf_rr_bmask, 0,
19427                         sizeof(*phba->fcf.fcf_rr_bmask));
19428         spin_lock_irq(&phba->hbalock);
19429         list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
19430                 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
19431                         continue;
19432                 /*
19433                  * the 1st priority that has not FLOGI failed
19434                  * will be the highest.
19435                  */
19436                 if (!next_fcf_pri)
19437                         next_fcf_pri = fcf_pri->fcf_rec.priority;
19438                 spin_unlock_irq(&phba->hbalock);
19439                 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
19440                         rc = lpfc_sli4_fcf_rr_index_set(phba,
19441                                                 fcf_pri->fcf_rec.fcf_index);
19442                         if (rc)
19443                                 return 0;
19444                 }
19445                 spin_lock_irq(&phba->hbalock);
19446         }
19447         /*
19448          * if next_fcf_pri was not set above and the list is not empty then
19449          * we have failed flogis on all of them. So reset flogi failed
19450          * and start at the beginning.
19451          */
19452         if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
19453                 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
19454                         fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
19455                         /*
19456                          * the 1st priority that has not FLOGI failed
19457                          * will be the highest.
19458                          */
19459                         if (!next_fcf_pri)
19460                                 next_fcf_pri = fcf_pri->fcf_rec.priority;
19461                         spin_unlock_irq(&phba->hbalock);
19462                         if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
19463                                 rc = lpfc_sli4_fcf_rr_index_set(phba,
19464                                                 fcf_pri->fcf_rec.fcf_index);
19465                                 if (rc)
19466                                         return 0;
19467                         }
19468                         spin_lock_irq(&phba->hbalock);
19469                 }
19470         } else
19471                 ret = 1;
19472         spin_unlock_irq(&phba->hbalock);
19473
19474         return ret;
19475 }
19476 /**
19477  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
19478  * @phba: pointer to lpfc hba data structure.
19479  *
19480  * This routine is to get the next eligible FCF record index in a round
19481  * robin fashion. If the next eligible FCF record index equals to the
19482  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
19483  * shall be returned, otherwise, the next eligible FCF record's index
19484  * shall be returned.
19485  **/
19486 uint16_t
19487 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
19488 {
19489         uint16_t next_fcf_index;
19490
19491 initial_priority:
19492         /* Search start from next bit of currently registered FCF index */
19493         next_fcf_index = phba->fcf.current_rec.fcf_indx;
19494
19495 next_priority:
19496         /* Determine the next fcf index to check */
19497         next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
19498         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
19499                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
19500                                        next_fcf_index);
19501
19502         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
19503         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19504                 /*
19505                  * If we have wrapped then we need to clear the bits that
19506                  * have been tested so that we can detect when we should
19507                  * change the priority level.
19508                  */
19509                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
19510                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
19511         }
19512
19513
19514         /* Check roundrobin failover list empty condition */
19515         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
19516                 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
19517                 /*
19518                  * If next fcf index is not found check if there are lower
19519                  * Priority level fcf's in the fcf_priority list.
19520                  * Set up the rr_bmask with all of the avaiable fcf bits
19521                  * at that level and continue the selection process.
19522                  */
19523                 if (lpfc_check_next_fcf_pri_level(phba))
19524                         goto initial_priority;
19525                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
19526                                 "2844 No roundrobin failover FCF available\n");
19527
19528                 return LPFC_FCOE_FCF_NEXT_NONE;
19529         }
19530
19531         if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
19532                 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
19533                 LPFC_FCF_FLOGI_FAILED) {
19534                 if (list_is_singular(&phba->fcf.fcf_pri_list))
19535                         return LPFC_FCOE_FCF_NEXT_NONE;
19536
19537                 goto next_priority;
19538         }
19539
19540         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19541                         "2845 Get next roundrobin failover FCF (x%x)\n",
19542                         next_fcf_index);
19543
19544         return next_fcf_index;
19545 }
19546
19547 /**
19548  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
19549  * @phba: pointer to lpfc hba data structure.
19550  * @fcf_index: index into the FCF table to 'set'
19551  *
19552  * This routine sets the FCF record index in to the eligible bmask for
19553  * roundrobin failover search. It checks to make sure that the index
19554  * does not go beyond the range of the driver allocated bmask dimension
19555  * before setting the bit.
19556  *
19557  * Returns 0 if the index bit successfully set, otherwise, it returns
19558  * -EINVAL.
19559  **/
19560 int
19561 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
19562 {
19563         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19564                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
19565                                 "2610 FCF (x%x) reached driver's book "
19566                                 "keeping dimension:x%x\n",
19567                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
19568                 return -EINVAL;
19569         }
19570         /* Set the eligible FCF record index bmask */
19571         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
19572
19573         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19574                         "2790 Set FCF (x%x) to roundrobin FCF failover "
19575                         "bmask\n", fcf_index);
19576
19577         return 0;
19578 }
19579
19580 /**
19581  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
19582  * @phba: pointer to lpfc hba data structure.
19583  * @fcf_index: index into the FCF table to 'clear'
19584  *
19585  * This routine clears the FCF record index from the eligible bmask for
19586  * roundrobin failover search. It checks to make sure that the index
19587  * does not go beyond the range of the driver allocated bmask dimension
19588  * before clearing the bit.
19589  **/
19590 void
19591 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
19592 {
19593         struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
19594         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19595                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
19596                                 "2762 FCF (x%x) reached driver's book "
19597                                 "keeping dimension:x%x\n",
19598                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
19599                 return;
19600         }
19601         /* Clear the eligible FCF record index bmask */
19602         spin_lock_irq(&phba->hbalock);
19603         list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
19604                                  list) {
19605                 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
19606                         list_del_init(&fcf_pri->list);
19607                         break;
19608                 }
19609         }
19610         spin_unlock_irq(&phba->hbalock);
19611         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
19612
19613         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19614                         "2791 Clear FCF (x%x) from roundrobin failover "
19615                         "bmask\n", fcf_index);
19616 }
19617
19618 /**
19619  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
19620  * @phba: pointer to lpfc hba data structure.
19621  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
19622  *
19623  * This routine is the completion routine for the rediscover FCF table mailbox
19624  * command. If the mailbox command returned failure, it will try to stop the
19625  * FCF rediscover wait timer.
19626  **/
19627 static void
19628 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
19629 {
19630         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
19631         uint32_t shdr_status, shdr_add_status;
19632
19633         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
19634
19635         shdr_status = bf_get(lpfc_mbox_hdr_status,
19636                              &redisc_fcf->header.cfg_shdr.response);
19637         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19638                              &redisc_fcf->header.cfg_shdr.response);
19639         if (shdr_status || shdr_add_status) {
19640                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
19641                                 "2746 Requesting for FCF rediscovery failed "
19642                                 "status x%x add_status x%x\n",
19643                                 shdr_status, shdr_add_status);
19644                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
19645                         spin_lock_irq(&phba->hbalock);
19646                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
19647                         spin_unlock_irq(&phba->hbalock);
19648                         /*
19649                          * CVL event triggered FCF rediscover request failed,
19650                          * last resort to re-try current registered FCF entry.
19651                          */
19652                         lpfc_retry_pport_discovery(phba);
19653                 } else {
19654                         spin_lock_irq(&phba->hbalock);
19655                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
19656                         spin_unlock_irq(&phba->hbalock);
19657                         /*
19658                          * DEAD FCF event triggered FCF rediscover request
19659                          * failed, last resort to fail over as a link down
19660                          * to FCF registration.
19661                          */
19662                         lpfc_sli4_fcf_dead_failthrough(phba);
19663                 }
19664         } else {
19665                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19666                                 "2775 Start FCF rediscover quiescent timer\n");
19667                 /*
19668                  * Start FCF rediscovery wait timer for pending FCF
19669                  * before rescan FCF record table.
19670                  */
19671                 lpfc_fcf_redisc_wait_start_timer(phba);
19672         }
19673
19674         mempool_free(mbox, phba->mbox_mem_pool);
19675 }
19676
19677 /**
19678  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
19679  * @phba: pointer to lpfc hba data structure.
19680  *
19681  * This routine is invoked to request for rediscovery of the entire FCF table
19682  * by the port.
19683  **/
19684 int
19685 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
19686 {
19687         LPFC_MBOXQ_t *mbox;
19688         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
19689         int rc, length;
19690
19691         /* Cancel retry delay timers to all vports before FCF rediscover */
19692         lpfc_cancel_all_vport_retry_delay_timer(phba);
19693
19694         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19695         if (!mbox) {
19696                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19697                                 "2745 Failed to allocate mbox for "
19698                                 "requesting FCF rediscover.\n");
19699                 return -ENOMEM;
19700         }
19701
19702         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
19703                   sizeof(struct lpfc_sli4_cfg_mhdr));
19704         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
19705                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
19706                          length, LPFC_SLI4_MBX_EMBED);
19707
19708         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
19709         /* Set count to 0 for invalidating the entire FCF database */
19710         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
19711
19712         /* Issue the mailbox command asynchronously */
19713         mbox->vport = phba->pport;
19714         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
19715         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
19716
19717         if (rc == MBX_NOT_FINISHED) {
19718                 mempool_free(mbox, phba->mbox_mem_pool);
19719                 return -EIO;
19720         }
19721         return 0;
19722 }
19723
19724 /**
19725  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
19726  * @phba: pointer to lpfc hba data structure.
19727  *
19728  * This function is the failover routine as a last resort to the FCF DEAD
19729  * event when driver failed to perform fast FCF failover.
19730  **/
19731 void
19732 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
19733 {
19734         uint32_t link_state;
19735
19736         /*
19737          * Last resort as FCF DEAD event failover will treat this as
19738          * a link down, but save the link state because we don't want
19739          * it to be changed to Link Down unless it is already down.
19740          */
19741         link_state = phba->link_state;
19742         lpfc_linkdown(phba);
19743         phba->link_state = link_state;
19744
19745         /* Unregister FCF if no devices connected to it */
19746         lpfc_unregister_unused_fcf(phba);
19747 }
19748
19749 /**
19750  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
19751  * @phba: pointer to lpfc hba data structure.
19752  * @rgn23_data: pointer to configure region 23 data.
19753  *
19754  * This function gets SLI3 port configure region 23 data through memory dump
19755  * mailbox command. When it successfully retrieves data, the size of the data
19756  * will be returned, otherwise, 0 will be returned.
19757  **/
19758 static uint32_t
19759 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19760 {
19761         LPFC_MBOXQ_t *pmb = NULL;
19762         MAILBOX_t *mb;
19763         uint32_t offset = 0;
19764         int i, rc;
19765
19766         if (!rgn23_data)
19767                 return 0;
19768
19769         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19770         if (!pmb) {
19771                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19772                                 "2600 failed to allocate mailbox memory\n");
19773                 return 0;
19774         }
19775         mb = &pmb->u.mb;
19776
19777         do {
19778                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
19779                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
19780
19781                 if (rc != MBX_SUCCESS) {
19782                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19783                                         "2601 failed to read config "
19784                                         "region 23, rc 0x%x Status 0x%x\n",
19785                                         rc, mb->mbxStatus);
19786                         mb->un.varDmp.word_cnt = 0;
19787                 }
19788                 /*
19789                  * dump mem may return a zero when finished or we got a
19790                  * mailbox error, either way we are done.
19791                  */
19792                 if (mb->un.varDmp.word_cnt == 0)
19793                         break;
19794
19795                 i =  mb->un.varDmp.word_cnt * sizeof(uint32_t);
19796                 if (offset + i >  DMP_RGN23_SIZE)
19797                         i =  DMP_RGN23_SIZE - offset;
19798                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
19799                                       rgn23_data  + offset, i);
19800                 offset += i;
19801         } while (offset < DMP_RGN23_SIZE);
19802
19803         mempool_free(pmb, phba->mbox_mem_pool);
19804         return offset;
19805 }
19806
19807 /**
19808  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19809  * @phba: pointer to lpfc hba data structure.
19810  * @rgn23_data: pointer to configure region 23 data.
19811  *
19812  * This function gets SLI4 port configure region 23 data through memory dump
19813  * mailbox command. When it successfully retrieves data, the size of the data
19814  * will be returned, otherwise, 0 will be returned.
19815  **/
19816 static uint32_t
19817 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19818 {
19819         LPFC_MBOXQ_t *mboxq = NULL;
19820         struct lpfc_dmabuf *mp = NULL;
19821         struct lpfc_mqe *mqe;
19822         uint32_t data_length = 0;
19823         int rc;
19824
19825         if (!rgn23_data)
19826                 return 0;
19827
19828         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19829         if (!mboxq) {
19830                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19831                                 "3105 failed to allocate mailbox memory\n");
19832                 return 0;
19833         }
19834
19835         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19836                 goto out;
19837         mqe = &mboxq->u.mqe;
19838         mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
19839         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19840         if (rc)
19841                 goto out;
19842         data_length = mqe->un.mb_words[5];
19843         if (data_length == 0)
19844                 goto out;
19845         if (data_length > DMP_RGN23_SIZE) {
19846                 data_length = 0;
19847                 goto out;
19848         }
19849         lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19850 out:
19851         mempool_free(mboxq, phba->mbox_mem_pool);
19852         if (mp) {
19853                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19854                 kfree(mp);
19855         }
19856         return data_length;
19857 }
19858
19859 /**
19860  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19861  * @phba: pointer to lpfc hba data structure.
19862  *
19863  * This function read region 23 and parse TLV for port status to
19864  * decide if the user disaled the port. If the TLV indicates the
19865  * port is disabled, the hba_flag is set accordingly.
19866  **/
19867 void
19868 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19869 {
19870         uint8_t *rgn23_data = NULL;
19871         uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19872         uint32_t offset = 0;
19873
19874         /* Get adapter Region 23 data */
19875         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19876         if (!rgn23_data)
19877                 goto out;
19878
19879         if (phba->sli_rev < LPFC_SLI_REV4)
19880                 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19881         else {
19882                 if_type = bf_get(lpfc_sli_intf_if_type,
19883                                  &phba->sli4_hba.sli_intf);
19884                 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19885                         goto out;
19886                 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19887         }
19888
19889         if (!data_size)
19890                 goto out;
19891
19892         /* Check the region signature first */
19893         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19894                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19895                         "2619 Config region 23 has bad signature\n");
19896                         goto out;
19897         }
19898         offset += 4;
19899
19900         /* Check the data structure version */
19901         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19902                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19903                         "2620 Config region 23 has bad version\n");
19904                 goto out;
19905         }
19906         offset += 4;
19907
19908         /* Parse TLV entries in the region */
19909         while (offset < data_size) {
19910                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19911                         break;
19912                 /*
19913                  * If the TLV is not driver specific TLV or driver id is
19914                  * not linux driver id, skip the record.
19915                  */
19916                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19917                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19918                     (rgn23_data[offset + 3] != 0)) {
19919                         offset += rgn23_data[offset + 1] * 4 + 4;
19920                         continue;
19921                 }
19922
19923                 /* Driver found a driver specific TLV in the config region */
19924                 sub_tlv_len = rgn23_data[offset + 1] * 4;
19925                 offset += 4;
19926                 tlv_offset = 0;
19927
19928                 /*
19929                  * Search for configured port state sub-TLV.
19930                  */
19931                 while ((offset < data_size) &&
19932                         (tlv_offset < sub_tlv_len)) {
19933                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19934                                 offset += 4;
19935                                 tlv_offset += 4;
19936                                 break;
19937                         }
19938                         if (rgn23_data[offset] != PORT_STE_TYPE) {
19939                                 offset += rgn23_data[offset + 1] * 4 + 4;
19940                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19941                                 continue;
19942                         }
19943
19944                         /* This HBA contains PORT_STE configured */
19945                         if (!rgn23_data[offset + 2])
19946                                 phba->hba_flag |= LINK_DISABLED;
19947
19948                         goto out;
19949                 }
19950         }
19951
19952 out:
19953         kfree(rgn23_data);
19954         return;
19955 }
19956
19957 /**
19958  * lpfc_wr_object - write an object to the firmware
19959  * @phba: HBA structure that indicates port to create a queue on.
19960  * @dmabuf_list: list of dmabufs to write to the port.
19961  * @size: the total byte value of the objects to write to the port.
19962  * @offset: the current offset to be used to start the transfer.
19963  *
19964  * This routine will create a wr_object mailbox command to send to the port.
19965  * the mailbox command will be constructed using the dma buffers described in
19966  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19967  * BDEs that the imbedded mailbox can support. The @offset variable will be
19968  * used to indicate the starting offset of the transfer and will also return
19969  * the offset after the write object mailbox has completed. @size is used to
19970  * determine the end of the object and whether the eof bit should be set.
19971  *
19972  * Return 0 is successful and offset will contain the the new offset to use
19973  * for the next write.
19974  * Return negative value for error cases.
19975  **/
19976 int
19977 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19978                uint32_t size, uint32_t *offset)
19979 {
19980         struct lpfc_mbx_wr_object *wr_object;
19981         LPFC_MBOXQ_t *mbox;
19982         int rc = 0, i = 0;
19983         uint32_t shdr_status, shdr_add_status, shdr_change_status, shdr_csf;
19984         uint32_t mbox_tmo;
19985         struct lpfc_dmabuf *dmabuf;
19986         uint32_t written = 0;
19987         bool check_change_status = false;
19988
19989         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19990         if (!mbox)
19991                 return -ENOMEM;
19992
19993         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19994                         LPFC_MBOX_OPCODE_WRITE_OBJECT,
19995                         sizeof(struct lpfc_mbx_wr_object) -
19996                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19997
19998         wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19999         wr_object->u.request.write_offset = *offset;
20000         sprintf((uint8_t *)wr_object->u.request.object_name, "/");
20001         wr_object->u.request.object_name[0] =
20002                 cpu_to_le32(wr_object->u.request.object_name[0]);
20003         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
20004         list_for_each_entry(dmabuf, dmabuf_list, list) {
20005                 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
20006                         break;
20007                 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
20008                 wr_object->u.request.bde[i].addrHigh =
20009                         putPaddrHigh(dmabuf->phys);
20010                 if (written + SLI4_PAGE_SIZE >= size) {
20011                         wr_object->u.request.bde[i].tus.f.bdeSize =
20012                                 (size - written);
20013                         written += (size - written);
20014                         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
20015                         bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
20016                         check_change_status = true;
20017                 } else {
20018                         wr_object->u.request.bde[i].tus.f.bdeSize =
20019                                 SLI4_PAGE_SIZE;
20020                         written += SLI4_PAGE_SIZE;
20021                 }
20022                 i++;
20023         }
20024         wr_object->u.request.bde_count = i;
20025         bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
20026         if (!phba->sli4_hba.intr_enable)
20027                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
20028         else {
20029                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
20030                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
20031         }
20032         /* The IOCTL status is embedded in the mailbox subheader. */
20033         shdr_status = bf_get(lpfc_mbox_hdr_status,
20034                              &wr_object->header.cfg_shdr.response);
20035         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20036                                  &wr_object->header.cfg_shdr.response);
20037         if (check_change_status) {
20038                 shdr_change_status = bf_get(lpfc_wr_object_change_status,
20039                                             &wr_object->u.response);
20040
20041                 if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
20042                     shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
20043                         shdr_csf = bf_get(lpfc_wr_object_csf,
20044                                           &wr_object->u.response);
20045                         if (shdr_csf)
20046                                 shdr_change_status =
20047                                                    LPFC_CHANGE_STATUS_PCI_RESET;
20048                 }
20049
20050                 switch (shdr_change_status) {
20051                 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
20052                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20053                                         "3198 Firmware write complete: System "
20054                                         "reboot required to instantiate\n");
20055                         break;
20056                 case (LPFC_CHANGE_STATUS_FW_RESET):
20057                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20058                                         "3199 Firmware write complete: Firmware"
20059                                         " reset required to instantiate\n");
20060                         break;
20061                 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
20062                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20063                                         "3200 Firmware write complete: Port "
20064                                         "Migration or PCI Reset required to "
20065                                         "instantiate\n");
20066                         break;
20067                 case (LPFC_CHANGE_STATUS_PCI_RESET):
20068                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20069                                         "3201 Firmware write complete: PCI "
20070                                         "Reset required to instantiate\n");
20071                         break;
20072                 default:
20073                         break;
20074                 }
20075         }
20076         if (rc != MBX_TIMEOUT)
20077                 mempool_free(mbox, phba->mbox_mem_pool);
20078         if (shdr_status || shdr_add_status || rc) {
20079                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20080                                 "3025 Write Object mailbox failed with "
20081                                 "status x%x add_status x%x, mbx status x%x\n",
20082                                 shdr_status, shdr_add_status, rc);
20083                 rc = -ENXIO;
20084                 *offset = shdr_add_status;
20085         } else
20086                 *offset += wr_object->u.response.actual_write_length;
20087         return rc;
20088 }
20089
20090 /**
20091  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
20092  * @vport: pointer to vport data structure.
20093  *
20094  * This function iterate through the mailboxq and clean up all REG_LOGIN
20095  * and REG_VPI mailbox commands associated with the vport. This function
20096  * is called when driver want to restart discovery of the vport due to
20097  * a Clear Virtual Link event.
20098  **/
20099 void
20100 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
20101 {
20102         struct lpfc_hba *phba = vport->phba;
20103         LPFC_MBOXQ_t *mb, *nextmb;
20104         struct lpfc_dmabuf *mp;
20105         struct lpfc_nodelist *ndlp;
20106         struct lpfc_nodelist *act_mbx_ndlp = NULL;
20107         LIST_HEAD(mbox_cmd_list);
20108         uint8_t restart_loop;
20109
20110         /* Clean up internally queued mailbox commands with the vport */
20111         spin_lock_irq(&phba->hbalock);
20112         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
20113                 if (mb->vport != vport)
20114                         continue;
20115
20116                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
20117                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
20118                         continue;
20119
20120                 list_del(&mb->list);
20121                 list_add_tail(&mb->list, &mbox_cmd_list);
20122         }
20123         /* Clean up active mailbox command with the vport */
20124         mb = phba->sli.mbox_active;
20125         if (mb && (mb->vport == vport)) {
20126                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
20127                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
20128                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
20129                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
20130                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
20131                         /* Put reference count for delayed processing */
20132                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
20133                         /* Unregister the RPI when mailbox complete */
20134                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
20135                 }
20136         }
20137         /* Cleanup any mailbox completions which are not yet processed */
20138         do {
20139                 restart_loop = 0;
20140                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
20141                         /*
20142                          * If this mailox is already processed or it is
20143                          * for another vport ignore it.
20144                          */
20145                         if ((mb->vport != vport) ||
20146                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
20147                                 continue;
20148
20149                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
20150                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
20151                                 continue;
20152
20153                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
20154                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
20155                                 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
20156                                 /* Unregister the RPI when mailbox complete */
20157                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
20158                                 restart_loop = 1;
20159                                 spin_unlock_irq(&phba->hbalock);
20160                                 spin_lock(&ndlp->lock);
20161                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
20162                                 spin_unlock(&ndlp->lock);
20163                                 spin_lock_irq(&phba->hbalock);
20164                                 break;
20165                         }
20166                 }
20167         } while (restart_loop);
20168
20169         spin_unlock_irq(&phba->hbalock);
20170
20171         /* Release the cleaned-up mailbox commands */
20172         while (!list_empty(&mbox_cmd_list)) {
20173                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
20174                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
20175                         mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
20176                         if (mp) {
20177                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
20178                                 kfree(mp);
20179                         }
20180                         mb->ctx_buf = NULL;
20181                         ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
20182                         mb->ctx_ndlp = NULL;
20183                         if (ndlp) {
20184                                 spin_lock(&ndlp->lock);
20185                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
20186                                 spin_unlock(&ndlp->lock);
20187                                 lpfc_nlp_put(ndlp);
20188                         }
20189                 }
20190                 mempool_free(mb, phba->mbox_mem_pool);
20191         }
20192
20193         /* Release the ndlp with the cleaned-up active mailbox command */
20194         if (act_mbx_ndlp) {
20195                 spin_lock(&act_mbx_ndlp->lock);
20196                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
20197                 spin_unlock(&act_mbx_ndlp->lock);
20198                 lpfc_nlp_put(act_mbx_ndlp);
20199         }
20200 }
20201
20202 /**
20203  * lpfc_drain_txq - Drain the txq
20204  * @phba: Pointer to HBA context object.
20205  *
20206  * This function attempt to submit IOCBs on the txq
20207  * to the adapter.  For SLI4 adapters, the txq contains
20208  * ELS IOCBs that have been deferred because the there
20209  * are no SGLs.  This congestion can occur with large
20210  * vport counts during node discovery.
20211  **/
20212
20213 uint32_t
20214 lpfc_drain_txq(struct lpfc_hba *phba)
20215 {
20216         LIST_HEAD(completions);
20217         struct lpfc_sli_ring *pring;
20218         struct lpfc_iocbq *piocbq = NULL;
20219         unsigned long iflags = 0;
20220         char *fail_msg = NULL;
20221         struct lpfc_sglq *sglq;
20222         union lpfc_wqe128 wqe;
20223         uint32_t txq_cnt = 0;
20224         struct lpfc_queue *wq;
20225
20226         if (phba->link_flag & LS_MDS_LOOPBACK) {
20227                 /* MDS WQE are posted only to first WQ*/
20228                 wq = phba->sli4_hba.hdwq[0].io_wq;
20229                 if (unlikely(!wq))
20230                         return 0;
20231                 pring = wq->pring;
20232         } else {
20233                 wq = phba->sli4_hba.els_wq;
20234                 if (unlikely(!wq))
20235                         return 0;
20236                 pring = lpfc_phba_elsring(phba);
20237         }
20238
20239         if (unlikely(!pring) || list_empty(&pring->txq))
20240                 return 0;
20241
20242         spin_lock_irqsave(&pring->ring_lock, iflags);
20243         list_for_each_entry(piocbq, &pring->txq, list) {
20244                 txq_cnt++;
20245         }
20246
20247         if (txq_cnt > pring->txq_max)
20248                 pring->txq_max = txq_cnt;
20249
20250         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20251
20252         while (!list_empty(&pring->txq)) {
20253                 spin_lock_irqsave(&pring->ring_lock, iflags);
20254
20255                 piocbq = lpfc_sli_ringtx_get(phba, pring);
20256                 if (!piocbq) {
20257                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20258                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20259                                 "2823 txq empty and txq_cnt is %d\n ",
20260                                 txq_cnt);
20261                         break;
20262                 }
20263                 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
20264                 if (!sglq) {
20265                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
20266                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20267                         break;
20268                 }
20269                 txq_cnt--;
20270
20271                 /* The xri and iocb resources secured,
20272                  * attempt to issue request
20273                  */
20274                 piocbq->sli4_lxritag = sglq->sli4_lxritag;
20275                 piocbq->sli4_xritag = sglq->sli4_xritag;
20276                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
20277                         fail_msg = "to convert bpl to sgl";
20278                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
20279                         fail_msg = "to convert iocb to wqe";
20280                 else if (lpfc_sli4_wq_put(wq, &wqe))
20281                         fail_msg = " - Wq is full";
20282                 else
20283                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
20284
20285                 if (fail_msg) {
20286                         /* Failed means we can't issue and need to cancel */
20287                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20288                                         "2822 IOCB failed %s iotag 0x%x "
20289                                         "xri 0x%x\n",
20290                                         fail_msg,
20291                                         piocbq->iotag, piocbq->sli4_xritag);
20292                         list_add_tail(&piocbq->list, &completions);
20293                 }
20294                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20295         }
20296
20297         /* Cancel all the IOCBs that cannot be issued */
20298         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
20299                                 IOERR_SLI_ABORTED);
20300
20301         return txq_cnt;
20302 }
20303
20304 /**
20305  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
20306  * @phba: Pointer to HBA context object.
20307  * @pwqeq: Pointer to command WQE.
20308  * @sglq: Pointer to the scatter gather queue object.
20309  *
20310  * This routine converts the bpl or bde that is in the WQE
20311  * to a sgl list for the sli4 hardware. The physical address
20312  * of the bpl/bde is converted back to a virtual address.
20313  * If the WQE contains a BPL then the list of BDE's is
20314  * converted to sli4_sge's. If the WQE contains a single
20315  * BDE then it is converted to a single sli_sge.
20316  * The WQE is still in cpu endianness so the contents of
20317  * the bpl can be used without byte swapping.
20318  *
20319  * Returns valid XRI = Success, NO_XRI = Failure.
20320  */
20321 static uint16_t
20322 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
20323                  struct lpfc_sglq *sglq)
20324 {
20325         uint16_t xritag = NO_XRI;
20326         struct ulp_bde64 *bpl = NULL;
20327         struct ulp_bde64 bde;
20328         struct sli4_sge *sgl  = NULL;
20329         struct lpfc_dmabuf *dmabuf;
20330         union lpfc_wqe128 *wqe;
20331         int numBdes = 0;
20332         int i = 0;
20333         uint32_t offset = 0; /* accumulated offset in the sg request list */
20334         int inbound = 0; /* number of sg reply entries inbound from firmware */
20335         uint32_t cmd;
20336
20337         if (!pwqeq || !sglq)
20338                 return xritag;
20339
20340         sgl  = (struct sli4_sge *)sglq->sgl;
20341         wqe = &pwqeq->wqe;
20342         pwqeq->iocb.ulpIoTag = pwqeq->iotag;
20343
20344         cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
20345         if (cmd == CMD_XMIT_BLS_RSP64_WQE)
20346                 return sglq->sli4_xritag;
20347         numBdes = pwqeq->rsvd2;
20348         if (numBdes) {
20349                 /* The addrHigh and addrLow fields within the WQE
20350                  * have not been byteswapped yet so there is no
20351                  * need to swap them back.
20352                  */
20353                 if (pwqeq->context3)
20354                         dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
20355                 else
20356                         return xritag;
20357
20358                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
20359                 if (!bpl)
20360                         return xritag;
20361
20362                 for (i = 0; i < numBdes; i++) {
20363                         /* Should already be byte swapped. */
20364                         sgl->addr_hi = bpl->addrHigh;
20365                         sgl->addr_lo = bpl->addrLow;
20366
20367                         sgl->word2 = le32_to_cpu(sgl->word2);
20368                         if ((i+1) == numBdes)
20369                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
20370                         else
20371                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
20372                         /* swap the size field back to the cpu so we
20373                          * can assign it to the sgl.
20374                          */
20375                         bde.tus.w = le32_to_cpu(bpl->tus.w);
20376                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
20377                         /* The offsets in the sgl need to be accumulated
20378                          * separately for the request and reply lists.
20379                          * The request is always first, the reply follows.
20380                          */
20381                         switch (cmd) {
20382                         case CMD_GEN_REQUEST64_WQE:
20383                                 /* add up the reply sg entries */
20384                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
20385                                         inbound++;
20386                                 /* first inbound? reset the offset */
20387                                 if (inbound == 1)
20388                                         offset = 0;
20389                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
20390                                 bf_set(lpfc_sli4_sge_type, sgl,
20391                                         LPFC_SGE_TYPE_DATA);
20392                                 offset += bde.tus.f.bdeSize;
20393                                 break;
20394                         case CMD_FCP_TRSP64_WQE:
20395                                 bf_set(lpfc_sli4_sge_offset, sgl, 0);
20396                                 bf_set(lpfc_sli4_sge_type, sgl,
20397                                         LPFC_SGE_TYPE_DATA);
20398                                 break;
20399                         case CMD_FCP_TSEND64_WQE:
20400                         case CMD_FCP_TRECEIVE64_WQE:
20401                                 bf_set(lpfc_sli4_sge_type, sgl,
20402                                         bpl->tus.f.bdeFlags);
20403                                 if (i < 3)
20404                                         offset = 0;
20405                                 else
20406                                         offset += bde.tus.f.bdeSize;
20407                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
20408                                 break;
20409                         }
20410                         sgl->word2 = cpu_to_le32(sgl->word2);
20411                         bpl++;
20412                         sgl++;
20413                 }
20414         } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
20415                 /* The addrHigh and addrLow fields of the BDE have not
20416                  * been byteswapped yet so they need to be swapped
20417                  * before putting them in the sgl.
20418                  */
20419                 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
20420                 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
20421                 sgl->word2 = le32_to_cpu(sgl->word2);
20422                 bf_set(lpfc_sli4_sge_last, sgl, 1);
20423                 sgl->word2 = cpu_to_le32(sgl->word2);
20424                 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
20425         }
20426         return sglq->sli4_xritag;
20427 }
20428
20429 /**
20430  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
20431  * @phba: Pointer to HBA context object.
20432  * @qp: Pointer to HDW queue.
20433  * @pwqe: Pointer to command WQE.
20434  **/
20435 int
20436 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
20437                     struct lpfc_iocbq *pwqe)
20438 {
20439         union lpfc_wqe128 *wqe = &pwqe->wqe;
20440         struct lpfc_async_xchg_ctx *ctxp;
20441         struct lpfc_queue *wq;
20442         struct lpfc_sglq *sglq;
20443         struct lpfc_sli_ring *pring;
20444         unsigned long iflags;
20445         uint32_t ret = 0;
20446
20447         /* NVME_LS and NVME_LS ABTS requests. */
20448         if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
20449                 pring =  phba->sli4_hba.nvmels_wq->pring;
20450                 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20451                                           qp, wq_access);
20452                 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
20453                 if (!sglq) {
20454                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20455                         return WQE_BUSY;
20456                 }
20457                 pwqe->sli4_lxritag = sglq->sli4_lxritag;
20458                 pwqe->sli4_xritag = sglq->sli4_xritag;
20459                 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
20460                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20461                         return WQE_ERROR;
20462                 }
20463                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
20464                        pwqe->sli4_xritag);
20465                 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
20466                 if (ret) {
20467                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20468                         return ret;
20469                 }
20470
20471                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20472                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20473
20474                 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
20475                 return 0;
20476         }
20477
20478         /* NVME_FCREQ and NVME_ABTS requests */
20479         if (pwqe->iocb_flag & LPFC_IO_NVME ||
20480             pwqe->iocb_flag & LPFC_IO_FCP) {
20481                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
20482                 wq = qp->io_wq;
20483                 pring = wq->pring;
20484
20485                 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
20486
20487                 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20488                                           qp, wq_access);
20489                 ret = lpfc_sli4_wq_put(wq, wqe);
20490                 if (ret) {
20491                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20492                         return ret;
20493                 }
20494                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20495                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20496
20497                 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
20498                 return 0;
20499         }
20500
20501         /* NVMET requests */
20502         if (pwqe->iocb_flag & LPFC_IO_NVMET) {
20503                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
20504                 wq = qp->io_wq;
20505                 pring = wq->pring;
20506
20507                 ctxp = pwqe->context2;
20508                 sglq = ctxp->ctxbuf->sglq;
20509                 if (pwqe->sli4_xritag ==  NO_XRI) {
20510                         pwqe->sli4_lxritag = sglq->sli4_lxritag;
20511                         pwqe->sli4_xritag = sglq->sli4_xritag;
20512                 }
20513                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
20514                        pwqe->sli4_xritag);
20515                 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
20516
20517                 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20518                                           qp, wq_access);
20519                 ret = lpfc_sli4_wq_put(wq, wqe);
20520                 if (ret) {
20521                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20522                         return ret;
20523                 }
20524                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20525                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20526
20527                 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
20528                 return 0;
20529         }
20530         return WQE_ERROR;
20531 }
20532
20533 /**
20534  * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort
20535  * @phba: Pointer to HBA context object.
20536  * @cmdiocb: Pointer to driver command iocb object.
20537  * @cmpl: completion function.
20538  *
20539  * Fill the appropriate fields for the abort WQE and call
20540  * internal routine lpfc_sli4_issue_wqe to send the WQE
20541  * This function is called with hbalock held and no ring_lock held.
20542  *
20543  * RETURNS 0 - SUCCESS
20544  **/
20545
20546 int
20547 lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
20548                             void *cmpl)
20549 {
20550         struct lpfc_vport *vport = cmdiocb->vport;
20551         struct lpfc_iocbq *abtsiocb = NULL;
20552         union lpfc_wqe128 *abtswqe;
20553         struct lpfc_io_buf *lpfc_cmd;
20554         int retval = IOCB_ERROR;
20555         u16 xritag = cmdiocb->sli4_xritag;
20556
20557         /*
20558          * The scsi command can not be in txq and it is in flight because the
20559          * pCmd is still pointing at the SCSI command we have to abort. There
20560          * is no need to search the txcmplq. Just send an abort to the FW.
20561          */
20562
20563         abtsiocb = __lpfc_sli_get_iocbq(phba);
20564         if (!abtsiocb)
20565                 return WQE_NORESOURCE;
20566
20567         /* Indicate the IO is being aborted by the driver. */
20568         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
20569
20570         abtswqe = &abtsiocb->wqe;
20571         memset(abtswqe, 0, sizeof(*abtswqe));
20572
20573         if (lpfc_is_link_up(phba))
20574                 bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);
20575         else
20576                 bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 0);
20577         bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);
20578         abtswqe->abort_cmd.rsrvd5 = 0;
20579         abtswqe->abort_cmd.wqe_com.abort_tag = xritag;
20580         bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);
20581         bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
20582         bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);
20583         bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);
20584         bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
20585         bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);
20586
20587         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
20588         abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;
20589         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
20590         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
20591                 abtsiocb->iocb_flag |= LPFC_IO_FCP;
20592         if (cmdiocb->iocb_flag & LPFC_IO_NVME)
20593                 abtsiocb->iocb_flag |= LPFC_IO_NVME;
20594         if (cmdiocb->iocb_flag & LPFC_IO_FOF)
20595                 abtsiocb->iocb_flag |= LPFC_IO_FOF;
20596         abtsiocb->vport = vport;
20597         abtsiocb->wqe_cmpl = cmpl;
20598
20599         lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);
20600         retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);
20601
20602         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
20603                          "0359 Abort xri x%x, original iotag x%x, "
20604                          "abort cmd iotag x%x retval x%x\n",
20605                          xritag, cmdiocb->iotag, abtsiocb->iotag, retval);
20606
20607         if (retval) {
20608                 cmdiocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
20609                 __lpfc_sli_release_iocbq(phba, abtsiocb);
20610         }
20611
20612         return retval;
20613 }
20614
20615 #ifdef LPFC_MXP_STAT
20616 /**
20617  * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
20618  * @phba: pointer to lpfc hba data structure.
20619  * @hwqid: belong to which HWQ.
20620  *
20621  * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
20622  * 15 seconds after a test case is running.
20623  *
20624  * The user should call lpfc_debugfs_multixripools_write before running a test
20625  * case to clear stat_snapshot_taken. Then the user starts a test case. During
20626  * test case is running, stat_snapshot_taken is incremented by 1 every time when
20627  * this routine is called from heartbeat timer. When stat_snapshot_taken is
20628  * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
20629  **/
20630 void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
20631 {
20632         struct lpfc_sli4_hdw_queue *qp;
20633         struct lpfc_multixri_pool *multixri_pool;
20634         struct lpfc_pvt_pool *pvt_pool;
20635         struct lpfc_pbl_pool *pbl_pool;
20636         u32 txcmplq_cnt;
20637
20638         qp = &phba->sli4_hba.hdwq[hwqid];
20639         multixri_pool = qp->p_multixri_pool;
20640         if (!multixri_pool)
20641                 return;
20642
20643         if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
20644                 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20645                 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20646                 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
20647
20648                 multixri_pool->stat_pbl_count = pbl_pool->count;
20649                 multixri_pool->stat_pvt_count = pvt_pool->count;
20650                 multixri_pool->stat_busy_count = txcmplq_cnt;
20651         }
20652
20653         multixri_pool->stat_snapshot_taken++;
20654 }
20655 #endif
20656
20657 /**
20658  * lpfc_adjust_pvt_pool_count - Adjust private pool count
20659  * @phba: pointer to lpfc hba data structure.
20660  * @hwqid: belong to which HWQ.
20661  *
20662  * This routine moves some XRIs from private to public pool when private pool
20663  * is not busy.
20664  **/
20665 void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
20666 {
20667         struct lpfc_multixri_pool *multixri_pool;
20668         u32 io_req_count;
20669         u32 prev_io_req_count;
20670
20671         multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20672         if (!multixri_pool)
20673                 return;
20674         io_req_count = multixri_pool->io_req_count;
20675         prev_io_req_count = multixri_pool->prev_io_req_count;
20676
20677         if (prev_io_req_count != io_req_count) {
20678                 /* Private pool is busy */
20679                 multixri_pool->prev_io_req_count = io_req_count;
20680         } else {
20681                 /* Private pool is not busy.
20682                  * Move XRIs from private to public pool.
20683                  */
20684                 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
20685         }
20686 }
20687
20688 /**
20689  * lpfc_adjust_high_watermark - Adjust high watermark
20690  * @phba: pointer to lpfc hba data structure.
20691  * @hwqid: belong to which HWQ.
20692  *
20693  * This routine sets high watermark as number of outstanding XRIs,
20694  * but make sure the new value is between xri_limit/2 and xri_limit.
20695  **/
20696 void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
20697 {
20698         u32 new_watermark;
20699         u32 watermark_max;
20700         u32 watermark_min;
20701         u32 xri_limit;
20702         u32 txcmplq_cnt;
20703         u32 abts_io_bufs;
20704         struct lpfc_multixri_pool *multixri_pool;
20705         struct lpfc_sli4_hdw_queue *qp;
20706
20707         qp = &phba->sli4_hba.hdwq[hwqid];
20708         multixri_pool = qp->p_multixri_pool;
20709         if (!multixri_pool)
20710                 return;
20711         xri_limit = multixri_pool->xri_limit;
20712
20713         watermark_max = xri_limit;
20714         watermark_min = xri_limit / 2;
20715
20716         txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
20717         abts_io_bufs = qp->abts_scsi_io_bufs;
20718         abts_io_bufs += qp->abts_nvme_io_bufs;
20719
20720         new_watermark = txcmplq_cnt + abts_io_bufs;
20721         new_watermark = min(watermark_max, new_watermark);
20722         new_watermark = max(watermark_min, new_watermark);
20723         multixri_pool->pvt_pool.high_watermark = new_watermark;
20724
20725 #ifdef LPFC_MXP_STAT
20726         multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
20727                                           new_watermark);
20728 #endif
20729 }
20730
20731 /**
20732  * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
20733  * @phba: pointer to lpfc hba data structure.
20734  * @hwqid: belong to which HWQ.
20735  *
20736  * This routine is called from hearbeat timer when pvt_pool is idle.
20737  * All free XRIs are moved from private to public pool on hwqid with 2 steps.
20738  * The first step moves (all - low_watermark) amount of XRIs.
20739  * The second step moves the rest of XRIs.
20740  **/
20741 void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
20742 {
20743         struct lpfc_pbl_pool *pbl_pool;
20744         struct lpfc_pvt_pool *pvt_pool;
20745         struct lpfc_sli4_hdw_queue *qp;
20746         struct lpfc_io_buf *lpfc_ncmd;
20747         struct lpfc_io_buf *lpfc_ncmd_next;
20748         unsigned long iflag;
20749         struct list_head tmp_list;
20750         u32 tmp_count;
20751
20752         qp = &phba->sli4_hba.hdwq[hwqid];
20753         pbl_pool = &qp->p_multixri_pool->pbl_pool;
20754         pvt_pool = &qp->p_multixri_pool->pvt_pool;
20755         tmp_count = 0;
20756
20757         lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
20758         lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
20759
20760         if (pvt_pool->count > pvt_pool->low_watermark) {
20761                 /* Step 1: move (all - low_watermark) from pvt_pool
20762                  * to pbl_pool
20763                  */
20764
20765                 /* Move low watermark of bufs from pvt_pool to tmp_list */
20766                 INIT_LIST_HEAD(&tmp_list);
20767                 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20768                                          &pvt_pool->list, list) {
20769                         list_move_tail(&lpfc_ncmd->list, &tmp_list);
20770                         tmp_count++;
20771                         if (tmp_count >= pvt_pool->low_watermark)
20772                                 break;
20773                 }
20774
20775                 /* Move all bufs from pvt_pool to pbl_pool */
20776                 list_splice_init(&pvt_pool->list, &pbl_pool->list);
20777
20778                 /* Move all bufs from tmp_list to pvt_pool */
20779                 list_splice(&tmp_list, &pvt_pool->list);
20780
20781                 pbl_pool->count += (pvt_pool->count - tmp_count);
20782                 pvt_pool->count = tmp_count;
20783         } else {
20784                 /* Step 2: move the rest from pvt_pool to pbl_pool */
20785                 list_splice_init(&pvt_pool->list, &pbl_pool->list);
20786                 pbl_pool->count += pvt_pool->count;
20787                 pvt_pool->count = 0;
20788         }
20789
20790         spin_unlock(&pvt_pool->lock);
20791         spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20792 }
20793
20794 /**
20795  * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
20796  * @phba: pointer to lpfc hba data structure
20797  * @qp: pointer to HDW queue
20798  * @pbl_pool: specified public free XRI pool
20799  * @pvt_pool: specified private free XRI pool
20800  * @count: number of XRIs to move
20801  *
20802  * This routine tries to move some free common bufs from the specified pbl_pool
20803  * to the specified pvt_pool. It might move less than count XRIs if there's not
20804  * enough in public pool.
20805  *
20806  * Return:
20807  *   true - if XRIs are successfully moved from the specified pbl_pool to the
20808  *          specified pvt_pool
20809  *   false - if the specified pbl_pool is empty or locked by someone else
20810  **/
20811 static bool
20812 _lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
20813                           struct lpfc_pbl_pool *pbl_pool,
20814                           struct lpfc_pvt_pool *pvt_pool, u32 count)
20815 {
20816         struct lpfc_io_buf *lpfc_ncmd;
20817         struct lpfc_io_buf *lpfc_ncmd_next;
20818         unsigned long iflag;
20819         int ret;
20820
20821         ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
20822         if (ret) {
20823                 if (pbl_pool->count) {
20824                         /* Move a batch of XRIs from public to private pool */
20825                         lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
20826                         list_for_each_entry_safe(lpfc_ncmd,
20827                                                  lpfc_ncmd_next,
20828                                                  &pbl_pool->list,
20829                                                  list) {
20830                                 list_move_tail(&lpfc_ncmd->list,
20831                                                &pvt_pool->list);
20832                                 pvt_pool->count++;
20833                                 pbl_pool->count--;
20834                                 count--;
20835                                 if (count == 0)
20836                                         break;
20837                         }
20838
20839                         spin_unlock(&pvt_pool->lock);
20840                         spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20841                         return true;
20842                 }
20843                 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20844         }
20845
20846         return false;
20847 }
20848
20849 /**
20850  * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
20851  * @phba: pointer to lpfc hba data structure.
20852  * @hwqid: belong to which HWQ.
20853  * @count: number of XRIs to move
20854  *
20855  * This routine tries to find some free common bufs in one of public pools with
20856  * Round Robin method. The search always starts from local hwqid, then the next
20857  * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
20858  * a batch of free common bufs are moved to private pool on hwqid.
20859  * It might move less than count XRIs if there's not enough in public pool.
20860  **/
20861 void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
20862 {
20863         struct lpfc_multixri_pool *multixri_pool;
20864         struct lpfc_multixri_pool *next_multixri_pool;
20865         struct lpfc_pvt_pool *pvt_pool;
20866         struct lpfc_pbl_pool *pbl_pool;
20867         struct lpfc_sli4_hdw_queue *qp;
20868         u32 next_hwqid;
20869         u32 hwq_count;
20870         int ret;
20871
20872         qp = &phba->sli4_hba.hdwq[hwqid];
20873         multixri_pool = qp->p_multixri_pool;
20874         pvt_pool = &multixri_pool->pvt_pool;
20875         pbl_pool = &multixri_pool->pbl_pool;
20876
20877         /* Check if local pbl_pool is available */
20878         ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
20879         if (ret) {
20880 #ifdef LPFC_MXP_STAT
20881                 multixri_pool->local_pbl_hit_count++;
20882 #endif
20883                 return;
20884         }
20885
20886         hwq_count = phba->cfg_hdw_queue;
20887
20888         /* Get the next hwqid which was found last time */
20889         next_hwqid = multixri_pool->rrb_next_hwqid;
20890
20891         do {
20892                 /* Go to next hwq */
20893                 next_hwqid = (next_hwqid + 1) % hwq_count;
20894
20895                 next_multixri_pool =
20896                         phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
20897                 pbl_pool = &next_multixri_pool->pbl_pool;
20898
20899                 /* Check if the public free xri pool is available */
20900                 ret = _lpfc_move_xri_pbl_to_pvt(
20901                         phba, qp, pbl_pool, pvt_pool, count);
20902
20903                 /* Exit while-loop if success or all hwqid are checked */
20904         } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
20905
20906         /* Starting point for the next time */
20907         multixri_pool->rrb_next_hwqid = next_hwqid;
20908
20909         if (!ret) {
20910                 /* stats: all public pools are empty*/
20911                 multixri_pool->pbl_empty_count++;
20912         }
20913
20914 #ifdef LPFC_MXP_STAT
20915         if (ret) {
20916                 if (next_hwqid == hwqid)
20917                         multixri_pool->local_pbl_hit_count++;
20918                 else
20919                         multixri_pool->other_pbl_hit_count++;
20920         }
20921 #endif
20922 }
20923
20924 /**
20925  * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
20926  * @phba: pointer to lpfc hba data structure.
20927  * @hwqid: belong to which HWQ.
20928  *
20929  * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
20930  * low watermark.
20931  **/
20932 void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
20933 {
20934         struct lpfc_multixri_pool *multixri_pool;
20935         struct lpfc_pvt_pool *pvt_pool;
20936
20937         multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20938         pvt_pool = &multixri_pool->pvt_pool;
20939
20940         if (pvt_pool->count < pvt_pool->low_watermark)
20941                 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20942 }
20943
20944 /**
20945  * lpfc_release_io_buf - Return one IO buf back to free pool
20946  * @phba: pointer to lpfc hba data structure.
20947  * @lpfc_ncmd: IO buf to be returned.
20948  * @qp: belong to which HWQ.
20949  *
20950  * This routine returns one IO buf back to free pool. If this is an urgent IO,
20951  * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
20952  * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
20953  * xri_limit.  If cfg_xri_rebalancing==0, the IO buf is returned to
20954  * lpfc_io_buf_list_put.
20955  **/
20956 void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
20957                          struct lpfc_sli4_hdw_queue *qp)
20958 {
20959         unsigned long iflag;
20960         struct lpfc_pbl_pool *pbl_pool;
20961         struct lpfc_pvt_pool *pvt_pool;
20962         struct lpfc_epd_pool *epd_pool;
20963         u32 txcmplq_cnt;
20964         u32 xri_owned;
20965         u32 xri_limit;
20966         u32 abts_io_bufs;
20967
20968         /* MUST zero fields if buffer is reused by another protocol */
20969         lpfc_ncmd->nvmeCmd = NULL;
20970         lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
20971         lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
20972
20973         if (phba->cfg_xpsgl && !phba->nvmet_support &&
20974             !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
20975                 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
20976
20977         if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
20978                 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
20979
20980         if (phba->cfg_xri_rebalancing) {
20981                 if (lpfc_ncmd->expedite) {
20982                         /* Return to expedite pool */
20983                         epd_pool = &phba->epd_pool;
20984                         spin_lock_irqsave(&epd_pool->lock, iflag);
20985                         list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
20986                         epd_pool->count++;
20987                         spin_unlock_irqrestore(&epd_pool->lock, iflag);
20988                         return;
20989                 }
20990
20991                 /* Avoid invalid access if an IO sneaks in and is being rejected
20992                  * just _after_ xri pools are destroyed in lpfc_offline.
20993                  * Nothing much can be done at this point.
20994                  */
20995                 if (!qp->p_multixri_pool)
20996                         return;
20997
20998                 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20999                 pvt_pool = &qp->p_multixri_pool->pvt_pool;
21000
21001                 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21002                 abts_io_bufs = qp->abts_scsi_io_bufs;
21003                 abts_io_bufs += qp->abts_nvme_io_bufs;
21004
21005                 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
21006                 xri_limit = qp->p_multixri_pool->xri_limit;
21007
21008 #ifdef LPFC_MXP_STAT
21009                 if (xri_owned <= xri_limit)
21010                         qp->p_multixri_pool->below_limit_count++;
21011                 else
21012                         qp->p_multixri_pool->above_limit_count++;
21013 #endif
21014
21015                 /* XRI goes to either public or private free xri pool
21016                  *     based on watermark and xri_limit
21017                  */
21018                 if ((pvt_pool->count < pvt_pool->low_watermark) ||
21019                     (xri_owned < xri_limit &&
21020                      pvt_pool->count < pvt_pool->high_watermark)) {
21021                         lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
21022                                                   qp, free_pvt_pool);
21023                         list_add_tail(&lpfc_ncmd->list,
21024                                       &pvt_pool->list);
21025                         pvt_pool->count++;
21026                         spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21027                 } else {
21028                         lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
21029                                                   qp, free_pub_pool);
21030                         list_add_tail(&lpfc_ncmd->list,
21031                                       &pbl_pool->list);
21032                         pbl_pool->count++;
21033                         spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21034                 }
21035         } else {
21036                 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
21037                                           qp, free_xri);
21038                 list_add_tail(&lpfc_ncmd->list,
21039                               &qp->lpfc_io_buf_list_put);
21040                 qp->put_io_bufs++;
21041                 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
21042                                        iflag);
21043         }
21044 }
21045
21046 /**
21047  * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
21048  * @phba: pointer to lpfc hba data structure.
21049  * @qp: pointer to HDW queue
21050  * @pvt_pool: pointer to private pool data structure.
21051  * @ndlp: pointer to lpfc nodelist data structure.
21052  *
21053  * This routine tries to get one free IO buf from private pool.
21054  *
21055  * Return:
21056  *   pointer to one free IO buf - if private pool is not empty
21057  *   NULL - if private pool is empty
21058  **/
21059 static struct lpfc_io_buf *
21060 lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
21061                                   struct lpfc_sli4_hdw_queue *qp,
21062                                   struct lpfc_pvt_pool *pvt_pool,
21063                                   struct lpfc_nodelist *ndlp)
21064 {
21065         struct lpfc_io_buf *lpfc_ncmd;
21066         struct lpfc_io_buf *lpfc_ncmd_next;
21067         unsigned long iflag;
21068
21069         lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
21070         list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21071                                  &pvt_pool->list, list) {
21072                 if (lpfc_test_rrq_active(
21073                         phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
21074                         continue;
21075                 list_del(&lpfc_ncmd->list);
21076                 pvt_pool->count--;
21077                 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21078                 return lpfc_ncmd;
21079         }
21080         spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21081
21082         return NULL;
21083 }
21084
21085 /**
21086  * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
21087  * @phba: pointer to lpfc hba data structure.
21088  *
21089  * This routine tries to get one free IO buf from expedite pool.
21090  *
21091  * Return:
21092  *   pointer to one free IO buf - if expedite pool is not empty
21093  *   NULL - if expedite pool is empty
21094  **/
21095 static struct lpfc_io_buf *
21096 lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
21097 {
21098         struct lpfc_io_buf *lpfc_ncmd;
21099         struct lpfc_io_buf *lpfc_ncmd_next;
21100         unsigned long iflag;
21101         struct lpfc_epd_pool *epd_pool;
21102
21103         epd_pool = &phba->epd_pool;
21104         lpfc_ncmd = NULL;
21105
21106         spin_lock_irqsave(&epd_pool->lock, iflag);
21107         if (epd_pool->count > 0) {
21108                 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21109                                          &epd_pool->list, list) {
21110                         list_del(&lpfc_ncmd->list);
21111                         epd_pool->count--;
21112                         break;
21113                 }
21114         }
21115         spin_unlock_irqrestore(&epd_pool->lock, iflag);
21116
21117         return lpfc_ncmd;
21118 }
21119
21120 /**
21121  * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
21122  * @phba: pointer to lpfc hba data structure.
21123  * @ndlp: pointer to lpfc nodelist data structure.
21124  * @hwqid: belong to which HWQ
21125  * @expedite: 1 means this request is urgent.
21126  *
21127  * This routine will do the following actions and then return a pointer to
21128  * one free IO buf.
21129  *
21130  * 1. If private free xri count is empty, move some XRIs from public to
21131  *    private pool.
21132  * 2. Get one XRI from private free xri pool.
21133  * 3. If we fail to get one from pvt_pool and this is an expedite request,
21134  *    get one free xri from expedite pool.
21135  *
21136  * Note: ndlp is only used on SCSI side for RRQ testing.
21137  *       The caller should pass NULL for ndlp on NVME side.
21138  *
21139  * Return:
21140  *   pointer to one free IO buf - if private pool is not empty
21141  *   NULL - if private pool is empty
21142  **/
21143 static struct lpfc_io_buf *
21144 lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
21145                                     struct lpfc_nodelist *ndlp,
21146                                     int hwqid, int expedite)
21147 {
21148         struct lpfc_sli4_hdw_queue *qp;
21149         struct lpfc_multixri_pool *multixri_pool;
21150         struct lpfc_pvt_pool *pvt_pool;
21151         struct lpfc_io_buf *lpfc_ncmd;
21152
21153         qp = &phba->sli4_hba.hdwq[hwqid];
21154         lpfc_ncmd = NULL;
21155         multixri_pool = qp->p_multixri_pool;
21156         pvt_pool = &multixri_pool->pvt_pool;
21157         multixri_pool->io_req_count++;
21158
21159         /* If pvt_pool is empty, move some XRIs from public to private pool */
21160         if (pvt_pool->count == 0)
21161                 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21162
21163         /* Get one XRI from private free xri pool */
21164         lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
21165
21166         if (lpfc_ncmd) {
21167                 lpfc_ncmd->hdwq = qp;
21168                 lpfc_ncmd->hdwq_no = hwqid;
21169         } else if (expedite) {
21170                 /* If we fail to get one from pvt_pool and this is an expedite
21171                  * request, get one free xri from expedite pool.
21172                  */
21173                 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
21174         }
21175
21176         return lpfc_ncmd;
21177 }
21178
21179 static inline struct lpfc_io_buf *
21180 lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
21181 {
21182         struct lpfc_sli4_hdw_queue *qp;
21183         struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
21184
21185         qp = &phba->sli4_hba.hdwq[idx];
21186         list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
21187                                  &qp->lpfc_io_buf_list_get, list) {
21188                 if (lpfc_test_rrq_active(phba, ndlp,
21189                                          lpfc_cmd->cur_iocbq.sli4_lxritag))
21190                         continue;
21191
21192                 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
21193                         continue;
21194
21195                 list_del_init(&lpfc_cmd->list);
21196                 qp->get_io_bufs--;
21197                 lpfc_cmd->hdwq = qp;
21198                 lpfc_cmd->hdwq_no = idx;
21199                 return lpfc_cmd;
21200         }
21201         return NULL;
21202 }
21203
21204 /**
21205  * lpfc_get_io_buf - Get one IO buffer from free pool
21206  * @phba: The HBA for which this call is being executed.
21207  * @ndlp: pointer to lpfc nodelist data structure.
21208  * @hwqid: belong to which HWQ
21209  * @expedite: 1 means this request is urgent.
21210  *
21211  * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
21212  * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
21213  * a IO buffer from head of @hdwq io_buf_list and returns to caller.
21214  *
21215  * Note: ndlp is only used on SCSI side for RRQ testing.
21216  *       The caller should pass NULL for ndlp on NVME side.
21217  *
21218  * Return codes:
21219  *   NULL - Error
21220  *   Pointer to lpfc_io_buf - Success
21221  **/
21222 struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
21223                                     struct lpfc_nodelist *ndlp,
21224                                     u32 hwqid, int expedite)
21225 {
21226         struct lpfc_sli4_hdw_queue *qp;
21227         unsigned long iflag;
21228         struct lpfc_io_buf *lpfc_cmd;
21229
21230         qp = &phba->sli4_hba.hdwq[hwqid];
21231         lpfc_cmd = NULL;
21232
21233         if (phba->cfg_xri_rebalancing)
21234                 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
21235                         phba, ndlp, hwqid, expedite);
21236         else {
21237                 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
21238                                           qp, alloc_xri_get);
21239                 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
21240                         lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
21241                 if (!lpfc_cmd) {
21242                         lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
21243                                           qp, alloc_xri_put);
21244                         list_splice(&qp->lpfc_io_buf_list_put,
21245                                     &qp->lpfc_io_buf_list_get);
21246                         qp->get_io_bufs += qp->put_io_bufs;
21247                         INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
21248                         qp->put_io_bufs = 0;
21249                         spin_unlock(&qp->io_buf_list_put_lock);
21250                         if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
21251                             expedite)
21252                                 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
21253                 }
21254                 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
21255         }
21256
21257         return lpfc_cmd;
21258 }
21259
21260 /**
21261  * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
21262  * @phba: The HBA for which this call is being executed.
21263  * @lpfc_buf: IO buf structure to append the SGL chunk
21264  *
21265  * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
21266  * and will allocate an SGL chunk if the pool is empty.
21267  *
21268  * Return codes:
21269  *   NULL - Error
21270  *   Pointer to sli4_hybrid_sgl - Success
21271  **/
21272 struct sli4_hybrid_sgl *
21273 lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
21274 {
21275         struct sli4_hybrid_sgl *list_entry = NULL;
21276         struct sli4_hybrid_sgl *tmp = NULL;
21277         struct sli4_hybrid_sgl *allocated_sgl = NULL;
21278         struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21279         struct list_head *buf_list = &hdwq->sgl_list;
21280         unsigned long iflags;
21281
21282         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21283
21284         if (likely(!list_empty(buf_list))) {
21285                 /* break off 1 chunk from the sgl_list */
21286                 list_for_each_entry_safe(list_entry, tmp,
21287                                          buf_list, list_node) {
21288                         list_move_tail(&list_entry->list_node,
21289                                        &lpfc_buf->dma_sgl_xtra_list);
21290                         break;
21291                 }
21292         } else {
21293                 /* allocate more */
21294                 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21295                 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
21296                                    cpu_to_node(hdwq->io_wq->chann));
21297                 if (!tmp) {
21298                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21299                                         "8353 error kmalloc memory for HDWQ "
21300                                         "%d %s\n",
21301                                         lpfc_buf->hdwq_no, __func__);
21302                         return NULL;
21303                 }
21304
21305                 tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
21306                                               GFP_ATOMIC, &tmp->dma_phys_sgl);
21307                 if (!tmp->dma_sgl) {
21308                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21309                                         "8354 error pool_alloc memory for HDWQ "
21310                                         "%d %s\n",
21311                                         lpfc_buf->hdwq_no, __func__);
21312                         kfree(tmp);
21313                         return NULL;
21314                 }
21315
21316                 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21317                 list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
21318         }
21319
21320         allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
21321                                         struct sli4_hybrid_sgl,
21322                                         list_node);
21323
21324         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21325
21326         return allocated_sgl;
21327 }
21328
21329 /**
21330  * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
21331  * @phba: The HBA for which this call is being executed.
21332  * @lpfc_buf: IO buf structure with the SGL chunk
21333  *
21334  * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
21335  *
21336  * Return codes:
21337  *   0 - Success
21338  *   -EINVAL - Error
21339  **/
21340 int
21341 lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
21342 {
21343         int rc = 0;
21344         struct sli4_hybrid_sgl *list_entry = NULL;
21345         struct sli4_hybrid_sgl *tmp = NULL;
21346         struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21347         struct list_head *buf_list = &hdwq->sgl_list;
21348         unsigned long iflags;
21349
21350         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21351
21352         if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
21353                 list_for_each_entry_safe(list_entry, tmp,
21354                                          &lpfc_buf->dma_sgl_xtra_list,
21355                                          list_node) {
21356                         list_move_tail(&list_entry->list_node,
21357                                        buf_list);
21358                 }
21359         } else {
21360                 rc = -EINVAL;
21361         }
21362
21363         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21364         return rc;
21365 }
21366
21367 /**
21368  * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
21369  * @phba: phba object
21370  * @hdwq: hdwq to cleanup sgl buff resources on
21371  *
21372  * This routine frees all SGL chunks of hdwq SGL chunk pool.
21373  *
21374  * Return codes:
21375  *   None
21376  **/
21377 void
21378 lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
21379                        struct lpfc_sli4_hdw_queue *hdwq)
21380 {
21381         struct list_head *buf_list = &hdwq->sgl_list;
21382         struct sli4_hybrid_sgl *list_entry = NULL;
21383         struct sli4_hybrid_sgl *tmp = NULL;
21384         unsigned long iflags;
21385
21386         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21387
21388         /* Free sgl pool */
21389         list_for_each_entry_safe(list_entry, tmp,
21390                                  buf_list, list_node) {
21391                 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
21392                               list_entry->dma_sgl,
21393                               list_entry->dma_phys_sgl);
21394                 list_del(&list_entry->list_node);
21395                 kfree(list_entry);
21396         }
21397
21398         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21399 }
21400
21401 /**
21402  * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
21403  * @phba: The HBA for which this call is being executed.
21404  * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
21405  *
21406  * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
21407  * and will allocate an CMD/RSP buffer if the pool is empty.
21408  *
21409  * Return codes:
21410  *   NULL - Error
21411  *   Pointer to fcp_cmd_rsp_buf - Success
21412  **/
21413 struct fcp_cmd_rsp_buf *
21414 lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21415                               struct lpfc_io_buf *lpfc_buf)
21416 {
21417         struct fcp_cmd_rsp_buf *list_entry = NULL;
21418         struct fcp_cmd_rsp_buf *tmp = NULL;
21419         struct fcp_cmd_rsp_buf *allocated_buf = NULL;
21420         struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21421         struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
21422         unsigned long iflags;
21423
21424         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21425
21426         if (likely(!list_empty(buf_list))) {
21427                 /* break off 1 chunk from the list */
21428                 list_for_each_entry_safe(list_entry, tmp,
21429                                          buf_list,
21430                                          list_node) {
21431                         list_move_tail(&list_entry->list_node,
21432                                        &lpfc_buf->dma_cmd_rsp_list);
21433                         break;
21434                 }
21435         } else {
21436                 /* allocate more */
21437                 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21438                 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
21439                                    cpu_to_node(hdwq->io_wq->chann));
21440                 if (!tmp) {
21441                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21442                                         "8355 error kmalloc memory for HDWQ "
21443                                         "%d %s\n",
21444                                         lpfc_buf->hdwq_no, __func__);
21445                         return NULL;
21446                 }
21447
21448                 tmp->fcp_cmnd = dma_pool_alloc(phba->lpfc_cmd_rsp_buf_pool,
21449                                                 GFP_ATOMIC,
21450                                                 &tmp->fcp_cmd_rsp_dma_handle);
21451
21452                 if (!tmp->fcp_cmnd) {
21453                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21454                                         "8356 error pool_alloc memory for HDWQ "
21455                                         "%d %s\n",
21456                                         lpfc_buf->hdwq_no, __func__);
21457                         kfree(tmp);
21458                         return NULL;
21459                 }
21460
21461                 tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
21462                                 sizeof(struct fcp_cmnd));
21463
21464                 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21465                 list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
21466         }
21467
21468         allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
21469                                         struct fcp_cmd_rsp_buf,
21470                                         list_node);
21471
21472         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21473
21474         return allocated_buf;
21475 }
21476
21477 /**
21478  * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
21479  * @phba: The HBA for which this call is being executed.
21480  * @lpfc_buf: IO buf structure with the CMD/RSP buf
21481  *
21482  * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
21483  *
21484  * Return codes:
21485  *   0 - Success
21486  *   -EINVAL - Error
21487  **/
21488 int
21489 lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21490                               struct lpfc_io_buf *lpfc_buf)
21491 {
21492         int rc = 0;
21493         struct fcp_cmd_rsp_buf *list_entry = NULL;
21494         struct fcp_cmd_rsp_buf *tmp = NULL;
21495         struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21496         struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
21497         unsigned long iflags;
21498
21499         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21500
21501         if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
21502                 list_for_each_entry_safe(list_entry, tmp,
21503                                          &lpfc_buf->dma_cmd_rsp_list,
21504                                          list_node) {
21505                         list_move_tail(&list_entry->list_node,
21506                                        buf_list);
21507                 }
21508         } else {
21509                 rc = -EINVAL;
21510         }
21511
21512         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21513         return rc;
21514 }
21515
21516 /**
21517  * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
21518  * @phba: phba object
21519  * @hdwq: hdwq to cleanup cmd rsp buff resources on
21520  *
21521  * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
21522  *
21523  * Return codes:
21524  *   None
21525  **/
21526 void
21527 lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21528                                struct lpfc_sli4_hdw_queue *hdwq)
21529 {
21530         struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
21531         struct fcp_cmd_rsp_buf *list_entry = NULL;
21532         struct fcp_cmd_rsp_buf *tmp = NULL;
21533         unsigned long iflags;
21534
21535         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21536
21537         /* Free cmd_rsp buf pool */
21538         list_for_each_entry_safe(list_entry, tmp,
21539                                  buf_list,
21540                                  list_node) {
21541                 dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
21542                               list_entry->fcp_cmnd,
21543                               list_entry->fcp_cmd_rsp_dma_handle);
21544                 list_del(&list_entry->list_node);
21545                 kfree(list_entry);
21546         }
21547
21548         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21549 }