Merge tag 'sunxi-fixes-for-5.15-1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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 active
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 active
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 | LPFC_IO_CMF |
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_cmf_sync_cmpl - Process a CMF_SYNC_WQE cmpl
1773  * @phba: Pointer to HBA context object.
1774  * @cmdiocb: Pointer to driver command iocb object.
1775  * @cmf_cmpl: Pointer to completed WCQE.
1776  *
1777  * This routine will inform the driver of any BW adjustments we need
1778  * to make. These changes will be picked up during the next CMF
1779  * timer interrupt. In addition, any BW changes will be logged
1780  * with LOG_CGN_MGMT.
1781  **/
1782 static void
1783 lpfc_cmf_sync_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1784                    struct lpfc_wcqe_complete *cmf_cmpl)
1785 {
1786         union lpfc_wqe128 *wqe;
1787         uint32_t status, info;
1788         uint64_t bw, bwdif, slop;
1789         uint64_t pcent, bwpcent;
1790         int asig, afpin, sigcnt, fpincnt;
1791         int wsigmax, wfpinmax, cg, tdp;
1792         char *s;
1793
1794         /* First check for error */
1795         status = bf_get(lpfc_wcqe_c_status, cmf_cmpl);
1796         if (status) {
1797                 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1798                                 "6211 CMF_SYNC_WQE Error "
1799                                 "req_tag x%x status x%x hwstatus x%x "
1800                                 "tdatap x%x parm x%x\n",
1801                                 bf_get(lpfc_wcqe_c_request_tag, cmf_cmpl),
1802                                 bf_get(lpfc_wcqe_c_status, cmf_cmpl),
1803                                 bf_get(lpfc_wcqe_c_hw_status, cmf_cmpl),
1804                                 cmf_cmpl->total_data_placed,
1805                                 cmf_cmpl->parameter);
1806                 goto out;
1807         }
1808
1809         /* Gather congestion information on a successful cmpl */
1810         info = cmf_cmpl->parameter;
1811         phba->cmf_active_info = info;
1812
1813         /* See if firmware info count is valid or has changed */
1814         if (info > LPFC_MAX_CMF_INFO || phba->cmf_info_per_interval == info)
1815                 info = 0;
1816         else
1817                 phba->cmf_info_per_interval = info;
1818
1819         tdp = bf_get(lpfc_wcqe_c_cmf_bw, cmf_cmpl);
1820         cg = bf_get(lpfc_wcqe_c_cmf_cg, cmf_cmpl);
1821
1822         /* Get BW requirement from firmware */
1823         bw = (uint64_t)tdp * LPFC_CMF_BLK_SIZE;
1824         if (!bw) {
1825                 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1826                                 "6212 CMF_SYNC_WQE x%x: NULL bw\n",
1827                                 bf_get(lpfc_wcqe_c_request_tag, cmf_cmpl));
1828                 goto out;
1829         }
1830
1831         /* Gather information needed for logging if a BW change is required */
1832         wqe = &cmdiocb->wqe;
1833         asig = bf_get(cmf_sync_asig, &wqe->cmf_sync);
1834         afpin = bf_get(cmf_sync_afpin, &wqe->cmf_sync);
1835         fpincnt = bf_get(cmf_sync_wfpincnt, &wqe->cmf_sync);
1836         sigcnt = bf_get(cmf_sync_wsigcnt, &wqe->cmf_sync);
1837         if (phba->cmf_max_bytes_per_interval != bw ||
1838             (asig || afpin || sigcnt || fpincnt)) {
1839                 /* Are we increasing or decreasing BW */
1840                 if (phba->cmf_max_bytes_per_interval <  bw) {
1841                         bwdif = bw - phba->cmf_max_bytes_per_interval;
1842                         s = "Increase";
1843                 } else {
1844                         bwdif = phba->cmf_max_bytes_per_interval - bw;
1845                         s = "Decrease";
1846                 }
1847
1848                 /* What is the change percentage */
1849                 slop = div_u64(phba->cmf_link_byte_count, 200); /*For rounding*/
1850                 pcent = div64_u64(bwdif * 100 + slop,
1851                                   phba->cmf_link_byte_count);
1852                 bwpcent = div64_u64(bw * 100 + slop,
1853                                     phba->cmf_link_byte_count);
1854                 if (asig) {
1855                         lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1856                                         "6237 BW Threshold %lld%% (%lld): "
1857                                         "%lld%% %s: Signal Alarm: cg:%d "
1858                                         "Info:%u\n",
1859                                         bwpcent, bw, pcent, s, cg,
1860                                         phba->cmf_active_info);
1861                 } else if (afpin) {
1862                         lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1863                                         "6238 BW Threshold %lld%% (%lld): "
1864                                         "%lld%% %s: FPIN Alarm: cg:%d "
1865                                         "Info:%u\n",
1866                                         bwpcent, bw, pcent, s, cg,
1867                                         phba->cmf_active_info);
1868                 } else if (sigcnt) {
1869                         wsigmax = bf_get(cmf_sync_wsigmax, &wqe->cmf_sync);
1870                         lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1871                                         "6239 BW Threshold %lld%% (%lld): "
1872                                         "%lld%% %s: Signal Warning: "
1873                                         "Cnt %d Max %d: cg:%d Info:%u\n",
1874                                         bwpcent, bw, pcent, s, sigcnt,
1875                                         wsigmax, cg, phba->cmf_active_info);
1876                 } else if (fpincnt) {
1877                         wfpinmax = bf_get(cmf_sync_wfpinmax, &wqe->cmf_sync);
1878                         lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1879                                         "6240 BW Threshold %lld%% (%lld): "
1880                                         "%lld%% %s: FPIN Warning: "
1881                                         "Cnt %d Max %d: cg:%d Info:%u\n",
1882                                         bwpcent, bw, pcent, s, fpincnt,
1883                                         wfpinmax, cg, phba->cmf_active_info);
1884                 } else {
1885                         lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1886                                         "6241 BW Threshold %lld%% (%lld): "
1887                                         "CMF %lld%% %s: cg:%d Info:%u\n",
1888                                         bwpcent, bw, pcent, s, cg,
1889                                         phba->cmf_active_info);
1890                 }
1891         } else if (info) {
1892                 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1893                                 "6246 Info Threshold %u\n", info);
1894         }
1895
1896         /* Save BW change to be picked up during next timer interrupt */
1897         phba->cmf_last_sync_bw = bw;
1898 out:
1899         lpfc_sli_release_iocbq(phba, cmdiocb);
1900 }
1901
1902 /**
1903  * lpfc_issue_cmf_sync_wqe - Issue a CMF_SYNC_WQE
1904  * @phba: Pointer to HBA context object.
1905  * @ms:   ms to set in WQE interval, 0 means use init op
1906  * @total: Total rcv bytes for this interval
1907  *
1908  * This routine is called every CMF timer interrupt. Its purpose is
1909  * to issue a CMF_SYNC_WQE to the firmware to inform it of any events
1910  * that may indicate we have congestion (FPINs or Signals). Upon
1911  * completion, the firmware will indicate any BW restrictions the
1912  * driver may need to take.
1913  **/
1914 int
1915 lpfc_issue_cmf_sync_wqe(struct lpfc_hba *phba, u32 ms, u64 total)
1916 {
1917         union lpfc_wqe128 *wqe;
1918         struct lpfc_iocbq *sync_buf;
1919         unsigned long iflags;
1920         u32 ret_val;
1921         u32 atot, wtot, max;
1922
1923         /* First address any alarm / warning activity */
1924         atot = atomic_xchg(&phba->cgn_sync_alarm_cnt, 0);
1925         wtot = atomic_xchg(&phba->cgn_sync_warn_cnt, 0);
1926
1927         /* ONLY Managed mode will send the CMF_SYNC_WQE to the HBA */
1928         if (phba->cmf_active_mode != LPFC_CFG_MANAGED ||
1929             phba->link_state == LPFC_LINK_DOWN)
1930                 return 0;
1931
1932         spin_lock_irqsave(&phba->hbalock, iflags);
1933         sync_buf = __lpfc_sli_get_iocbq(phba);
1934         if (!sync_buf) {
1935                 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT,
1936                                 "6213 No available WQEs for CMF_SYNC_WQE\n");
1937                 ret_val = ENOMEM;
1938                 goto out_unlock;
1939         }
1940
1941         wqe = &sync_buf->wqe;
1942
1943         /* WQEs are reused.  Clear stale data and set key fields to zero */
1944         memset(wqe, 0, sizeof(*wqe));
1945
1946         /* If this is the very first CMF_SYNC_WQE, issue an init operation */
1947         if (!ms) {
1948                 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1949                                 "6441 CMF Init %d - CMF_SYNC_WQE\n",
1950                                 phba->fc_eventTag);
1951                 bf_set(cmf_sync_op, &wqe->cmf_sync, 1); /* 1=init */
1952                 bf_set(cmf_sync_interval, &wqe->cmf_sync, LPFC_CMF_INTERVAL);
1953                 goto initpath;
1954         }
1955
1956         bf_set(cmf_sync_op, &wqe->cmf_sync, 0); /* 0=recalc */
1957         bf_set(cmf_sync_interval, &wqe->cmf_sync, ms);
1958
1959         /* Check for alarms / warnings */
1960         if (atot) {
1961                 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1962                         /* We hit an Signal alarm condition */
1963                         bf_set(cmf_sync_asig, &wqe->cmf_sync, 1);
1964                 } else {
1965                         /* We hit a FPIN alarm condition */
1966                         bf_set(cmf_sync_afpin, &wqe->cmf_sync, 1);
1967                 }
1968         } else if (wtot) {
1969                 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
1970                     phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1971                         /* We hit an Signal warning condition */
1972                         max = LPFC_SEC_TO_MSEC / lpfc_fabric_cgn_frequency *
1973                                 lpfc_acqe_cgn_frequency;
1974                         bf_set(cmf_sync_wsigmax, &wqe->cmf_sync, max);
1975                         bf_set(cmf_sync_wsigcnt, &wqe->cmf_sync, wtot);
1976                 } else {
1977                         /* We hit a FPIN warning condition */
1978                         bf_set(cmf_sync_wfpinmax, &wqe->cmf_sync, 1);
1979                         bf_set(cmf_sync_wfpincnt, &wqe->cmf_sync, 1);
1980                 }
1981         }
1982
1983         /* Update total read blocks during previous timer interval */
1984         wqe->cmf_sync.read_bytes = (u32)(total / LPFC_CMF_BLK_SIZE);
1985
1986 initpath:
1987         bf_set(cmf_sync_ver, &wqe->cmf_sync, LPFC_CMF_SYNC_VER);
1988         wqe->cmf_sync.event_tag = phba->fc_eventTag;
1989         bf_set(cmf_sync_cmnd, &wqe->cmf_sync, CMD_CMF_SYNC_WQE);
1990
1991         /* Setup reqtag to match the wqe completion. */
1992         bf_set(cmf_sync_reqtag, &wqe->cmf_sync, sync_buf->iotag);
1993
1994         bf_set(cmf_sync_qosd, &wqe->cmf_sync, 1);
1995
1996         bf_set(cmf_sync_cmd_type, &wqe->cmf_sync, CMF_SYNC_COMMAND);
1997         bf_set(cmf_sync_wqec, &wqe->cmf_sync, 1);
1998         bf_set(cmf_sync_cqid, &wqe->cmf_sync, LPFC_WQE_CQ_ID_DEFAULT);
1999
2000         sync_buf->vport = phba->pport;
2001         sync_buf->wqe_cmpl = lpfc_cmf_sync_cmpl;
2002         sync_buf->iocb_cmpl = NULL;
2003         sync_buf->context1 = NULL;
2004         sync_buf->context2 = NULL;
2005         sync_buf->context3 = NULL;
2006         sync_buf->sli4_xritag = NO_XRI;
2007
2008         sync_buf->iocb_flag |= LPFC_IO_CMF;
2009         ret_val = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], sync_buf);
2010         if (ret_val)
2011                 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
2012                                 "6214 Cannot issue CMF_SYNC_WQE: x%x\n",
2013                                 ret_val);
2014 out_unlock:
2015         spin_unlock_irqrestore(&phba->hbalock, iflags);
2016         return ret_val;
2017 }
2018
2019 /**
2020  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
2021  * @phba: Pointer to HBA context object.
2022  * @pring: Pointer to driver SLI ring object.
2023  *
2024  * This function is called with hbalock held and the caller must post the
2025  * iocb without releasing the lock. If the caller releases the lock,
2026  * iocb slot returned by the function is not guaranteed to be available.
2027  * The function returns pointer to the next available iocb slot if there
2028  * is available slot in the ring, else it returns NULL.
2029  * If the get index of the ring is ahead of the put index, the function
2030  * will post an error attention event to the worker thread to take the
2031  * HBA to offline state.
2032  **/
2033 static IOCB_t *
2034 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2035 {
2036         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2037         uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
2038
2039         lockdep_assert_held(&phba->hbalock);
2040
2041         if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
2042            (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
2043                 pring->sli.sli3.next_cmdidx = 0;
2044
2045         if (unlikely(pring->sli.sli3.local_getidx ==
2046                 pring->sli.sli3.next_cmdidx)) {
2047
2048                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
2049
2050                 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
2051                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2052                                         "0315 Ring %d issue: portCmdGet %d "
2053                                         "is bigger than cmd ring %d\n",
2054                                         pring->ringno,
2055                                         pring->sli.sli3.local_getidx,
2056                                         max_cmd_idx);
2057
2058                         phba->link_state = LPFC_HBA_ERROR;
2059                         /*
2060                          * All error attention handlers are posted to
2061                          * worker thread
2062                          */
2063                         phba->work_ha |= HA_ERATT;
2064                         phba->work_hs = HS_FFER3;
2065
2066                         lpfc_worker_wake_up(phba);
2067
2068                         return NULL;
2069                 }
2070
2071                 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
2072                         return NULL;
2073         }
2074
2075         return lpfc_cmd_iocb(phba, pring);
2076 }
2077
2078 /**
2079  * lpfc_sli_next_iotag - Get an iotag for the iocb
2080  * @phba: Pointer to HBA context object.
2081  * @iocbq: Pointer to driver iocb object.
2082  *
2083  * This function gets an iotag for the iocb. If there is no unused iotag and
2084  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
2085  * array and assigns a new iotag.
2086  * The function returns the allocated iotag if successful, else returns zero.
2087  * Zero is not a valid iotag.
2088  * The caller is not required to hold any lock.
2089  **/
2090 uint16_t
2091 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
2092 {
2093         struct lpfc_iocbq **new_arr;
2094         struct lpfc_iocbq **old_arr;
2095         size_t new_len;
2096         struct lpfc_sli *psli = &phba->sli;
2097         uint16_t iotag;
2098
2099         spin_lock_irq(&phba->hbalock);
2100         iotag = psli->last_iotag;
2101         if(++iotag < psli->iocbq_lookup_len) {
2102                 psli->last_iotag = iotag;
2103                 psli->iocbq_lookup[iotag] = iocbq;
2104                 spin_unlock_irq(&phba->hbalock);
2105                 iocbq->iotag = iotag;
2106                 return iotag;
2107         } else if (psli->iocbq_lookup_len < (0xffff
2108                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
2109                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2110                 spin_unlock_irq(&phba->hbalock);
2111                 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
2112                                   GFP_KERNEL);
2113                 if (new_arr) {
2114                         spin_lock_irq(&phba->hbalock);
2115                         old_arr = psli->iocbq_lookup;
2116                         if (new_len <= psli->iocbq_lookup_len) {
2117                                 /* highly unprobable case */
2118                                 kfree(new_arr);
2119                                 iotag = psli->last_iotag;
2120                                 if(++iotag < psli->iocbq_lookup_len) {
2121                                         psli->last_iotag = iotag;
2122                                         psli->iocbq_lookup[iotag] = iocbq;
2123                                         spin_unlock_irq(&phba->hbalock);
2124                                         iocbq->iotag = iotag;
2125                                         return iotag;
2126                                 }
2127                                 spin_unlock_irq(&phba->hbalock);
2128                                 return 0;
2129                         }
2130                         if (psli->iocbq_lookup)
2131                                 memcpy(new_arr, old_arr,
2132                                        ((psli->last_iotag  + 1) *
2133                                         sizeof (struct lpfc_iocbq *)));
2134                         psli->iocbq_lookup = new_arr;
2135                         psli->iocbq_lookup_len = new_len;
2136                         psli->last_iotag = iotag;
2137                         psli->iocbq_lookup[iotag] = iocbq;
2138                         spin_unlock_irq(&phba->hbalock);
2139                         iocbq->iotag = iotag;
2140                         kfree(old_arr);
2141                         return iotag;
2142                 }
2143         } else
2144                 spin_unlock_irq(&phba->hbalock);
2145
2146         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2147                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
2148                         psli->last_iotag);
2149
2150         return 0;
2151 }
2152
2153 /**
2154  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
2155  * @phba: Pointer to HBA context object.
2156  * @pring: Pointer to driver SLI ring object.
2157  * @iocb: Pointer to iocb slot in the ring.
2158  * @nextiocb: Pointer to driver iocb object which need to be
2159  *            posted to firmware.
2160  *
2161  * This function is called to post a new iocb to the firmware. This
2162  * function copies the new iocb to ring iocb slot and updates the
2163  * ring pointers. It adds the new iocb to txcmplq if there is
2164  * a completion call back for this iocb else the function will free the
2165  * iocb object.  The hbalock is asserted held in the code path calling
2166  * this routine.
2167  **/
2168 static void
2169 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2170                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
2171 {
2172         /*
2173          * Set up an iotag
2174          */
2175         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
2176
2177
2178         if (pring->ringno == LPFC_ELS_RING) {
2179                 lpfc_debugfs_slow_ring_trc(phba,
2180                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2181                         *(((uint32_t *) &nextiocb->iocb) + 4),
2182                         *(((uint32_t *) &nextiocb->iocb) + 6),
2183                         *(((uint32_t *) &nextiocb->iocb) + 7));
2184         }
2185
2186         /*
2187          * Issue iocb command to adapter
2188          */
2189         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
2190         wmb();
2191         pring->stats.iocb_cmd++;
2192
2193         /*
2194          * If there is no completion routine to call, we can release the
2195          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
2196          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
2197          */
2198         if (nextiocb->iocb_cmpl)
2199                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
2200         else
2201                 __lpfc_sli_release_iocbq(phba, nextiocb);
2202
2203         /*
2204          * Let the HBA know what IOCB slot will be the next one the
2205          * driver will put a command into.
2206          */
2207         pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
2208         writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
2209 }
2210
2211 /**
2212  * lpfc_sli_update_full_ring - Update the chip attention register
2213  * @phba: Pointer to HBA context object.
2214  * @pring: Pointer to driver SLI ring object.
2215  *
2216  * The caller is not required to hold any lock for calling this function.
2217  * This function updates the chip attention bits for the ring to inform firmware
2218  * that there are pending work to be done for this ring and requests an
2219  * interrupt when there is space available in the ring. This function is
2220  * called when the driver is unable to post more iocbs to the ring due
2221  * to unavailability of space in the ring.
2222  **/
2223 static void
2224 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2225 {
2226         int ringno = pring->ringno;
2227
2228         pring->flag |= LPFC_CALL_RING_AVAILABLE;
2229
2230         wmb();
2231
2232         /*
2233          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
2234          * The HBA will tell us when an IOCB entry is available.
2235          */
2236         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
2237         readl(phba->CAregaddr); /* flush */
2238
2239         pring->stats.iocb_cmd_full++;
2240 }
2241
2242 /**
2243  * lpfc_sli_update_ring - Update chip attention register
2244  * @phba: Pointer to HBA context object.
2245  * @pring: Pointer to driver SLI ring object.
2246  *
2247  * This function updates the chip attention register bit for the
2248  * given ring to inform HBA that there is more work to be done
2249  * in this ring. The caller is not required to hold any lock.
2250  **/
2251 static void
2252 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2253 {
2254         int ringno = pring->ringno;
2255
2256         /*
2257          * Tell the HBA that there is work to do in this ring.
2258          */
2259         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
2260                 wmb();
2261                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
2262                 readl(phba->CAregaddr); /* flush */
2263         }
2264 }
2265
2266 /**
2267  * lpfc_sli_resume_iocb - Process iocbs in the txq
2268  * @phba: Pointer to HBA context object.
2269  * @pring: Pointer to driver SLI ring object.
2270  *
2271  * This function is called with hbalock held to post pending iocbs
2272  * in the txq to the firmware. This function is called when driver
2273  * detects space available in the ring.
2274  **/
2275 static void
2276 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2277 {
2278         IOCB_t *iocb;
2279         struct lpfc_iocbq *nextiocb;
2280
2281         lockdep_assert_held(&phba->hbalock);
2282
2283         /*
2284          * Check to see if:
2285          *  (a) there is anything on the txq to send
2286          *  (b) link is up
2287          *  (c) link attention events can be processed (fcp ring only)
2288          *  (d) IOCB processing is not blocked by the outstanding mbox command.
2289          */
2290
2291         if (lpfc_is_link_up(phba) &&
2292             (!list_empty(&pring->txq)) &&
2293             (pring->ringno != LPFC_FCP_RING ||
2294              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
2295
2296                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2297                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
2298                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2299
2300                 if (iocb)
2301                         lpfc_sli_update_ring(phba, pring);
2302                 else
2303                         lpfc_sli_update_full_ring(phba, pring);
2304         }
2305
2306         return;
2307 }
2308
2309 /**
2310  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
2311  * @phba: Pointer to HBA context object.
2312  * @hbqno: HBQ number.
2313  *
2314  * This function is called with hbalock held to get the next
2315  * available slot for the given HBQ. If there is free slot
2316  * available for the HBQ it will return pointer to the next available
2317  * HBQ entry else it will return NULL.
2318  **/
2319 static struct lpfc_hbq_entry *
2320 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
2321 {
2322         struct hbq_s *hbqp = &phba->hbqs[hbqno];
2323
2324         lockdep_assert_held(&phba->hbalock);
2325
2326         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
2327             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
2328                 hbqp->next_hbqPutIdx = 0;
2329
2330         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
2331                 uint32_t raw_index = phba->hbq_get[hbqno];
2332                 uint32_t getidx = le32_to_cpu(raw_index);
2333
2334                 hbqp->local_hbqGetIdx = getidx;
2335
2336                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
2337                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2338                                         "1802 HBQ %d: local_hbqGetIdx "
2339                                         "%u is > than hbqp->entry_count %u\n",
2340                                         hbqno, hbqp->local_hbqGetIdx,
2341                                         hbqp->entry_count);
2342
2343                         phba->link_state = LPFC_HBA_ERROR;
2344                         return NULL;
2345                 }
2346
2347                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
2348                         return NULL;
2349         }
2350
2351         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
2352                         hbqp->hbqPutIdx;
2353 }
2354
2355 /**
2356  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
2357  * @phba: Pointer to HBA context object.
2358  *
2359  * This function is called with no lock held to free all the
2360  * hbq buffers while uninitializing the SLI interface. It also
2361  * frees the HBQ buffers returned by the firmware but not yet
2362  * processed by the upper layers.
2363  **/
2364 void
2365 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
2366 {
2367         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2368         struct hbq_dmabuf *hbq_buf;
2369         unsigned long flags;
2370         int i, hbq_count;
2371
2372         hbq_count = lpfc_sli_hbq_count();
2373         /* Return all memory used by all HBQs */
2374         spin_lock_irqsave(&phba->hbalock, flags);
2375         for (i = 0; i < hbq_count; ++i) {
2376                 list_for_each_entry_safe(dmabuf, next_dmabuf,
2377                                 &phba->hbqs[i].hbq_buffer_list, list) {
2378                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
2379                         list_del(&hbq_buf->dbuf.list);
2380                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2381                 }
2382                 phba->hbqs[i].buffer_count = 0;
2383         }
2384
2385         /* Mark the HBQs not in use */
2386         phba->hbq_in_use = 0;
2387         spin_unlock_irqrestore(&phba->hbalock, flags);
2388 }
2389
2390 /**
2391  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
2392  * @phba: Pointer to HBA context object.
2393  * @hbqno: HBQ number.
2394  * @hbq_buf: Pointer to HBQ buffer.
2395  *
2396  * This function is called with the hbalock held to post a
2397  * hbq buffer to the firmware. If the function finds an empty
2398  * slot in the HBQ, it will post the buffer. The function will return
2399  * pointer to the hbq entry if it successfully post the buffer
2400  * else it will return NULL.
2401  **/
2402 static int
2403 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
2404                          struct hbq_dmabuf *hbq_buf)
2405 {
2406         lockdep_assert_held(&phba->hbalock);
2407         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2408 }
2409
2410 /**
2411  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2412  * @phba: Pointer to HBA context object.
2413  * @hbqno: HBQ number.
2414  * @hbq_buf: Pointer to HBQ buffer.
2415  *
2416  * This function is called with the hbalock held to post a hbq buffer to the
2417  * firmware. If the function finds an empty slot in the HBQ, it will post the
2418  * buffer and place it on the hbq_buffer_list. The function will return zero if
2419  * it successfully post the buffer else it will return an error.
2420  **/
2421 static int
2422 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2423                             struct hbq_dmabuf *hbq_buf)
2424 {
2425         struct lpfc_hbq_entry *hbqe;
2426         dma_addr_t physaddr = hbq_buf->dbuf.phys;
2427
2428         lockdep_assert_held(&phba->hbalock);
2429         /* Get next HBQ entry slot to use */
2430         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2431         if (hbqe) {
2432                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2433
2434                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2435                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
2436                 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
2437                 hbqe->bde.tus.f.bdeFlags = 0;
2438                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2439                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2440                                 /* Sync SLIM */
2441                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2442                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
2443                                 /* flush */
2444                 readl(phba->hbq_put + hbqno);
2445                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
2446                 return 0;
2447         } else
2448                 return -ENOMEM;
2449 }
2450
2451 /**
2452  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2453  * @phba: Pointer to HBA context object.
2454  * @hbqno: HBQ number.
2455  * @hbq_buf: Pointer to HBQ buffer.
2456  *
2457  * This function is called with the hbalock held to post an RQE to the SLI4
2458  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2459  * the hbq_buffer_list and return zero, otherwise it will return an error.
2460  **/
2461 static int
2462 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2463                             struct hbq_dmabuf *hbq_buf)
2464 {
2465         int rc;
2466         struct lpfc_rqe hrqe;
2467         struct lpfc_rqe drqe;
2468         struct lpfc_queue *hrq;
2469         struct lpfc_queue *drq;
2470
2471         if (hbqno != LPFC_ELS_HBQ)
2472                 return 1;
2473         hrq = phba->sli4_hba.hdr_rq;
2474         drq = phba->sli4_hba.dat_rq;
2475
2476         lockdep_assert_held(&phba->hbalock);
2477         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2478         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2479         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2480         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
2481         rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
2482         if (rc < 0)
2483                 return rc;
2484         hbq_buf->tag = (rc | (hbqno << 16));
2485         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2486         return 0;
2487 }
2488
2489 /* HBQ for ELS and CT traffic. */
2490 static struct lpfc_hbq_init lpfc_els_hbq = {
2491         .rn = 1,
2492         .entry_count = 256,
2493         .mask_count = 0,
2494         .profile = 0,
2495         .ring_mask = (1 << LPFC_ELS_RING),
2496         .buffer_count = 0,
2497         .init_count = 40,
2498         .add_count = 40,
2499 };
2500
2501 /* Array of HBQs */
2502 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
2503         &lpfc_els_hbq,
2504 };
2505
2506 /**
2507  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
2508  * @phba: Pointer to HBA context object.
2509  * @hbqno: HBQ number.
2510  * @count: Number of HBQ buffers to be posted.
2511  *
2512  * This function is called with no lock held to post more hbq buffers to the
2513  * given HBQ. The function returns the number of HBQ buffers successfully
2514  * posted.
2515  **/
2516 static int
2517 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2518 {
2519         uint32_t i, posted = 0;
2520         unsigned long flags;
2521         struct hbq_dmabuf *hbq_buffer;
2522         LIST_HEAD(hbq_buf_list);
2523         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
2524                 return 0;
2525
2526         if ((phba->hbqs[hbqno].buffer_count + count) >
2527             lpfc_hbq_defs[hbqno]->entry_count)
2528                 count = lpfc_hbq_defs[hbqno]->entry_count -
2529                                         phba->hbqs[hbqno].buffer_count;
2530         if (!count)
2531                 return 0;
2532         /* Allocate HBQ entries */
2533         for (i = 0; i < count; i++) {
2534                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2535                 if (!hbq_buffer)
2536                         break;
2537                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2538         }
2539         /* Check whether HBQ is still in use */
2540         spin_lock_irqsave(&phba->hbalock, flags);
2541         if (!phba->hbq_in_use)
2542                 goto err;
2543         while (!list_empty(&hbq_buf_list)) {
2544                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2545                                  dbuf.list);
2546                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2547                                       (hbqno << 16));
2548                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
2549                         phba->hbqs[hbqno].buffer_count++;
2550                         posted++;
2551                 } else
2552                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2553         }
2554         spin_unlock_irqrestore(&phba->hbalock, flags);
2555         return posted;
2556 err:
2557         spin_unlock_irqrestore(&phba->hbalock, flags);
2558         while (!list_empty(&hbq_buf_list)) {
2559                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2560                                  dbuf.list);
2561                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2562         }
2563         return 0;
2564 }
2565
2566 /**
2567  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
2568  * @phba: Pointer to HBA context object.
2569  * @qno: HBQ number.
2570  *
2571  * This function posts more buffers to the HBQ. This function
2572  * is called with no lock held. The function returns the number of HBQ entries
2573  * successfully allocated.
2574  **/
2575 int
2576 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
2577 {
2578         if (phba->sli_rev == LPFC_SLI_REV4)
2579                 return 0;
2580         else
2581                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2582                                          lpfc_hbq_defs[qno]->add_count);
2583 }
2584
2585 /**
2586  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
2587  * @phba: Pointer to HBA context object.
2588  * @qno:  HBQ queue number.
2589  *
2590  * This function is called from SLI initialization code path with
2591  * no lock held to post initial HBQ buffers to firmware. The
2592  * function returns the number of HBQ entries successfully allocated.
2593  **/
2594 static int
2595 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2596 {
2597         if (phba->sli_rev == LPFC_SLI_REV4)
2598                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2599                                         lpfc_hbq_defs[qno]->entry_count);
2600         else
2601                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2602                                          lpfc_hbq_defs[qno]->init_count);
2603 }
2604
2605 /*
2606  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2607  *
2608  * This function removes the first hbq buffer on an hbq list and returns a
2609  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2610  **/
2611 static struct hbq_dmabuf *
2612 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2613 {
2614         struct lpfc_dmabuf *d_buf;
2615
2616         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2617         if (!d_buf)
2618                 return NULL;
2619         return container_of(d_buf, struct hbq_dmabuf, dbuf);
2620 }
2621
2622 /**
2623  * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2624  * @phba: Pointer to HBA context object.
2625  * @hrq: HBQ number.
2626  *
2627  * This function removes the first RQ buffer on an RQ buffer list and returns a
2628  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2629  **/
2630 static struct rqb_dmabuf *
2631 lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2632 {
2633         struct lpfc_dmabuf *h_buf;
2634         struct lpfc_rqb *rqbp;
2635
2636         rqbp = hrq->rqbp;
2637         list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2638                          struct lpfc_dmabuf, list);
2639         if (!h_buf)
2640                 return NULL;
2641         rqbp->buffer_count--;
2642         return container_of(h_buf, struct rqb_dmabuf, hbuf);
2643 }
2644
2645 /**
2646  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
2647  * @phba: Pointer to HBA context object.
2648  * @tag: Tag of the hbq buffer.
2649  *
2650  * This function searches for the hbq buffer associated with the given tag in
2651  * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2652  * otherwise it returns NULL.
2653  **/
2654 static struct hbq_dmabuf *
2655 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2656 {
2657         struct lpfc_dmabuf *d_buf;
2658         struct hbq_dmabuf *hbq_buf;
2659         uint32_t hbqno;
2660
2661         hbqno = tag >> 16;
2662         if (hbqno >= LPFC_MAX_HBQS)
2663                 return NULL;
2664
2665         spin_lock_irq(&phba->hbalock);
2666         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2667                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2668                 if (hbq_buf->tag == tag) {
2669                         spin_unlock_irq(&phba->hbalock);
2670                         return hbq_buf;
2671                 }
2672         }
2673         spin_unlock_irq(&phba->hbalock);
2674         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2675                         "1803 Bad hbq tag. Data: x%x x%x\n",
2676                         tag, phba->hbqs[tag >> 16].buffer_count);
2677         return NULL;
2678 }
2679
2680 /**
2681  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2682  * @phba: Pointer to HBA context object.
2683  * @hbq_buffer: Pointer to HBQ buffer.
2684  *
2685  * This function is called with hbalock. This function gives back
2686  * the hbq buffer to firmware. If the HBQ does not have space to
2687  * post the buffer, it will free the buffer.
2688  **/
2689 void
2690 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2691 {
2692         uint32_t hbqno;
2693
2694         if (hbq_buffer) {
2695                 hbqno = hbq_buffer->tag >> 16;
2696                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2697                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2698         }
2699 }
2700
2701 /**
2702  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2703  * @mbxCommand: mailbox command code.
2704  *
2705  * This function is called by the mailbox event handler function to verify
2706  * that the completed mailbox command is a legitimate mailbox command. If the
2707  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2708  * and the mailbox event handler will take the HBA offline.
2709  **/
2710 static int
2711 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2712 {
2713         uint8_t ret;
2714
2715         switch (mbxCommand) {
2716         case MBX_LOAD_SM:
2717         case MBX_READ_NV:
2718         case MBX_WRITE_NV:
2719         case MBX_WRITE_VPARMS:
2720         case MBX_RUN_BIU_DIAG:
2721         case MBX_INIT_LINK:
2722         case MBX_DOWN_LINK:
2723         case MBX_CONFIG_LINK:
2724         case MBX_CONFIG_RING:
2725         case MBX_RESET_RING:
2726         case MBX_READ_CONFIG:
2727         case MBX_READ_RCONFIG:
2728         case MBX_READ_SPARM:
2729         case MBX_READ_STATUS:
2730         case MBX_READ_RPI:
2731         case MBX_READ_XRI:
2732         case MBX_READ_REV:
2733         case MBX_READ_LNK_STAT:
2734         case MBX_REG_LOGIN:
2735         case MBX_UNREG_LOGIN:
2736         case MBX_CLEAR_LA:
2737         case MBX_DUMP_MEMORY:
2738         case MBX_DUMP_CONTEXT:
2739         case MBX_RUN_DIAGS:
2740         case MBX_RESTART:
2741         case MBX_UPDATE_CFG:
2742         case MBX_DOWN_LOAD:
2743         case MBX_DEL_LD_ENTRY:
2744         case MBX_RUN_PROGRAM:
2745         case MBX_SET_MASK:
2746         case MBX_SET_VARIABLE:
2747         case MBX_UNREG_D_ID:
2748         case MBX_KILL_BOARD:
2749         case MBX_CONFIG_FARP:
2750         case MBX_BEACON:
2751         case MBX_LOAD_AREA:
2752         case MBX_RUN_BIU_DIAG64:
2753         case MBX_CONFIG_PORT:
2754         case MBX_READ_SPARM64:
2755         case MBX_READ_RPI64:
2756         case MBX_REG_LOGIN64:
2757         case MBX_READ_TOPOLOGY:
2758         case MBX_WRITE_WWN:
2759         case MBX_SET_DEBUG:
2760         case MBX_LOAD_EXP_ROM:
2761         case MBX_ASYNCEVT_ENABLE:
2762         case MBX_REG_VPI:
2763         case MBX_UNREG_VPI:
2764         case MBX_HEARTBEAT:
2765         case MBX_PORT_CAPABILITIES:
2766         case MBX_PORT_IOV_CONTROL:
2767         case MBX_SLI4_CONFIG:
2768         case MBX_SLI4_REQ_FTRS:
2769         case MBX_REG_FCFI:
2770         case MBX_UNREG_FCFI:
2771         case MBX_REG_VFI:
2772         case MBX_UNREG_VFI:
2773         case MBX_INIT_VPI:
2774         case MBX_INIT_VFI:
2775         case MBX_RESUME_RPI:
2776         case MBX_READ_EVENT_LOG_STATUS:
2777         case MBX_READ_EVENT_LOG:
2778         case MBX_SECURITY_MGMT:
2779         case MBX_AUTH_PORT:
2780         case MBX_ACCESS_VDATA:
2781                 ret = mbxCommand;
2782                 break;
2783         default:
2784                 ret = MBX_SHUTDOWN;
2785                 break;
2786         }
2787         return ret;
2788 }
2789
2790 /**
2791  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2792  * @phba: Pointer to HBA context object.
2793  * @pmboxq: Pointer to mailbox command.
2794  *
2795  * This is completion handler function for mailbox commands issued from
2796  * lpfc_sli_issue_mbox_wait function. This function is called by the
2797  * mailbox event handler function with no lock held. This function
2798  * will wake up thread waiting on the wait queue pointed by context1
2799  * of the mailbox.
2800  **/
2801 void
2802 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2803 {
2804         unsigned long drvr_flag;
2805         struct completion *pmbox_done;
2806
2807         /*
2808          * If pmbox_done is empty, the driver thread gave up waiting and
2809          * continued running.
2810          */
2811         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2812         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2813         pmbox_done = (struct completion *)pmboxq->context3;
2814         if (pmbox_done)
2815                 complete(pmbox_done);
2816         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2817         return;
2818 }
2819
2820 static void
2821 __lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2822 {
2823         unsigned long iflags;
2824
2825         if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
2826                 lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
2827                 spin_lock_irqsave(&ndlp->lock, iflags);
2828                 ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
2829                 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
2830                 spin_unlock_irqrestore(&ndlp->lock, iflags);
2831         }
2832         ndlp->nlp_flag &= ~NLP_UNREG_INP;
2833 }
2834
2835 /**
2836  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2837  * @phba: Pointer to HBA context object.
2838  * @pmb: Pointer to mailbox object.
2839  *
2840  * This function is the default mailbox completion handler. It
2841  * frees the memory resources associated with the completed mailbox
2842  * command. If the completed command is a REG_LOGIN mailbox command,
2843  * this function will issue a UREG_LOGIN to re-claim the RPI.
2844  **/
2845 void
2846 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2847 {
2848         struct lpfc_vport  *vport = pmb->vport;
2849         struct lpfc_dmabuf *mp;
2850         struct lpfc_nodelist *ndlp;
2851         struct Scsi_Host *shost;
2852         uint16_t rpi, vpi;
2853         int rc;
2854
2855         mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
2856
2857         if (mp) {
2858                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2859                 kfree(mp);
2860         }
2861
2862         /*
2863          * If a REG_LOGIN succeeded  after node is destroyed or node
2864          * is in re-discovery driver need to cleanup the RPI.
2865          */
2866         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2867             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2868             !pmb->u.mb.mbxStatus) {
2869                 rpi = pmb->u.mb.un.varWords[0];
2870                 vpi = pmb->u.mb.un.varRegLogin.vpi;
2871                 if (phba->sli_rev == LPFC_SLI_REV4)
2872                         vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
2873                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2874                 pmb->vport = vport;
2875                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2876                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2877                 if (rc != MBX_NOT_FINISHED)
2878                         return;
2879         }
2880
2881         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2882                 !(phba->pport->load_flag & FC_UNLOADING) &&
2883                 !pmb->u.mb.mbxStatus) {
2884                 shost = lpfc_shost_from_vport(vport);
2885                 spin_lock_irq(shost->host_lock);
2886                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2887                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2888                 spin_unlock_irq(shost->host_lock);
2889         }
2890
2891         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2892                 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2893                 lpfc_nlp_put(ndlp);
2894                 pmb->ctx_buf = NULL;
2895                 pmb->ctx_ndlp = NULL;
2896         }
2897
2898         if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2899                 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2900
2901                 /* Check to see if there are any deferred events to process */
2902                 if (ndlp) {
2903                         lpfc_printf_vlog(
2904                                 vport,
2905                                 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2906                                 "1438 UNREG cmpl deferred mbox x%x "
2907                                 "on NPort x%x Data: x%x x%x x%px x%x x%x\n",
2908                                 ndlp->nlp_rpi, ndlp->nlp_DID,
2909                                 ndlp->nlp_flag, ndlp->nlp_defer_did,
2910                                 ndlp, vport->load_flag, kref_read(&ndlp->kref));
2911
2912                         if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2913                             (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
2914                                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
2915                                 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2916                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2917                         } else {
2918                                 __lpfc_sli_rpi_release(vport, ndlp);
2919                         }
2920
2921                         /* The unreg_login mailbox is complete and had a
2922                          * reference that has to be released.  The PLOGI
2923                          * got its own ref.
2924                          */
2925                         lpfc_nlp_put(ndlp);
2926                         pmb->ctx_ndlp = NULL;
2927                 }
2928         }
2929
2930         /* This nlp_put pairs with lpfc_sli4_resume_rpi */
2931         if (pmb->u.mb.mbxCommand == MBX_RESUME_RPI) {
2932                 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2933                 lpfc_nlp_put(ndlp);
2934         }
2935
2936         /* Check security permission status on INIT_LINK mailbox command */
2937         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2938             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2939                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2940                                 "2860 SLI authentication is required "
2941                                 "for INIT_LINK but has not done yet\n");
2942
2943         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2944                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2945         else
2946                 mempool_free(pmb, phba->mbox_mem_pool);
2947 }
2948  /**
2949  * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2950  * @phba: Pointer to HBA context object.
2951  * @pmb: Pointer to mailbox object.
2952  *
2953  * This function is the unreg rpi mailbox completion handler. It
2954  * frees the memory resources associated with the completed mailbox
2955  * command. An additional reference is put on the ndlp to prevent
2956  * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2957  * the unreg mailbox command completes, this routine puts the
2958  * reference back.
2959  *
2960  **/
2961 void
2962 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2963 {
2964         struct lpfc_vport  *vport = pmb->vport;
2965         struct lpfc_nodelist *ndlp;
2966
2967         ndlp = pmb->ctx_ndlp;
2968         if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2969                 if (phba->sli_rev == LPFC_SLI_REV4 &&
2970                     (bf_get(lpfc_sli_intf_if_type,
2971                      &phba->sli4_hba.sli_intf) >=
2972                      LPFC_SLI_INTF_IF_TYPE_2)) {
2973                         if (ndlp) {
2974                                 lpfc_printf_vlog(
2975                                          vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2976                                          "0010 UNREG_LOGIN vpi:%x "
2977                                          "rpi:%x DID:%x defer x%x flg x%x "
2978                                          "x%px\n",
2979                                          vport->vpi, ndlp->nlp_rpi,
2980                                          ndlp->nlp_DID, ndlp->nlp_defer_did,
2981                                          ndlp->nlp_flag,
2982                                          ndlp);
2983                                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2984
2985                                 /* Check to see if there are any deferred
2986                                  * events to process
2987                                  */
2988                                 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2989                                     (ndlp->nlp_defer_did !=
2990                                     NLP_EVT_NOTHING_PENDING)) {
2991                                         lpfc_printf_vlog(
2992                                                 vport, KERN_INFO, LOG_DISCOVERY,
2993                                                 "4111 UNREG cmpl deferred "
2994                                                 "clr x%x on "
2995                                                 "NPort x%x Data: x%x x%px\n",
2996                                                 ndlp->nlp_rpi, ndlp->nlp_DID,
2997                                                 ndlp->nlp_defer_did, ndlp);
2998                                         ndlp->nlp_flag &= ~NLP_UNREG_INP;
2999                                         ndlp->nlp_defer_did =
3000                                                 NLP_EVT_NOTHING_PENDING;
3001                                         lpfc_issue_els_plogi(
3002                                                 vport, ndlp->nlp_DID, 0);
3003                                 } else {
3004                                         __lpfc_sli_rpi_release(vport, ndlp);
3005                                 }
3006                                 lpfc_nlp_put(ndlp);
3007                         }
3008                 }
3009         }
3010
3011         mempool_free(pmb, phba->mbox_mem_pool);
3012 }
3013
3014 /**
3015  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
3016  * @phba: Pointer to HBA context object.
3017  *
3018  * This function is called with no lock held. This function processes all
3019  * the completed mailbox commands and gives it to upper layers. The interrupt
3020  * service routine processes mailbox completion interrupt and adds completed
3021  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
3022  * Worker thread call lpfc_sli_handle_mb_event, which will return the
3023  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
3024  * function returns the mailbox commands to the upper layer by calling the
3025  * completion handler function of each mailbox.
3026  **/
3027 int
3028 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
3029 {
3030         MAILBOX_t *pmbox;
3031         LPFC_MBOXQ_t *pmb;
3032         int rc;
3033         LIST_HEAD(cmplq);
3034
3035         phba->sli.slistat.mbox_event++;
3036
3037         /* Get all completed mailboxe buffers into the cmplq */
3038         spin_lock_irq(&phba->hbalock);
3039         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
3040         spin_unlock_irq(&phba->hbalock);
3041
3042         /* Get a Mailbox buffer to setup mailbox commands for callback */
3043         do {
3044                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
3045                 if (pmb == NULL)
3046                         break;
3047
3048                 pmbox = &pmb->u.mb;
3049
3050                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
3051                         if (pmb->vport) {
3052                                 lpfc_debugfs_disc_trc(pmb->vport,
3053                                         LPFC_DISC_TRC_MBOX_VPORT,
3054                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
3055                                         (uint32_t)pmbox->mbxCommand,
3056                                         pmbox->un.varWords[0],
3057                                         pmbox->un.varWords[1]);
3058                         }
3059                         else {
3060                                 lpfc_debugfs_disc_trc(phba->pport,
3061                                         LPFC_DISC_TRC_MBOX,
3062                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
3063                                         (uint32_t)pmbox->mbxCommand,
3064                                         pmbox->un.varWords[0],
3065                                         pmbox->un.varWords[1]);
3066                         }
3067                 }
3068
3069                 /*
3070                  * It is a fatal error if unknown mbox command completion.
3071                  */
3072                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
3073                     MBX_SHUTDOWN) {
3074                         /* Unknown mailbox command compl */
3075                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3076                                         "(%d):0323 Unknown Mailbox command "
3077                                         "x%x (x%x/x%x) Cmpl\n",
3078                                         pmb->vport ? pmb->vport->vpi :
3079                                         LPFC_VPORT_UNKNOWN,
3080                                         pmbox->mbxCommand,
3081                                         lpfc_sli_config_mbox_subsys_get(phba,
3082                                                                         pmb),
3083                                         lpfc_sli_config_mbox_opcode_get(phba,
3084                                                                         pmb));
3085                         phba->link_state = LPFC_HBA_ERROR;
3086                         phba->work_hs = HS_FFER3;
3087                         lpfc_handle_eratt(phba);
3088                         continue;
3089                 }
3090
3091                 if (pmbox->mbxStatus) {
3092                         phba->sli.slistat.mbox_stat_err++;
3093                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
3094                                 /* Mbox cmd cmpl error - RETRYing */
3095                                 lpfc_printf_log(phba, KERN_INFO,
3096                                         LOG_MBOX | LOG_SLI,
3097                                         "(%d):0305 Mbox cmd cmpl "
3098                                         "error - RETRYing Data: x%x "
3099                                         "(x%x/x%x) x%x x%x x%x\n",
3100                                         pmb->vport ? pmb->vport->vpi :
3101                                         LPFC_VPORT_UNKNOWN,
3102                                         pmbox->mbxCommand,
3103                                         lpfc_sli_config_mbox_subsys_get(phba,
3104                                                                         pmb),
3105                                         lpfc_sli_config_mbox_opcode_get(phba,
3106                                                                         pmb),
3107                                         pmbox->mbxStatus,
3108                                         pmbox->un.varWords[0],
3109                                         pmb->vport ? pmb->vport->port_state :
3110                                         LPFC_VPORT_UNKNOWN);
3111                                 pmbox->mbxStatus = 0;
3112                                 pmbox->mbxOwner = OWN_HOST;
3113                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3114                                 if (rc != MBX_NOT_FINISHED)
3115                                         continue;
3116                         }
3117                 }
3118
3119                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
3120                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
3121                                 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
3122                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
3123                                 "x%x x%x x%x\n",
3124                                 pmb->vport ? pmb->vport->vpi : 0,
3125                                 pmbox->mbxCommand,
3126                                 lpfc_sli_config_mbox_subsys_get(phba, pmb),
3127                                 lpfc_sli_config_mbox_opcode_get(phba, pmb),
3128                                 pmb->mbox_cmpl,
3129                                 *((uint32_t *) pmbox),
3130                                 pmbox->un.varWords[0],
3131                                 pmbox->un.varWords[1],
3132                                 pmbox->un.varWords[2],
3133                                 pmbox->un.varWords[3],
3134                                 pmbox->un.varWords[4],
3135                                 pmbox->un.varWords[5],
3136                                 pmbox->un.varWords[6],
3137                                 pmbox->un.varWords[7],
3138                                 pmbox->un.varWords[8],
3139                                 pmbox->un.varWords[9],
3140                                 pmbox->un.varWords[10]);
3141
3142                 if (pmb->mbox_cmpl)
3143                         pmb->mbox_cmpl(phba,pmb);
3144         } while (1);
3145         return 0;
3146 }
3147
3148 /**
3149  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
3150  * @phba: Pointer to HBA context object.
3151  * @pring: Pointer to driver SLI ring object.
3152  * @tag: buffer tag.
3153  *
3154  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
3155  * is set in the tag the buffer is posted for a particular exchange,
3156  * the function will return the buffer without replacing the buffer.
3157  * If the buffer is for unsolicited ELS or CT traffic, this function
3158  * returns the buffer and also posts another buffer to the firmware.
3159  **/
3160 static struct lpfc_dmabuf *
3161 lpfc_sli_get_buff(struct lpfc_hba *phba,
3162                   struct lpfc_sli_ring *pring,
3163                   uint32_t tag)
3164 {
3165         struct hbq_dmabuf *hbq_entry;
3166
3167         if (tag & QUE_BUFTAG_BIT)
3168                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
3169         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
3170         if (!hbq_entry)
3171                 return NULL;
3172         return &hbq_entry->dbuf;
3173 }
3174
3175 /**
3176  * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
3177  *                              containing a NVME LS request.
3178  * @phba: pointer to lpfc hba data structure.
3179  * @piocb: pointer to the iocbq struct representing the sequence starting
3180  *        frame.
3181  *
3182  * This routine initially validates the NVME LS, validates there is a login
3183  * with the port that sent the LS, and then calls the appropriate nvme host
3184  * or target LS request handler.
3185  **/
3186 static void
3187 lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
3188 {
3189         struct lpfc_nodelist *ndlp;
3190         struct lpfc_dmabuf *d_buf;
3191         struct hbq_dmabuf *nvmebuf;
3192         struct fc_frame_header *fc_hdr;
3193         struct lpfc_async_xchg_ctx *axchg = NULL;
3194         char *failwhy = NULL;
3195         uint32_t oxid, sid, did, fctl, size;
3196         int ret = 1;
3197
3198         d_buf = piocb->context2;
3199
3200         nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
3201         fc_hdr = nvmebuf->hbuf.virt;
3202         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
3203         sid = sli4_sid_from_fc_hdr(fc_hdr);
3204         did = sli4_did_from_fc_hdr(fc_hdr);
3205         fctl = (fc_hdr->fh_f_ctl[0] << 16 |
3206                 fc_hdr->fh_f_ctl[1] << 8 |
3207                 fc_hdr->fh_f_ctl[2]);
3208         size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
3209
3210         lpfc_nvmeio_data(phba, "NVME LS    RCV: xri x%x sz %d from %06x\n",
3211                          oxid, size, sid);
3212
3213         if (phba->pport->load_flag & FC_UNLOADING) {
3214                 failwhy = "Driver Unloading";
3215         } else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
3216                 failwhy = "NVME FC4 Disabled";
3217         } else if (!phba->nvmet_support && !phba->pport->localport) {
3218                 failwhy = "No Localport";
3219         } else if (phba->nvmet_support && !phba->targetport) {
3220                 failwhy = "No Targetport";
3221         } else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
3222                 failwhy = "Bad NVME LS R_CTL";
3223         } else if (unlikely((fctl & 0x00FF0000) !=
3224                         (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
3225                 failwhy = "Bad NVME LS F_CTL";
3226         } else {
3227                 axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);
3228                 if (!axchg)
3229                         failwhy = "No CTX memory";
3230         }
3231
3232         if (unlikely(failwhy)) {
3233                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3234                                 "6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
3235                                 sid, oxid, failwhy);
3236                 goto out_fail;
3237         }
3238
3239         /* validate the source of the LS is logged in */
3240         ndlp = lpfc_findnode_did(phba->pport, sid);
3241         if (!ndlp ||
3242             ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3243              (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3244                 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
3245                                 "6216 NVME Unsol rcv: No ndlp: "
3246                                 "NPort_ID x%x oxid x%x\n",
3247                                 sid, oxid);
3248                 goto out_fail;
3249         }
3250
3251         axchg->phba = phba;
3252         axchg->ndlp = ndlp;
3253         axchg->size = size;
3254         axchg->oxid = oxid;
3255         axchg->sid = sid;
3256         axchg->wqeq = NULL;
3257         axchg->state = LPFC_NVME_STE_LS_RCV;
3258         axchg->entry_cnt = 1;
3259         axchg->rqb_buffer = (void *)nvmebuf;
3260         axchg->hdwq = &phba->sli4_hba.hdwq[0];
3261         axchg->payload = nvmebuf->dbuf.virt;
3262         INIT_LIST_HEAD(&axchg->list);
3263
3264         if (phba->nvmet_support) {
3265                 ret = lpfc_nvmet_handle_lsreq(phba, axchg);
3266                 spin_lock_irq(&ndlp->lock);
3267                 if (!ret && !(ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH)) {
3268                         ndlp->fc4_xpt_flags |= NLP_XPT_HAS_HH;
3269                         spin_unlock_irq(&ndlp->lock);
3270
3271                         /* This reference is a single occurrence to hold the
3272                          * node valid until the nvmet transport calls
3273                          * host_release.
3274                          */
3275                         if (!lpfc_nlp_get(ndlp))
3276                                 goto out_fail;
3277
3278                         lpfc_printf_log(phba, KERN_ERR, LOG_NODE,
3279                                         "6206 NVMET unsol ls_req ndlp x%px "
3280                                         "DID x%x xflags x%x refcnt %d\n",
3281                                         ndlp, ndlp->nlp_DID,
3282                                         ndlp->fc4_xpt_flags,
3283                                         kref_read(&ndlp->kref));
3284                 } else {
3285                         spin_unlock_irq(&ndlp->lock);
3286                 }
3287         } else {
3288                 ret = lpfc_nvme_handle_lsreq(phba, axchg);
3289         }
3290
3291         /* if zero, LS was successfully handled. If non-zero, LS not handled */
3292         if (!ret)
3293                 return;
3294
3295 out_fail:
3296         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3297                         "6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
3298                         "NVMe%s handler failed %d\n",
3299                         did, sid, oxid,
3300                         (phba->nvmet_support) ? "T" : "I", ret);
3301
3302         /* recycle receive buffer */
3303         lpfc_in_buf_free(phba, &nvmebuf->dbuf);
3304
3305         /* If start of new exchange, abort it */
3306         if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
3307                 ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
3308
3309         if (ret)
3310                 kfree(axchg);
3311 }
3312
3313 /**
3314  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
3315  * @phba: Pointer to HBA context object.
3316  * @pring: Pointer to driver SLI ring object.
3317  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
3318  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
3319  * @fch_type: the type for the first frame of the sequence.
3320  *
3321  * This function is called with no lock held. This function uses the r_ctl and
3322  * type of the received sequence to find the correct callback function to call
3323  * to process the sequence.
3324  **/
3325 static int
3326 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3327                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
3328                          uint32_t fch_type)
3329 {
3330         int i;
3331
3332         switch (fch_type) {
3333         case FC_TYPE_NVME:
3334                 lpfc_nvme_unsol_ls_handler(phba, saveq);
3335                 return 1;
3336         default:
3337                 break;
3338         }
3339
3340         /* unSolicited Responses */
3341         if (pring->prt[0].profile) {
3342                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
3343                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
3344                                                                         saveq);
3345                 return 1;
3346         }
3347         /* We must search, based on rctl / type
3348            for the right routine */
3349         for (i = 0; i < pring->num_mask; i++) {
3350                 if ((pring->prt[i].rctl == fch_r_ctl) &&
3351                     (pring->prt[i].type == fch_type)) {
3352                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
3353                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
3354                                                 (phba, pring, saveq);
3355                         return 1;
3356                 }
3357         }
3358         return 0;
3359 }
3360
3361 /**
3362  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
3363  * @phba: Pointer to HBA context object.
3364  * @pring: Pointer to driver SLI ring object.
3365  * @saveq: Pointer to the unsolicited iocb.
3366  *
3367  * This function is called with no lock held by the ring event handler
3368  * when there is an unsolicited iocb posted to the response ring by the
3369  * firmware. This function gets the buffer associated with the iocbs
3370  * and calls the event handler for the ring. This function handles both
3371  * qring buffers and hbq buffers.
3372  * When the function returns 1 the caller can free the iocb object otherwise
3373  * upper layer functions will free the iocb objects.
3374  **/
3375 static int
3376 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3377                             struct lpfc_iocbq *saveq)
3378 {
3379         IOCB_t           * irsp;
3380         WORD5            * w5p;
3381         uint32_t           Rctl, Type;
3382         struct lpfc_iocbq *iocbq;
3383         struct lpfc_dmabuf *dmzbuf;
3384
3385         irsp = &(saveq->iocb);
3386
3387         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
3388                 if (pring->lpfc_sli_rcv_async_status)
3389                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
3390                 else
3391                         lpfc_printf_log(phba,
3392                                         KERN_WARNING,
3393                                         LOG_SLI,
3394                                         "0316 Ring %d handler: unexpected "
3395                                         "ASYNC_STATUS iocb received evt_code "
3396                                         "0x%x\n",
3397                                         pring->ringno,
3398                                         irsp->un.asyncstat.evt_code);
3399                 return 1;
3400         }
3401
3402         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
3403                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
3404                 if (irsp->ulpBdeCount > 0) {
3405                         dmzbuf = lpfc_sli_get_buff(phba, pring,
3406                                         irsp->un.ulpWord[3]);
3407                         lpfc_in_buf_free(phba, dmzbuf);
3408                 }
3409
3410                 if (irsp->ulpBdeCount > 1) {
3411                         dmzbuf = lpfc_sli_get_buff(phba, pring,
3412                                         irsp->unsli3.sli3Words[3]);
3413                         lpfc_in_buf_free(phba, dmzbuf);
3414                 }
3415
3416                 if (irsp->ulpBdeCount > 2) {
3417                         dmzbuf = lpfc_sli_get_buff(phba, pring,
3418                                 irsp->unsli3.sli3Words[7]);
3419                         lpfc_in_buf_free(phba, dmzbuf);
3420                 }
3421
3422                 return 1;
3423         }
3424
3425         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3426                 if (irsp->ulpBdeCount != 0) {
3427                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
3428                                                 irsp->un.ulpWord[3]);
3429                         if (!saveq->context2)
3430                                 lpfc_printf_log(phba,
3431                                         KERN_ERR,
3432                                         LOG_SLI,
3433                                         "0341 Ring %d Cannot find buffer for "
3434                                         "an unsolicited iocb. tag 0x%x\n",
3435                                         pring->ringno,
3436                                         irsp->un.ulpWord[3]);
3437                 }
3438                 if (irsp->ulpBdeCount == 2) {
3439                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
3440                                                 irsp->unsli3.sli3Words[7]);
3441                         if (!saveq->context3)
3442                                 lpfc_printf_log(phba,
3443                                         KERN_ERR,
3444                                         LOG_SLI,
3445                                         "0342 Ring %d Cannot find buffer for an"
3446                                         " unsolicited iocb. tag 0x%x\n",
3447                                         pring->ringno,
3448                                         irsp->unsli3.sli3Words[7]);
3449                 }
3450                 list_for_each_entry(iocbq, &saveq->list, list) {
3451                         irsp = &(iocbq->iocb);
3452                         if (irsp->ulpBdeCount != 0) {
3453                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
3454                                                         irsp->un.ulpWord[3]);
3455                                 if (!iocbq->context2)
3456                                         lpfc_printf_log(phba,
3457                                                 KERN_ERR,
3458                                                 LOG_SLI,
3459                                                 "0343 Ring %d Cannot find "
3460                                                 "buffer for an unsolicited iocb"
3461                                                 ". tag 0x%x\n", pring->ringno,
3462                                                 irsp->un.ulpWord[3]);
3463                         }
3464                         if (irsp->ulpBdeCount == 2) {
3465                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
3466                                                 irsp->unsli3.sli3Words[7]);
3467                                 if (!iocbq->context3)
3468                                         lpfc_printf_log(phba,
3469                                                 KERN_ERR,
3470                                                 LOG_SLI,
3471                                                 "0344 Ring %d Cannot find "
3472                                                 "buffer for an unsolicited "
3473                                                 "iocb. tag 0x%x\n",
3474                                                 pring->ringno,
3475                                                 irsp->unsli3.sli3Words[7]);
3476                         }
3477                 }
3478         }
3479         if (irsp->ulpBdeCount != 0 &&
3480             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
3481              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
3482                 int found = 0;
3483
3484                 /* search continue save q for same XRI */
3485                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
3486                         if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
3487                                 saveq->iocb.unsli3.rcvsli3.ox_id) {
3488                                 list_add_tail(&saveq->list, &iocbq->list);
3489                                 found = 1;
3490                                 break;
3491                         }
3492                 }
3493                 if (!found)
3494                         list_add_tail(&saveq->clist,
3495                                       &pring->iocb_continue_saveq);
3496                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
3497                         list_del_init(&iocbq->clist);
3498                         saveq = iocbq;
3499                         irsp = &(saveq->iocb);
3500                 } else
3501                         return 0;
3502         }
3503         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
3504             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
3505             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
3506                 Rctl = FC_RCTL_ELS_REQ;
3507                 Type = FC_TYPE_ELS;
3508         } else {
3509                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
3510                 Rctl = w5p->hcsw.Rctl;
3511                 Type = w5p->hcsw.Type;
3512
3513                 /* Firmware Workaround */
3514                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
3515                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
3516                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
3517                         Rctl = FC_RCTL_ELS_REQ;
3518                         Type = FC_TYPE_ELS;
3519                         w5p->hcsw.Rctl = Rctl;
3520                         w5p->hcsw.Type = Type;
3521                 }
3522         }
3523
3524         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
3525                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3526                                 "0313 Ring %d handler: unexpected Rctl x%x "
3527                                 "Type x%x received\n",
3528                                 pring->ringno, Rctl, Type);
3529
3530         return 1;
3531 }
3532
3533 /**
3534  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
3535  * @phba: Pointer to HBA context object.
3536  * @pring: Pointer to driver SLI ring object.
3537  * @prspiocb: Pointer to response iocb object.
3538  *
3539  * This function looks up the iocb_lookup table to get the command iocb
3540  * corresponding to the given response iocb using the iotag of the
3541  * response iocb. The driver calls this function with the hbalock held
3542  * for SLI3 ports or the ring lock held for SLI4 ports.
3543  * This function returns the command iocb object if it finds the command
3544  * iocb else returns NULL.
3545  **/
3546 static struct lpfc_iocbq *
3547 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3548                       struct lpfc_sli_ring *pring,
3549                       struct lpfc_iocbq *prspiocb)
3550 {
3551         struct lpfc_iocbq *cmd_iocb = NULL;
3552         uint16_t iotag;
3553         spinlock_t *temp_lock = NULL;
3554         unsigned long iflag = 0;
3555
3556         if (phba->sli_rev == LPFC_SLI_REV4)
3557                 temp_lock = &pring->ring_lock;
3558         else
3559                 temp_lock = &phba->hbalock;
3560
3561         spin_lock_irqsave(temp_lock, iflag);
3562         iotag = prspiocb->iocb.ulpIoTag;
3563
3564         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3565                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
3566                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3567                         /* remove from txcmpl queue list */
3568                         list_del_init(&cmd_iocb->list);
3569                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3570                         pring->txcmplq_cnt--;
3571                         spin_unlock_irqrestore(temp_lock, iflag);
3572                         return cmd_iocb;
3573                 }
3574         }
3575
3576         spin_unlock_irqrestore(temp_lock, iflag);
3577         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3578                         "0317 iotag x%x is out of "
3579                         "range: max iotag x%x wd0 x%x\n",
3580                         iotag, phba->sli.last_iotag,
3581                         *(((uint32_t *) &prspiocb->iocb) + 7));
3582         return NULL;
3583 }
3584
3585 /**
3586  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3587  * @phba: Pointer to HBA context object.
3588  * @pring: Pointer to driver SLI ring object.
3589  * @iotag: IOCB tag.
3590  *
3591  * This function looks up the iocb_lookup table to get the command iocb
3592  * corresponding to the given iotag. The driver calls this function with
3593  * the ring lock held because this function is an SLI4 port only helper.
3594  * This function returns the command iocb object if it finds the command
3595  * iocb else returns NULL.
3596  **/
3597 static struct lpfc_iocbq *
3598 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3599                              struct lpfc_sli_ring *pring, uint16_t iotag)
3600 {
3601         struct lpfc_iocbq *cmd_iocb = NULL;
3602         spinlock_t *temp_lock = NULL;
3603         unsigned long iflag = 0;
3604
3605         if (phba->sli_rev == LPFC_SLI_REV4)
3606                 temp_lock = &pring->ring_lock;
3607         else
3608                 temp_lock = &phba->hbalock;
3609
3610         spin_lock_irqsave(temp_lock, iflag);
3611         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3612                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
3613                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3614                         /* remove from txcmpl queue list */
3615                         list_del_init(&cmd_iocb->list);
3616                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3617                         pring->txcmplq_cnt--;
3618                         spin_unlock_irqrestore(temp_lock, iflag);
3619                         return cmd_iocb;
3620                 }
3621         }
3622
3623         spin_unlock_irqrestore(temp_lock, iflag);
3624         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3625                         "0372 iotag x%x lookup error: max iotag (x%x) "
3626                         "iocb_flag x%x\n",
3627                         iotag, phba->sli.last_iotag,
3628                         cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3629         return NULL;
3630 }
3631
3632 /**
3633  * lpfc_sli_process_sol_iocb - process solicited iocb completion
3634  * @phba: Pointer to HBA context object.
3635  * @pring: Pointer to driver SLI ring object.
3636  * @saveq: Pointer to the response iocb to be processed.
3637  *
3638  * This function is called by the ring event handler for non-fcp
3639  * rings when there is a new response iocb in the response ring.
3640  * The caller is not required to hold any locks. This function
3641  * gets the command iocb associated with the response iocb and
3642  * calls the completion handler for the command iocb. If there
3643  * is no completion handler, the function will free the resources
3644  * associated with command iocb. If the response iocb is for
3645  * an already aborted command iocb, the status of the completion
3646  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3647  * This function always returns 1.
3648  **/
3649 static int
3650 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3651                           struct lpfc_iocbq *saveq)
3652 {
3653         struct lpfc_iocbq *cmdiocbp;
3654         int rc = 1;
3655         unsigned long iflag;
3656
3657         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
3658         if (cmdiocbp) {
3659                 if (cmdiocbp->iocb_cmpl) {
3660                         /*
3661                          * If an ELS command failed send an event to mgmt
3662                          * application.
3663                          */
3664                         if (saveq->iocb.ulpStatus &&
3665                              (pring->ringno == LPFC_ELS_RING) &&
3666                              (cmdiocbp->iocb.ulpCommand ==
3667                                 CMD_ELS_REQUEST64_CR))
3668                                 lpfc_send_els_failure_event(phba,
3669                                         cmdiocbp, saveq);
3670
3671                         /*
3672                          * Post all ELS completions to the worker thread.
3673                          * All other are passed to the completion callback.
3674                          */
3675                         if (pring->ringno == LPFC_ELS_RING) {
3676                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3677                                     (cmdiocbp->iocb_flag &
3678                                                         LPFC_DRIVER_ABORTED)) {
3679                                         spin_lock_irqsave(&phba->hbalock,
3680                                                           iflag);
3681                                         cmdiocbp->iocb_flag &=
3682                                                 ~LPFC_DRIVER_ABORTED;
3683                                         spin_unlock_irqrestore(&phba->hbalock,
3684                                                                iflag);
3685                                         saveq->iocb.ulpStatus =
3686                                                 IOSTAT_LOCAL_REJECT;
3687                                         saveq->iocb.un.ulpWord[4] =
3688                                                 IOERR_SLI_ABORTED;
3689
3690                                         /* Firmware could still be in progress
3691                                          * of DMAing payload, so don't free data
3692                                          * buffer till after a hbeat.
3693                                          */
3694                                         spin_lock_irqsave(&phba->hbalock,
3695                                                           iflag);
3696                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
3697                                         spin_unlock_irqrestore(&phba->hbalock,
3698                                                                iflag);
3699                                 }
3700                                 if (phba->sli_rev == LPFC_SLI_REV4) {
3701                                         if (saveq->iocb_flag &
3702                                             LPFC_EXCHANGE_BUSY) {
3703                                                 /* Set cmdiocb flag for the
3704                                                  * exchange busy so sgl (xri)
3705                                                  * will not be released until
3706                                                  * the abort xri is received
3707                                                  * from hba.
3708                                                  */
3709                                                 spin_lock_irqsave(
3710                                                         &phba->hbalock, iflag);
3711                                                 cmdiocbp->iocb_flag |=
3712                                                         LPFC_EXCHANGE_BUSY;
3713                                                 spin_unlock_irqrestore(
3714                                                         &phba->hbalock, iflag);
3715                                         }
3716                                         if (cmdiocbp->iocb_flag &
3717                                             LPFC_DRIVER_ABORTED) {
3718                                                 /*
3719                                                  * Clear LPFC_DRIVER_ABORTED
3720                                                  * bit in case it was driver
3721                                                  * initiated abort.
3722                                                  */
3723                                                 spin_lock_irqsave(
3724                                                         &phba->hbalock, iflag);
3725                                                 cmdiocbp->iocb_flag &=
3726                                                         ~LPFC_DRIVER_ABORTED;
3727                                                 spin_unlock_irqrestore(
3728                                                         &phba->hbalock, iflag);
3729                                                 cmdiocbp->iocb.ulpStatus =
3730                                                         IOSTAT_LOCAL_REJECT;
3731                                                 cmdiocbp->iocb.un.ulpWord[4] =
3732                                                         IOERR_ABORT_REQUESTED;
3733                                                 /*
3734                                                  * For SLI4, irsiocb contains
3735                                                  * NO_XRI in sli_xritag, it
3736                                                  * shall not affect releasing
3737                                                  * sgl (xri) process.
3738                                                  */
3739                                                 saveq->iocb.ulpStatus =
3740                                                         IOSTAT_LOCAL_REJECT;
3741                                                 saveq->iocb.un.ulpWord[4] =
3742                                                         IOERR_SLI_ABORTED;
3743                                                 spin_lock_irqsave(
3744                                                         &phba->hbalock, iflag);
3745                                                 saveq->iocb_flag |=
3746                                                         LPFC_DELAY_MEM_FREE;
3747                                                 spin_unlock_irqrestore(
3748                                                         &phba->hbalock, iflag);
3749                                         }
3750                                 }
3751                         }
3752                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
3753                 } else
3754                         lpfc_sli_release_iocbq(phba, cmdiocbp);
3755         } else {
3756                 /*
3757                  * Unknown initiating command based on the response iotag.
3758                  * This could be the case on the ELS ring because of
3759                  * lpfc_els_abort().
3760                  */
3761                 if (pring->ringno != LPFC_ELS_RING) {
3762                         /*
3763                          * Ring <ringno> handler: unexpected completion IoTag
3764                          * <IoTag>
3765                          */
3766                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3767                                          "0322 Ring %d handler: "
3768                                          "unexpected completion IoTag x%x "
3769                                          "Data: x%x x%x x%x x%x\n",
3770                                          pring->ringno,
3771                                          saveq->iocb.ulpIoTag,
3772                                          saveq->iocb.ulpStatus,
3773                                          saveq->iocb.un.ulpWord[4],
3774                                          saveq->iocb.ulpCommand,
3775                                          saveq->iocb.ulpContext);
3776                 }
3777         }
3778
3779         return rc;
3780 }
3781
3782 /**
3783  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
3784  * @phba: Pointer to HBA context object.
3785  * @pring: Pointer to driver SLI ring object.
3786  *
3787  * This function is called from the iocb ring event handlers when
3788  * put pointer is ahead of the get pointer for a ring. This function signal
3789  * an error attention condition to the worker thread and the worker
3790  * thread will transition the HBA to offline state.
3791  **/
3792 static void
3793 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3794 {
3795         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3796         /*
3797          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3798          * rsp ring <portRspMax>
3799          */
3800         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3801                         "0312 Ring %d handler: portRspPut %d "
3802                         "is bigger than rsp ring %d\n",
3803                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
3804                         pring->sli.sli3.numRiocb);
3805
3806         phba->link_state = LPFC_HBA_ERROR;
3807
3808         /*
3809          * All error attention handlers are posted to
3810          * worker thread
3811          */
3812         phba->work_ha |= HA_ERATT;
3813         phba->work_hs = HS_FFER3;
3814
3815         lpfc_worker_wake_up(phba);
3816
3817         return;
3818 }
3819
3820 /**
3821  * lpfc_poll_eratt - Error attention polling timer timeout handler
3822  * @t: Context to fetch pointer to address of HBA context object from.
3823  *
3824  * This function is invoked by the Error Attention polling timer when the
3825  * timer times out. It will check the SLI Error Attention register for
3826  * possible attention events. If so, it will post an Error Attention event
3827  * and wake up worker thread to process it. Otherwise, it will set up the
3828  * Error Attention polling timer for the next poll.
3829  **/
3830 void lpfc_poll_eratt(struct timer_list *t)
3831 {
3832         struct lpfc_hba *phba;
3833         uint32_t eratt = 0;
3834         uint64_t sli_intr, cnt;
3835
3836         phba = from_timer(phba, t, eratt_poll);
3837
3838         /* Here we will also keep track of interrupts per sec of the hba */
3839         sli_intr = phba->sli.slistat.sli_intr;
3840
3841         if (phba->sli.slistat.sli_prev_intr > sli_intr)
3842                 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3843                         sli_intr);
3844         else
3845                 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3846
3847         /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3848         do_div(cnt, phba->eratt_poll_interval);
3849         phba->sli.slistat.sli_ips = cnt;
3850
3851         phba->sli.slistat.sli_prev_intr = sli_intr;
3852
3853         /* Check chip HA register for error event */
3854         eratt = lpfc_sli_check_eratt(phba);
3855
3856         if (eratt)
3857                 /* Tell the worker thread there is work to do */
3858                 lpfc_worker_wake_up(phba);
3859         else
3860                 /* Restart the timer for next eratt poll */
3861                 mod_timer(&phba->eratt_poll,
3862                           jiffies +
3863                           msecs_to_jiffies(1000 * phba->eratt_poll_interval));
3864         return;
3865 }
3866
3867
3868 /**
3869  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3870  * @phba: Pointer to HBA context object.
3871  * @pring: Pointer to driver SLI ring object.
3872  * @mask: Host attention register mask for this ring.
3873  *
3874  * This function is called from the interrupt context when there is a ring
3875  * event for the fcp ring. The caller does not hold any lock.
3876  * The function processes each response iocb in the response ring until it
3877  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3878  * LE bit set. The function will call the completion handler of the command iocb
3879  * if the response iocb indicates a completion for a command iocb or it is
3880  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3881  * function if this is an unsolicited iocb.
3882  * This routine presumes LPFC_FCP_RING handling and doesn't bother
3883  * to check it explicitly.
3884  */
3885 int
3886 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3887                                 struct lpfc_sli_ring *pring, uint32_t mask)
3888 {
3889         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3890         IOCB_t *irsp = NULL;
3891         IOCB_t *entry = NULL;
3892         struct lpfc_iocbq *cmdiocbq = NULL;
3893         struct lpfc_iocbq rspiocbq;
3894         uint32_t status;
3895         uint32_t portRspPut, portRspMax;
3896         int rc = 1;
3897         lpfc_iocb_type type;
3898         unsigned long iflag;
3899         uint32_t rsp_cmpl = 0;
3900
3901         spin_lock_irqsave(&phba->hbalock, iflag);
3902         pring->stats.iocb_event++;
3903
3904         /*
3905          * The next available response entry should never exceed the maximum
3906          * entries.  If it does, treat it as an adapter hardware error.
3907          */
3908         portRspMax = pring->sli.sli3.numRiocb;
3909         portRspPut = le32_to_cpu(pgp->rspPutInx);
3910         if (unlikely(portRspPut >= portRspMax)) {
3911                 lpfc_sli_rsp_pointers_error(phba, pring);
3912                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3913                 return 1;
3914         }
3915         if (phba->fcp_ring_in_use) {
3916                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3917                 return 1;
3918         } else
3919                 phba->fcp_ring_in_use = 1;
3920
3921         rmb();
3922         while (pring->sli.sli3.rspidx != portRspPut) {
3923                 /*
3924                  * Fetch an entry off the ring and copy it into a local data
3925                  * structure.  The copy involves a byte-swap since the
3926                  * network byte order and pci byte orders are different.
3927                  */
3928                 entry = lpfc_resp_iocb(phba, pring);
3929                 phba->last_completion_time = jiffies;
3930
3931                 if (++pring->sli.sli3.rspidx >= portRspMax)
3932                         pring->sli.sli3.rspidx = 0;
3933
3934                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3935                                       (uint32_t *) &rspiocbq.iocb,
3936                                       phba->iocb_rsp_size);
3937                 INIT_LIST_HEAD(&(rspiocbq.list));
3938                 irsp = &rspiocbq.iocb;
3939
3940                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3941                 pring->stats.iocb_rsp++;
3942                 rsp_cmpl++;
3943
3944                 if (unlikely(irsp->ulpStatus)) {
3945                         /*
3946                          * If resource errors reported from HBA, reduce
3947                          * queuedepths of the SCSI device.
3948                          */
3949                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3950                             ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3951                              IOERR_NO_RESOURCES)) {
3952                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3953                                 phba->lpfc_rampdown_queue_depth(phba);
3954                                 spin_lock_irqsave(&phba->hbalock, iflag);
3955                         }
3956
3957                         /* Rsp ring <ringno> error: IOCB */
3958                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3959                                         "0336 Rsp Ring %d error: IOCB Data: "
3960                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3961                                         pring->ringno,
3962                                         irsp->un.ulpWord[0],
3963                                         irsp->un.ulpWord[1],
3964                                         irsp->un.ulpWord[2],
3965                                         irsp->un.ulpWord[3],
3966                                         irsp->un.ulpWord[4],
3967                                         irsp->un.ulpWord[5],
3968                                         *(uint32_t *)&irsp->un1,
3969                                         *((uint32_t *)&irsp->un1 + 1));
3970                 }
3971
3972                 switch (type) {
3973                 case LPFC_ABORT_IOCB:
3974                 case LPFC_SOL_IOCB:
3975                         /*
3976                          * Idle exchange closed via ABTS from port.  No iocb
3977                          * resources need to be recovered.
3978                          */
3979                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3980                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3981                                                 "0333 IOCB cmd 0x%x"
3982                                                 " processed. Skipping"
3983                                                 " completion\n",
3984                                                 irsp->ulpCommand);
3985                                 break;
3986                         }
3987
3988                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3989                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3990                                                          &rspiocbq);
3991                         spin_lock_irqsave(&phba->hbalock, iflag);
3992                         if (unlikely(!cmdiocbq))
3993                                 break;
3994                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3995                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3996                         if (cmdiocbq->iocb_cmpl) {
3997                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3998                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3999                                                       &rspiocbq);
4000                                 spin_lock_irqsave(&phba->hbalock, iflag);
4001                         }
4002                         break;
4003                 case LPFC_UNSOL_IOCB:
4004                         spin_unlock_irqrestore(&phba->hbalock, iflag);
4005                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
4006                         spin_lock_irqsave(&phba->hbalock, iflag);
4007                         break;
4008                 default:
4009                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
4010                                 char adaptermsg[LPFC_MAX_ADPTMSG];
4011                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4012                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
4013                                        MAX_MSG_DATA);
4014                                 dev_warn(&((phba->pcidev)->dev),
4015                                          "lpfc%d: %s\n",
4016                                          phba->brd_no, adaptermsg);
4017                         } else {
4018                                 /* Unknown IOCB command */
4019                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4020                                                 "0334 Unknown IOCB command "
4021                                                 "Data: x%x, x%x x%x x%x x%x\n",
4022                                                 type, irsp->ulpCommand,
4023                                                 irsp->ulpStatus,
4024                                                 irsp->ulpIoTag,
4025                                                 irsp->ulpContext);
4026                         }
4027                         break;
4028                 }
4029
4030                 /*
4031                  * The response IOCB has been processed.  Update the ring
4032                  * pointer in SLIM.  If the port response put pointer has not
4033                  * been updated, sync the pgp->rspPutInx and fetch the new port
4034                  * response put pointer.
4035                  */
4036                 writel(pring->sli.sli3.rspidx,
4037                         &phba->host_gp[pring->ringno].rspGetInx);
4038
4039                 if (pring->sli.sli3.rspidx == portRspPut)
4040                         portRspPut = le32_to_cpu(pgp->rspPutInx);
4041         }
4042
4043         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
4044                 pring->stats.iocb_rsp_full++;
4045                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4046                 writel(status, phba->CAregaddr);
4047                 readl(phba->CAregaddr);
4048         }
4049         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4050                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4051                 pring->stats.iocb_cmd_empty++;
4052
4053                 /* Force update of the local copy of cmdGetInx */
4054                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4055                 lpfc_sli_resume_iocb(phba, pring);
4056
4057                 if ((pring->lpfc_sli_cmd_available))
4058                         (pring->lpfc_sli_cmd_available) (phba, pring);
4059
4060         }
4061
4062         phba->fcp_ring_in_use = 0;
4063         spin_unlock_irqrestore(&phba->hbalock, iflag);
4064         return rc;
4065 }
4066
4067 /**
4068  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
4069  * @phba: Pointer to HBA context object.
4070  * @pring: Pointer to driver SLI ring object.
4071  * @rspiocbp: Pointer to driver response IOCB object.
4072  *
4073  * This function is called from the worker thread when there is a slow-path
4074  * response IOCB to process. This function chains all the response iocbs until
4075  * seeing the iocb with the LE bit set. The function will call
4076  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
4077  * completion of a command iocb. The function will call the
4078  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
4079  * The function frees the resources or calls the completion handler if this
4080  * iocb is an abort completion. The function returns NULL when the response
4081  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
4082  * this function shall chain the iocb on to the iocb_continueq and return the
4083  * response iocb passed in.
4084  **/
4085 static struct lpfc_iocbq *
4086 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4087                         struct lpfc_iocbq *rspiocbp)
4088 {
4089         struct lpfc_iocbq *saveq;
4090         struct lpfc_iocbq *cmdiocbp;
4091         struct lpfc_iocbq *next_iocb;
4092         IOCB_t *irsp = NULL;
4093         uint32_t free_saveq;
4094         uint8_t iocb_cmd_type;
4095         lpfc_iocb_type type;
4096         unsigned long iflag;
4097         int rc;
4098
4099         spin_lock_irqsave(&phba->hbalock, iflag);
4100         /* First add the response iocb to the countinueq list */
4101         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
4102         pring->iocb_continueq_cnt++;
4103
4104         /* Now, determine whether the list is completed for processing */
4105         irsp = &rspiocbp->iocb;
4106         if (irsp->ulpLe) {
4107                 /*
4108                  * By default, the driver expects to free all resources
4109                  * associated with this iocb completion.
4110                  */
4111                 free_saveq = 1;
4112                 saveq = list_get_first(&pring->iocb_continueq,
4113                                        struct lpfc_iocbq, list);
4114                 irsp = &(saveq->iocb);
4115                 list_del_init(&pring->iocb_continueq);
4116                 pring->iocb_continueq_cnt = 0;
4117
4118                 pring->stats.iocb_rsp++;
4119
4120                 /*
4121                  * If resource errors reported from HBA, reduce
4122                  * queuedepths of the SCSI device.
4123                  */
4124                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
4125                     ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
4126                      IOERR_NO_RESOURCES)) {
4127                         spin_unlock_irqrestore(&phba->hbalock, iflag);
4128                         phba->lpfc_rampdown_queue_depth(phba);
4129                         spin_lock_irqsave(&phba->hbalock, iflag);
4130                 }
4131
4132                 if (irsp->ulpStatus) {
4133                         /* Rsp ring <ringno> error: IOCB */
4134                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4135                                         "0328 Rsp Ring %d error: "
4136                                         "IOCB Data: "
4137                                         "x%x x%x x%x x%x "
4138                                         "x%x x%x x%x x%x "
4139                                         "x%x x%x x%x x%x "
4140                                         "x%x x%x x%x x%x\n",
4141                                         pring->ringno,
4142                                         irsp->un.ulpWord[0],
4143                                         irsp->un.ulpWord[1],
4144                                         irsp->un.ulpWord[2],
4145                                         irsp->un.ulpWord[3],
4146                                         irsp->un.ulpWord[4],
4147                                         irsp->un.ulpWord[5],
4148                                         *(((uint32_t *) irsp) + 6),
4149                                         *(((uint32_t *) irsp) + 7),
4150                                         *(((uint32_t *) irsp) + 8),
4151                                         *(((uint32_t *) irsp) + 9),
4152                                         *(((uint32_t *) irsp) + 10),
4153                                         *(((uint32_t *) irsp) + 11),
4154                                         *(((uint32_t *) irsp) + 12),
4155                                         *(((uint32_t *) irsp) + 13),
4156                                         *(((uint32_t *) irsp) + 14),
4157                                         *(((uint32_t *) irsp) + 15));
4158                 }
4159
4160                 /*
4161                  * Fetch the IOCB command type and call the correct completion
4162                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
4163                  * get freed back to the lpfc_iocb_list by the discovery
4164                  * kernel thread.
4165                  */
4166                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
4167                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
4168                 switch (type) {
4169                 case LPFC_SOL_IOCB:
4170                         spin_unlock_irqrestore(&phba->hbalock, iflag);
4171                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
4172                         spin_lock_irqsave(&phba->hbalock, iflag);
4173                         break;
4174
4175                 case LPFC_UNSOL_IOCB:
4176                         spin_unlock_irqrestore(&phba->hbalock, iflag);
4177                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
4178                         spin_lock_irqsave(&phba->hbalock, iflag);
4179                         if (!rc)
4180                                 free_saveq = 0;
4181                         break;
4182
4183                 case LPFC_ABORT_IOCB:
4184                         cmdiocbp = NULL;
4185                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX) {
4186                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
4187                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
4188                                                                  saveq);
4189                                 spin_lock_irqsave(&phba->hbalock, iflag);
4190                         }
4191                         if (cmdiocbp) {
4192                                 /* Call the specified completion routine */
4193                                 if (cmdiocbp->iocb_cmpl) {
4194                                         spin_unlock_irqrestore(&phba->hbalock,
4195                                                                iflag);
4196                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
4197                                                               saveq);
4198                                         spin_lock_irqsave(&phba->hbalock,
4199                                                           iflag);
4200                                 } else
4201                                         __lpfc_sli_release_iocbq(phba,
4202                                                                  cmdiocbp);
4203                         }
4204                         break;
4205
4206                 case LPFC_UNKNOWN_IOCB:
4207                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
4208                                 char adaptermsg[LPFC_MAX_ADPTMSG];
4209                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4210                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
4211                                        MAX_MSG_DATA);
4212                                 dev_warn(&((phba->pcidev)->dev),
4213                                          "lpfc%d: %s\n",
4214                                          phba->brd_no, adaptermsg);
4215                         } else {
4216                                 /* Unknown IOCB command */
4217                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4218                                                 "0335 Unknown IOCB "
4219                                                 "command Data: x%x "
4220                                                 "x%x x%x x%x\n",
4221                                                 irsp->ulpCommand,
4222                                                 irsp->ulpStatus,
4223                                                 irsp->ulpIoTag,
4224                                                 irsp->ulpContext);
4225                         }
4226                         break;
4227                 }
4228
4229                 if (free_saveq) {
4230                         list_for_each_entry_safe(rspiocbp, next_iocb,
4231                                                  &saveq->list, list) {
4232                                 list_del_init(&rspiocbp->list);
4233                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
4234                         }
4235                         __lpfc_sli_release_iocbq(phba, saveq);
4236                 }
4237                 rspiocbp = NULL;
4238         }
4239         spin_unlock_irqrestore(&phba->hbalock, iflag);
4240         return rspiocbp;
4241 }
4242
4243 /**
4244  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
4245  * @phba: Pointer to HBA context object.
4246  * @pring: Pointer to driver SLI ring object.
4247  * @mask: Host attention register mask for this ring.
4248  *
4249  * This routine wraps the actual slow_ring event process routine from the
4250  * API jump table function pointer from the lpfc_hba struct.
4251  **/
4252 void
4253 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
4254                                 struct lpfc_sli_ring *pring, uint32_t mask)
4255 {
4256         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
4257 }
4258
4259 /**
4260  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
4261  * @phba: Pointer to HBA context object.
4262  * @pring: Pointer to driver SLI ring object.
4263  * @mask: Host attention register mask for this ring.
4264  *
4265  * This function is called from the worker thread when there is a ring event
4266  * for non-fcp rings. The caller does not hold any lock. The function will
4267  * remove each response iocb in the response ring and calls the handle
4268  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4269  **/
4270 static void
4271 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
4272                                    struct lpfc_sli_ring *pring, uint32_t mask)
4273 {
4274         struct lpfc_pgp *pgp;
4275         IOCB_t *entry;
4276         IOCB_t *irsp = NULL;
4277         struct lpfc_iocbq *rspiocbp = NULL;
4278         uint32_t portRspPut, portRspMax;
4279         unsigned long iflag;
4280         uint32_t status;
4281
4282         pgp = &phba->port_gp[pring->ringno];
4283         spin_lock_irqsave(&phba->hbalock, iflag);
4284         pring->stats.iocb_event++;
4285
4286         /*
4287          * The next available response entry should never exceed the maximum
4288          * entries.  If it does, treat it as an adapter hardware error.
4289          */
4290         portRspMax = pring->sli.sli3.numRiocb;
4291         portRspPut = le32_to_cpu(pgp->rspPutInx);
4292         if (portRspPut >= portRspMax) {
4293                 /*
4294                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
4295                  * rsp ring <portRspMax>
4296                  */
4297                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4298                                 "0303 Ring %d handler: portRspPut %d "
4299                                 "is bigger than rsp ring %d\n",
4300                                 pring->ringno, portRspPut, portRspMax);
4301
4302                 phba->link_state = LPFC_HBA_ERROR;
4303                 spin_unlock_irqrestore(&phba->hbalock, iflag);
4304
4305                 phba->work_hs = HS_FFER3;
4306                 lpfc_handle_eratt(phba);
4307
4308                 return;
4309         }
4310
4311         rmb();
4312         while (pring->sli.sli3.rspidx != portRspPut) {
4313                 /*
4314                  * Build a completion list and call the appropriate handler.
4315                  * The process is to get the next available response iocb, get
4316                  * a free iocb from the list, copy the response data into the
4317                  * free iocb, insert to the continuation list, and update the
4318                  * next response index to slim.  This process makes response
4319                  * iocb's in the ring available to DMA as fast as possible but
4320                  * pays a penalty for a copy operation.  Since the iocb is
4321                  * only 32 bytes, this penalty is considered small relative to
4322                  * the PCI reads for register values and a slim write.  When
4323                  * the ulpLe field is set, the entire Command has been
4324                  * received.
4325                  */
4326                 entry = lpfc_resp_iocb(phba, pring);
4327
4328                 phba->last_completion_time = jiffies;
4329                 rspiocbp = __lpfc_sli_get_iocbq(phba);
4330                 if (rspiocbp == NULL) {
4331                         printk(KERN_ERR "%s: out of buffers! Failing "
4332                                "completion.\n", __func__);
4333                         break;
4334                 }
4335
4336                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
4337                                       phba->iocb_rsp_size);
4338                 irsp = &rspiocbp->iocb;
4339
4340                 if (++pring->sli.sli3.rspidx >= portRspMax)
4341                         pring->sli.sli3.rspidx = 0;
4342
4343                 if (pring->ringno == LPFC_ELS_RING) {
4344                         lpfc_debugfs_slow_ring_trc(phba,
4345                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
4346                                 *(((uint32_t *) irsp) + 4),
4347                                 *(((uint32_t *) irsp) + 6),
4348                                 *(((uint32_t *) irsp) + 7));
4349                 }
4350
4351                 writel(pring->sli.sli3.rspidx,
4352                         &phba->host_gp[pring->ringno].rspGetInx);
4353
4354                 spin_unlock_irqrestore(&phba->hbalock, iflag);
4355                 /* Handle the response IOCB */
4356                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
4357                 spin_lock_irqsave(&phba->hbalock, iflag);
4358
4359                 /*
4360                  * If the port response put pointer has not been updated, sync
4361                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
4362                  * response put pointer.
4363                  */
4364                 if (pring->sli.sli3.rspidx == portRspPut) {
4365                         portRspPut = le32_to_cpu(pgp->rspPutInx);
4366                 }
4367         } /* while (pring->sli.sli3.rspidx != portRspPut) */
4368
4369         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
4370                 /* At least one response entry has been freed */
4371                 pring->stats.iocb_rsp_full++;
4372                 /* SET RxRE_RSP in Chip Att register */
4373                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4374                 writel(status, phba->CAregaddr);
4375                 readl(phba->CAregaddr); /* flush */
4376         }
4377         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4378                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4379                 pring->stats.iocb_cmd_empty++;
4380
4381                 /* Force update of the local copy of cmdGetInx */
4382                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4383                 lpfc_sli_resume_iocb(phba, pring);
4384
4385                 if ((pring->lpfc_sli_cmd_available))
4386                         (pring->lpfc_sli_cmd_available) (phba, pring);
4387
4388         }
4389
4390         spin_unlock_irqrestore(&phba->hbalock, iflag);
4391         return;
4392 }
4393
4394 /**
4395  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
4396  * @phba: Pointer to HBA context object.
4397  * @pring: Pointer to driver SLI ring object.
4398  * @mask: Host attention register mask for this ring.
4399  *
4400  * This function is called from the worker thread when there is a pending
4401  * ELS response iocb on the driver internal slow-path response iocb worker
4402  * queue. The caller does not hold any lock. The function will remove each
4403  * response iocb from the response worker queue and calls the handle
4404  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4405  **/
4406 static void
4407 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4408                                    struct lpfc_sli_ring *pring, uint32_t mask)
4409 {
4410         struct lpfc_iocbq *irspiocbq;
4411         struct hbq_dmabuf *dmabuf;
4412         struct lpfc_cq_event *cq_event;
4413         unsigned long iflag;
4414         int count = 0;
4415
4416         spin_lock_irqsave(&phba->hbalock, iflag);
4417         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
4418         spin_unlock_irqrestore(&phba->hbalock, iflag);
4419         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4420                 /* Get the response iocb from the head of work queue */
4421                 spin_lock_irqsave(&phba->hbalock, iflag);
4422                 list_remove_head(&phba->sli4_hba.sp_queue_event,
4423                                  cq_event, struct lpfc_cq_event, list);
4424                 spin_unlock_irqrestore(&phba->hbalock, iflag);
4425
4426                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4427                 case CQE_CODE_COMPL_WQE:
4428                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4429                                                  cq_event);
4430                         /* Translate ELS WCQE to response IOCBQ */
4431                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
4432                                                                    irspiocbq);
4433                         if (irspiocbq)
4434                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
4435                                                            irspiocbq);
4436                         count++;
4437                         break;
4438                 case CQE_CODE_RECEIVE:
4439                 case CQE_CODE_RECEIVE_V1:
4440                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
4441                                               cq_event);
4442                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
4443                         count++;
4444                         break;
4445                 default:
4446                         break;
4447                 }
4448
4449                 /* Limit the number of events to 64 to avoid soft lockups */
4450                 if (count == 64)
4451                         break;
4452         }
4453 }
4454
4455 /**
4456  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
4457  * @phba: Pointer to HBA context object.
4458  * @pring: Pointer to driver SLI ring object.
4459  *
4460  * This function aborts all iocbs in the given ring and frees all the iocb
4461  * objects in txq. This function issues an abort iocb for all the iocb commands
4462  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4463  * the return of this function. The caller is not required to hold any locks.
4464  **/
4465 void
4466 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4467 {
4468         LIST_HEAD(completions);
4469         struct lpfc_iocbq *iocb, *next_iocb;
4470
4471         if (pring->ringno == LPFC_ELS_RING) {
4472                 lpfc_fabric_abort_hba(phba);
4473         }
4474
4475         /* Error everything on txq and txcmplq
4476          * First do the txq.
4477          */
4478         if (phba->sli_rev >= LPFC_SLI_REV4) {
4479                 spin_lock_irq(&pring->ring_lock);
4480                 list_splice_init(&pring->txq, &completions);
4481                 pring->txq_cnt = 0;
4482                 spin_unlock_irq(&pring->ring_lock);
4483
4484                 spin_lock_irq(&phba->hbalock);
4485                 /* Next issue ABTS for everything on the txcmplq */
4486                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
4487                         lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
4488                 spin_unlock_irq(&phba->hbalock);
4489         } else {
4490                 spin_lock_irq(&phba->hbalock);
4491                 list_splice_init(&pring->txq, &completions);
4492                 pring->txq_cnt = 0;
4493
4494                 /* Next issue ABTS for everything on the txcmplq */
4495                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
4496                         lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
4497                 spin_unlock_irq(&phba->hbalock);
4498         }
4499         /* Make sure HBA is alive */
4500         lpfc_issue_hb_tmo(phba);
4501
4502         /* Cancel all the IOCBs from the completions list */
4503         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4504                               IOERR_SLI_ABORTED);
4505 }
4506
4507 /**
4508  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4509  * @phba: Pointer to HBA context object.
4510  *
4511  * This function aborts all iocbs in FCP rings and frees all the iocb
4512  * objects in txq. This function issues an abort iocb for all the iocb commands
4513  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4514  * the return of this function. The caller is not required to hold any locks.
4515  **/
4516 void
4517 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4518 {
4519         struct lpfc_sli *psli = &phba->sli;
4520         struct lpfc_sli_ring  *pring;
4521         uint32_t i;
4522
4523         /* Look on all the FCP Rings for the iotag */
4524         if (phba->sli_rev >= LPFC_SLI_REV4) {
4525                 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4526                         pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4527                         lpfc_sli_abort_iocb_ring(phba, pring);
4528                 }
4529         } else {
4530                 pring = &psli->sli3_ring[LPFC_FCP_RING];
4531                 lpfc_sli_abort_iocb_ring(phba, pring);
4532         }
4533 }
4534
4535 /**
4536  * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
4537  * @phba: Pointer to HBA context object.
4538  *
4539  * This function flushes all iocbs in the IO ring and frees all the iocb
4540  * objects in txq and txcmplq. This function will not issue abort iocbs
4541  * for all the iocb commands in txcmplq, they will just be returned with
4542  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4543  * slot has been permanently disabled.
4544  **/
4545 void
4546 lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
4547 {
4548         LIST_HEAD(txq);
4549         LIST_HEAD(txcmplq);
4550         struct lpfc_sli *psli = &phba->sli;
4551         struct lpfc_sli_ring  *pring;
4552         uint32_t i;
4553         struct lpfc_iocbq *piocb, *next_iocb;
4554
4555         spin_lock_irq(&phba->hbalock);
4556         if (phba->hba_flag & HBA_IOQ_FLUSH ||
4557             !phba->sli4_hba.hdwq) {
4558                 spin_unlock_irq(&phba->hbalock);
4559                 return;
4560         }
4561         /* Indicate the I/O queues are flushed */
4562         phba->hba_flag |= HBA_IOQ_FLUSH;
4563         spin_unlock_irq(&phba->hbalock);
4564
4565         /* Look on all the FCP Rings for the iotag */
4566         if (phba->sli_rev >= LPFC_SLI_REV4) {
4567                 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4568                         pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4569
4570                         spin_lock_irq(&pring->ring_lock);
4571                         /* Retrieve everything on txq */
4572                         list_splice_init(&pring->txq, &txq);
4573                         list_for_each_entry_safe(piocb, next_iocb,
4574                                                  &pring->txcmplq, list)
4575                                 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4576                         /* Retrieve everything on the txcmplq */
4577                         list_splice_init(&pring->txcmplq, &txcmplq);
4578                         pring->txq_cnt = 0;
4579                         pring->txcmplq_cnt = 0;
4580                         spin_unlock_irq(&pring->ring_lock);
4581
4582                         /* Flush the txq */
4583                         lpfc_sli_cancel_iocbs(phba, &txq,
4584                                               IOSTAT_LOCAL_REJECT,
4585                                               IOERR_SLI_DOWN);
4586                         /* Flush the txcmpq */
4587                         lpfc_sli_cancel_iocbs(phba, &txcmplq,
4588                                               IOSTAT_LOCAL_REJECT,
4589                                               IOERR_SLI_DOWN);
4590                 }
4591         } else {
4592                 pring = &psli->sli3_ring[LPFC_FCP_RING];
4593
4594                 spin_lock_irq(&phba->hbalock);
4595                 /* Retrieve everything on txq */
4596                 list_splice_init(&pring->txq, &txq);
4597                 list_for_each_entry_safe(piocb, next_iocb,
4598                                          &pring->txcmplq, list)
4599                         piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4600                 /* Retrieve everything on the txcmplq */
4601                 list_splice_init(&pring->txcmplq, &txcmplq);
4602                 pring->txq_cnt = 0;
4603                 pring->txcmplq_cnt = 0;
4604                 spin_unlock_irq(&phba->hbalock);
4605
4606                 /* Flush the txq */
4607                 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4608                                       IOERR_SLI_DOWN);
4609                 /* Flush the txcmpq */
4610                 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4611                                       IOERR_SLI_DOWN);
4612         }
4613 }
4614
4615 /**
4616  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
4617  * @phba: Pointer to HBA context object.
4618  * @mask: Bit mask to be checked.
4619  *
4620  * This function reads the host status register and compares
4621  * with the provided bit mask to check if HBA completed
4622  * the restart. This function will wait in a loop for the
4623  * HBA to complete restart. If the HBA does not restart within
4624  * 15 iterations, the function will reset the HBA again. The
4625  * function returns 1 when HBA fail to restart otherwise returns
4626  * zero.
4627  **/
4628 static int
4629 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
4630 {
4631         uint32_t status;
4632         int i = 0;
4633         int retval = 0;
4634
4635         /* Read the HBA Host Status Register */
4636         if (lpfc_readl(phba->HSregaddr, &status))
4637                 return 1;
4638
4639         phba->hba_flag |= HBA_NEEDS_CFG_PORT;
4640
4641         /*
4642          * Check status register every 100ms for 5 retries, then every
4643          * 500ms for 5, then every 2.5 sec for 5, then reset board and
4644          * every 2.5 sec for 4.
4645          * Break our of the loop if errors occurred during init.
4646          */
4647         while (((status & mask) != mask) &&
4648                !(status & HS_FFERM) &&
4649                i++ < 20) {
4650
4651                 if (i <= 5)
4652                         msleep(10);
4653                 else if (i <= 10)
4654                         msleep(500);
4655                 else
4656                         msleep(2500);
4657
4658                 if (i == 15) {
4659                                 /* Do post */
4660                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4661                         lpfc_sli_brdrestart(phba);
4662                 }
4663                 /* Read the HBA Host Status Register */
4664                 if (lpfc_readl(phba->HSregaddr, &status)) {
4665                         retval = 1;
4666                         break;
4667                 }
4668         }
4669
4670         /* Check to see if any errors occurred during init */
4671         if ((status & HS_FFERM) || (i >= 20)) {
4672                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4673                                 "2751 Adapter failed to restart, "
4674                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4675                                 status,
4676                                 readl(phba->MBslimaddr + 0xa8),
4677                                 readl(phba->MBslimaddr + 0xac));
4678                 phba->link_state = LPFC_HBA_ERROR;
4679                 retval = 1;
4680         }
4681
4682         return retval;
4683 }
4684
4685 /**
4686  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4687  * @phba: Pointer to HBA context object.
4688  * @mask: Bit mask to be checked.
4689  *
4690  * This function checks the host status register to check if HBA is
4691  * ready. This function will wait in a loop for the HBA to be ready
4692  * If the HBA is not ready , the function will will reset the HBA PCI
4693  * function again. The function returns 1 when HBA fail to be ready
4694  * otherwise returns zero.
4695  **/
4696 static int
4697 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4698 {
4699         uint32_t status;
4700         int retval = 0;
4701
4702         /* Read the HBA Host Status Register */
4703         status = lpfc_sli4_post_status_check(phba);
4704
4705         if (status) {
4706                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4707                 lpfc_sli_brdrestart(phba);
4708                 status = lpfc_sli4_post_status_check(phba);
4709         }
4710
4711         /* Check to see if any errors occurred during init */
4712         if (status) {
4713                 phba->link_state = LPFC_HBA_ERROR;
4714                 retval = 1;
4715         } else
4716                 phba->sli4_hba.intr_enable = 0;
4717
4718         phba->hba_flag &= ~HBA_SETUP;
4719         return retval;
4720 }
4721
4722 /**
4723  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4724  * @phba: Pointer to HBA context object.
4725  * @mask: Bit mask to be checked.
4726  *
4727  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4728  * from the API jump table function pointer from the lpfc_hba struct.
4729  **/
4730 int
4731 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4732 {
4733         return phba->lpfc_sli_brdready(phba, mask);
4734 }
4735
4736 #define BARRIER_TEST_PATTERN (0xdeadbeef)
4737
4738 /**
4739  * lpfc_reset_barrier - Make HBA ready for HBA reset
4740  * @phba: Pointer to HBA context object.
4741  *
4742  * This function is called before resetting an HBA. This function is called
4743  * with hbalock held and requests HBA to quiesce DMAs before a reset.
4744  **/
4745 void lpfc_reset_barrier(struct lpfc_hba *phba)
4746 {
4747         uint32_t __iomem *resp_buf;
4748         uint32_t __iomem *mbox_buf;
4749         volatile uint32_t mbox;
4750         uint32_t hc_copy, ha_copy, resp_data;
4751         int  i;
4752         uint8_t hdrtype;
4753
4754         lockdep_assert_held(&phba->hbalock);
4755
4756         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4757         if (hdrtype != 0x80 ||
4758             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4759              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4760                 return;
4761
4762         /*
4763          * Tell the other part of the chip to suspend temporarily all
4764          * its DMA activity.
4765          */
4766         resp_buf = phba->MBslimaddr;
4767
4768         /* Disable the error attention */
4769         if (lpfc_readl(phba->HCregaddr, &hc_copy))
4770                 return;
4771         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4772         readl(phba->HCregaddr); /* flush */
4773         phba->link_flag |= LS_IGNORE_ERATT;
4774
4775         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4776                 return;
4777         if (ha_copy & HA_ERATT) {
4778                 /* Clear Chip error bit */
4779                 writel(HA_ERATT, phba->HAregaddr);
4780                 phba->pport->stopped = 1;
4781         }
4782
4783         mbox = 0;
4784         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4785         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4786
4787         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4788         mbox_buf = phba->MBslimaddr;
4789         writel(mbox, mbox_buf);
4790
4791         for (i = 0; i < 50; i++) {
4792                 if (lpfc_readl((resp_buf + 1), &resp_data))
4793                         return;
4794                 if (resp_data != ~(BARRIER_TEST_PATTERN))
4795                         mdelay(1);
4796                 else
4797                         break;
4798         }
4799         resp_data = 0;
4800         if (lpfc_readl((resp_buf + 1), &resp_data))
4801                 return;
4802         if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4803                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4804                     phba->pport->stopped)
4805                         goto restore_hc;
4806                 else
4807                         goto clear_errat;
4808         }
4809
4810         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
4811         resp_data = 0;
4812         for (i = 0; i < 500; i++) {
4813                 if (lpfc_readl(resp_buf, &resp_data))
4814                         return;
4815                 if (resp_data != mbox)
4816                         mdelay(1);
4817                 else
4818                         break;
4819         }
4820
4821 clear_errat:
4822
4823         while (++i < 500) {
4824                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4825                         return;
4826                 if (!(ha_copy & HA_ERATT))
4827                         mdelay(1);
4828                 else
4829                         break;
4830         }
4831
4832         if (readl(phba->HAregaddr) & HA_ERATT) {
4833                 writel(HA_ERATT, phba->HAregaddr);
4834                 phba->pport->stopped = 1;
4835         }
4836
4837 restore_hc:
4838         phba->link_flag &= ~LS_IGNORE_ERATT;
4839         writel(hc_copy, phba->HCregaddr);
4840         readl(phba->HCregaddr); /* flush */
4841 }
4842
4843 /**
4844  * lpfc_sli_brdkill - Issue a kill_board mailbox command
4845  * @phba: Pointer to HBA context object.
4846  *
4847  * This function issues a kill_board mailbox command and waits for
4848  * the error attention interrupt. This function is called for stopping
4849  * the firmware processing. The caller is not required to hold any
4850  * locks. This function calls lpfc_hba_down_post function to free
4851  * any pending commands after the kill. The function will return 1 when it
4852  * fails to kill the board else will return 0.
4853  **/
4854 int
4855 lpfc_sli_brdkill(struct lpfc_hba *phba)
4856 {
4857         struct lpfc_sli *psli;
4858         LPFC_MBOXQ_t *pmb;
4859         uint32_t status;
4860         uint32_t ha_copy;
4861         int retval;
4862         int i = 0;
4863
4864         psli = &phba->sli;
4865
4866         /* Kill HBA */
4867         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4868                         "0329 Kill HBA Data: x%x x%x\n",
4869                         phba->pport->port_state, psli->sli_flag);
4870
4871         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4872         if (!pmb)
4873                 return 1;
4874
4875         /* Disable the error attention */
4876         spin_lock_irq(&phba->hbalock);
4877         if (lpfc_readl(phba->HCregaddr, &status)) {
4878                 spin_unlock_irq(&phba->hbalock);
4879                 mempool_free(pmb, phba->mbox_mem_pool);
4880                 return 1;
4881         }
4882         status &= ~HC_ERINT_ENA;
4883         writel(status, phba->HCregaddr);
4884         readl(phba->HCregaddr); /* flush */
4885         phba->link_flag |= LS_IGNORE_ERATT;
4886         spin_unlock_irq(&phba->hbalock);
4887
4888         lpfc_kill_board(phba, pmb);
4889         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4890         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4891
4892         if (retval != MBX_SUCCESS) {
4893                 if (retval != MBX_BUSY)
4894                         mempool_free(pmb, phba->mbox_mem_pool);
4895                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4896                                 "2752 KILL_BOARD command failed retval %d\n",
4897                                 retval);
4898                 spin_lock_irq(&phba->hbalock);
4899                 phba->link_flag &= ~LS_IGNORE_ERATT;
4900                 spin_unlock_irq(&phba->hbalock);
4901                 return 1;
4902         }
4903
4904         spin_lock_irq(&phba->hbalock);
4905         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4906         spin_unlock_irq(&phba->hbalock);
4907
4908         mempool_free(pmb, phba->mbox_mem_pool);
4909
4910         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4911          * attention every 100ms for 3 seconds. If we don't get ERATT after
4912          * 3 seconds we still set HBA_ERROR state because the status of the
4913          * board is now undefined.
4914          */
4915         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4916                 return 1;
4917         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4918                 mdelay(100);
4919                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4920                         return 1;
4921         }
4922
4923         del_timer_sync(&psli->mbox_tmo);
4924         if (ha_copy & HA_ERATT) {
4925                 writel(HA_ERATT, phba->HAregaddr);
4926                 phba->pport->stopped = 1;
4927         }
4928         spin_lock_irq(&phba->hbalock);
4929         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4930         psli->mbox_active = NULL;
4931         phba->link_flag &= ~LS_IGNORE_ERATT;
4932         spin_unlock_irq(&phba->hbalock);
4933
4934         lpfc_hba_down_post(phba);
4935         phba->link_state = LPFC_HBA_ERROR;
4936
4937         return ha_copy & HA_ERATT ? 0 : 1;
4938 }
4939
4940 /**
4941  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
4942  * @phba: Pointer to HBA context object.
4943  *
4944  * This function resets the HBA by writing HC_INITFF to the control
4945  * register. After the HBA resets, this function resets all the iocb ring
4946  * indices. This function disables PCI layer parity checking during
4947  * the reset.
4948  * This function returns 0 always.
4949  * The caller is not required to hold any locks.
4950  **/
4951 int
4952 lpfc_sli_brdreset(struct lpfc_hba *phba)
4953 {
4954         struct lpfc_sli *psli;
4955         struct lpfc_sli_ring *pring;
4956         uint16_t cfg_value;
4957         int i;
4958
4959         psli = &phba->sli;
4960
4961         /* Reset HBA */
4962         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4963                         "0325 Reset HBA Data: x%x x%x\n",
4964                         (phba->pport) ? phba->pport->port_state : 0,
4965                         psli->sli_flag);
4966
4967         /* perform board reset */
4968         phba->fc_eventTag = 0;
4969         phba->link_events = 0;
4970         phba->hba_flag |= HBA_NEEDS_CFG_PORT;
4971         if (phba->pport) {
4972                 phba->pport->fc_myDID = 0;
4973                 phba->pport->fc_prevDID = 0;
4974         }
4975
4976         /* Turn off parity checking and serr during the physical reset */
4977         if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
4978                 return -EIO;
4979
4980         pci_write_config_word(phba->pcidev, PCI_COMMAND,
4981                               (cfg_value &
4982                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4983
4984         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4985
4986         /* Now toggle INITFF bit in the Host Control Register */
4987         writel(HC_INITFF, phba->HCregaddr);
4988         mdelay(1);
4989         readl(phba->HCregaddr); /* flush */
4990         writel(0, phba->HCregaddr);
4991         readl(phba->HCregaddr); /* flush */
4992
4993         /* Restore PCI cmd register */
4994         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4995
4996         /* Initialize relevant SLI info */
4997         for (i = 0; i < psli->num_rings; i++) {
4998                 pring = &psli->sli3_ring[i];
4999                 pring->flag = 0;
5000                 pring->sli.sli3.rspidx = 0;
5001                 pring->sli.sli3.next_cmdidx  = 0;
5002                 pring->sli.sli3.local_getidx = 0;
5003                 pring->sli.sli3.cmdidx = 0;
5004                 pring->missbufcnt = 0;
5005         }
5006
5007         phba->link_state = LPFC_WARM_START;
5008         return 0;
5009 }
5010
5011 /**
5012  * lpfc_sli4_brdreset - Reset a sli-4 HBA
5013  * @phba: Pointer to HBA context object.
5014  *
5015  * This function resets a SLI4 HBA. This function disables PCI layer parity
5016  * checking during resets the device. The caller is not required to hold
5017  * any locks.
5018  *
5019  * This function returns 0 on success else returns negative error code.
5020  **/
5021 int
5022 lpfc_sli4_brdreset(struct lpfc_hba *phba)
5023 {
5024         struct lpfc_sli *psli = &phba->sli;
5025         uint16_t cfg_value;
5026         int rc = 0;
5027
5028         /* Reset HBA */
5029         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5030                         "0295 Reset HBA Data: x%x x%x x%x\n",
5031                         phba->pport->port_state, psli->sli_flag,
5032                         phba->hba_flag);
5033
5034         /* perform board reset */
5035         phba->fc_eventTag = 0;
5036         phba->link_events = 0;
5037         phba->pport->fc_myDID = 0;
5038         phba->pport->fc_prevDID = 0;
5039         phba->hba_flag &= ~HBA_SETUP;
5040
5041         spin_lock_irq(&phba->hbalock);
5042         psli->sli_flag &= ~(LPFC_PROCESS_LA);
5043         phba->fcf.fcf_flag = 0;
5044         spin_unlock_irq(&phba->hbalock);
5045
5046         /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
5047         if (phba->hba_flag & HBA_FW_DUMP_OP) {
5048                 phba->hba_flag &= ~HBA_FW_DUMP_OP;
5049                 return rc;
5050         }
5051
5052         /* Now physically reset the device */
5053         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5054                         "0389 Performing PCI function reset!\n");
5055
5056         /* Turn off parity checking and serr during the physical reset */
5057         if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
5058                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5059                                 "3205 PCI read Config failed\n");
5060                 return -EIO;
5061         }
5062
5063         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
5064                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5065
5066         /* Perform FCoE PCI function reset before freeing queue memory */
5067         rc = lpfc_pci_function_reset(phba);
5068
5069         /* Restore PCI cmd register */
5070         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5071
5072         return rc;
5073 }
5074
5075 /**
5076  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
5077  * @phba: Pointer to HBA context object.
5078  *
5079  * This function is called in the SLI initialization code path to
5080  * restart the HBA. The caller is not required to hold any lock.
5081  * This function writes MBX_RESTART mailbox command to the SLIM and
5082  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
5083  * function to free any pending commands. The function enables
5084  * POST only during the first initialization. The function returns zero.
5085  * The function does not guarantee completion of MBX_RESTART mailbox
5086  * command before the return of this function.
5087  **/
5088 static int
5089 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
5090 {
5091         MAILBOX_t *mb;
5092         struct lpfc_sli *psli;
5093         volatile uint32_t word0;
5094         void __iomem *to_slim;
5095         uint32_t hba_aer_enabled;
5096
5097         spin_lock_irq(&phba->hbalock);
5098
5099         /* Take PCIe device Advanced Error Reporting (AER) state */
5100         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
5101
5102         psli = &phba->sli;
5103
5104         /* Restart HBA */
5105         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5106                         "0337 Restart HBA Data: x%x x%x\n",
5107                         (phba->pport) ? phba->pport->port_state : 0,
5108                         psli->sli_flag);
5109
5110         word0 = 0;
5111         mb = (MAILBOX_t *) &word0;
5112         mb->mbxCommand = MBX_RESTART;
5113         mb->mbxHc = 1;
5114
5115         lpfc_reset_barrier(phba);
5116
5117         to_slim = phba->MBslimaddr;
5118         writel(*(uint32_t *) mb, to_slim);
5119         readl(to_slim); /* flush */
5120
5121         /* Only skip post after fc_ffinit is completed */
5122         if (phba->pport && phba->pport->port_state)
5123                 word0 = 1;      /* This is really setting up word1 */
5124         else
5125                 word0 = 0;      /* This is really setting up word1 */
5126         to_slim = phba->MBslimaddr + sizeof (uint32_t);
5127         writel(*(uint32_t *) mb, to_slim);
5128         readl(to_slim); /* flush */
5129
5130         lpfc_sli_brdreset(phba);
5131         if (phba->pport)
5132                 phba->pport->stopped = 0;
5133         phba->link_state = LPFC_INIT_START;
5134         phba->hba_flag = 0;
5135         spin_unlock_irq(&phba->hbalock);
5136
5137         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
5138         psli->stats_start = ktime_get_seconds();
5139
5140         /* Give the INITFF and Post time to settle. */
5141         mdelay(100);
5142
5143         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
5144         if (hba_aer_enabled)
5145                 pci_disable_pcie_error_reporting(phba->pcidev);
5146
5147         lpfc_hba_down_post(phba);
5148
5149         return 0;
5150 }
5151
5152 /**
5153  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
5154  * @phba: Pointer to HBA context object.
5155  *
5156  * This function is called in the SLI initialization code path to restart
5157  * a SLI4 HBA. The caller is not required to hold any lock.
5158  * At the end of the function, it calls lpfc_hba_down_post function to
5159  * free any pending commands.
5160  **/
5161 static int
5162 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
5163 {
5164         struct lpfc_sli *psli = &phba->sli;
5165         uint32_t hba_aer_enabled;
5166         int rc;
5167
5168         /* Restart HBA */
5169         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5170                         "0296 Restart HBA Data: x%x x%x\n",
5171                         phba->pport->port_state, psli->sli_flag);
5172
5173         /* Take PCIe device Advanced Error Reporting (AER) state */
5174         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
5175
5176         rc = lpfc_sli4_brdreset(phba);
5177         if (rc) {
5178                 phba->link_state = LPFC_HBA_ERROR;
5179                 goto hba_down_queue;
5180         }
5181
5182         spin_lock_irq(&phba->hbalock);
5183         phba->pport->stopped = 0;
5184         phba->link_state = LPFC_INIT_START;
5185         phba->hba_flag = 0;
5186         spin_unlock_irq(&phba->hbalock);
5187
5188         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
5189         psli->stats_start = ktime_get_seconds();
5190
5191         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
5192         if (hba_aer_enabled)
5193                 pci_disable_pcie_error_reporting(phba->pcidev);
5194
5195 hba_down_queue:
5196         lpfc_hba_down_post(phba);
5197         lpfc_sli4_queue_destroy(phba);
5198
5199         return rc;
5200 }
5201
5202 /**
5203  * lpfc_sli_brdrestart - Wrapper func for restarting hba
5204  * @phba: Pointer to HBA context object.
5205  *
5206  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
5207  * API jump table function pointer from the lpfc_hba struct.
5208 **/
5209 int
5210 lpfc_sli_brdrestart(struct lpfc_hba *phba)
5211 {
5212         return phba->lpfc_sli_brdrestart(phba);
5213 }
5214
5215 /**
5216  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
5217  * @phba: Pointer to HBA context object.
5218  *
5219  * This function is called after a HBA restart to wait for successful
5220  * restart of the HBA. Successful restart of the HBA is indicated by
5221  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
5222  * iteration, the function will restart the HBA again. The function returns
5223  * zero if HBA successfully restarted else returns negative error code.
5224  **/
5225 int
5226 lpfc_sli_chipset_init(struct lpfc_hba *phba)
5227 {
5228         uint32_t status, i = 0;
5229
5230         /* Read the HBA Host Status Register */
5231         if (lpfc_readl(phba->HSregaddr, &status))
5232                 return -EIO;
5233
5234         /* Check status register to see what current state is */
5235         i = 0;
5236         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
5237
5238                 /* Check every 10ms for 10 retries, then every 100ms for 90
5239                  * retries, then every 1 sec for 50 retires for a total of
5240                  * ~60 seconds before reset the board again and check every
5241                  * 1 sec for 50 retries. The up to 60 seconds before the
5242                  * board ready is required by the Falcon FIPS zeroization
5243                  * complete, and any reset the board in between shall cause
5244                  * restart of zeroization, further delay the board ready.
5245                  */
5246                 if (i++ >= 200) {
5247                         /* Adapter failed to init, timeout, status reg
5248                            <status> */
5249                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5250                                         "0436 Adapter failed to init, "
5251                                         "timeout, status reg x%x, "
5252                                         "FW Data: A8 x%x AC x%x\n", status,
5253                                         readl(phba->MBslimaddr + 0xa8),
5254                                         readl(phba->MBslimaddr + 0xac));
5255                         phba->link_state = LPFC_HBA_ERROR;
5256                         return -ETIMEDOUT;
5257                 }
5258
5259                 /* Check to see if any errors occurred during init */
5260                 if (status & HS_FFERM) {
5261                         /* ERROR: During chipset initialization */
5262                         /* Adapter failed to init, chipset, status reg
5263                            <status> */
5264                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5265                                         "0437 Adapter failed to init, "
5266                                         "chipset, status reg x%x, "
5267                                         "FW Data: A8 x%x AC x%x\n", status,
5268                                         readl(phba->MBslimaddr + 0xa8),
5269                                         readl(phba->MBslimaddr + 0xac));
5270                         phba->link_state = LPFC_HBA_ERROR;
5271                         return -EIO;
5272                 }
5273
5274                 if (i <= 10)
5275                         msleep(10);
5276                 else if (i <= 100)
5277                         msleep(100);
5278                 else
5279                         msleep(1000);
5280
5281                 if (i == 150) {
5282                         /* Do post */
5283                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5284                         lpfc_sli_brdrestart(phba);
5285                 }
5286                 /* Read the HBA Host Status Register */
5287                 if (lpfc_readl(phba->HSregaddr, &status))
5288                         return -EIO;
5289         }
5290
5291         /* Check to see if any errors occurred during init */
5292         if (status & HS_FFERM) {
5293                 /* ERROR: During chipset initialization */
5294                 /* Adapter failed to init, chipset, status reg <status> */
5295                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5296                                 "0438 Adapter failed to init, chipset, "
5297                                 "status reg x%x, "
5298                                 "FW Data: A8 x%x AC x%x\n", status,
5299                                 readl(phba->MBslimaddr + 0xa8),
5300                                 readl(phba->MBslimaddr + 0xac));
5301                 phba->link_state = LPFC_HBA_ERROR;
5302                 return -EIO;
5303         }
5304
5305         phba->hba_flag |= HBA_NEEDS_CFG_PORT;
5306
5307         /* Clear all interrupt enable conditions */
5308         writel(0, phba->HCregaddr);
5309         readl(phba->HCregaddr); /* flush */
5310
5311         /* setup host attn register */
5312         writel(0xffffffff, phba->HAregaddr);
5313         readl(phba->HAregaddr); /* flush */
5314         return 0;
5315 }
5316
5317 /**
5318  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
5319  *
5320  * This function calculates and returns the number of HBQs required to be
5321  * configured.
5322  **/
5323 int
5324 lpfc_sli_hbq_count(void)
5325 {
5326         return ARRAY_SIZE(lpfc_hbq_defs);
5327 }
5328
5329 /**
5330  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
5331  *
5332  * This function adds the number of hbq entries in every HBQ to get
5333  * the total number of hbq entries required for the HBA and returns
5334  * the total count.
5335  **/
5336 static int
5337 lpfc_sli_hbq_entry_count(void)
5338 {
5339         int  hbq_count = lpfc_sli_hbq_count();
5340         int  count = 0;
5341         int  i;
5342
5343         for (i = 0; i < hbq_count; ++i)
5344                 count += lpfc_hbq_defs[i]->entry_count;
5345         return count;
5346 }
5347
5348 /**
5349  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
5350  *
5351  * This function calculates amount of memory required for all hbq entries
5352  * to be configured and returns the total memory required.
5353  **/
5354 int
5355 lpfc_sli_hbq_size(void)
5356 {
5357         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
5358 }
5359
5360 /**
5361  * lpfc_sli_hbq_setup - configure and initialize HBQs
5362  * @phba: Pointer to HBA context object.
5363  *
5364  * This function is called during the SLI initialization to configure
5365  * all the HBQs and post buffers to the HBQ. The caller is not
5366  * required to hold any locks. This function will return zero if successful
5367  * else it will return negative error code.
5368  **/
5369 static int
5370 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
5371 {
5372         int  hbq_count = lpfc_sli_hbq_count();
5373         LPFC_MBOXQ_t *pmb;
5374         MAILBOX_t *pmbox;
5375         uint32_t hbqno;
5376         uint32_t hbq_entry_index;
5377
5378                                 /* Get a Mailbox buffer to setup mailbox
5379                                  * commands for HBA initialization
5380                                  */
5381         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5382
5383         if (!pmb)
5384                 return -ENOMEM;
5385
5386         pmbox = &pmb->u.mb;
5387
5388         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
5389         phba->link_state = LPFC_INIT_MBX_CMDS;
5390         phba->hbq_in_use = 1;
5391
5392         hbq_entry_index = 0;
5393         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
5394                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
5395                 phba->hbqs[hbqno].hbqPutIdx      = 0;
5396                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
5397                 phba->hbqs[hbqno].entry_count =
5398                         lpfc_hbq_defs[hbqno]->entry_count;
5399                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
5400                         hbq_entry_index, pmb);
5401                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
5402
5403                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
5404                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
5405                            mbxStatus <status>, ring <num> */
5406
5407                         lpfc_printf_log(phba, KERN_ERR,
5408                                         LOG_SLI | LOG_VPORT,
5409                                         "1805 Adapter failed to init. "
5410                                         "Data: x%x x%x x%x\n",
5411                                         pmbox->mbxCommand,
5412                                         pmbox->mbxStatus, hbqno);
5413
5414                         phba->link_state = LPFC_HBA_ERROR;
5415                         mempool_free(pmb, phba->mbox_mem_pool);
5416                         return -ENXIO;
5417                 }
5418         }
5419         phba->hbq_count = hbq_count;
5420
5421         mempool_free(pmb, phba->mbox_mem_pool);
5422
5423         /* Initially populate or replenish the HBQs */
5424         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5425                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
5426         return 0;
5427 }
5428
5429 /**
5430  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5431  * @phba: Pointer to HBA context object.
5432  *
5433  * This function is called during the SLI initialization to configure
5434  * all the HBQs and post buffers to the HBQ. The caller is not
5435  * required to hold any locks. This function will return zero if successful
5436  * else it will return negative error code.
5437  **/
5438 static int
5439 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5440 {
5441         phba->hbq_in_use = 1;
5442         /**
5443          * Specific case when the MDS diagnostics is enabled and supported.
5444          * The receive buffer count is truncated to manage the incoming
5445          * traffic.
5446          **/
5447         if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5448                 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5449                         lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5450         else
5451                 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5452                         lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
5453         phba->hbq_count = 1;
5454         lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
5455         /* Initially populate or replenish the HBQs */
5456         return 0;
5457 }
5458
5459 /**
5460  * lpfc_sli_config_port - Issue config port mailbox command
5461  * @phba: Pointer to HBA context object.
5462  * @sli_mode: sli mode - 2/3
5463  *
5464  * This function is called by the sli initialization code path
5465  * to issue config_port mailbox command. This function restarts the
5466  * HBA firmware and issues a config_port mailbox command to configure
5467  * the SLI interface in the sli mode specified by sli_mode
5468  * variable. The caller is not required to hold any locks.
5469  * The function returns 0 if successful, else returns negative error
5470  * code.
5471  **/
5472 int
5473 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
5474 {
5475         LPFC_MBOXQ_t *pmb;
5476         uint32_t resetcount = 0, rc = 0, done = 0;
5477
5478         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5479         if (!pmb) {
5480                 phba->link_state = LPFC_HBA_ERROR;
5481                 return -ENOMEM;
5482         }
5483
5484         phba->sli_rev = sli_mode;
5485         while (resetcount < 2 && !done) {
5486                 spin_lock_irq(&phba->hbalock);
5487                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5488                 spin_unlock_irq(&phba->hbalock);
5489                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5490                 lpfc_sli_brdrestart(phba);
5491                 rc = lpfc_sli_chipset_init(phba);
5492                 if (rc)
5493                         break;
5494
5495                 spin_lock_irq(&phba->hbalock);
5496                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5497                 spin_unlock_irq(&phba->hbalock);
5498                 resetcount++;
5499
5500                 /* Call pre CONFIG_PORT mailbox command initialization.  A
5501                  * value of 0 means the call was successful.  Any other
5502                  * nonzero value is a failure, but if ERESTART is returned,
5503                  * the driver may reset the HBA and try again.
5504                  */
5505                 rc = lpfc_config_port_prep(phba);
5506                 if (rc == -ERESTART) {
5507                         phba->link_state = LPFC_LINK_UNKNOWN;
5508                         continue;
5509                 } else if (rc)
5510                         break;
5511
5512                 phba->link_state = LPFC_INIT_MBX_CMDS;
5513                 lpfc_config_port(phba, pmb);
5514                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5515                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5516                                         LPFC_SLI3_HBQ_ENABLED |
5517                                         LPFC_SLI3_CRP_ENABLED |
5518                                         LPFC_SLI3_DSS_ENABLED);
5519                 if (rc != MBX_SUCCESS) {
5520                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5521                                 "0442 Adapter failed to init, mbxCmd x%x "
5522                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
5523                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
5524                         spin_lock_irq(&phba->hbalock);
5525                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
5526                         spin_unlock_irq(&phba->hbalock);
5527                         rc = -ENXIO;
5528                 } else {
5529                         /* Allow asynchronous mailbox command to go through */
5530                         spin_lock_irq(&phba->hbalock);
5531                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5532                         spin_unlock_irq(&phba->hbalock);
5533                         done = 1;
5534
5535                         if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5536                             (pmb->u.mb.un.varCfgPort.gasabt == 0))
5537                                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5538                                         "3110 Port did not grant ASABT\n");
5539                 }
5540         }
5541         if (!done) {
5542                 rc = -EINVAL;
5543                 goto do_prep_failed;
5544         }
5545         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5546                 if (!pmb->u.mb.un.varCfgPort.cMA) {
5547                         rc = -ENXIO;
5548                         goto do_prep_failed;
5549                 }
5550                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
5551                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
5552                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5553                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5554                                 phba->max_vpi : phba->max_vports;
5555
5556                 } else
5557                         phba->max_vpi = 0;
5558                 if (pmb->u.mb.un.varCfgPort.gerbm)
5559                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
5560                 if (pmb->u.mb.un.varCfgPort.gcrp)
5561                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
5562
5563                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5564                 phba->port_gp = phba->mbox->us.s3_pgp.port;
5565
5566                 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5567                         if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5568                                 phba->cfg_enable_bg = 0;
5569                                 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5570                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5571                                                 "0443 Adapter did not grant "
5572                                                 "BlockGuard\n");
5573                         }
5574                 }
5575         } else {
5576                 phba->hbq_get = NULL;
5577                 phba->port_gp = phba->mbox->us.s2.port;
5578                 phba->max_vpi = 0;
5579         }
5580 do_prep_failed:
5581         mempool_free(pmb, phba->mbox_mem_pool);
5582         return rc;
5583 }
5584
5585
5586 /**
5587  * lpfc_sli_hba_setup - SLI initialization function
5588  * @phba: Pointer to HBA context object.
5589  *
5590  * This function is the main SLI initialization function. This function
5591  * is called by the HBA initialization code, HBA reset code and HBA
5592  * error attention handler code. Caller is not required to hold any
5593  * locks. This function issues config_port mailbox command to configure
5594  * the SLI, setup iocb rings and HBQ rings. In the end the function
5595  * calls the config_port_post function to issue init_link mailbox
5596  * command and to start the discovery. The function will return zero
5597  * if successful, else it will return negative error code.
5598  **/
5599 int
5600 lpfc_sli_hba_setup(struct lpfc_hba *phba)
5601 {
5602         uint32_t rc;
5603         int  i;
5604         int longs;
5605
5606         /* Enable ISR already does config_port because of config_msi mbx */
5607         if (phba->hba_flag & HBA_NEEDS_CFG_PORT) {
5608                 rc = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
5609                 if (rc)
5610                         return -EIO;
5611                 phba->hba_flag &= ~HBA_NEEDS_CFG_PORT;
5612         }
5613         phba->fcp_embed_io = 0; /* SLI4 FC support only */
5614
5615         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5616         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5617                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5618                 if (!rc) {
5619                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5620                                         "2709 This device supports "
5621                                         "Advanced Error Reporting (AER)\n");
5622                         spin_lock_irq(&phba->hbalock);
5623                         phba->hba_flag |= HBA_AER_ENABLED;
5624                         spin_unlock_irq(&phba->hbalock);
5625                 } else {
5626                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5627                                         "2708 This device does not support "
5628                                         "Advanced Error Reporting (AER): %d\n",
5629                                         rc);
5630                         phba->cfg_aer_support = 0;
5631                 }
5632         }
5633
5634         if (phba->sli_rev == 3) {
5635                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5636                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
5637         } else {
5638                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5639                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
5640                 phba->sli3_options = 0;
5641         }
5642
5643         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5644                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5645                         phba->sli_rev, phba->max_vpi);
5646         rc = lpfc_sli_ring_map(phba);
5647
5648         if (rc)
5649                 goto lpfc_sli_hba_setup_error;
5650
5651         /* Initialize VPIs. */
5652         if (phba->sli_rev == LPFC_SLI_REV3) {
5653                 /*
5654                  * The VPI bitmask and physical ID array are allocated
5655                  * and initialized once only - at driver load.  A port
5656                  * reset doesn't need to reinitialize this memory.
5657                  */
5658                 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5659                         longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5660                         phba->vpi_bmask = kcalloc(longs,
5661                                                   sizeof(unsigned long),
5662                                                   GFP_KERNEL);
5663                         if (!phba->vpi_bmask) {
5664                                 rc = -ENOMEM;
5665                                 goto lpfc_sli_hba_setup_error;
5666                         }
5667
5668                         phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5669                                                 sizeof(uint16_t),
5670                                                 GFP_KERNEL);
5671                         if (!phba->vpi_ids) {
5672                                 kfree(phba->vpi_bmask);
5673                                 rc = -ENOMEM;
5674                                 goto lpfc_sli_hba_setup_error;
5675                         }
5676                         for (i = 0; i < phba->max_vpi; i++)
5677                                 phba->vpi_ids[i] = i;
5678                 }
5679         }
5680
5681         /* Init HBQs */
5682         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5683                 rc = lpfc_sli_hbq_setup(phba);
5684                 if (rc)
5685                         goto lpfc_sli_hba_setup_error;
5686         }
5687         spin_lock_irq(&phba->hbalock);
5688         phba->sli.sli_flag |= LPFC_PROCESS_LA;
5689         spin_unlock_irq(&phba->hbalock);
5690
5691         rc = lpfc_config_port_post(phba);
5692         if (rc)
5693                 goto lpfc_sli_hba_setup_error;
5694
5695         return rc;
5696
5697 lpfc_sli_hba_setup_error:
5698         phba->link_state = LPFC_HBA_ERROR;
5699         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5700                         "0445 Firmware initialization failed\n");
5701         return rc;
5702 }
5703
5704 /**
5705  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5706  * @phba: Pointer to HBA context object.
5707  *
5708  * This function issue a dump mailbox command to read config region
5709  * 23 and parse the records in the region and populate driver
5710  * data structure.
5711  **/
5712 static int
5713 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
5714 {
5715         LPFC_MBOXQ_t *mboxq;
5716         struct lpfc_dmabuf *mp;
5717         struct lpfc_mqe *mqe;
5718         uint32_t data_length;
5719         int rc;
5720
5721         /* Program the default value of vlan_id and fc_map */
5722         phba->valid_vlan = 0;
5723         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5724         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5725         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5726
5727         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5728         if (!mboxq)
5729                 return -ENOMEM;
5730
5731         mqe = &mboxq->u.mqe;
5732         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5733                 rc = -ENOMEM;
5734                 goto out_free_mboxq;
5735         }
5736
5737         mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
5738         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5739
5740         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5741                         "(%d):2571 Mailbox cmd x%x Status x%x "
5742                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5743                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5744                         "CQ: x%x x%x x%x x%x\n",
5745                         mboxq->vport ? mboxq->vport->vpi : 0,
5746                         bf_get(lpfc_mqe_command, mqe),
5747                         bf_get(lpfc_mqe_status, mqe),
5748                         mqe->un.mb_words[0], mqe->un.mb_words[1],
5749                         mqe->un.mb_words[2], mqe->un.mb_words[3],
5750                         mqe->un.mb_words[4], mqe->un.mb_words[5],
5751                         mqe->un.mb_words[6], mqe->un.mb_words[7],
5752                         mqe->un.mb_words[8], mqe->un.mb_words[9],
5753                         mqe->un.mb_words[10], mqe->un.mb_words[11],
5754                         mqe->un.mb_words[12], mqe->un.mb_words[13],
5755                         mqe->un.mb_words[14], mqe->un.mb_words[15],
5756                         mqe->un.mb_words[16], mqe->un.mb_words[50],
5757                         mboxq->mcqe.word0,
5758                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5759                         mboxq->mcqe.trailer);
5760
5761         if (rc) {
5762                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5763                 kfree(mp);
5764                 rc = -EIO;
5765                 goto out_free_mboxq;
5766         }
5767         data_length = mqe->un.mb_words[5];
5768         if (data_length > DMP_RGN23_SIZE) {
5769                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5770                 kfree(mp);
5771                 rc = -EIO;
5772                 goto out_free_mboxq;
5773         }
5774
5775         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5776         lpfc_mbuf_free(phba, mp->virt, mp->phys);
5777         kfree(mp);
5778         rc = 0;
5779
5780 out_free_mboxq:
5781         mempool_free(mboxq, phba->mbox_mem_pool);
5782         return rc;
5783 }
5784
5785 /**
5786  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5787  * @phba: pointer to lpfc hba data structure.
5788  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5789  * @vpd: pointer to the memory to hold resulting port vpd data.
5790  * @vpd_size: On input, the number of bytes allocated to @vpd.
5791  *            On output, the number of data bytes in @vpd.
5792  *
5793  * This routine executes a READ_REV SLI4 mailbox command.  In
5794  * addition, this routine gets the port vpd data.
5795  *
5796  * Return codes
5797  *      0 - successful
5798  *      -ENOMEM - could not allocated memory.
5799  **/
5800 static int
5801 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5802                     uint8_t *vpd, uint32_t *vpd_size)
5803 {
5804         int rc = 0;
5805         uint32_t dma_size;
5806         struct lpfc_dmabuf *dmabuf;
5807         struct lpfc_mqe *mqe;
5808
5809         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5810         if (!dmabuf)
5811                 return -ENOMEM;
5812
5813         /*
5814          * Get a DMA buffer for the vpd data resulting from the READ_REV
5815          * mailbox command.
5816          */
5817         dma_size = *vpd_size;
5818         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5819                                           &dmabuf->phys, GFP_KERNEL);
5820         if (!dmabuf->virt) {
5821                 kfree(dmabuf);
5822                 return -ENOMEM;
5823         }
5824
5825         /*
5826          * The SLI4 implementation of READ_REV conflicts at word1,
5827          * bits 31:16 and SLI4 adds vpd functionality not present
5828          * in SLI3.  This code corrects the conflicts.
5829          */
5830         lpfc_read_rev(phba, mboxq);
5831         mqe = &mboxq->u.mqe;
5832         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5833         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5834         mqe->un.read_rev.word1 &= 0x0000FFFF;
5835         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5836         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5837
5838         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5839         if (rc) {
5840                 dma_free_coherent(&phba->pcidev->dev, dma_size,
5841                                   dmabuf->virt, dmabuf->phys);
5842                 kfree(dmabuf);
5843                 return -EIO;
5844         }
5845
5846         /*
5847          * The available vpd length cannot be bigger than the
5848          * DMA buffer passed to the port.  Catch the less than
5849          * case and update the caller's size.
5850          */
5851         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5852                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5853
5854         memcpy(vpd, dmabuf->virt, *vpd_size);
5855
5856         dma_free_coherent(&phba->pcidev->dev, dma_size,
5857                           dmabuf->virt, dmabuf->phys);
5858         kfree(dmabuf);
5859         return 0;
5860 }
5861
5862 /**
5863  * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
5864  * @phba: pointer to lpfc hba data structure.
5865  *
5866  * This routine retrieves SLI4 device physical port name this PCI function
5867  * is attached to.
5868  *
5869  * Return codes
5870  *      0 - successful
5871  *      otherwise - failed to retrieve controller attributes
5872  **/
5873 static int
5874 lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
5875 {
5876         LPFC_MBOXQ_t *mboxq;
5877         struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5878         struct lpfc_controller_attribute *cntl_attr;
5879         void *virtaddr = NULL;
5880         uint32_t alloclen, reqlen;
5881         uint32_t shdr_status, shdr_add_status;
5882         union lpfc_sli4_cfg_shdr *shdr;
5883         int rc;
5884
5885         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5886         if (!mboxq)
5887                 return -ENOMEM;
5888
5889         /* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
5890         reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5891         alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5892                         LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5893                         LPFC_SLI4_MBX_NEMBED);
5894
5895         if (alloclen < reqlen) {
5896                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5897                                 "3084 Allocated DMA memory size (%d) is "
5898                                 "less than the requested DMA memory size "
5899                                 "(%d)\n", alloclen, reqlen);
5900                 rc = -ENOMEM;
5901                 goto out_free_mboxq;
5902         }
5903         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5904         virtaddr = mboxq->sge_array->addr[0];
5905         mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5906         shdr = &mbx_cntl_attr->cfg_shdr;
5907         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5908         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5909         if (shdr_status || shdr_add_status || rc) {
5910                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5911                                 "3085 Mailbox x%x (x%x/x%x) failed, "
5912                                 "rc:x%x, status:x%x, add_status:x%x\n",
5913                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5914                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5915                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5916                                 rc, shdr_status, shdr_add_status);
5917                 rc = -ENXIO;
5918                 goto out_free_mboxq;
5919         }
5920
5921         cntl_attr = &mbx_cntl_attr->cntl_attr;
5922         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5923         phba->sli4_hba.lnk_info.lnk_tp =
5924                 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5925         phba->sli4_hba.lnk_info.lnk_no =
5926                 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5927         phba->sli4_hba.flash_id = bf_get(lpfc_cntl_attr_flash_id, cntl_attr);
5928         phba->sli4_hba.asic_rev = bf_get(lpfc_cntl_attr_asic_rev, cntl_attr);
5929
5930         memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
5931         strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
5932                 sizeof(phba->BIOSVersion));
5933
5934         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5935                         "3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s, "
5936                         "flash_id: x%02x, asic_rev: x%02x\n",
5937                         phba->sli4_hba.lnk_info.lnk_tp,
5938                         phba->sli4_hba.lnk_info.lnk_no,
5939                         phba->BIOSVersion, phba->sli4_hba.flash_id,
5940                         phba->sli4_hba.asic_rev);
5941 out_free_mboxq:
5942         if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5943                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5944         else
5945                 mempool_free(mboxq, phba->mbox_mem_pool);
5946         return rc;
5947 }
5948
5949 /**
5950  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5951  * @phba: pointer to lpfc hba data structure.
5952  *
5953  * This routine retrieves SLI4 device physical port name this PCI function
5954  * is attached to.
5955  *
5956  * Return codes
5957  *      0 - successful
5958  *      otherwise - failed to retrieve physical port name
5959  **/
5960 static int
5961 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5962 {
5963         LPFC_MBOXQ_t *mboxq;
5964         struct lpfc_mbx_get_port_name *get_port_name;
5965         uint32_t shdr_status, shdr_add_status;
5966         union lpfc_sli4_cfg_shdr *shdr;
5967         char cport_name = 0;
5968         int rc;
5969
5970         /* We assume nothing at this point */
5971         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5972         phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5973
5974         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5975         if (!mboxq)
5976                 return -ENOMEM;
5977         /* obtain link type and link number via READ_CONFIG */
5978         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5979         lpfc_sli4_read_config(phba);
5980         if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5981                 goto retrieve_ppname;
5982
5983         /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5984         rc = lpfc_sli4_get_ctl_attr(phba);
5985         if (rc)
5986                 goto out_free_mboxq;
5987
5988 retrieve_ppname:
5989         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5990                 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5991                 sizeof(struct lpfc_mbx_get_port_name) -
5992                 sizeof(struct lpfc_sli4_cfg_mhdr),
5993                 LPFC_SLI4_MBX_EMBED);
5994         get_port_name = &mboxq->u.mqe.un.get_port_name;
5995         shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5996         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5997         bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5998                 phba->sli4_hba.lnk_info.lnk_tp);
5999         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6000         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6001         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6002         if (shdr_status || shdr_add_status || rc) {
6003                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6004                                 "3087 Mailbox x%x (x%x/x%x) failed: "
6005                                 "rc:x%x, status:x%x, add_status:x%x\n",
6006                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6007                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6008                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6009                                 rc, shdr_status, shdr_add_status);
6010                 rc = -ENXIO;
6011                 goto out_free_mboxq;
6012         }
6013         switch (phba->sli4_hba.lnk_info.lnk_no) {
6014         case LPFC_LINK_NUMBER_0:
6015                 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
6016                                 &get_port_name->u.response);
6017                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6018                 break;
6019         case LPFC_LINK_NUMBER_1:
6020                 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
6021                                 &get_port_name->u.response);
6022                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6023                 break;
6024         case LPFC_LINK_NUMBER_2:
6025                 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
6026                                 &get_port_name->u.response);
6027                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6028                 break;
6029         case LPFC_LINK_NUMBER_3:
6030                 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
6031                                 &get_port_name->u.response);
6032                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6033                 break;
6034         default:
6035                 break;
6036         }
6037
6038         if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
6039                 phba->Port[0] = cport_name;
6040                 phba->Port[1] = '\0';
6041                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6042                                 "3091 SLI get port name: %s\n", phba->Port);
6043         }
6044
6045 out_free_mboxq:
6046         if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6047                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
6048         else
6049                 mempool_free(mboxq, phba->mbox_mem_pool);
6050         return rc;
6051 }
6052
6053 /**
6054  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
6055  * @phba: pointer to lpfc hba data structure.
6056  *
6057  * This routine is called to explicitly arm the SLI4 device's completion and
6058  * event queues
6059  **/
6060 static void
6061 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
6062 {
6063         int qidx;
6064         struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
6065         struct lpfc_sli4_hdw_queue *qp;
6066         struct lpfc_queue *eq;
6067
6068         sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
6069         sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
6070         if (sli4_hba->nvmels_cq)
6071                 sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
6072                                            LPFC_QUEUE_REARM);
6073
6074         if (sli4_hba->hdwq) {
6075                 /* Loop thru all Hardware Queues */
6076                 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
6077                         qp = &sli4_hba->hdwq[qidx];
6078                         /* ARM the corresponding CQ */
6079                         sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
6080                                                 LPFC_QUEUE_REARM);
6081                 }
6082
6083                 /* Loop thru all IRQ vectors */
6084                 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
6085                         eq = sli4_hba->hba_eq_hdl[qidx].eq;
6086                         /* ARM the corresponding EQ */
6087                         sli4_hba->sli4_write_eq_db(phba, eq,
6088                                                    0, LPFC_QUEUE_REARM);
6089                 }
6090         }
6091
6092         if (phba->nvmet_support) {
6093                 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
6094                         sli4_hba->sli4_write_cq_db(phba,
6095                                 sli4_hba->nvmet_cqset[qidx], 0,
6096                                 LPFC_QUEUE_REARM);
6097                 }
6098         }
6099 }
6100
6101 /**
6102  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
6103  * @phba: Pointer to HBA context object.
6104  * @type: The resource extent type.
6105  * @extnt_count: buffer to hold port available extent count.
6106  * @extnt_size: buffer to hold element count per extent.
6107  *
6108  * This function calls the port and retrievs the number of available
6109  * extents and their size for a particular extent type.
6110  *
6111  * Returns: 0 if successful.  Nonzero otherwise.
6112  **/
6113 int
6114 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
6115                                uint16_t *extnt_count, uint16_t *extnt_size)
6116 {
6117         int rc = 0;
6118         uint32_t length;
6119         uint32_t mbox_tmo;
6120         struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
6121         LPFC_MBOXQ_t *mbox;
6122
6123         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6124         if (!mbox)
6125                 return -ENOMEM;
6126
6127         /* Find out how many extents are available for this resource type */
6128         length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
6129                   sizeof(struct lpfc_sli4_cfg_mhdr));
6130         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6131                          LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
6132                          length, LPFC_SLI4_MBX_EMBED);
6133
6134         /* Send an extents count of 0 - the GET doesn't use it. */
6135         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6136                                         LPFC_SLI4_MBX_EMBED);
6137         if (unlikely(rc)) {
6138                 rc = -EIO;
6139                 goto err_exit;
6140         }
6141
6142         if (!phba->sli4_hba.intr_enable)
6143                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6144         else {
6145                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6146                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6147         }
6148         if (unlikely(rc)) {
6149                 rc = -EIO;
6150                 goto err_exit;
6151         }
6152
6153         rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
6154         if (bf_get(lpfc_mbox_hdr_status,
6155                    &rsrc_info->header.cfg_shdr.response)) {
6156                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6157                                 "2930 Failed to get resource extents "
6158                                 "Status 0x%x Add'l Status 0x%x\n",
6159                                 bf_get(lpfc_mbox_hdr_status,
6160                                        &rsrc_info->header.cfg_shdr.response),
6161                                 bf_get(lpfc_mbox_hdr_add_status,
6162                                        &rsrc_info->header.cfg_shdr.response));
6163                 rc = -EIO;
6164                 goto err_exit;
6165         }
6166
6167         *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
6168                               &rsrc_info->u.rsp);
6169         *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
6170                              &rsrc_info->u.rsp);
6171
6172         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6173                         "3162 Retrieved extents type-%d from port: count:%d, "
6174                         "size:%d\n", type, *extnt_count, *extnt_size);
6175
6176 err_exit:
6177         mempool_free(mbox, phba->mbox_mem_pool);
6178         return rc;
6179 }
6180
6181 /**
6182  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
6183  * @phba: Pointer to HBA context object.
6184  * @type: The extent type to check.
6185  *
6186  * This function reads the current available extents from the port and checks
6187  * if the extent count or extent size has changed since the last access.
6188  * Callers use this routine post port reset to understand if there is a
6189  * extent reprovisioning requirement.
6190  *
6191  * Returns:
6192  *   -Error: error indicates problem.
6193  *   1: Extent count or size has changed.
6194  *   0: No changes.
6195  **/
6196 static int
6197 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
6198 {
6199         uint16_t curr_ext_cnt, rsrc_ext_cnt;
6200         uint16_t size_diff, rsrc_ext_size;
6201         int rc = 0;
6202         struct lpfc_rsrc_blks *rsrc_entry;
6203         struct list_head *rsrc_blk_list = NULL;
6204
6205         size_diff = 0;
6206         curr_ext_cnt = 0;
6207         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6208                                             &rsrc_ext_cnt,
6209                                             &rsrc_ext_size);
6210         if (unlikely(rc))
6211                 return -EIO;
6212
6213         switch (type) {
6214         case LPFC_RSC_TYPE_FCOE_RPI:
6215                 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6216                 break;
6217         case LPFC_RSC_TYPE_FCOE_VPI:
6218                 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
6219                 break;
6220         case LPFC_RSC_TYPE_FCOE_XRI:
6221                 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6222                 break;
6223         case LPFC_RSC_TYPE_FCOE_VFI:
6224                 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6225                 break;
6226         default:
6227                 break;
6228         }
6229
6230         list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
6231                 curr_ext_cnt++;
6232                 if (rsrc_entry->rsrc_size != rsrc_ext_size)
6233                         size_diff++;
6234         }
6235
6236         if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
6237                 rc = 1;
6238
6239         return rc;
6240 }
6241
6242 /**
6243  * lpfc_sli4_cfg_post_extnts -
6244  * @phba: Pointer to HBA context object.
6245  * @extnt_cnt: number of available extents.
6246  * @type: the extent type (rpi, xri, vfi, vpi).
6247  * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.
6248  * @mbox: pointer to the caller's allocated mailbox structure.
6249  *
6250  * This function executes the extents allocation request.  It also
6251  * takes care of the amount of memory needed to allocate or get the
6252  * allocated extents. It is the caller's responsibility to evaluate
6253  * the response.
6254  *
6255  * Returns:
6256  *   -Error:  Error value describes the condition found.
6257  *   0: if successful
6258  **/
6259 static int
6260 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6261                           uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
6262 {
6263         int rc = 0;
6264         uint32_t req_len;
6265         uint32_t emb_len;
6266         uint32_t alloc_len, mbox_tmo;
6267
6268         /* Calculate the total requested length of the dma memory */
6269         req_len = extnt_cnt * sizeof(uint16_t);
6270
6271         /*
6272          * Calculate the size of an embedded mailbox.  The uint32_t
6273          * accounts for extents-specific word.
6274          */
6275         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6276                 sizeof(uint32_t);
6277
6278         /*
6279          * Presume the allocation and response will fit into an embedded
6280          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6281          */
6282         *emb = LPFC_SLI4_MBX_EMBED;
6283         if (req_len > emb_len) {
6284                 req_len = extnt_cnt * sizeof(uint16_t) +
6285                         sizeof(union lpfc_sli4_cfg_shdr) +
6286                         sizeof(uint32_t);
6287                 *emb = LPFC_SLI4_MBX_NEMBED;
6288         }
6289
6290         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6291                                      LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
6292                                      req_len, *emb);
6293         if (alloc_len < req_len) {
6294                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6295                         "2982 Allocated DMA memory size (x%x) is "
6296                         "less than the requested DMA memory "
6297                         "size (x%x)\n", alloc_len, req_len);
6298                 return -ENOMEM;
6299         }
6300         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6301         if (unlikely(rc))
6302                 return -EIO;
6303
6304         if (!phba->sli4_hba.intr_enable)
6305                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6306         else {
6307                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6308                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6309         }
6310
6311         if (unlikely(rc))
6312                 rc = -EIO;
6313         return rc;
6314 }
6315
6316 /**
6317  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
6318  * @phba: Pointer to HBA context object.
6319  * @type:  The resource extent type to allocate.
6320  *
6321  * This function allocates the number of elements for the specified
6322  * resource type.
6323  **/
6324 static int
6325 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
6326 {
6327         bool emb = false;
6328         uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
6329         uint16_t rsrc_id, rsrc_start, j, k;
6330         uint16_t *ids;
6331         int i, rc;
6332         unsigned long longs;
6333         unsigned long *bmask;
6334         struct lpfc_rsrc_blks *rsrc_blks;
6335         LPFC_MBOXQ_t *mbox;
6336         uint32_t length;
6337         struct lpfc_id_range *id_array = NULL;
6338         void *virtaddr = NULL;
6339         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6340         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6341         struct list_head *ext_blk_list;
6342
6343         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6344                                             &rsrc_cnt,
6345                                             &rsrc_size);
6346         if (unlikely(rc))
6347                 return -EIO;
6348
6349         if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
6350                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6351                         "3009 No available Resource Extents "
6352                         "for resource type 0x%x: Count: 0x%x, "
6353                         "Size 0x%x\n", type, rsrc_cnt,
6354                         rsrc_size);
6355                 return -ENOMEM;
6356         }
6357
6358         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
6359                         "2903 Post resource extents type-0x%x: "
6360                         "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6361
6362         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6363         if (!mbox)
6364                 return -ENOMEM;
6365
6366         rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6367         if (unlikely(rc)) {
6368                 rc = -EIO;
6369                 goto err_exit;
6370         }
6371
6372         /*
6373          * Figure out where the response is located.  Then get local pointers
6374          * to the response data.  The port does not guarantee to respond to
6375          * all extents counts request so update the local variable with the
6376          * allocated count from the port.
6377          */
6378         if (emb == LPFC_SLI4_MBX_EMBED) {
6379                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6380                 id_array = &rsrc_ext->u.rsp.id[0];
6381                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6382         } else {
6383                 virtaddr = mbox->sge_array->addr[0];
6384                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6385                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6386                 id_array = &n_rsrc->id;
6387         }
6388
6389         longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6390         rsrc_id_cnt = rsrc_cnt * rsrc_size;
6391
6392         /*
6393          * Based on the resource size and count, correct the base and max
6394          * resource values.
6395          */
6396         length = sizeof(struct lpfc_rsrc_blks);
6397         switch (type) {
6398         case LPFC_RSC_TYPE_FCOE_RPI:
6399                 phba->sli4_hba.rpi_bmask = kcalloc(longs,
6400                                                    sizeof(unsigned long),
6401                                                    GFP_KERNEL);
6402                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6403                         rc = -ENOMEM;
6404                         goto err_exit;
6405                 }
6406                 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
6407                                                  sizeof(uint16_t),
6408                                                  GFP_KERNEL);
6409                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6410                         kfree(phba->sli4_hba.rpi_bmask);
6411                         rc = -ENOMEM;
6412                         goto err_exit;
6413                 }
6414
6415                 /*
6416                  * The next_rpi was initialized with the maximum available
6417                  * count but the port may allocate a smaller number.  Catch
6418                  * that case and update the next_rpi.
6419                  */
6420                 phba->sli4_hba.next_rpi = rsrc_id_cnt;
6421
6422                 /* Initialize local ptrs for common extent processing later. */
6423                 bmask = phba->sli4_hba.rpi_bmask;
6424                 ids = phba->sli4_hba.rpi_ids;
6425                 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6426                 break;
6427         case LPFC_RSC_TYPE_FCOE_VPI:
6428                 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6429                                           GFP_KERNEL);
6430                 if (unlikely(!phba->vpi_bmask)) {
6431                         rc = -ENOMEM;
6432                         goto err_exit;
6433                 }
6434                 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
6435                                          GFP_KERNEL);
6436                 if (unlikely(!phba->vpi_ids)) {
6437                         kfree(phba->vpi_bmask);
6438                         rc = -ENOMEM;
6439                         goto err_exit;
6440                 }
6441
6442                 /* Initialize local ptrs for common extent processing later. */
6443                 bmask = phba->vpi_bmask;
6444                 ids = phba->vpi_ids;
6445                 ext_blk_list = &phba->lpfc_vpi_blk_list;
6446                 break;
6447         case LPFC_RSC_TYPE_FCOE_XRI:
6448                 phba->sli4_hba.xri_bmask = kcalloc(longs,
6449                                                    sizeof(unsigned long),
6450                                                    GFP_KERNEL);
6451                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6452                         rc = -ENOMEM;
6453                         goto err_exit;
6454                 }
6455                 phba->sli4_hba.max_cfg_param.xri_used = 0;
6456                 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
6457                                                  sizeof(uint16_t),
6458                                                  GFP_KERNEL);
6459                 if (unlikely(!phba->sli4_hba.xri_ids)) {
6460                         kfree(phba->sli4_hba.xri_bmask);
6461                         rc = -ENOMEM;
6462                         goto err_exit;
6463                 }
6464
6465                 /* Initialize local ptrs for common extent processing later. */
6466                 bmask = phba->sli4_hba.xri_bmask;
6467                 ids = phba->sli4_hba.xri_ids;
6468                 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6469                 break;
6470         case LPFC_RSC_TYPE_FCOE_VFI:
6471                 phba->sli4_hba.vfi_bmask = kcalloc(longs,
6472                                                    sizeof(unsigned long),
6473                                                    GFP_KERNEL);
6474                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6475                         rc = -ENOMEM;
6476                         goto err_exit;
6477                 }
6478                 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
6479                                                  sizeof(uint16_t),
6480                                                  GFP_KERNEL);
6481                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6482                         kfree(phba->sli4_hba.vfi_bmask);
6483                         rc = -ENOMEM;
6484                         goto err_exit;
6485                 }
6486
6487                 /* Initialize local ptrs for common extent processing later. */
6488                 bmask = phba->sli4_hba.vfi_bmask;
6489                 ids = phba->sli4_hba.vfi_ids;
6490                 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6491                 break;
6492         default:
6493                 /* Unsupported Opcode.  Fail call. */
6494                 id_array = NULL;
6495                 bmask = NULL;
6496                 ids = NULL;
6497                 ext_blk_list = NULL;
6498                 goto err_exit;
6499         }
6500
6501         /*
6502          * Complete initializing the extent configuration with the
6503          * allocated ids assigned to this function.  The bitmask serves
6504          * as an index into the array and manages the available ids.  The
6505          * array just stores the ids communicated to the port via the wqes.
6506          */
6507         for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6508                 if ((i % 2) == 0)
6509                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6510                                          &id_array[k]);
6511                 else
6512                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6513                                          &id_array[k]);
6514
6515                 rsrc_blks = kzalloc(length, GFP_KERNEL);
6516                 if (unlikely(!rsrc_blks)) {
6517                         rc = -ENOMEM;
6518                         kfree(bmask);
6519                         kfree(ids);
6520                         goto err_exit;
6521                 }
6522                 rsrc_blks->rsrc_start = rsrc_id;
6523                 rsrc_blks->rsrc_size = rsrc_size;
6524                 list_add_tail(&rsrc_blks->list, ext_blk_list);
6525                 rsrc_start = rsrc_id;
6526                 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6527                         phba->sli4_hba.io_xri_start = rsrc_start +
6528                                 lpfc_sli4_get_iocb_cnt(phba);
6529                 }
6530
6531                 while (rsrc_id < (rsrc_start + rsrc_size)) {
6532                         ids[j] = rsrc_id;
6533                         rsrc_id++;
6534                         j++;
6535                 }
6536                 /* Entire word processed.  Get next word.*/
6537                 if ((i % 2) == 1)
6538                         k++;
6539         }
6540  err_exit:
6541         lpfc_sli4_mbox_cmd_free(phba, mbox);
6542         return rc;
6543 }
6544
6545
6546
6547 /**
6548  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6549  * @phba: Pointer to HBA context object.
6550  * @type: the extent's type.
6551  *
6552  * This function deallocates all extents of a particular resource type.
6553  * SLI4 does not allow for deallocating a particular extent range.  It
6554  * is the caller's responsibility to release all kernel memory resources.
6555  **/
6556 static int
6557 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6558 {
6559         int rc;
6560         uint32_t length, mbox_tmo = 0;
6561         LPFC_MBOXQ_t *mbox;
6562         struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6563         struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6564
6565         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6566         if (!mbox)
6567                 return -ENOMEM;
6568
6569         /*
6570          * This function sends an embedded mailbox because it only sends the
6571          * the resource type.  All extents of this type are released by the
6572          * port.
6573          */
6574         length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6575                   sizeof(struct lpfc_sli4_cfg_mhdr));
6576         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6577                          LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6578                          length, LPFC_SLI4_MBX_EMBED);
6579
6580         /* Send an extents count of 0 - the dealloc doesn't use it. */
6581         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6582                                         LPFC_SLI4_MBX_EMBED);
6583         if (unlikely(rc)) {
6584                 rc = -EIO;
6585                 goto out_free_mbox;
6586         }
6587         if (!phba->sli4_hba.intr_enable)
6588                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6589         else {
6590                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6591                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6592         }
6593         if (unlikely(rc)) {
6594                 rc = -EIO;
6595                 goto out_free_mbox;
6596         }
6597
6598         dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6599         if (bf_get(lpfc_mbox_hdr_status,
6600                    &dealloc_rsrc->header.cfg_shdr.response)) {
6601                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6602                                 "2919 Failed to release resource extents "
6603                                 "for type %d - Status 0x%x Add'l Status 0x%x. "
6604                                 "Resource memory not released.\n",
6605                                 type,
6606                                 bf_get(lpfc_mbox_hdr_status,
6607                                     &dealloc_rsrc->header.cfg_shdr.response),
6608                                 bf_get(lpfc_mbox_hdr_add_status,
6609                                     &dealloc_rsrc->header.cfg_shdr.response));
6610                 rc = -EIO;
6611                 goto out_free_mbox;
6612         }
6613
6614         /* Release kernel memory resources for the specific type. */
6615         switch (type) {
6616         case LPFC_RSC_TYPE_FCOE_VPI:
6617                 kfree(phba->vpi_bmask);
6618                 kfree(phba->vpi_ids);
6619                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6620                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6621                                     &phba->lpfc_vpi_blk_list, list) {
6622                         list_del_init(&rsrc_blk->list);
6623                         kfree(rsrc_blk);
6624                 }
6625                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6626                 break;
6627         case LPFC_RSC_TYPE_FCOE_XRI:
6628                 kfree(phba->sli4_hba.xri_bmask);
6629                 kfree(phba->sli4_hba.xri_ids);
6630                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6631                                     &phba->sli4_hba.lpfc_xri_blk_list, list) {
6632                         list_del_init(&rsrc_blk->list);
6633                         kfree(rsrc_blk);
6634                 }
6635                 break;
6636         case LPFC_RSC_TYPE_FCOE_VFI:
6637                 kfree(phba->sli4_hba.vfi_bmask);
6638                 kfree(phba->sli4_hba.vfi_ids);
6639                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6640                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6641                                     &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6642                         list_del_init(&rsrc_blk->list);
6643                         kfree(rsrc_blk);
6644                 }
6645                 break;
6646         case LPFC_RSC_TYPE_FCOE_RPI:
6647                 /* RPI bitmask and physical id array are cleaned up earlier. */
6648                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6649                                     &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6650                         list_del_init(&rsrc_blk->list);
6651                         kfree(rsrc_blk);
6652                 }
6653                 break;
6654         default:
6655                 break;
6656         }
6657
6658         bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6659
6660  out_free_mbox:
6661         mempool_free(mbox, phba->mbox_mem_pool);
6662         return rc;
6663 }
6664
6665 static void
6666 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6667                   uint32_t feature)
6668 {
6669         uint32_t len;
6670         u32 sig_freq = 0;
6671
6672         len = sizeof(struct lpfc_mbx_set_feature) -
6673                 sizeof(struct lpfc_sli4_cfg_mhdr);
6674         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6675                          LPFC_MBOX_OPCODE_SET_FEATURES, len,
6676                          LPFC_SLI4_MBX_EMBED);
6677
6678         switch (feature) {
6679         case LPFC_SET_UE_RECOVERY:
6680                 bf_set(lpfc_mbx_set_feature_UER,
6681                        &mbox->u.mqe.un.set_feature, 1);
6682                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6683                 mbox->u.mqe.un.set_feature.param_len = 8;
6684                 break;
6685         case LPFC_SET_MDS_DIAGS:
6686                 bf_set(lpfc_mbx_set_feature_mds,
6687                        &mbox->u.mqe.un.set_feature, 1);
6688                 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
6689                        &mbox->u.mqe.un.set_feature, 1);
6690                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6691                 mbox->u.mqe.un.set_feature.param_len = 8;
6692                 break;
6693         case LPFC_SET_CGN_SIGNAL:
6694                 if (phba->cmf_active_mode == LPFC_CFG_OFF)
6695                         sig_freq = 0;
6696                 else
6697                         sig_freq = phba->cgn_sig_freq;
6698
6699                 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
6700                         bf_set(lpfc_mbx_set_feature_CGN_alarm_freq,
6701                                &mbox->u.mqe.un.set_feature, sig_freq);
6702                         bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6703                                &mbox->u.mqe.un.set_feature, sig_freq);
6704                 }
6705
6706                 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY)
6707                         bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6708                                &mbox->u.mqe.un.set_feature, sig_freq);
6709
6710                 if (phba->cmf_active_mode == LPFC_CFG_OFF ||
6711                     phba->cgn_reg_signal == EDC_CG_SIG_NOTSUPPORTED)
6712                         sig_freq = 0;
6713                 else
6714                         sig_freq = lpfc_acqe_cgn_frequency;
6715
6716                 bf_set(lpfc_mbx_set_feature_CGN_acqe_freq,
6717                        &mbox->u.mqe.un.set_feature, sig_freq);
6718
6719                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_CGN_SIGNAL;
6720                 mbox->u.mqe.un.set_feature.param_len = 12;
6721                 break;
6722         case LPFC_SET_DUAL_DUMP:
6723                 bf_set(lpfc_mbx_set_feature_dd,
6724                        &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6725                 bf_set(lpfc_mbx_set_feature_ddquery,
6726                        &mbox->u.mqe.un.set_feature, 0);
6727                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6728                 mbox->u.mqe.un.set_feature.param_len = 4;
6729                 break;
6730         case LPFC_SET_ENABLE_MI:
6731                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_MI;
6732                 mbox->u.mqe.un.set_feature.param_len = 4;
6733                 bf_set(lpfc_mbx_set_feature_milunq, &mbox->u.mqe.un.set_feature,
6734                        phba->pport->cfg_lun_queue_depth);
6735                 bf_set(lpfc_mbx_set_feature_mi, &mbox->u.mqe.un.set_feature,
6736                        phba->sli4_hba.pc_sli4_params.mi_ver);
6737                 break;
6738         case LPFC_SET_ENABLE_CMF:
6739                 bf_set(lpfc_mbx_set_feature_dd, &mbox->u.mqe.un.set_feature, 1);
6740                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_CMF;
6741                 mbox->u.mqe.un.set_feature.param_len = 4;
6742                 bf_set(lpfc_mbx_set_feature_cmf,
6743                        &mbox->u.mqe.un.set_feature, 1);
6744                 break;
6745         }
6746         return;
6747 }
6748
6749 /**
6750  * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6751  * @phba: Pointer to HBA context object.
6752  *
6753  * Disable FW logging into host memory on the adapter. To
6754  * be done before reading logs from the host memory.
6755  **/
6756 void
6757 lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6758 {
6759         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6760
6761         spin_lock_irq(&phba->hbalock);
6762         ras_fwlog->state = INACTIVE;
6763         spin_unlock_irq(&phba->hbalock);
6764
6765         /* Disable FW logging to host memory */
6766         writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6767                phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6768
6769         /* Wait 10ms for firmware to stop using DMA buffer */
6770         usleep_range(10 * 1000, 20 * 1000);
6771 }
6772
6773 /**
6774  * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6775  * @phba: Pointer to HBA context object.
6776  *
6777  * This function is called to free memory allocated for RAS FW logging
6778  * support in the driver.
6779  **/
6780 void
6781 lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6782 {
6783         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6784         struct lpfc_dmabuf *dmabuf, *next;
6785
6786         if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6787                 list_for_each_entry_safe(dmabuf, next,
6788                                     &ras_fwlog->fwlog_buff_list,
6789                                     list) {
6790                         list_del(&dmabuf->list);
6791                         dma_free_coherent(&phba->pcidev->dev,
6792                                           LPFC_RAS_MAX_ENTRY_SIZE,
6793                                           dmabuf->virt, dmabuf->phys);
6794                         kfree(dmabuf);
6795                 }
6796         }
6797
6798         if (ras_fwlog->lwpd.virt) {
6799                 dma_free_coherent(&phba->pcidev->dev,
6800                                   sizeof(uint32_t) * 2,
6801                                   ras_fwlog->lwpd.virt,
6802                                   ras_fwlog->lwpd.phys);
6803                 ras_fwlog->lwpd.virt = NULL;
6804         }
6805
6806         spin_lock_irq(&phba->hbalock);
6807         ras_fwlog->state = INACTIVE;
6808         spin_unlock_irq(&phba->hbalock);
6809 }
6810
6811 /**
6812  * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6813  * @phba: Pointer to HBA context object.
6814  * @fwlog_buff_count: Count of buffers to be created.
6815  *
6816  * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6817  * to update FW log is posted to the adapter.
6818  * Buffer count is calculated based on module param ras_fwlog_buffsize
6819  * Size of each buffer posted to FW is 64K.
6820  **/
6821
6822 static int
6823 lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6824                         uint32_t fwlog_buff_count)
6825 {
6826         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6827         struct lpfc_dmabuf *dmabuf;
6828         int rc = 0, i = 0;
6829
6830         /* Initialize List */
6831         INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6832
6833         /* Allocate memory for the LWPD */
6834         ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6835                                             sizeof(uint32_t) * 2,
6836                                             &ras_fwlog->lwpd.phys,
6837                                             GFP_KERNEL);
6838         if (!ras_fwlog->lwpd.virt) {
6839                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6840                                 "6185 LWPD Memory Alloc Failed\n");
6841
6842                 return -ENOMEM;
6843         }
6844
6845         ras_fwlog->fw_buffcount = fwlog_buff_count;
6846         for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6847                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6848                                  GFP_KERNEL);
6849                 if (!dmabuf) {
6850                         rc = -ENOMEM;
6851                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6852                                         "6186 Memory Alloc failed FW logging");
6853                         goto free_mem;
6854                 }
6855
6856                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6857                                                   LPFC_RAS_MAX_ENTRY_SIZE,
6858                                                   &dmabuf->phys, GFP_KERNEL);
6859                 if (!dmabuf->virt) {
6860                         kfree(dmabuf);
6861                         rc = -ENOMEM;
6862                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6863                                         "6187 DMA Alloc Failed FW logging");
6864                         goto free_mem;
6865                 }
6866                 dmabuf->buffer_tag = i;
6867                 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6868         }
6869
6870 free_mem:
6871         if (rc)
6872                 lpfc_sli4_ras_dma_free(phba);
6873
6874         return rc;
6875 }
6876
6877 /**
6878  * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6879  * @phba: pointer to lpfc hba data structure.
6880  * @pmb: pointer to the driver internal queue element for mailbox command.
6881  *
6882  * Completion handler for driver's RAS MBX command to the device.
6883  **/
6884 static void
6885 lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6886 {
6887         MAILBOX_t *mb;
6888         union lpfc_sli4_cfg_shdr *shdr;
6889         uint32_t shdr_status, shdr_add_status;
6890         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6891
6892         mb = &pmb->u.mb;
6893
6894         shdr = (union lpfc_sli4_cfg_shdr *)
6895                 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6896         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6897         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6898
6899         if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
6900                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6901                                 "6188 FW LOG mailbox "
6902                                 "completed with status x%x add_status x%x,"
6903                                 " mbx status x%x\n",
6904                                 shdr_status, shdr_add_status, mb->mbxStatus);
6905
6906                 ras_fwlog->ras_hwsupport = false;
6907                 goto disable_ras;
6908         }
6909
6910         spin_lock_irq(&phba->hbalock);
6911         ras_fwlog->state = ACTIVE;
6912         spin_unlock_irq(&phba->hbalock);
6913         mempool_free(pmb, phba->mbox_mem_pool);
6914
6915         return;
6916
6917 disable_ras:
6918         /* Free RAS DMA memory */
6919         lpfc_sli4_ras_dma_free(phba);
6920         mempool_free(pmb, phba->mbox_mem_pool);
6921 }
6922
6923 /**
6924  * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6925  * @phba: pointer to lpfc hba data structure.
6926  * @fwlog_level: Logging verbosity level.
6927  * @fwlog_enable: Enable/Disable logging.
6928  *
6929  * Initialize memory and post mailbox command to enable FW logging in host
6930  * memory.
6931  **/
6932 int
6933 lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6934                          uint32_t fwlog_level,
6935                          uint32_t fwlog_enable)
6936 {
6937         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6938         struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6939         struct lpfc_dmabuf *dmabuf;
6940         LPFC_MBOXQ_t *mbox;
6941         uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6942         int rc = 0;
6943
6944         spin_lock_irq(&phba->hbalock);
6945         ras_fwlog->state = INACTIVE;
6946         spin_unlock_irq(&phba->hbalock);
6947
6948         fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6949                           phba->cfg_ras_fwlog_buffsize);
6950         fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6951
6952         /*
6953          * If re-enabling FW logging support use earlier allocated
6954          * DMA buffers while posting MBX command.
6955          **/
6956         if (!ras_fwlog->lwpd.virt) {
6957                 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6958                 if (rc) {
6959                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6960                                         "6189 FW Log Memory Allocation Failed");
6961                         return rc;
6962                 }
6963         }
6964
6965         /* Setup Mailbox command */
6966         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6967         if (!mbox) {
6968                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6969                                 "6190 RAS MBX Alloc Failed");
6970                 rc = -ENOMEM;
6971                 goto mem_free;
6972         }
6973
6974         ras_fwlog->fw_loglevel = fwlog_level;
6975         len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6976                 sizeof(struct lpfc_sli4_cfg_mhdr));
6977
6978         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6979                          LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6980                          len, LPFC_SLI4_MBX_EMBED);
6981
6982         mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6983         bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6984                fwlog_enable);
6985         bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6986                ras_fwlog->fw_loglevel);
6987         bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6988                ras_fwlog->fw_buffcount);
6989         bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6990                LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6991
6992         /* Update DMA buffer address */
6993         list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6994                 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6995
6996                 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6997                         putPaddrLow(dmabuf->phys);
6998
6999                 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
7000                         putPaddrHigh(dmabuf->phys);
7001         }
7002
7003         /* Update LPWD address */
7004         mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
7005         mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
7006
7007         spin_lock_irq(&phba->hbalock);
7008         ras_fwlog->state = REG_INPROGRESS;
7009         spin_unlock_irq(&phba->hbalock);
7010         mbox->vport = phba->pport;
7011         mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
7012
7013         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7014
7015         if (rc == MBX_NOT_FINISHED) {
7016                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7017                                 "6191 FW-Log Mailbox failed. "
7018                                 "status %d mbxStatus : x%x", rc,
7019                                 bf_get(lpfc_mqe_status, &mbox->u.mqe));
7020                 mempool_free(mbox, phba->mbox_mem_pool);
7021                 rc = -EIO;
7022                 goto mem_free;
7023         } else
7024                 rc = 0;
7025 mem_free:
7026         if (rc)
7027                 lpfc_sli4_ras_dma_free(phba);
7028
7029         return rc;
7030 }
7031
7032 /**
7033  * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
7034  * @phba: Pointer to HBA context object.
7035  *
7036  * Check if RAS is supported on the adapter and initialize it.
7037  **/
7038 void
7039 lpfc_sli4_ras_setup(struct lpfc_hba *phba)
7040 {
7041         /* Check RAS FW Log needs to be enabled or not */
7042         if (lpfc_check_fwlog_support(phba))
7043                 return;
7044
7045         lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
7046                                  LPFC_RAS_ENABLE_LOGGING);
7047 }
7048
7049 /**
7050  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
7051  * @phba: Pointer to HBA context object.
7052  *
7053  * This function allocates all SLI4 resource identifiers.
7054  **/
7055 int
7056 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
7057 {
7058         int i, rc, error = 0;
7059         uint16_t count, base;
7060         unsigned long longs;
7061
7062         if (!phba->sli4_hba.rpi_hdrs_in_use)
7063                 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
7064         if (phba->sli4_hba.extents_in_use) {
7065                 /*
7066                  * The port supports resource extents. The XRI, VPI, VFI, RPI
7067                  * resource extent count must be read and allocated before
7068                  * provisioning the resource id arrays.
7069                  */
7070                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7071                     LPFC_IDX_RSRC_RDY) {
7072                         /*
7073                          * Extent-based resources are set - the driver could
7074                          * be in a port reset. Figure out if any corrective
7075                          * actions need to be taken.
7076                          */
7077                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7078                                                  LPFC_RSC_TYPE_FCOE_VFI);
7079                         if (rc != 0)
7080                                 error++;
7081                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7082                                                  LPFC_RSC_TYPE_FCOE_VPI);
7083                         if (rc != 0)
7084                                 error++;
7085                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7086                                                  LPFC_RSC_TYPE_FCOE_XRI);
7087                         if (rc != 0)
7088                                 error++;
7089                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7090                                                  LPFC_RSC_TYPE_FCOE_RPI);
7091                         if (rc != 0)
7092                                 error++;
7093
7094                         /*
7095                          * It's possible that the number of resources
7096                          * provided to this port instance changed between
7097                          * resets.  Detect this condition and reallocate
7098                          * resources.  Otherwise, there is no action.
7099                          */
7100                         if (error) {
7101                                 lpfc_printf_log(phba, KERN_INFO,
7102                                                 LOG_MBOX | LOG_INIT,
7103                                                 "2931 Detected extent resource "
7104                                                 "change.  Reallocating all "
7105                                                 "extents.\n");
7106                                 rc = lpfc_sli4_dealloc_extent(phba,
7107                                                  LPFC_RSC_TYPE_FCOE_VFI);
7108                                 rc = lpfc_sli4_dealloc_extent(phba,
7109                                                  LPFC_RSC_TYPE_FCOE_VPI);
7110                                 rc = lpfc_sli4_dealloc_extent(phba,
7111                                                  LPFC_RSC_TYPE_FCOE_XRI);
7112                                 rc = lpfc_sli4_dealloc_extent(phba,
7113                                                  LPFC_RSC_TYPE_FCOE_RPI);
7114                         } else
7115                                 return 0;
7116                 }
7117
7118                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7119                 if (unlikely(rc))
7120                         goto err_exit;
7121
7122                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7123                 if (unlikely(rc))
7124                         goto err_exit;
7125
7126                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7127                 if (unlikely(rc))
7128                         goto err_exit;
7129
7130                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7131                 if (unlikely(rc))
7132                         goto err_exit;
7133                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7134                        LPFC_IDX_RSRC_RDY);
7135                 return rc;
7136         } else {
7137                 /*
7138                  * The port does not support resource extents.  The XRI, VPI,
7139                  * VFI, RPI resource ids were determined from READ_CONFIG.
7140                  * Just allocate the bitmasks and provision the resource id
7141                  * arrays.  If a port reset is active, the resources don't
7142                  * need any action - just exit.
7143                  */
7144                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7145                     LPFC_IDX_RSRC_RDY) {
7146                         lpfc_sli4_dealloc_resource_identifiers(phba);
7147                         lpfc_sli4_remove_rpis(phba);
7148                 }
7149                 /* RPIs. */
7150                 count = phba->sli4_hba.max_cfg_param.max_rpi;
7151                 if (count <= 0) {
7152                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7153                                         "3279 Invalid provisioning of "
7154                                         "rpi:%d\n", count);
7155                         rc = -EINVAL;
7156                         goto err_exit;
7157                 }
7158                 base = phba->sli4_hba.max_cfg_param.rpi_base;
7159                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7160                 phba->sli4_hba.rpi_bmask = kcalloc(longs,
7161                                                    sizeof(unsigned long),
7162                                                    GFP_KERNEL);
7163                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
7164                         rc = -ENOMEM;
7165                         goto err_exit;
7166                 }
7167                 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
7168                                                  GFP_KERNEL);
7169                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
7170                         rc = -ENOMEM;
7171                         goto free_rpi_bmask;
7172                 }
7173
7174                 for (i = 0; i < count; i++)
7175                         phba->sli4_hba.rpi_ids[i] = base + i;
7176
7177                 /* VPIs. */
7178                 count = phba->sli4_hba.max_cfg_param.max_vpi;
7179                 if (count <= 0) {
7180                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7181                                         "3280 Invalid provisioning of "
7182                                         "vpi:%d\n", count);
7183                         rc = -EINVAL;
7184                         goto free_rpi_ids;
7185                 }
7186                 base = phba->sli4_hba.max_cfg_param.vpi_base;
7187                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7188                 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
7189                                           GFP_KERNEL);
7190                 if (unlikely(!phba->vpi_bmask)) {
7191                         rc = -ENOMEM;
7192                         goto free_rpi_ids;
7193                 }
7194                 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
7195                                         GFP_KERNEL);
7196                 if (unlikely(!phba->vpi_ids)) {
7197                         rc = -ENOMEM;
7198                         goto free_vpi_bmask;
7199                 }
7200
7201                 for (i = 0; i < count; i++)
7202                         phba->vpi_ids[i] = base + i;
7203
7204                 /* XRIs. */
7205                 count = phba->sli4_hba.max_cfg_param.max_xri;
7206                 if (count <= 0) {
7207                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7208                                         "3281 Invalid provisioning of "
7209                                         "xri:%d\n", count);
7210                         rc = -EINVAL;
7211                         goto free_vpi_ids;
7212                 }
7213                 base = phba->sli4_hba.max_cfg_param.xri_base;
7214                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7215                 phba->sli4_hba.xri_bmask = kcalloc(longs,
7216                                                    sizeof(unsigned long),
7217                                                    GFP_KERNEL);
7218                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
7219                         rc = -ENOMEM;
7220                         goto free_vpi_ids;
7221                 }
7222                 phba->sli4_hba.max_cfg_param.xri_used = 0;
7223                 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
7224                                                  GFP_KERNEL);
7225                 if (unlikely(!phba->sli4_hba.xri_ids)) {
7226                         rc = -ENOMEM;
7227                         goto free_xri_bmask;
7228                 }
7229
7230                 for (i = 0; i < count; i++)
7231                         phba->sli4_hba.xri_ids[i] = base + i;
7232
7233                 /* VFIs. */
7234                 count = phba->sli4_hba.max_cfg_param.max_vfi;
7235                 if (count <= 0) {
7236                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7237                                         "3282 Invalid provisioning of "
7238                                         "vfi:%d\n", count);
7239                         rc = -EINVAL;
7240                         goto free_xri_ids;
7241                 }
7242                 base = phba->sli4_hba.max_cfg_param.vfi_base;
7243                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
7244                 phba->sli4_hba.vfi_bmask = kcalloc(longs,
7245                                                    sizeof(unsigned long),
7246                                                    GFP_KERNEL);
7247                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
7248                         rc = -ENOMEM;
7249                         goto free_xri_ids;
7250                 }
7251                 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
7252                                                  GFP_KERNEL);
7253                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
7254                         rc = -ENOMEM;
7255                         goto free_vfi_bmask;
7256                 }
7257
7258                 for (i = 0; i < count; i++)
7259                         phba->sli4_hba.vfi_ids[i] = base + i;
7260
7261                 /*
7262                  * Mark all resources ready.  An HBA reset doesn't need
7263                  * to reset the initialization.
7264                  */
7265                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7266                        LPFC_IDX_RSRC_RDY);
7267                 return 0;
7268         }
7269
7270  free_vfi_bmask:
7271         kfree(phba->sli4_hba.vfi_bmask);
7272         phba->sli4_hba.vfi_bmask = NULL;
7273  free_xri_ids:
7274         kfree(phba->sli4_hba.xri_ids);
7275         phba->sli4_hba.xri_ids = NULL;
7276  free_xri_bmask:
7277         kfree(phba->sli4_hba.xri_bmask);
7278         phba->sli4_hba.xri_bmask = NULL;
7279  free_vpi_ids:
7280         kfree(phba->vpi_ids);
7281         phba->vpi_ids = NULL;
7282  free_vpi_bmask:
7283         kfree(phba->vpi_bmask);
7284         phba->vpi_bmask = NULL;
7285  free_rpi_ids:
7286         kfree(phba->sli4_hba.rpi_ids);
7287         phba->sli4_hba.rpi_ids = NULL;
7288  free_rpi_bmask:
7289         kfree(phba->sli4_hba.rpi_bmask);
7290         phba->sli4_hba.rpi_bmask = NULL;
7291  err_exit:
7292         return rc;
7293 }
7294
7295 /**
7296  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
7297  * @phba: Pointer to HBA context object.
7298  *
7299  * This function allocates the number of elements for the specified
7300  * resource type.
7301  **/
7302 int
7303 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
7304 {
7305         if (phba->sli4_hba.extents_in_use) {
7306                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7307                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7308                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7309                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7310         } else {
7311                 kfree(phba->vpi_bmask);
7312                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
7313                 kfree(phba->vpi_ids);
7314                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7315                 kfree(phba->sli4_hba.xri_bmask);
7316                 kfree(phba->sli4_hba.xri_ids);
7317                 kfree(phba->sli4_hba.vfi_bmask);
7318                 kfree(phba->sli4_hba.vfi_ids);
7319                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7320                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7321         }
7322
7323         return 0;
7324 }
7325
7326 /**
7327  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
7328  * @phba: Pointer to HBA context object.
7329  * @type: The resource extent type.
7330  * @extnt_cnt: buffer to hold port extent count response
7331  * @extnt_size: buffer to hold port extent size response.
7332  *
7333  * This function calls the port to read the host allocated extents
7334  * for a particular type.
7335  **/
7336 int
7337 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
7338                                uint16_t *extnt_cnt, uint16_t *extnt_size)
7339 {
7340         bool emb;
7341         int rc = 0;
7342         uint16_t curr_blks = 0;
7343         uint32_t req_len, emb_len;
7344         uint32_t alloc_len, mbox_tmo;
7345         struct list_head *blk_list_head;
7346         struct lpfc_rsrc_blks *rsrc_blk;
7347         LPFC_MBOXQ_t *mbox;
7348         void *virtaddr = NULL;
7349         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
7350         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
7351         union  lpfc_sli4_cfg_shdr *shdr;
7352
7353         switch (type) {
7354         case LPFC_RSC_TYPE_FCOE_VPI:
7355                 blk_list_head = &phba->lpfc_vpi_blk_list;
7356                 break;
7357         case LPFC_RSC_TYPE_FCOE_XRI:
7358                 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
7359                 break;
7360         case LPFC_RSC_TYPE_FCOE_VFI:
7361                 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
7362                 break;
7363         case LPFC_RSC_TYPE_FCOE_RPI:
7364                 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
7365                 break;
7366         default:
7367                 return -EIO;
7368         }
7369
7370         /* Count the number of extents currently allocatd for this type. */
7371         list_for_each_entry(rsrc_blk, blk_list_head, list) {
7372                 if (curr_blks == 0) {
7373                         /*
7374                          * The GET_ALLOCATED mailbox does not return the size,
7375                          * just the count.  The size should be just the size
7376                          * stored in the current allocated block and all sizes
7377                          * for an extent type are the same so set the return
7378                          * value now.
7379                          */
7380                         *extnt_size = rsrc_blk->rsrc_size;
7381                 }
7382                 curr_blks++;
7383         }
7384
7385         /*
7386          * Calculate the size of an embedded mailbox.  The uint32_t
7387          * accounts for extents-specific word.
7388          */
7389         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
7390                 sizeof(uint32_t);
7391
7392         /*
7393          * Presume the allocation and response will fit into an embedded
7394          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
7395          */
7396         emb = LPFC_SLI4_MBX_EMBED;
7397         req_len = emb_len;
7398         if (req_len > emb_len) {
7399                 req_len = curr_blks * sizeof(uint16_t) +
7400                         sizeof(union lpfc_sli4_cfg_shdr) +
7401                         sizeof(uint32_t);
7402                 emb = LPFC_SLI4_MBX_NEMBED;
7403         }
7404
7405         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7406         if (!mbox)
7407                 return -ENOMEM;
7408         memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
7409
7410         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7411                                      LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
7412                                      req_len, emb);
7413         if (alloc_len < req_len) {
7414                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7415                         "2983 Allocated DMA memory size (x%x) is "
7416                         "less than the requested DMA memory "
7417                         "size (x%x)\n", alloc_len, req_len);
7418                 rc = -ENOMEM;
7419                 goto err_exit;
7420         }
7421         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
7422         if (unlikely(rc)) {
7423                 rc = -EIO;
7424                 goto err_exit;
7425         }
7426
7427         if (!phba->sli4_hba.intr_enable)
7428                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7429         else {
7430                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
7431                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7432         }
7433
7434         if (unlikely(rc)) {
7435                 rc = -EIO;
7436                 goto err_exit;
7437         }
7438
7439         /*
7440          * Figure out where the response is located.  Then get local pointers
7441          * to the response data.  The port does not guarantee to respond to
7442          * all extents counts request so update the local variable with the
7443          * allocated count from the port.
7444          */
7445         if (emb == LPFC_SLI4_MBX_EMBED) {
7446                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7447                 shdr = &rsrc_ext->header.cfg_shdr;
7448                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7449         } else {
7450                 virtaddr = mbox->sge_array->addr[0];
7451                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7452                 shdr = &n_rsrc->cfg_shdr;
7453                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7454         }
7455
7456         if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
7457                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7458                         "2984 Failed to read allocated resources "
7459                         "for type %d - Status 0x%x Add'l Status 0x%x.\n",
7460                         type,
7461                         bf_get(lpfc_mbox_hdr_status, &shdr->response),
7462                         bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7463                 rc = -EIO;
7464                 goto err_exit;
7465         }
7466  err_exit:
7467         lpfc_sli4_mbox_cmd_free(phba, mbox);
7468         return rc;
7469 }
7470
7471 /**
7472  * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
7473  * @phba: pointer to lpfc hba data structure.
7474  * @sgl_list: linked link of sgl buffers to post
7475  * @cnt: number of linked list buffers
7476  *
7477  * This routine walks the list of buffers that have been allocated and
7478  * repost them to the port by using SGL block post. This is needed after a
7479  * pci_function_reset/warm_start or start. It attempts to construct blocks
7480  * of buffer sgls which contains contiguous xris and uses the non-embedded
7481  * SGL block post mailbox commands to post them to the port. For single
7482  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7483  * mailbox command for posting.
7484  *
7485  * Returns: 0 = success, non-zero failure.
7486  **/
7487 static int
7488 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7489                           struct list_head *sgl_list, int cnt)
7490 {
7491         struct lpfc_sglq *sglq_entry = NULL;
7492         struct lpfc_sglq *sglq_entry_next = NULL;
7493         struct lpfc_sglq *sglq_entry_first = NULL;
7494         int status, total_cnt;
7495         int post_cnt = 0, num_posted = 0, block_cnt = 0;
7496         int last_xritag = NO_XRI;
7497         LIST_HEAD(prep_sgl_list);
7498         LIST_HEAD(blck_sgl_list);
7499         LIST_HEAD(allc_sgl_list);
7500         LIST_HEAD(post_sgl_list);
7501         LIST_HEAD(free_sgl_list);
7502
7503         spin_lock_irq(&phba->hbalock);
7504         spin_lock(&phba->sli4_hba.sgl_list_lock);
7505         list_splice_init(sgl_list, &allc_sgl_list);
7506         spin_unlock(&phba->sli4_hba.sgl_list_lock);
7507         spin_unlock_irq(&phba->hbalock);
7508
7509         total_cnt = cnt;
7510         list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7511                                  &allc_sgl_list, list) {
7512                 list_del_init(&sglq_entry->list);
7513                 block_cnt++;
7514                 if ((last_xritag != NO_XRI) &&
7515                     (sglq_entry->sli4_xritag != last_xritag + 1)) {
7516                         /* a hole in xri block, form a sgl posting block */
7517                         list_splice_init(&prep_sgl_list, &blck_sgl_list);
7518                         post_cnt = block_cnt - 1;
7519                         /* prepare list for next posting block */
7520                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
7521                         block_cnt = 1;
7522                 } else {
7523                         /* prepare list for next posting block */
7524                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
7525                         /* enough sgls for non-embed sgl mbox command */
7526                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7527                                 list_splice_init(&prep_sgl_list,
7528                                                  &blck_sgl_list);
7529                                 post_cnt = block_cnt;
7530                                 block_cnt = 0;
7531                         }
7532                 }
7533                 num_posted++;
7534
7535                 /* keep track of last sgl's xritag */
7536                 last_xritag = sglq_entry->sli4_xritag;
7537
7538                 /* end of repost sgl list condition for buffers */
7539                 if (num_posted == total_cnt) {
7540                         if (post_cnt == 0) {
7541                                 list_splice_init(&prep_sgl_list,
7542                                                  &blck_sgl_list);
7543                                 post_cnt = block_cnt;
7544                         } else if (block_cnt == 1) {
7545                                 status = lpfc_sli4_post_sgl(phba,
7546                                                 sglq_entry->phys, 0,
7547                                                 sglq_entry->sli4_xritag);
7548                                 if (!status) {
7549                                         /* successful, put sgl to posted list */
7550                                         list_add_tail(&sglq_entry->list,
7551                                                       &post_sgl_list);
7552                                 } else {
7553                                         /* Failure, put sgl to free list */
7554                                         lpfc_printf_log(phba, KERN_WARNING,
7555                                                 LOG_SLI,
7556                                                 "3159 Failed to post "
7557                                                 "sgl, xritag:x%x\n",
7558                                                 sglq_entry->sli4_xritag);
7559                                         list_add_tail(&sglq_entry->list,
7560                                                       &free_sgl_list);
7561                                         total_cnt--;
7562                                 }
7563                         }
7564                 }
7565
7566                 /* continue until a nembed page worth of sgls */
7567                 if (post_cnt == 0)
7568                         continue;
7569
7570                 /* post the buffer list sgls as a block */
7571                 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7572                                                  post_cnt);
7573
7574                 if (!status) {
7575                         /* success, put sgl list to posted sgl list */
7576                         list_splice_init(&blck_sgl_list, &post_sgl_list);
7577                 } else {
7578                         /* Failure, put sgl list to free sgl list */
7579                         sglq_entry_first = list_first_entry(&blck_sgl_list,
7580                                                             struct lpfc_sglq,
7581                                                             list);
7582                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7583                                         "3160 Failed to post sgl-list, "
7584                                         "xritag:x%x-x%x\n",
7585                                         sglq_entry_first->sli4_xritag,
7586                                         (sglq_entry_first->sli4_xritag +
7587                                          post_cnt - 1));
7588                         list_splice_init(&blck_sgl_list, &free_sgl_list);
7589                         total_cnt -= post_cnt;
7590                 }
7591
7592                 /* don't reset xirtag due to hole in xri block */
7593                 if (block_cnt == 0)
7594                         last_xritag = NO_XRI;
7595
7596                 /* reset sgl post count for next round of posting */
7597                 post_cnt = 0;
7598         }
7599
7600         /* free the sgls failed to post */
7601         lpfc_free_sgl_list(phba, &free_sgl_list);
7602
7603         /* push sgls posted to the available list */
7604         if (!list_empty(&post_sgl_list)) {
7605                 spin_lock_irq(&phba->hbalock);
7606                 spin_lock(&phba->sli4_hba.sgl_list_lock);
7607                 list_splice_init(&post_sgl_list, sgl_list);
7608                 spin_unlock(&phba->sli4_hba.sgl_list_lock);
7609                 spin_unlock_irq(&phba->hbalock);
7610         } else {
7611                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7612                                 "3161 Failure to post sgl to port.\n");
7613                 return -EIO;
7614         }
7615
7616         /* return the number of XRIs actually posted */
7617         return total_cnt;
7618 }
7619
7620 /**
7621  * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
7622  * @phba: pointer to lpfc hba data structure.
7623  *
7624  * This routine walks the list of nvme buffers that have been allocated and
7625  * repost them to the port by using SGL block post. This is needed after a
7626  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7627  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
7628  * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
7629  *
7630  * Returns: 0 = success, non-zero failure.
7631  **/
7632 static int
7633 lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
7634 {
7635         LIST_HEAD(post_nblist);
7636         int num_posted, rc = 0;
7637
7638         /* get all NVME buffers need to repost to a local list */
7639         lpfc_io_buf_flush(phba, &post_nblist);
7640
7641         /* post the list of nvme buffer sgls to port if available */
7642         if (!list_empty(&post_nblist)) {
7643                 num_posted = lpfc_sli4_post_io_sgl_list(
7644                         phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
7645                 /* failed to post any nvme buffer, return error */
7646                 if (num_posted == 0)
7647                         rc = -EIO;
7648         }
7649         return rc;
7650 }
7651
7652 static void
7653 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7654 {
7655         uint32_t len;
7656
7657         len = sizeof(struct lpfc_mbx_set_host_data) -
7658                 sizeof(struct lpfc_sli4_cfg_mhdr);
7659         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7660                          LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7661                          LPFC_SLI4_MBX_EMBED);
7662
7663         mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
7664         mbox->u.mqe.un.set_host_data.param_len =
7665                                         LPFC_HOST_OS_DRIVER_VERSION_SIZE;
7666         snprintf(mbox->u.mqe.un.set_host_data.un.data,
7667                  LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7668                  "Linux %s v"LPFC_DRIVER_VERSION,
7669                  (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7670 }
7671
7672 int
7673 lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
7674                     struct lpfc_queue *drq, int count, int idx)
7675 {
7676         int rc, i;
7677         struct lpfc_rqe hrqe;
7678         struct lpfc_rqe drqe;
7679         struct lpfc_rqb *rqbp;
7680         unsigned long flags;
7681         struct rqb_dmabuf *rqb_buffer;
7682         LIST_HEAD(rqb_buf_list);
7683
7684         rqbp = hrq->rqbp;
7685         for (i = 0; i < count; i++) {
7686                 spin_lock_irqsave(&phba->hbalock, flags);
7687                 /* IF RQ is already full, don't bother */
7688                 if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
7689                         spin_unlock_irqrestore(&phba->hbalock, flags);
7690                         break;
7691                 }
7692                 spin_unlock_irqrestore(&phba->hbalock, flags);
7693
7694                 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7695                 if (!rqb_buffer)
7696                         break;
7697                 rqb_buffer->hrq = hrq;
7698                 rqb_buffer->drq = drq;
7699                 rqb_buffer->idx = idx;
7700                 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7701         }
7702
7703         spin_lock_irqsave(&phba->hbalock, flags);
7704         while (!list_empty(&rqb_buf_list)) {
7705                 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7706                                  hbuf.list);
7707
7708                 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7709                 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7710                 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7711                 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7712                 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7713                 if (rc < 0) {
7714                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7715                                         "6421 Cannot post to HRQ %d: %x %x %x "
7716                                         "DRQ %x %x\n",
7717                                         hrq->queue_id,
7718                                         hrq->host_index,
7719                                         hrq->hba_index,
7720                                         hrq->entry_count,
7721                                         drq->host_index,
7722                                         drq->hba_index);
7723                         rqbp->rqb_free_buffer(phba, rqb_buffer);
7724                 } else {
7725                         list_add_tail(&rqb_buffer->hbuf.list,
7726                                       &rqbp->rqb_buffer_list);
7727                         rqbp->buffer_count++;
7728                 }
7729         }
7730         spin_unlock_irqrestore(&phba->hbalock, flags);
7731         return 1;
7732 }
7733
7734 static void
7735 lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7736 {
7737         struct lpfc_vport *vport = pmb->vport;
7738         union lpfc_sli4_cfg_shdr *shdr;
7739         u32 shdr_status, shdr_add_status;
7740         u32 sig, acqe;
7741
7742         /* Two outcomes. (1) Set featurs was successul and EDC negotiation
7743          * is done. (2) Mailbox failed and send FPIN support only.
7744          */
7745         shdr = (union lpfc_sli4_cfg_shdr *)
7746                 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7747         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7748         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7749         if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7750                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
7751                                 "2516 CGN SET_FEATURE mbox failed with "
7752                                 "status x%x add_status x%x, mbx status x%x "
7753                                 "Reset Congestion to FPINs only\n",
7754                                 shdr_status, shdr_add_status,
7755                                 pmb->u.mb.mbxStatus);
7756                 /* If there is a mbox error, move on to RDF */
7757                 phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7758                 phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7759                 goto out;
7760         }
7761
7762         /* Zero out Congestion Signal ACQE counter */
7763         phba->cgn_acqe_cnt = 0;
7764         atomic64_set(&phba->cgn_acqe_stat.warn, 0);
7765         atomic64_set(&phba->cgn_acqe_stat.alarm, 0);
7766
7767         acqe = bf_get(lpfc_mbx_set_feature_CGN_acqe_freq,
7768                       &pmb->u.mqe.un.set_feature);
7769         sig = bf_get(lpfc_mbx_set_feature_CGN_warn_freq,
7770                      &pmb->u.mqe.un.set_feature);
7771         lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7772                         "4620 SET_FEATURES Success: Freq: %ds %dms "
7773                         " Reg: x%x x%x\n", acqe, sig,
7774                         phba->cgn_reg_signal, phba->cgn_reg_fpin);
7775 out:
7776         mempool_free(pmb, phba->mbox_mem_pool);
7777
7778         /* Register for FPIN events from the fabric now that the
7779          * EDC common_set_features has completed.
7780          */
7781         lpfc_issue_els_rdf(vport, 0);
7782 }
7783
7784 int
7785 lpfc_config_cgn_signal(struct lpfc_hba *phba)
7786 {
7787         LPFC_MBOXQ_t *mboxq;
7788         u32 rc;
7789
7790         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7791         if (!mboxq)
7792                 goto out_rdf;
7793
7794         lpfc_set_features(phba, mboxq, LPFC_SET_CGN_SIGNAL);
7795         mboxq->vport = phba->pport;
7796         mboxq->mbox_cmpl = lpfc_mbx_cmpl_cgn_set_ftrs;
7797
7798         lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7799                         "4621 SET_FEATURES: FREQ sig x%x acqe x%x: "
7800                         "Reg: x%x x%x\n",
7801                         phba->cgn_sig_freq, lpfc_acqe_cgn_frequency,
7802                         phba->cgn_reg_signal, phba->cgn_reg_fpin);
7803
7804         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7805         if (rc == MBX_NOT_FINISHED)
7806                 goto out;
7807         return 0;
7808
7809 out:
7810         mempool_free(mboxq, phba->mbox_mem_pool);
7811 out_rdf:
7812         /* If there is a mbox error, move on to RDF */
7813         phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7814         phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7815         lpfc_issue_els_rdf(phba->pport, 0);
7816         return -EIO;
7817 }
7818
7819 /**
7820  * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
7821  * @phba: pointer to lpfc hba data structure.
7822  *
7823  * This routine initializes the per-cq idle_stat to dynamically dictate
7824  * polling decisions.
7825  *
7826  * Return codes:
7827  *   None
7828  **/
7829 static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
7830 {
7831         int i;
7832         struct lpfc_sli4_hdw_queue *hdwq;
7833         struct lpfc_queue *cq;
7834         struct lpfc_idle_stat *idle_stat;
7835         u64 wall;
7836
7837         for_each_present_cpu(i) {
7838                 hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
7839                 cq = hdwq->io_cq;
7840
7841                 /* Skip if we've already handled this cq's primary CPU */
7842                 if (cq->chann != i)
7843                         continue;
7844
7845                 idle_stat = &phba->sli4_hba.idle_stat[i];
7846
7847                 idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
7848                 idle_stat->prev_wall = wall;
7849
7850                 if (phba->nvmet_support ||
7851                     phba->cmf_active_mode != LPFC_CFG_OFF)
7852                         cq->poll_mode = LPFC_QUEUE_WORK;
7853                 else
7854                         cq->poll_mode = LPFC_IRQ_POLL;
7855         }
7856
7857         if (!phba->nvmet_support)
7858                 schedule_delayed_work(&phba->idle_stat_delay_work,
7859                                       msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
7860 }
7861
7862 static void lpfc_sli4_dip(struct lpfc_hba *phba)
7863 {
7864         uint32_t if_type;
7865
7866         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7867         if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
7868             if_type == LPFC_SLI_INTF_IF_TYPE_6) {
7869                 struct lpfc_register reg_data;
7870
7871                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7872                                &reg_data.word0))
7873                         return;
7874
7875                 if (bf_get(lpfc_sliport_status_dip, &reg_data))
7876                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7877                                         "2904 Firmware Dump Image Present"
7878                                         " on Adapter");
7879         }
7880 }
7881
7882 /**
7883  * lpfc_cmf_setup - Initialize idle_stat tracking
7884  * @phba: Pointer to HBA context object.
7885  *
7886  * This is called from HBA setup during driver load or when the HBA
7887  * comes online. this does all the initialization to support CMF and MI.
7888  **/
7889 static int
7890 lpfc_cmf_setup(struct lpfc_hba *phba)
7891 {
7892         LPFC_MBOXQ_t *mboxq;
7893         struct lpfc_mqe *mqe;
7894         struct lpfc_dmabuf *mp;
7895         struct lpfc_pc_sli4_params *sli4_params;
7896         struct lpfc_sli4_parameters *mbx_sli4_parameters;
7897         int length;
7898         int rc, cmf, mi_ver;
7899
7900         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7901         if (!mboxq)
7902                 return -ENOMEM;
7903         mqe = &mboxq->u.mqe;
7904
7905         /* Read the port's SLI4 Config Parameters */
7906         length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
7907                   sizeof(struct lpfc_sli4_cfg_mhdr));
7908         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7909                          LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
7910                          length, LPFC_SLI4_MBX_EMBED);
7911
7912         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7913         if (unlikely(rc)) {
7914                 mempool_free(mboxq, phba->mbox_mem_pool);
7915                 return rc;
7916         }
7917
7918         /* Gather info on CMF and MI support */
7919         sli4_params = &phba->sli4_hba.pc_sli4_params;
7920         mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
7921         sli4_params->mi_ver = bf_get(cfg_mi_ver, mbx_sli4_parameters);
7922         sli4_params->cmf = bf_get(cfg_cmf, mbx_sli4_parameters);
7923
7924         /* Are we forcing MI off via module parameter? */
7925         if (!phba->cfg_enable_mi)
7926                 sli4_params->mi_ver = 0;
7927
7928         /* Always try to enable MI feature if we can */
7929         if (sli4_params->mi_ver) {
7930                 lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_MI);
7931                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7932                 mi_ver = bf_get(lpfc_mbx_set_feature_mi,
7933                                  &mboxq->u.mqe.un.set_feature);
7934
7935                 if (rc == MBX_SUCCESS) {
7936                         if (mi_ver) {
7937                                 lpfc_printf_log(phba,
7938                                                 KERN_WARNING, LOG_CGN_MGMT,
7939                                                 "6215 MI is enabled\n");
7940                                 sli4_params->mi_ver = mi_ver;
7941                         } else {
7942                                 lpfc_printf_log(phba,
7943                                                 KERN_WARNING, LOG_CGN_MGMT,
7944                                                 "6338 MI is disabled\n");
7945                                 sli4_params->mi_ver = 0;
7946                         }
7947                 } else {
7948                         /* mi_ver is already set from GET_SLI4_PARAMETERS */
7949                         lpfc_printf_log(phba, KERN_INFO,
7950                                         LOG_CGN_MGMT | LOG_INIT,
7951                                         "6245 Enable MI Mailbox x%x (x%x/x%x) "
7952                                         "failed, rc:x%x mi:x%x\n",
7953                                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7954                                         lpfc_sli_config_mbox_subsys_get
7955                                                 (phba, mboxq),
7956                                         lpfc_sli_config_mbox_opcode_get
7957                                                 (phba, mboxq),
7958                                         rc, sli4_params->mi_ver);
7959                 }
7960         } else {
7961                 lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
7962                                 "6217 MI is disabled\n");
7963         }
7964
7965         /* Ensure FDMI is enabled for MI if enable_mi is set */
7966         if (sli4_params->mi_ver)
7967                 phba->cfg_fdmi_on = LPFC_FDMI_SUPPORT;
7968
7969         /* Always try to enable CMF feature if we can */
7970         if (sli4_params->cmf) {
7971                 lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_CMF);
7972                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7973                 cmf = bf_get(lpfc_mbx_set_feature_cmf,
7974                              &mboxq->u.mqe.un.set_feature);
7975                 if (rc == MBX_SUCCESS && cmf) {
7976                         lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
7977                                         "6218 CMF is enabled: mode %d\n",
7978                                         phba->cmf_active_mode);
7979                 } else {
7980                         lpfc_printf_log(phba, KERN_WARNING,
7981                                         LOG_CGN_MGMT | LOG_INIT,
7982                                         "6219 Enable CMF Mailbox x%x (x%x/x%x) "
7983                                         "failed, rc:x%x dd:x%x\n",
7984                                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7985                                         lpfc_sli_config_mbox_subsys_get
7986                                                 (phba, mboxq),
7987                                         lpfc_sli_config_mbox_opcode_get
7988                                                 (phba, mboxq),
7989                                         rc, cmf);
7990                         sli4_params->cmf = 0;
7991                         phba->cmf_active_mode = LPFC_CFG_OFF;
7992                         goto no_cmf;
7993                 }
7994
7995                 /* Allocate Congestion Information Buffer */
7996                 if (!phba->cgn_i) {
7997                         mp = kmalloc(sizeof(*mp), GFP_KERNEL);
7998                         if (mp)
7999                                 mp->virt = dma_alloc_coherent
8000                                                 (&phba->pcidev->dev,
8001                                                 sizeof(struct lpfc_cgn_info),
8002                                                 &mp->phys, GFP_KERNEL);
8003                         if (!mp || !mp->virt) {
8004                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8005                                                 "2640 Failed to alloc memory "
8006                                                 "for Congestion Info\n");
8007                                 kfree(mp);
8008                                 sli4_params->cmf = 0;
8009                                 phba->cmf_active_mode = LPFC_CFG_OFF;
8010                                 goto no_cmf;
8011                         }
8012                         phba->cgn_i = mp;
8013
8014                         /* initialize congestion buffer info */
8015                         lpfc_init_congestion_buf(phba);
8016                         lpfc_init_congestion_stat(phba);
8017                 }
8018
8019                 rc = lpfc_sli4_cgn_params_read(phba);
8020                 if (rc < 0) {
8021                         lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8022                                         "6242 Error reading Cgn Params (%d)\n",
8023                                         rc);
8024                         /* Ensure CGN Mode is off */
8025                         sli4_params->cmf = 0;
8026                 } else if (!rc) {
8027                         lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8028                                         "6243 CGN Event empty object.\n");
8029                         /* Ensure CGN Mode is off */
8030                         sli4_params->cmf = 0;
8031                 }
8032         } else {
8033 no_cmf:
8034                 lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8035                                 "6220 CMF is disabled\n");
8036         }
8037
8038         /* Only register congestion buffer with firmware if BOTH
8039          * CMF and E2E are enabled.
8040          */
8041         if (sli4_params->cmf && sli4_params->mi_ver) {
8042                 rc = lpfc_reg_congestion_buf(phba);
8043                 if (rc) {
8044                         dma_free_coherent(&phba->pcidev->dev,
8045                                           sizeof(struct lpfc_cgn_info),
8046                                           phba->cgn_i->virt, phba->cgn_i->phys);
8047                         kfree(phba->cgn_i);
8048                         phba->cgn_i = NULL;
8049                         /* Ensure CGN Mode is off */
8050                         phba->cmf_active_mode = LPFC_CFG_OFF;
8051                         return 0;
8052                 }
8053         }
8054         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8055                         "6470 Setup MI version %d CMF %d mode %d\n",
8056                         sli4_params->mi_ver, sli4_params->cmf,
8057                         phba->cmf_active_mode);
8058
8059         mempool_free(mboxq, phba->mbox_mem_pool);
8060
8061         /* Initialize atomic counters */
8062         atomic_set(&phba->cgn_fabric_warn_cnt, 0);
8063         atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
8064         atomic_set(&phba->cgn_sync_alarm_cnt, 0);
8065         atomic_set(&phba->cgn_sync_warn_cnt, 0);
8066         atomic_set(&phba->cgn_driver_evt_cnt, 0);
8067         atomic_set(&phba->cgn_latency_evt_cnt, 0);
8068         atomic64_set(&phba->cgn_latency_evt, 0);
8069
8070         phba->cmf_interval_rate = LPFC_CMF_INTERVAL;
8071
8072         /* Allocate RX Monitor Buffer */
8073         if (!phba->rxtable) {
8074                 phba->rxtable = kmalloc_array(LPFC_MAX_RXMONITOR_ENTRY,
8075                                               sizeof(struct rxtable_entry),
8076                                               GFP_KERNEL);
8077                 if (!phba->rxtable) {
8078                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8079                                         "2644 Failed to alloc memory "
8080                                         "for RX Monitor Buffer\n");
8081                         return -ENOMEM;
8082                 }
8083         }
8084         atomic_set(&phba->rxtable_idx_head, 0);
8085         atomic_set(&phba->rxtable_idx_tail, 0);
8086         return 0;
8087 }
8088
8089 static int
8090 lpfc_set_host_tm(struct lpfc_hba *phba)
8091 {
8092         LPFC_MBOXQ_t *mboxq;
8093         uint32_t len, rc;
8094         struct timespec64 cur_time;
8095         struct tm broken;
8096         uint32_t month, day, year;
8097         uint32_t hour, minute, second;
8098         struct lpfc_mbx_set_host_date_time *tm;
8099
8100         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8101         if (!mboxq)
8102                 return -ENOMEM;
8103
8104         len = sizeof(struct lpfc_mbx_set_host_data) -
8105                 sizeof(struct lpfc_sli4_cfg_mhdr);
8106         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8107                          LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
8108                          LPFC_SLI4_MBX_EMBED);
8109
8110         mboxq->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_DATE_TIME;
8111         mboxq->u.mqe.un.set_host_data.param_len =
8112                         sizeof(struct lpfc_mbx_set_host_date_time);
8113         tm = &mboxq->u.mqe.un.set_host_data.un.tm;
8114         ktime_get_real_ts64(&cur_time);
8115         time64_to_tm(cur_time.tv_sec, 0, &broken);
8116         month = broken.tm_mon + 1;
8117         day = broken.tm_mday;
8118         year = broken.tm_year - 100;
8119         hour = broken.tm_hour;
8120         minute = broken.tm_min;
8121         second = broken.tm_sec;
8122         bf_set(lpfc_mbx_set_host_month, tm, month);
8123         bf_set(lpfc_mbx_set_host_day, tm, day);
8124         bf_set(lpfc_mbx_set_host_year, tm, year);
8125         bf_set(lpfc_mbx_set_host_hour, tm, hour);
8126         bf_set(lpfc_mbx_set_host_min, tm, minute);
8127         bf_set(lpfc_mbx_set_host_sec, tm, second);
8128
8129         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8130         mempool_free(mboxq, phba->mbox_mem_pool);
8131         return rc;
8132 }
8133
8134 /**
8135  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
8136  * @phba: Pointer to HBA context object.
8137  *
8138  * This function is the main SLI4 device initialization PCI function. This
8139  * function is called by the HBA initialization code, HBA reset code and
8140  * HBA error attention handler code. Caller is not required to hold any
8141  * locks.
8142  **/
8143 int
8144 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
8145 {
8146         int rc, i, cnt, len, dd;
8147         LPFC_MBOXQ_t *mboxq;
8148         struct lpfc_mqe *mqe;
8149         uint8_t *vpd;
8150         uint32_t vpd_size;
8151         uint32_t ftr_rsp = 0;
8152         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
8153         struct lpfc_vport *vport = phba->pport;
8154         struct lpfc_dmabuf *mp;
8155         struct lpfc_rqb *rqbp;
8156
8157         /* Perform a PCI function reset to start from clean */
8158         rc = lpfc_pci_function_reset(phba);
8159         if (unlikely(rc))
8160                 return -ENODEV;
8161
8162         /* Check the HBA Host Status Register for readyness */
8163         rc = lpfc_sli4_post_status_check(phba);
8164         if (unlikely(rc))
8165                 return -ENODEV;
8166         else {
8167                 spin_lock_irq(&phba->hbalock);
8168                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
8169                 spin_unlock_irq(&phba->hbalock);
8170         }
8171
8172         lpfc_sli4_dip(phba);
8173
8174         /*
8175          * Allocate a single mailbox container for initializing the
8176          * port.
8177          */
8178         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8179         if (!mboxq)
8180                 return -ENOMEM;
8181
8182         /* Issue READ_REV to collect vpd and FW information. */
8183         vpd_size = SLI4_PAGE_SIZE;
8184         vpd = kzalloc(vpd_size, GFP_KERNEL);
8185         if (!vpd) {
8186                 rc = -ENOMEM;
8187                 goto out_free_mbox;
8188         }
8189
8190         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
8191         if (unlikely(rc)) {
8192                 kfree(vpd);
8193                 goto out_free_mbox;
8194         }
8195
8196         mqe = &mboxq->u.mqe;
8197         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
8198         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
8199                 phba->hba_flag |= HBA_FCOE_MODE;
8200                 phba->fcp_embed_io = 0; /* SLI4 FC support only */
8201         } else {
8202                 phba->hba_flag &= ~HBA_FCOE_MODE;
8203         }
8204
8205         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
8206                 LPFC_DCBX_CEE_MODE)
8207                 phba->hba_flag |= HBA_FIP_SUPPORT;
8208         else
8209                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
8210
8211         phba->hba_flag &= ~HBA_IOQ_FLUSH;
8212
8213         if (phba->sli_rev != LPFC_SLI_REV4) {
8214                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8215                         "0376 READ_REV Error. SLI Level %d "
8216                         "FCoE enabled %d\n",
8217                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
8218                 rc = -EIO;
8219                 kfree(vpd);
8220                 goto out_free_mbox;
8221         }
8222
8223         rc = lpfc_set_host_tm(phba);
8224         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
8225                         "6468 Set host date / time: Status x%x:\n", rc);
8226
8227         /*
8228          * Continue initialization with default values even if driver failed
8229          * to read FCoE param config regions, only read parameters if the
8230          * board is FCoE
8231          */
8232         if (phba->hba_flag & HBA_FCOE_MODE &&
8233             lpfc_sli4_read_fcoe_params(phba))
8234                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
8235                         "2570 Failed to read FCoE parameters\n");
8236
8237         /*
8238          * Retrieve sli4 device physical port name, failure of doing it
8239          * is considered as non-fatal.
8240          */
8241         rc = lpfc_sli4_retrieve_pport_name(phba);
8242         if (!rc)
8243                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8244                                 "3080 Successful retrieving SLI4 device "
8245                                 "physical port name: %s.\n", phba->Port);
8246
8247         rc = lpfc_sli4_get_ctl_attr(phba);
8248         if (!rc)
8249                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8250                                 "8351 Successful retrieving SLI4 device "
8251                                 "CTL ATTR\n");
8252
8253         /*
8254          * Evaluate the read rev and vpd data. Populate the driver
8255          * state with the results. If this routine fails, the failure
8256          * is not fatal as the driver will use generic values.
8257          */
8258         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
8259         if (unlikely(!rc)) {
8260                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8261                                 "0377 Error %d parsing vpd. "
8262                                 "Using defaults.\n", rc);
8263                 rc = 0;
8264         }
8265         kfree(vpd);
8266
8267         /* Save information as VPD data */
8268         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
8269         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
8270
8271         /*
8272          * This is because first G7 ASIC doesn't support the standard
8273          * 0x5a NVME cmd descriptor type/subtype
8274          */
8275         if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8276                         LPFC_SLI_INTF_IF_TYPE_6) &&
8277             (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
8278             (phba->vpd.rev.smRev == 0) &&
8279             (phba->cfg_nvme_embed_cmd == 1))
8280                 phba->cfg_nvme_embed_cmd = 0;
8281
8282         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
8283         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
8284                                          &mqe->un.read_rev);
8285         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
8286                                        &mqe->un.read_rev);
8287         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
8288                                             &mqe->un.read_rev);
8289         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
8290                                            &mqe->un.read_rev);
8291         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
8292         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
8293         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
8294         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
8295         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
8296         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
8297         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8298                         "(%d):0380 READ_REV Status x%x "
8299                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
8300                         mboxq->vport ? mboxq->vport->vpi : 0,
8301                         bf_get(lpfc_mqe_status, mqe),
8302                         phba->vpd.rev.opFwName,
8303                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
8304                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
8305
8306         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8307             LPFC_SLI_INTF_IF_TYPE_0) {
8308                 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
8309                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8310                 if (rc == MBX_SUCCESS) {
8311                         phba->hba_flag |= HBA_RECOVERABLE_UE;
8312                         /* Set 1Sec interval to detect UE */
8313                         phba->eratt_poll_interval = 1;
8314                         phba->sli4_hba.ue_to_sr = bf_get(
8315                                         lpfc_mbx_set_feature_UESR,
8316                                         &mboxq->u.mqe.un.set_feature);
8317                         phba->sli4_hba.ue_to_rp = bf_get(
8318                                         lpfc_mbx_set_feature_UERP,
8319                                         &mboxq->u.mqe.un.set_feature);
8320                 }
8321         }
8322
8323         if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
8324                 /* Enable MDS Diagnostics only if the SLI Port supports it */
8325                 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
8326                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8327                 if (rc != MBX_SUCCESS)
8328                         phba->mds_diags_support = 0;
8329         }
8330
8331         /*
8332          * Discover the port's supported feature set and match it against the
8333          * hosts requests.
8334          */
8335         lpfc_request_features(phba, mboxq);
8336         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8337         if (unlikely(rc)) {
8338                 rc = -EIO;
8339                 goto out_free_mbox;
8340         }
8341
8342         /* Disable VMID if app header is not supported */
8343         if (phba->cfg_vmid_app_header && !(bf_get(lpfc_mbx_rq_ftr_rsp_ashdr,
8344                                                   &mqe->un.req_ftrs))) {
8345                 bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 0);
8346                 phba->cfg_vmid_app_header = 0;
8347                 lpfc_printf_log(phba, KERN_DEBUG, LOG_SLI,
8348                                 "1242 vmid feature not supported\n");
8349         }
8350
8351         /*
8352          * The port must support FCP initiator mode as this is the
8353          * only mode running in the host.
8354          */
8355         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
8356                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8357                                 "0378 No support for fcpi mode.\n");
8358                 ftr_rsp++;
8359         }
8360
8361         /* Performance Hints are ONLY for FCoE */
8362         if (phba->hba_flag & HBA_FCOE_MODE) {
8363                 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
8364                         phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
8365                 else
8366                         phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
8367         }
8368
8369         /*
8370          * If the port cannot support the host's requested features
8371          * then turn off the global config parameters to disable the
8372          * feature in the driver.  This is not a fatal error.
8373          */
8374         if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
8375                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
8376                         phba->cfg_enable_bg = 0;
8377                         phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
8378                         ftr_rsp++;
8379                 }
8380         }
8381
8382         if (phba->max_vpi && phba->cfg_enable_npiv &&
8383             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8384                 ftr_rsp++;
8385
8386         if (ftr_rsp) {
8387                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8388                                 "0379 Feature Mismatch Data: x%08x %08x "
8389                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
8390                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
8391                                 phba->cfg_enable_npiv, phba->max_vpi);
8392                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
8393                         phba->cfg_enable_bg = 0;
8394                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8395                         phba->cfg_enable_npiv = 0;
8396         }
8397
8398         /* These SLI3 features are assumed in SLI4 */
8399         spin_lock_irq(&phba->hbalock);
8400         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
8401         spin_unlock_irq(&phba->hbalock);
8402
8403         /* Always try to enable dual dump feature if we can */
8404         lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
8405         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8406         dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
8407         if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
8408                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8409                                 "6448 Dual Dump is enabled\n");
8410         else
8411                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
8412                                 "6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
8413                                 "rc:x%x dd:x%x\n",
8414                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8415                                 lpfc_sli_config_mbox_subsys_get(
8416                                         phba, mboxq),
8417                                 lpfc_sli_config_mbox_opcode_get(
8418                                         phba, mboxq),
8419                                 rc, dd);
8420         /*
8421          * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
8422          * calls depends on these resources to complete port setup.
8423          */
8424         rc = lpfc_sli4_alloc_resource_identifiers(phba);
8425         if (rc) {
8426                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8427                                 "2920 Failed to alloc Resource IDs "
8428                                 "rc = x%x\n", rc);
8429                 goto out_free_mbox;
8430         }
8431
8432         lpfc_set_host_data(phba, mboxq);
8433
8434         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8435         if (rc) {
8436                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8437                                 "2134 Failed to set host os driver version %x",
8438                                 rc);
8439         }
8440
8441         /* Read the port's service parameters. */
8442         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
8443         if (rc) {
8444                 phba->link_state = LPFC_HBA_ERROR;
8445                 rc = -ENOMEM;
8446                 goto out_free_mbox;
8447         }
8448
8449         mboxq->vport = vport;
8450         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8451         mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
8452         if (rc == MBX_SUCCESS) {
8453                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
8454                 rc = 0;
8455         }
8456
8457         /*
8458          * This memory was allocated by the lpfc_read_sparam routine. Release
8459          * it to the mbuf pool.
8460          */
8461         lpfc_mbuf_free(phba, mp->virt, mp->phys);
8462         kfree(mp);
8463         mboxq->ctx_buf = NULL;
8464         if (unlikely(rc)) {
8465                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8466                                 "0382 READ_SPARAM command failed "
8467                                 "status %d, mbxStatus x%x\n",
8468                                 rc, bf_get(lpfc_mqe_status, mqe));
8469                 phba->link_state = LPFC_HBA_ERROR;
8470                 rc = -EIO;
8471                 goto out_free_mbox;
8472         }
8473
8474         lpfc_update_vport_wwn(vport);
8475
8476         /* Update the fc_host data structures with new wwn. */
8477         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
8478         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
8479
8480         /* Create all the SLI4 queues */
8481         rc = lpfc_sli4_queue_create(phba);
8482         if (rc) {
8483                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8484                                 "3089 Failed to allocate queues\n");
8485                 rc = -ENODEV;
8486                 goto out_free_mbox;
8487         }
8488         /* Set up all the queues to the device */
8489         rc = lpfc_sli4_queue_setup(phba);
8490         if (unlikely(rc)) {
8491                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8492                                 "0381 Error %d during queue setup.\n ", rc);
8493                 goto out_stop_timers;
8494         }
8495         /* Initialize the driver internal SLI layer lists. */
8496         lpfc_sli4_setup(phba);
8497         lpfc_sli4_queue_init(phba);
8498
8499         /* update host els xri-sgl sizes and mappings */
8500         rc = lpfc_sli4_els_sgl_update(phba);
8501         if (unlikely(rc)) {
8502                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8503                                 "1400 Failed to update xri-sgl size and "
8504                                 "mapping: %d\n", rc);
8505                 goto out_destroy_queue;
8506         }
8507
8508         /* register the els sgl pool to the port */
8509         rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
8510                                        phba->sli4_hba.els_xri_cnt);
8511         if (unlikely(rc < 0)) {
8512                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8513                                 "0582 Error %d during els sgl post "
8514                                 "operation\n", rc);
8515                 rc = -ENODEV;
8516                 goto out_destroy_queue;
8517         }
8518         phba->sli4_hba.els_xri_cnt = rc;
8519
8520         if (phba->nvmet_support) {
8521                 /* update host nvmet xri-sgl sizes and mappings */
8522                 rc = lpfc_sli4_nvmet_sgl_update(phba);
8523                 if (unlikely(rc)) {
8524                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8525                                         "6308 Failed to update nvmet-sgl size "
8526                                         "and mapping: %d\n", rc);
8527                         goto out_destroy_queue;
8528                 }
8529
8530                 /* register the nvmet sgl pool to the port */
8531                 rc = lpfc_sli4_repost_sgl_list(
8532                         phba,
8533                         &phba->sli4_hba.lpfc_nvmet_sgl_list,
8534                         phba->sli4_hba.nvmet_xri_cnt);
8535                 if (unlikely(rc < 0)) {
8536                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8537                                         "3117 Error %d during nvmet "
8538                                         "sgl post\n", rc);
8539                         rc = -ENODEV;
8540                         goto out_destroy_queue;
8541                 }
8542                 phba->sli4_hba.nvmet_xri_cnt = rc;
8543
8544                 /* We allocate an iocbq for every receive context SGL.
8545                  * The additional allocation is for abort and ls handling.
8546                  */
8547                 cnt = phba->sli4_hba.nvmet_xri_cnt +
8548                         phba->sli4_hba.max_cfg_param.max_xri;
8549         } else {
8550                 /* update host common xri-sgl sizes and mappings */
8551                 rc = lpfc_sli4_io_sgl_update(phba);
8552                 if (unlikely(rc)) {
8553                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8554                                         "6082 Failed to update nvme-sgl size "
8555                                         "and mapping: %d\n", rc);
8556                         goto out_destroy_queue;
8557                 }
8558
8559                 /* register the allocated common sgl pool to the port */
8560                 rc = lpfc_sli4_repost_io_sgl_list(phba);
8561                 if (unlikely(rc)) {
8562                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8563                                         "6116 Error %d during nvme sgl post "
8564                                         "operation\n", rc);
8565                         /* Some NVME buffers were moved to abort nvme list */
8566                         /* A pci function reset will repost them */
8567                         rc = -ENODEV;
8568                         goto out_destroy_queue;
8569                 }
8570                 /* Each lpfc_io_buf job structure has an iocbq element.
8571                  * This cnt provides for abort, els, ct and ls requests.
8572                  */
8573                 cnt = phba->sli4_hba.max_cfg_param.max_xri;
8574         }
8575
8576         if (!phba->sli.iocbq_lookup) {
8577                 /* Initialize and populate the iocb list per host */
8578                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8579                                 "2821 initialize iocb list with %d entries\n",
8580                                 cnt);
8581                 rc = lpfc_init_iocb_list(phba, cnt);
8582                 if (rc) {
8583                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8584                                         "1413 Failed to init iocb list.\n");
8585                         goto out_destroy_queue;
8586                 }
8587         }
8588
8589         if (phba->nvmet_support)
8590                 lpfc_nvmet_create_targetport(phba);
8591
8592         if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
8593                 /* Post initial buffers to all RQs created */
8594                 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
8595                         rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
8596                         INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
8597                         rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
8598                         rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
8599                         rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
8600                         rqbp->buffer_count = 0;
8601
8602                         lpfc_post_rq_buffer(
8603                                 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
8604                                 phba->sli4_hba.nvmet_mrq_data[i],
8605                                 phba->cfg_nvmet_mrq_post, i);
8606                 }
8607         }
8608
8609         /* Post the rpi header region to the device. */
8610         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
8611         if (unlikely(rc)) {
8612                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8613                                 "0393 Error %d during rpi post operation\n",
8614                                 rc);
8615                 rc = -ENODEV;
8616                 goto out_free_iocblist;
8617         }
8618         lpfc_sli4_node_prep(phba);
8619
8620         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
8621                 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
8622                         /*
8623                          * The FC Port needs to register FCFI (index 0)
8624                          */
8625                         lpfc_reg_fcfi(phba, mboxq);
8626                         mboxq->vport = phba->pport;
8627                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8628                         if (rc != MBX_SUCCESS)
8629                                 goto out_unset_queue;
8630                         rc = 0;
8631                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
8632                                                 &mboxq->u.mqe.un.reg_fcfi);
8633                 } else {
8634                         /* We are a NVME Target mode with MRQ > 1 */
8635
8636                         /* First register the FCFI */
8637                         lpfc_reg_fcfi_mrq(phba, mboxq, 0);
8638                         mboxq->vport = phba->pport;
8639                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8640                         if (rc != MBX_SUCCESS)
8641                                 goto out_unset_queue;
8642                         rc = 0;
8643                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
8644                                                 &mboxq->u.mqe.un.reg_fcfi_mrq);
8645
8646                         /* Next register the MRQs */
8647                         lpfc_reg_fcfi_mrq(phba, mboxq, 1);
8648                         mboxq->vport = phba->pport;
8649                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8650                         if (rc != MBX_SUCCESS)
8651                                 goto out_unset_queue;
8652                         rc = 0;
8653                 }
8654                 /* Check if the port is configured to be disabled */
8655                 lpfc_sli_read_link_ste(phba);
8656         }
8657
8658         /* Don't post more new bufs if repost already recovered
8659          * the nvme sgls.
8660          */
8661         if (phba->nvmet_support == 0) {
8662                 if (phba->sli4_hba.io_xri_cnt == 0) {
8663                         len = lpfc_new_io_buf(
8664                                               phba, phba->sli4_hba.io_xri_max);
8665                         if (len == 0) {
8666                                 rc = -ENOMEM;
8667                                 goto out_unset_queue;
8668                         }
8669
8670                         if (phba->cfg_xri_rebalancing)
8671                                 lpfc_create_multixri_pools(phba);
8672                 }
8673         } else {
8674                 phba->cfg_xri_rebalancing = 0;
8675         }
8676
8677         /* Allow asynchronous mailbox command to go through */
8678         spin_lock_irq(&phba->hbalock);
8679         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8680         spin_unlock_irq(&phba->hbalock);
8681
8682         /* Post receive buffers to the device */
8683         lpfc_sli4_rb_setup(phba);
8684
8685         /* Reset HBA FCF states after HBA reset */
8686         phba->fcf.fcf_flag = 0;
8687         phba->fcf.current_rec.flag = 0;
8688
8689         /* Start the ELS watchdog timer */
8690         mod_timer(&vport->els_tmofunc,
8691                   jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
8692
8693         /* Start heart beat timer */
8694         mod_timer(&phba->hb_tmofunc,
8695                   jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
8696         phba->hba_flag &= ~(HBA_HBEAT_INP | HBA_HBEAT_TMO);
8697         phba->last_completion_time = jiffies;
8698
8699         /* start eq_delay heartbeat */
8700         if (phba->cfg_auto_imax)
8701                 queue_delayed_work(phba->wq, &phba->eq_delay_work,
8702                                    msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
8703
8704         /* start per phba idle_stat_delay heartbeat */
8705         lpfc_init_idle_stat_hb(phba);
8706
8707         /* Start error attention (ERATT) polling timer */
8708         mod_timer(&phba->eratt_poll,
8709                   jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
8710
8711         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
8712         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
8713                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
8714                 if (!rc) {
8715                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8716                                         "2829 This device supports "
8717                                         "Advanced Error Reporting (AER)\n");
8718                         spin_lock_irq(&phba->hbalock);
8719                         phba->hba_flag |= HBA_AER_ENABLED;
8720                         spin_unlock_irq(&phba->hbalock);
8721                 } else {
8722                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8723                                         "2830 This device does not support "
8724                                         "Advanced Error Reporting (AER)\n");
8725                         phba->cfg_aer_support = 0;
8726                 }
8727                 rc = 0;
8728         }
8729
8730         /*
8731          * The port is ready, set the host's link state to LINK_DOWN
8732          * in preparation for link interrupts.
8733          */
8734         spin_lock_irq(&phba->hbalock);
8735         phba->link_state = LPFC_LINK_DOWN;
8736
8737         /* Check if physical ports are trunked */
8738         if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
8739                 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
8740         if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
8741                 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
8742         if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
8743                 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
8744         if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
8745                 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
8746         spin_unlock_irq(&phba->hbalock);
8747
8748         /* Arm the CQs and then EQs on device */
8749         lpfc_sli4_arm_cqeq_intr(phba);
8750
8751         /* Indicate device interrupt mode */
8752         phba->sli4_hba.intr_enable = 1;
8753
8754         /* Setup CMF after HBA is initialized */
8755         lpfc_cmf_setup(phba);
8756
8757         if (!(phba->hba_flag & HBA_FCOE_MODE) &&
8758             (phba->hba_flag & LINK_DISABLED)) {
8759                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8760                                 "3103 Adapter Link is disabled.\n");
8761                 lpfc_down_link(phba, mboxq);
8762                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8763                 if (rc != MBX_SUCCESS) {
8764                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8765                                         "3104 Adapter failed to issue "
8766                                         "DOWN_LINK mbox cmd, rc:x%x\n", rc);
8767                         goto out_io_buff_free;
8768                 }
8769         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
8770                 /* don't perform init_link on SLI4 FC port loopback test */
8771                 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
8772                         rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
8773                         if (rc)
8774                                 goto out_io_buff_free;
8775                 }
8776         }
8777         mempool_free(mboxq, phba->mbox_mem_pool);
8778
8779         phba->hba_flag |= HBA_SETUP;
8780         return rc;
8781
8782 out_io_buff_free:
8783         /* Free allocated IO Buffers */
8784         lpfc_io_free(phba);
8785 out_unset_queue:
8786         /* Unset all the queues set up in this routine when error out */
8787         lpfc_sli4_queue_unset(phba);
8788 out_free_iocblist:
8789         lpfc_free_iocb_list(phba);
8790 out_destroy_queue:
8791         lpfc_sli4_queue_destroy(phba);
8792 out_stop_timers:
8793         lpfc_stop_hba_timers(phba);
8794 out_free_mbox:
8795         mempool_free(mboxq, phba->mbox_mem_pool);
8796         return rc;
8797 }
8798
8799 /**
8800  * lpfc_mbox_timeout - Timeout call back function for mbox timer
8801  * @t: Context to fetch pointer to hba structure from.
8802  *
8803  * This is the callback function for mailbox timer. The mailbox
8804  * timer is armed when a new mailbox command is issued and the timer
8805  * is deleted when the mailbox complete. The function is called by
8806  * the kernel timer code when a mailbox does not complete within
8807  * expected time. This function wakes up the worker thread to
8808  * process the mailbox timeout and returns. All the processing is
8809  * done by the worker thread function lpfc_mbox_timeout_handler.
8810  **/
8811 void
8812 lpfc_mbox_timeout(struct timer_list *t)
8813 {
8814         struct lpfc_hba  *phba = from_timer(phba, t, sli.mbox_tmo);
8815         unsigned long iflag;
8816         uint32_t tmo_posted;
8817
8818         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
8819         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
8820         if (!tmo_posted)
8821                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
8822         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
8823
8824         if (!tmo_posted)
8825                 lpfc_worker_wake_up(phba);
8826         return;
8827 }
8828
8829 /**
8830  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
8831  *                                    are pending
8832  * @phba: Pointer to HBA context object.
8833  *
8834  * This function checks if any mailbox completions are present on the mailbox
8835  * completion queue.
8836  **/
8837 static bool
8838 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
8839 {
8840
8841         uint32_t idx;
8842         struct lpfc_queue *mcq;
8843         struct lpfc_mcqe *mcqe;
8844         bool pending_completions = false;
8845         uint8_t qe_valid;
8846
8847         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8848                 return false;
8849
8850         /* Check for completions on mailbox completion queue */
8851
8852         mcq = phba->sli4_hba.mbx_cq;
8853         idx = mcq->hba_index;
8854         qe_valid = mcq->qe_valid;
8855         while (bf_get_le32(lpfc_cqe_valid,
8856                (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
8857                 mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
8858                 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
8859                     (!bf_get_le32(lpfc_trailer_async, mcqe))) {
8860                         pending_completions = true;
8861                         break;
8862                 }
8863                 idx = (idx + 1) % mcq->entry_count;
8864                 if (mcq->hba_index == idx)
8865                         break;
8866
8867                 /* if the index wrapped around, toggle the valid bit */
8868                 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
8869                         qe_valid = (qe_valid) ? 0 : 1;
8870         }
8871         return pending_completions;
8872
8873 }
8874
8875 /**
8876  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
8877  *                                            that were missed.
8878  * @phba: Pointer to HBA context object.
8879  *
8880  * For sli4, it is possible to miss an interrupt. As such mbox completions
8881  * maybe missed causing erroneous mailbox timeouts to occur. This function
8882  * checks to see if mbox completions are on the mailbox completion queue
8883  * and will process all the completions associated with the eq for the
8884  * mailbox completion queue.
8885  **/
8886 static bool
8887 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
8888 {
8889         struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
8890         uint32_t eqidx;
8891         struct lpfc_queue *fpeq = NULL;
8892         struct lpfc_queue *eq;
8893         bool mbox_pending;
8894
8895         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8896                 return false;
8897
8898         /* Find the EQ associated with the mbox CQ */
8899         if (sli4_hba->hdwq) {
8900                 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
8901                         eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
8902                         if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
8903                                 fpeq = eq;
8904                                 break;
8905                         }
8906                 }
8907         }
8908         if (!fpeq)
8909                 return false;
8910
8911         /* Turn off interrupts from this EQ */
8912
8913         sli4_hba->sli4_eq_clr_intr(fpeq);
8914
8915         /* Check to see if a mbox completion is pending */
8916
8917         mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
8918
8919         /*
8920          * If a mbox completion is pending, process all the events on EQ
8921          * associated with the mbox completion queue (this could include
8922          * mailbox commands, async events, els commands, receive queue data
8923          * and fcp commands)
8924          */
8925
8926         if (mbox_pending)
8927                 /* process and rearm the EQ */
8928                 lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
8929         else
8930                 /* Always clear and re-arm the EQ */
8931                 sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
8932
8933         return mbox_pending;
8934
8935 }
8936
8937 /**
8938  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
8939  * @phba: Pointer to HBA context object.
8940  *
8941  * This function is called from worker thread when a mailbox command times out.
8942  * The caller is not required to hold any locks. This function will reset the
8943  * HBA and recover all the pending commands.
8944  **/
8945 void
8946 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
8947 {
8948         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
8949         MAILBOX_t *mb = NULL;
8950
8951         struct lpfc_sli *psli = &phba->sli;
8952
8953         /* If the mailbox completed, process the completion */
8954         lpfc_sli4_process_missed_mbox_completions(phba);
8955
8956         if (!(psli->sli_flag & LPFC_SLI_ACTIVE))
8957                 return;
8958
8959         if (pmbox != NULL)
8960                 mb = &pmbox->u.mb;
8961         /* Check the pmbox pointer first.  There is a race condition
8962          * between the mbox timeout handler getting executed in the
8963          * worklist and the mailbox actually completing. When this
8964          * race condition occurs, the mbox_active will be NULL.
8965          */
8966         spin_lock_irq(&phba->hbalock);
8967         if (pmbox == NULL) {
8968                 lpfc_printf_log(phba, KERN_WARNING,
8969                                 LOG_MBOX | LOG_SLI,
8970                                 "0353 Active Mailbox cleared - mailbox timeout "
8971                                 "exiting\n");
8972                 spin_unlock_irq(&phba->hbalock);
8973                 return;
8974         }
8975
8976         /* Mbox cmd <mbxCommand> timeout */
8977         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8978                         "0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
8979                         mb->mbxCommand,
8980                         phba->pport->port_state,
8981                         phba->sli.sli_flag,
8982                         phba->sli.mbox_active);
8983         spin_unlock_irq(&phba->hbalock);
8984
8985         /* Setting state unknown so lpfc_sli_abort_iocb_ring
8986          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
8987          * it to fail all outstanding SCSI IO.
8988          */
8989         spin_lock_irq(&phba->pport->work_port_lock);
8990         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8991         spin_unlock_irq(&phba->pport->work_port_lock);
8992         spin_lock_irq(&phba->hbalock);
8993         phba->link_state = LPFC_LINK_UNKNOWN;
8994         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
8995         spin_unlock_irq(&phba->hbalock);
8996
8997         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8998                         "0345 Resetting board due to mailbox timeout\n");
8999
9000         /* Reset the HBA device */
9001         lpfc_reset_hba(phba);
9002 }
9003
9004 /**
9005  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
9006  * @phba: Pointer to HBA context object.
9007  * @pmbox: Pointer to mailbox object.
9008  * @flag: Flag indicating how the mailbox need to be processed.
9009  *
9010  * This function is called by discovery code and HBA management code
9011  * to submit a mailbox command to firmware with SLI-3 interface spec. This
9012  * function gets the hbalock to protect the data structures.
9013  * The mailbox command can be submitted in polling mode, in which case
9014  * this function will wait in a polling loop for the completion of the
9015  * mailbox.
9016  * If the mailbox is submitted in no_wait mode (not polling) the
9017  * function will submit the command and returns immediately without waiting
9018  * for the mailbox completion. The no_wait is supported only when HBA
9019  * is in SLI2/SLI3 mode - interrupts are enabled.
9020  * The SLI interface allows only one mailbox pending at a time. If the
9021  * mailbox is issued in polling mode and there is already a mailbox
9022  * pending, then the function will return an error. If the mailbox is issued
9023  * in NO_WAIT mode and there is a mailbox pending already, the function
9024  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
9025  * The sli layer owns the mailbox object until the completion of mailbox
9026  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
9027  * return codes the caller owns the mailbox command after the return of
9028  * the function.
9029  **/
9030 static int
9031 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
9032                        uint32_t flag)
9033 {
9034         MAILBOX_t *mbx;
9035         struct lpfc_sli *psli = &phba->sli;
9036         uint32_t status, evtctr;
9037         uint32_t ha_copy, hc_copy;
9038         int i;
9039         unsigned long timeout;
9040         unsigned long drvr_flag = 0;
9041         uint32_t word0, ldata;
9042         void __iomem *to_slim;
9043         int processing_queue = 0;
9044
9045         spin_lock_irqsave(&phba->hbalock, drvr_flag);
9046         if (!pmbox) {
9047                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9048                 /* processing mbox queue from intr_handler */
9049                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9050                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9051                         return MBX_SUCCESS;
9052                 }
9053                 processing_queue = 1;
9054                 pmbox = lpfc_mbox_get(phba);
9055                 if (!pmbox) {
9056                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9057                         return MBX_SUCCESS;
9058                 }
9059         }
9060
9061         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
9062                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
9063                 if(!pmbox->vport) {
9064                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9065                         lpfc_printf_log(phba, KERN_ERR,
9066                                         LOG_MBOX | LOG_VPORT,
9067                                         "1806 Mbox x%x failed. No vport\n",
9068                                         pmbox->u.mb.mbxCommand);
9069                         dump_stack();
9070                         goto out_not_finished;
9071                 }
9072         }
9073
9074         /* If the PCI channel is in offline state, do not post mbox. */
9075         if (unlikely(pci_channel_offline(phba->pcidev))) {
9076                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9077                 goto out_not_finished;
9078         }
9079
9080         /* If HBA has a deferred error attention, fail the iocb. */
9081         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9082                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9083                 goto out_not_finished;
9084         }
9085
9086         psli = &phba->sli;
9087
9088         mbx = &pmbox->u.mb;
9089         status = MBX_SUCCESS;
9090
9091         if (phba->link_state == LPFC_HBA_ERROR) {
9092                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9093
9094                 /* Mbox command <mbxCommand> cannot issue */
9095                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9096                                 "(%d):0311 Mailbox command x%x cannot "
9097                                 "issue Data: x%x x%x\n",
9098                                 pmbox->vport ? pmbox->vport->vpi : 0,
9099                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9100                 goto out_not_finished;
9101         }
9102
9103         if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9104                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
9105                         !(hc_copy & HC_MBINT_ENA)) {
9106                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9107                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9108                                 "(%d):2528 Mailbox command x%x cannot "
9109                                 "issue Data: x%x x%x\n",
9110                                 pmbox->vport ? pmbox->vport->vpi : 0,
9111                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9112                         goto out_not_finished;
9113                 }
9114         }
9115
9116         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9117                 /* Polling for a mbox command when another one is already active
9118                  * is not allowed in SLI. Also, the driver must have established
9119                  * SLI2 mode to queue and process multiple mbox commands.
9120                  */
9121
9122                 if (flag & MBX_POLL) {
9123                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9124
9125                         /* Mbox command <mbxCommand> cannot issue */
9126                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9127                                         "(%d):2529 Mailbox command x%x "
9128                                         "cannot issue Data: x%x x%x\n",
9129                                         pmbox->vport ? pmbox->vport->vpi : 0,
9130                                         pmbox->u.mb.mbxCommand,
9131                                         psli->sli_flag, flag);
9132                         goto out_not_finished;
9133                 }
9134
9135                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
9136                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9137                         /* Mbox command <mbxCommand> cannot issue */
9138                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9139                                         "(%d):2530 Mailbox command x%x "
9140                                         "cannot issue Data: x%x x%x\n",
9141                                         pmbox->vport ? pmbox->vport->vpi : 0,
9142                                         pmbox->u.mb.mbxCommand,
9143                                         psli->sli_flag, flag);
9144                         goto out_not_finished;
9145                 }
9146
9147                 /* Another mailbox command is still being processed, queue this
9148                  * command to be processed later.
9149                  */
9150                 lpfc_mbox_put(phba, pmbox);
9151
9152                 /* Mbox cmd issue - BUSY */
9153                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9154                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
9155                                 "x%x x%x x%x x%x\n",
9156                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
9157                                 mbx->mbxCommand,
9158                                 phba->pport ? phba->pport->port_state : 0xff,
9159                                 psli->sli_flag, flag);
9160
9161                 psli->slistat.mbox_busy++;
9162                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9163
9164                 if (pmbox->vport) {
9165                         lpfc_debugfs_disc_trc(pmbox->vport,
9166                                 LPFC_DISC_TRC_MBOX_VPORT,
9167                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
9168                                 (uint32_t)mbx->mbxCommand,
9169                                 mbx->un.varWords[0], mbx->un.varWords[1]);
9170                 }
9171                 else {
9172                         lpfc_debugfs_disc_trc(phba->pport,
9173                                 LPFC_DISC_TRC_MBOX,
9174                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
9175                                 (uint32_t)mbx->mbxCommand,
9176                                 mbx->un.varWords[0], mbx->un.varWords[1]);
9177                 }
9178
9179                 return MBX_BUSY;
9180         }
9181
9182         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9183
9184         /* If we are not polling, we MUST be in SLI2 mode */
9185         if (flag != MBX_POLL) {
9186                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
9187                     (mbx->mbxCommand != MBX_KILL_BOARD)) {
9188                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9189                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9190                         /* Mbox command <mbxCommand> cannot issue */
9191                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9192                                         "(%d):2531 Mailbox command x%x "
9193                                         "cannot issue Data: x%x x%x\n",
9194                                         pmbox->vport ? pmbox->vport->vpi : 0,
9195                                         pmbox->u.mb.mbxCommand,
9196                                         psli->sli_flag, flag);
9197                         goto out_not_finished;
9198                 }
9199                 /* timeout active mbox command */
9200                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
9201                                            1000);
9202                 mod_timer(&psli->mbox_tmo, jiffies + timeout);
9203         }
9204
9205         /* Mailbox cmd <cmd> issue */
9206         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9207                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
9208                         "x%x\n",
9209                         pmbox->vport ? pmbox->vport->vpi : 0,
9210                         mbx->mbxCommand,
9211                         phba->pport ? phba->pport->port_state : 0xff,
9212                         psli->sli_flag, flag);
9213
9214         if (mbx->mbxCommand != MBX_HEARTBEAT) {
9215                 if (pmbox->vport) {
9216                         lpfc_debugfs_disc_trc(pmbox->vport,
9217                                 LPFC_DISC_TRC_MBOX_VPORT,
9218                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
9219                                 (uint32_t)mbx->mbxCommand,
9220                                 mbx->un.varWords[0], mbx->un.varWords[1]);
9221                 }
9222                 else {
9223                         lpfc_debugfs_disc_trc(phba->pport,
9224                                 LPFC_DISC_TRC_MBOX,
9225                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
9226                                 (uint32_t)mbx->mbxCommand,
9227                                 mbx->un.varWords[0], mbx->un.varWords[1]);
9228                 }
9229         }
9230
9231         psli->slistat.mbox_cmd++;
9232         evtctr = psli->slistat.mbox_event;
9233
9234         /* next set own bit for the adapter and copy over command word */
9235         mbx->mbxOwner = OWN_CHIP;
9236
9237         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9238                 /* Populate mbox extension offset word. */
9239                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
9240                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
9241                                 = (uint8_t *)phba->mbox_ext
9242                                   - (uint8_t *)phba->mbox;
9243                 }
9244
9245                 /* Copy the mailbox extension data */
9246                 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
9247                         lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
9248                                               (uint8_t *)phba->mbox_ext,
9249                                               pmbox->in_ext_byte_len);
9250                 }
9251                 /* Copy command data to host SLIM area */
9252                 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
9253         } else {
9254                 /* Populate mbox extension offset word. */
9255                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
9256                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
9257                                 = MAILBOX_HBA_EXT_OFFSET;
9258
9259                 /* Copy the mailbox extension data */
9260                 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
9261                         lpfc_memcpy_to_slim(phba->MBslimaddr +
9262                                 MAILBOX_HBA_EXT_OFFSET,
9263                                 pmbox->ctx_buf, pmbox->in_ext_byte_len);
9264
9265                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
9266                         /* copy command data into host mbox for cmpl */
9267                         lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
9268                                               MAILBOX_CMD_SIZE);
9269
9270                 /* First copy mbox command data to HBA SLIM, skip past first
9271                    word */
9272                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
9273                 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
9274                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
9275
9276                 /* Next copy over first word, with mbxOwner set */
9277                 ldata = *((uint32_t *)mbx);
9278                 to_slim = phba->MBslimaddr;
9279                 writel(ldata, to_slim);
9280                 readl(to_slim); /* flush */
9281
9282                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
9283                         /* switch over to host mailbox */
9284                         psli->sli_flag |= LPFC_SLI_ACTIVE;
9285         }
9286
9287         wmb();
9288
9289         switch (flag) {
9290         case MBX_NOWAIT:
9291                 /* Set up reference to mailbox command */
9292                 psli->mbox_active = pmbox;
9293                 /* Interrupt board to do it */
9294                 writel(CA_MBATT, phba->CAregaddr);
9295                 readl(phba->CAregaddr); /* flush */
9296                 /* Don't wait for it to finish, just return */
9297                 break;
9298
9299         case MBX_POLL:
9300                 /* Set up null reference to mailbox command */
9301                 psli->mbox_active = NULL;
9302                 /* Interrupt board to do it */
9303                 writel(CA_MBATT, phba->CAregaddr);
9304                 readl(phba->CAregaddr); /* flush */
9305
9306                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9307                         /* First read mbox status word */
9308                         word0 = *((uint32_t *)phba->mbox);
9309                         word0 = le32_to_cpu(word0);
9310                 } else {
9311                         /* First read mbox status word */
9312                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
9313                                 spin_unlock_irqrestore(&phba->hbalock,
9314                                                        drvr_flag);
9315                                 goto out_not_finished;
9316                         }
9317                 }
9318
9319                 /* Read the HBA Host Attention Register */
9320                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9321                         spin_unlock_irqrestore(&phba->hbalock,
9322                                                        drvr_flag);
9323                         goto out_not_finished;
9324                 }
9325                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
9326                                                         1000) + jiffies;
9327                 i = 0;
9328                 /* Wait for command to complete */
9329                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
9330                        (!(ha_copy & HA_MBATT) &&
9331                         (phba->link_state > LPFC_WARM_START))) {
9332                         if (time_after(jiffies, timeout)) {
9333                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9334                                 spin_unlock_irqrestore(&phba->hbalock,
9335                                                        drvr_flag);
9336                                 goto out_not_finished;
9337                         }
9338
9339                         /* Check if we took a mbox interrupt while we were
9340                            polling */
9341                         if (((word0 & OWN_CHIP) != OWN_CHIP)
9342                             && (evtctr != psli->slistat.mbox_event))
9343                                 break;
9344
9345                         if (i++ > 10) {
9346                                 spin_unlock_irqrestore(&phba->hbalock,
9347                                                        drvr_flag);
9348                                 msleep(1);
9349                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
9350                         }
9351
9352                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9353                                 /* First copy command data */
9354                                 word0 = *((uint32_t *)phba->mbox);
9355                                 word0 = le32_to_cpu(word0);
9356                                 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
9357                                         MAILBOX_t *slimmb;
9358                                         uint32_t slimword0;
9359                                         /* Check real SLIM for any errors */
9360                                         slimword0 = readl(phba->MBslimaddr);
9361                                         slimmb = (MAILBOX_t *) & slimword0;
9362                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
9363                                             && slimmb->mbxStatus) {
9364                                                 psli->sli_flag &=
9365                                                     ~LPFC_SLI_ACTIVE;
9366                                                 word0 = slimword0;
9367                                         }
9368                                 }
9369                         } else {
9370                                 /* First copy command data */
9371                                 word0 = readl(phba->MBslimaddr);
9372                         }
9373                         /* Read the HBA Host Attention Register */
9374                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9375                                 spin_unlock_irqrestore(&phba->hbalock,
9376                                                        drvr_flag);
9377                                 goto out_not_finished;
9378                         }
9379                 }
9380
9381                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9382                         /* copy results back to user */
9383                         lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
9384                                                 MAILBOX_CMD_SIZE);
9385                         /* Copy the mailbox extension data */
9386                         if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
9387                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
9388                                                       pmbox->ctx_buf,
9389                                                       pmbox->out_ext_byte_len);
9390                         }
9391                 } else {
9392                         /* First copy command data */
9393                         lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
9394                                                 MAILBOX_CMD_SIZE);
9395                         /* Copy the mailbox extension data */
9396                         if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
9397                                 lpfc_memcpy_from_slim(
9398                                         pmbox->ctx_buf,
9399                                         phba->MBslimaddr +
9400                                         MAILBOX_HBA_EXT_OFFSET,
9401                                         pmbox->out_ext_byte_len);
9402                         }
9403                 }
9404
9405                 writel(HA_MBATT, phba->HAregaddr);
9406                 readl(phba->HAregaddr); /* flush */
9407
9408                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9409                 status = mbx->mbxStatus;
9410         }
9411
9412         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9413         return status;
9414
9415 out_not_finished:
9416         if (processing_queue) {
9417                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
9418                 lpfc_mbox_cmpl_put(phba, pmbox);
9419         }
9420         return MBX_NOT_FINISHED;
9421 }
9422
9423 /**
9424  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
9425  * @phba: Pointer to HBA context object.
9426  *
9427  * The function blocks the posting of SLI4 asynchronous mailbox commands from
9428  * the driver internal pending mailbox queue. It will then try to wait out the
9429  * possible outstanding mailbox command before return.
9430  *
9431  * Returns:
9432  *      0 - the outstanding mailbox command completed; otherwise, the wait for
9433  *      the outstanding mailbox command timed out.
9434  **/
9435 static int
9436 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
9437 {
9438         struct lpfc_sli *psli = &phba->sli;
9439         LPFC_MBOXQ_t *mboxq;
9440         int rc = 0;
9441         unsigned long timeout = 0;
9442         u32 sli_flag;
9443         u8 cmd, subsys, opcode;
9444
9445         /* Mark the asynchronous mailbox command posting as blocked */
9446         spin_lock_irq(&phba->hbalock);
9447         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9448         /* Determine how long we might wait for the active mailbox
9449          * command to be gracefully completed by firmware.
9450          */
9451         if (phba->sli.mbox_active)
9452                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9453                                                 phba->sli.mbox_active) *
9454                                                 1000) + jiffies;
9455         spin_unlock_irq(&phba->hbalock);
9456
9457         /* Make sure the mailbox is really active */
9458         if (timeout)
9459                 lpfc_sli4_process_missed_mbox_completions(phba);
9460
9461         /* Wait for the outstanding mailbox command to complete */
9462         while (phba->sli.mbox_active) {
9463                 /* Check active mailbox complete status every 2ms */
9464                 msleep(2);
9465                 if (time_after(jiffies, timeout)) {
9466                         /* Timeout, mark the outstanding cmd not complete */
9467
9468                         /* Sanity check sli.mbox_active has not completed or
9469                          * cancelled from another context during last 2ms sleep,
9470                          * so take hbalock to be sure before logging.
9471                          */
9472                         spin_lock_irq(&phba->hbalock);
9473                         if (phba->sli.mbox_active) {
9474                                 mboxq = phba->sli.mbox_active;
9475                                 cmd = mboxq->u.mb.mbxCommand;
9476                                 subsys = lpfc_sli_config_mbox_subsys_get(phba,
9477                                                                          mboxq);
9478                                 opcode = lpfc_sli_config_mbox_opcode_get(phba,
9479                                                                          mboxq);
9480                                 sli_flag = psli->sli_flag;
9481                                 spin_unlock_irq(&phba->hbalock);
9482                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9483                                                 "2352 Mailbox command x%x "
9484                                                 "(x%x/x%x) sli_flag x%x could "
9485                                                 "not complete\n",
9486                                                 cmd, subsys, opcode,
9487                                                 sli_flag);
9488                         } else {
9489                                 spin_unlock_irq(&phba->hbalock);
9490                         }
9491
9492                         rc = 1;
9493                         break;
9494                 }
9495         }
9496
9497         /* Can not cleanly block async mailbox command, fails it */
9498         if (rc) {
9499                 spin_lock_irq(&phba->hbalock);
9500                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9501                 spin_unlock_irq(&phba->hbalock);
9502         }
9503         return rc;
9504 }
9505
9506 /**
9507  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
9508  * @phba: Pointer to HBA context object.
9509  *
9510  * The function unblocks and resume posting of SLI4 asynchronous mailbox
9511  * commands from the driver internal pending mailbox queue. It makes sure
9512  * that there is no outstanding mailbox command before resuming posting
9513  * asynchronous mailbox commands. If, for any reason, there is outstanding
9514  * mailbox command, it will try to wait it out before resuming asynchronous
9515  * mailbox command posting.
9516  **/
9517 static void
9518 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
9519 {
9520         struct lpfc_sli *psli = &phba->sli;
9521
9522         spin_lock_irq(&phba->hbalock);
9523         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9524                 /* Asynchronous mailbox posting is not blocked, do nothing */
9525                 spin_unlock_irq(&phba->hbalock);
9526                 return;
9527         }
9528
9529         /* Outstanding synchronous mailbox command is guaranteed to be done,
9530          * successful or timeout, after timing-out the outstanding mailbox
9531          * command shall always be removed, so just unblock posting async
9532          * mailbox command and resume
9533          */
9534         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9535         spin_unlock_irq(&phba->hbalock);
9536
9537         /* wake up worker thread to post asynchronous mailbox command */
9538         lpfc_worker_wake_up(phba);
9539 }
9540
9541 /**
9542  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
9543  * @phba: Pointer to HBA context object.
9544  * @mboxq: Pointer to mailbox object.
9545  *
9546  * The function waits for the bootstrap mailbox register ready bit from
9547  * port for twice the regular mailbox command timeout value.
9548  *
9549  *      0 - no timeout on waiting for bootstrap mailbox register ready.
9550  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out.
9551  **/
9552 static int
9553 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9554 {
9555         uint32_t db_ready;
9556         unsigned long timeout;
9557         struct lpfc_register bmbx_reg;
9558
9559         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
9560                                    * 1000) + jiffies;
9561
9562         do {
9563                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
9564                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
9565                 if (!db_ready)
9566                         mdelay(2);
9567
9568                 if (time_after(jiffies, timeout))
9569                         return MBXERR_ERROR;
9570         } while (!db_ready);
9571
9572         return 0;
9573 }
9574
9575 /**
9576  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
9577  * @phba: Pointer to HBA context object.
9578  * @mboxq: Pointer to mailbox object.
9579  *
9580  * The function posts a mailbox to the port.  The mailbox is expected
9581  * to be comletely filled in and ready for the port to operate on it.
9582  * This routine executes a synchronous completion operation on the
9583  * mailbox by polling for its completion.
9584  *
9585  * The caller must not be holding any locks when calling this routine.
9586  *
9587  * Returns:
9588  *      MBX_SUCCESS - mailbox posted successfully
9589  *      Any of the MBX error values.
9590  **/
9591 static int
9592 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9593 {
9594         int rc = MBX_SUCCESS;
9595         unsigned long iflag;
9596         uint32_t mcqe_status;
9597         uint32_t mbx_cmnd;
9598         struct lpfc_sli *psli = &phba->sli;
9599         struct lpfc_mqe *mb = &mboxq->u.mqe;
9600         struct lpfc_bmbx_create *mbox_rgn;
9601         struct dma_address *dma_address;
9602
9603         /*
9604          * Only one mailbox can be active to the bootstrap mailbox region
9605          * at a time and there is no queueing provided.
9606          */
9607         spin_lock_irqsave(&phba->hbalock, iflag);
9608         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9609                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9610                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9611                                 "(%d):2532 Mailbox command x%x (x%x/x%x) "
9612                                 "cannot issue Data: x%x x%x\n",
9613                                 mboxq->vport ? mboxq->vport->vpi : 0,
9614                                 mboxq->u.mb.mbxCommand,
9615                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9616                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9617                                 psli->sli_flag, MBX_POLL);
9618                 return MBXERR_ERROR;
9619         }
9620         /* The server grabs the token and owns it until release */
9621         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9622         phba->sli.mbox_active = mboxq;
9623         spin_unlock_irqrestore(&phba->hbalock, iflag);
9624
9625         /* wait for bootstrap mbox register for readyness */
9626         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9627         if (rc)
9628                 goto exit;
9629         /*
9630          * Initialize the bootstrap memory region to avoid stale data areas
9631          * in the mailbox post.  Then copy the caller's mailbox contents to
9632          * the bmbx mailbox region.
9633          */
9634         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
9635         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
9636         lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
9637                                sizeof(struct lpfc_mqe));
9638
9639         /* Post the high mailbox dma address to the port and wait for ready. */
9640         dma_address = &phba->sli4_hba.bmbx.dma_address;
9641         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
9642
9643         /* wait for bootstrap mbox register for hi-address write done */
9644         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9645         if (rc)
9646                 goto exit;
9647
9648         /* Post the low mailbox dma address to the port. */
9649         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
9650
9651         /* wait for bootstrap mbox register for low address write done */
9652         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9653         if (rc)
9654                 goto exit;
9655
9656         /*
9657          * Read the CQ to ensure the mailbox has completed.
9658          * If so, update the mailbox status so that the upper layers
9659          * can complete the request normally.
9660          */
9661         lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
9662                                sizeof(struct lpfc_mqe));
9663         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
9664         lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
9665                                sizeof(struct lpfc_mcqe));
9666         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
9667         /*
9668          * When the CQE status indicates a failure and the mailbox status
9669          * indicates success then copy the CQE status into the mailbox status
9670          * (and prefix it with x4000).
9671          */
9672         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
9673                 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
9674                         bf_set(lpfc_mqe_status, mb,
9675                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
9676                 rc = MBXERR_ERROR;
9677         } else
9678                 lpfc_sli4_swap_str(phba, mboxq);
9679
9680         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9681                         "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
9682                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
9683                         " x%x x%x CQ: x%x x%x x%x x%x\n",
9684                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
9685                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9686                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9687                         bf_get(lpfc_mqe_status, mb),
9688                         mb->un.mb_words[0], mb->un.mb_words[1],
9689                         mb->un.mb_words[2], mb->un.mb_words[3],
9690                         mb->un.mb_words[4], mb->un.mb_words[5],
9691                         mb->un.mb_words[6], mb->un.mb_words[7],
9692                         mb->un.mb_words[8], mb->un.mb_words[9],
9693                         mb->un.mb_words[10], mb->un.mb_words[11],
9694                         mb->un.mb_words[12], mboxq->mcqe.word0,
9695                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
9696                         mboxq->mcqe.trailer);
9697 exit:
9698         /* We are holding the token, no needed for lock when release */
9699         spin_lock_irqsave(&phba->hbalock, iflag);
9700         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9701         phba->sli.mbox_active = NULL;
9702         spin_unlock_irqrestore(&phba->hbalock, iflag);
9703         return rc;
9704 }
9705
9706 /**
9707  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
9708  * @phba: Pointer to HBA context object.
9709  * @mboxq: Pointer to mailbox object.
9710  * @flag: Flag indicating how the mailbox need to be processed.
9711  *
9712  * This function is called by discovery code and HBA management code to submit
9713  * a mailbox command to firmware with SLI-4 interface spec.
9714  *
9715  * Return codes the caller owns the mailbox command after the return of the
9716  * function.
9717  **/
9718 static int
9719 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
9720                        uint32_t flag)
9721 {
9722         struct lpfc_sli *psli = &phba->sli;
9723         unsigned long iflags;
9724         int rc;
9725
9726         /* dump from issue mailbox command if setup */
9727         lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
9728
9729         rc = lpfc_mbox_dev_check(phba);
9730         if (unlikely(rc)) {
9731                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9732                                 "(%d):2544 Mailbox command x%x (x%x/x%x) "
9733                                 "cannot issue Data: x%x x%x\n",
9734                                 mboxq->vport ? mboxq->vport->vpi : 0,
9735                                 mboxq->u.mb.mbxCommand,
9736                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9737                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9738                                 psli->sli_flag, flag);
9739                 goto out_not_finished;
9740         }
9741
9742         /* Detect polling mode and jump to a handler */
9743         if (!phba->sli4_hba.intr_enable) {
9744                 if (flag == MBX_POLL)
9745                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
9746                 else
9747                         rc = -EIO;
9748                 if (rc != MBX_SUCCESS)
9749                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
9750                                         "(%d):2541 Mailbox command x%x "
9751                                         "(x%x/x%x) failure: "
9752                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
9753                                         "Data: x%x x%x\n,",
9754                                         mboxq->vport ? mboxq->vport->vpi : 0,
9755                                         mboxq->u.mb.mbxCommand,
9756                                         lpfc_sli_config_mbox_subsys_get(phba,
9757                                                                         mboxq),
9758                                         lpfc_sli_config_mbox_opcode_get(phba,
9759                                                                         mboxq),
9760                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
9761                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
9762                                         bf_get(lpfc_mcqe_ext_status,
9763                                                &mboxq->mcqe),
9764                                         psli->sli_flag, flag);
9765                 return rc;
9766         } else if (flag == MBX_POLL) {
9767                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
9768                                 "(%d):2542 Try to issue mailbox command "
9769                                 "x%x (x%x/x%x) synchronously ahead of async "
9770                                 "mailbox command queue: x%x x%x\n",
9771                                 mboxq->vport ? mboxq->vport->vpi : 0,
9772                                 mboxq->u.mb.mbxCommand,
9773                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9774                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9775                                 psli->sli_flag, flag);
9776                 /* Try to block the asynchronous mailbox posting */
9777                 rc = lpfc_sli4_async_mbox_block(phba);
9778                 if (!rc) {
9779                         /* Successfully blocked, now issue sync mbox cmd */
9780                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
9781                         if (rc != MBX_SUCCESS)
9782                                 lpfc_printf_log(phba, KERN_WARNING,
9783                                         LOG_MBOX | LOG_SLI,
9784                                         "(%d):2597 Sync Mailbox command "
9785                                         "x%x (x%x/x%x) failure: "
9786                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
9787                                         "Data: x%x x%x\n,",
9788                                         mboxq->vport ? mboxq->vport->vpi : 0,
9789                                         mboxq->u.mb.mbxCommand,
9790                                         lpfc_sli_config_mbox_subsys_get(phba,
9791                                                                         mboxq),
9792                                         lpfc_sli_config_mbox_opcode_get(phba,
9793                                                                         mboxq),
9794                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
9795                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
9796                                         bf_get(lpfc_mcqe_ext_status,
9797                                                &mboxq->mcqe),
9798                                         psli->sli_flag, flag);
9799                         /* Unblock the async mailbox posting afterward */
9800                         lpfc_sli4_async_mbox_unblock(phba);
9801                 }
9802                 return rc;
9803         }
9804
9805         /* Now, interrupt mode asynchronous mailbox command */
9806         rc = lpfc_mbox_cmd_check(phba, mboxq);
9807         if (rc) {
9808                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9809                                 "(%d):2543 Mailbox command x%x (x%x/x%x) "
9810                                 "cannot issue Data: x%x x%x\n",
9811                                 mboxq->vport ? mboxq->vport->vpi : 0,
9812                                 mboxq->u.mb.mbxCommand,
9813                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9814                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9815                                 psli->sli_flag, flag);
9816                 goto out_not_finished;
9817         }
9818
9819         /* Put the mailbox command to the driver internal FIFO */
9820         psli->slistat.mbox_busy++;
9821         spin_lock_irqsave(&phba->hbalock, iflags);
9822         lpfc_mbox_put(phba, mboxq);
9823         spin_unlock_irqrestore(&phba->hbalock, iflags);
9824         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9825                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
9826                         "x%x (x%x/x%x) x%x x%x x%x\n",
9827                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
9828                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
9829                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9830                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9831                         phba->pport->port_state,
9832                         psli->sli_flag, MBX_NOWAIT);
9833         /* Wake up worker thread to transport mailbox command from head */
9834         lpfc_worker_wake_up(phba);
9835
9836         return MBX_BUSY;
9837
9838 out_not_finished:
9839         return MBX_NOT_FINISHED;
9840 }
9841
9842 /**
9843  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
9844  * @phba: Pointer to HBA context object.
9845  *
9846  * This function is called by worker thread to send a mailbox command to
9847  * SLI4 HBA firmware.
9848  *
9849  **/
9850 int
9851 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
9852 {
9853         struct lpfc_sli *psli = &phba->sli;
9854         LPFC_MBOXQ_t *mboxq;
9855         int rc = MBX_SUCCESS;
9856         unsigned long iflags;
9857         struct lpfc_mqe *mqe;
9858         uint32_t mbx_cmnd;
9859
9860         /* Check interrupt mode before post async mailbox command */
9861         if (unlikely(!phba->sli4_hba.intr_enable))
9862                 return MBX_NOT_FINISHED;
9863
9864         /* Check for mailbox command service token */
9865         spin_lock_irqsave(&phba->hbalock, iflags);
9866         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9867                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9868                 return MBX_NOT_FINISHED;
9869         }
9870         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9871                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9872                 return MBX_NOT_FINISHED;
9873         }
9874         if (unlikely(phba->sli.mbox_active)) {
9875                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9876                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9877                                 "0384 There is pending active mailbox cmd\n");
9878                 return MBX_NOT_FINISHED;
9879         }
9880         /* Take the mailbox command service token */
9881         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9882
9883         /* Get the next mailbox command from head of queue */
9884         mboxq = lpfc_mbox_get(phba);
9885
9886         /* If no more mailbox command waiting for post, we're done */
9887         if (!mboxq) {
9888                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9889                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9890                 return MBX_SUCCESS;
9891         }
9892         phba->sli.mbox_active = mboxq;
9893         spin_unlock_irqrestore(&phba->hbalock, iflags);
9894
9895         /* Check device readiness for posting mailbox command */
9896         rc = lpfc_mbox_dev_check(phba);
9897         if (unlikely(rc))
9898                 /* Driver clean routine will clean up pending mailbox */
9899                 goto out_not_finished;
9900
9901         /* Prepare the mbox command to be posted */
9902         mqe = &mboxq->u.mqe;
9903         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
9904
9905         /* Start timer for the mbox_tmo and log some mailbox post messages */
9906         mod_timer(&psli->mbox_tmo, (jiffies +
9907                   msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
9908
9909         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9910                         "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
9911                         "x%x x%x\n",
9912                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
9913                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9914                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9915                         phba->pport->port_state, psli->sli_flag);
9916
9917         if (mbx_cmnd != MBX_HEARTBEAT) {
9918                 if (mboxq->vport) {
9919                         lpfc_debugfs_disc_trc(mboxq->vport,
9920                                 LPFC_DISC_TRC_MBOX_VPORT,
9921                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
9922                                 mbx_cmnd, mqe->un.mb_words[0],
9923                                 mqe->un.mb_words[1]);
9924                 } else {
9925                         lpfc_debugfs_disc_trc(phba->pport,
9926                                 LPFC_DISC_TRC_MBOX,
9927                                 "MBOX Send: cmd:x%x mb:x%x x%x",
9928                                 mbx_cmnd, mqe->un.mb_words[0],
9929                                 mqe->un.mb_words[1]);
9930                 }
9931         }
9932         psli->slistat.mbox_cmd++;
9933
9934         /* Post the mailbox command to the port */
9935         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
9936         if (rc != MBX_SUCCESS) {
9937                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9938                                 "(%d):2533 Mailbox command x%x (x%x/x%x) "
9939                                 "cannot issue Data: x%x x%x\n",
9940                                 mboxq->vport ? mboxq->vport->vpi : 0,
9941                                 mboxq->u.mb.mbxCommand,
9942                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9943                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9944                                 psli->sli_flag, MBX_NOWAIT);
9945                 goto out_not_finished;
9946         }
9947
9948         return rc;
9949
9950 out_not_finished:
9951         spin_lock_irqsave(&phba->hbalock, iflags);
9952         if (phba->sli.mbox_active) {
9953                 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9954                 __lpfc_mbox_cmpl_put(phba, mboxq);
9955                 /* Release the token */
9956                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9957                 phba->sli.mbox_active = NULL;
9958         }
9959         spin_unlock_irqrestore(&phba->hbalock, iflags);
9960
9961         return MBX_NOT_FINISHED;
9962 }
9963
9964 /**
9965  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
9966  * @phba: Pointer to HBA context object.
9967  * @pmbox: Pointer to mailbox object.
9968  * @flag: Flag indicating how the mailbox need to be processed.
9969  *
9970  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
9971  * the API jump table function pointer from the lpfc_hba struct.
9972  *
9973  * Return codes the caller owns the mailbox command after the return of the
9974  * function.
9975  **/
9976 int
9977 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
9978 {
9979         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
9980 }
9981
9982 /**
9983  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
9984  * @phba: The hba struct for which this call is being executed.
9985  * @dev_grp: The HBA PCI-Device group number.
9986  *
9987  * This routine sets up the mbox interface API function jump table in @phba
9988  * struct.
9989  * Returns: 0 - success, -ENODEV - failure.
9990  **/
9991 int
9992 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9993 {
9994
9995         switch (dev_grp) {
9996         case LPFC_PCI_DEV_LP:
9997                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
9998                 phba->lpfc_sli_handle_slow_ring_event =
9999                                 lpfc_sli_handle_slow_ring_event_s3;
10000                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
10001                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
10002                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
10003                 break;
10004         case LPFC_PCI_DEV_OC:
10005                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
10006                 phba->lpfc_sli_handle_slow_ring_event =
10007                                 lpfc_sli_handle_slow_ring_event_s4;
10008                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
10009                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
10010                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
10011                 break;
10012         default:
10013                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10014                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
10015                                 dev_grp);
10016                 return -ENODEV;
10017         }
10018         return 0;
10019 }
10020
10021 /**
10022  * __lpfc_sli_ringtx_put - Add an iocb to the txq
10023  * @phba: Pointer to HBA context object.
10024  * @pring: Pointer to driver SLI ring object.
10025  * @piocb: Pointer to address of newly added command iocb.
10026  *
10027  * This function is called with hbalock held for SLI3 ports or
10028  * the ring lock held for SLI4 ports to add a command
10029  * iocb to the txq when SLI layer cannot submit the command iocb
10030  * to the ring.
10031  **/
10032 void
10033 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10034                     struct lpfc_iocbq *piocb)
10035 {
10036         if (phba->sli_rev == LPFC_SLI_REV4)
10037                 lockdep_assert_held(&pring->ring_lock);
10038         else
10039                 lockdep_assert_held(&phba->hbalock);
10040         /* Insert the caller's iocb in the txq tail for later processing. */
10041         list_add_tail(&piocb->list, &pring->txq);
10042 }
10043
10044 /**
10045  * lpfc_sli_next_iocb - Get the next iocb in the txq
10046  * @phba: Pointer to HBA context object.
10047  * @pring: Pointer to driver SLI ring object.
10048  * @piocb: Pointer to address of newly added command iocb.
10049  *
10050  * This function is called with hbalock held before a new
10051  * iocb is submitted to the firmware. This function checks
10052  * txq to flush the iocbs in txq to Firmware before
10053  * submitting new iocbs to the Firmware.
10054  * If there are iocbs in the txq which need to be submitted
10055  * to firmware, lpfc_sli_next_iocb returns the first element
10056  * of the txq after dequeuing it from txq.
10057  * If there is no iocb in the txq then the function will return
10058  * *piocb and *piocb is set to NULL. Caller needs to check
10059  * *piocb to find if there are more commands in the txq.
10060  **/
10061 static struct lpfc_iocbq *
10062 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10063                    struct lpfc_iocbq **piocb)
10064 {
10065         struct lpfc_iocbq * nextiocb;
10066
10067         lockdep_assert_held(&phba->hbalock);
10068
10069         nextiocb = lpfc_sli_ringtx_get(phba, pring);
10070         if (!nextiocb) {
10071                 nextiocb = *piocb;
10072                 *piocb = NULL;
10073         }
10074
10075         return nextiocb;
10076 }
10077
10078 /**
10079  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
10080  * @phba: Pointer to HBA context object.
10081  * @ring_number: SLI ring number to issue iocb on.
10082  * @piocb: Pointer to command iocb.
10083  * @flag: Flag indicating if this command can be put into txq.
10084  *
10085  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
10086  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
10087  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
10088  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
10089  * this function allows only iocbs for posting buffers. This function finds
10090  * next available slot in the command ring and posts the command to the
10091  * available slot and writes the port attention register to request HBA start
10092  * processing new iocb. If there is no slot available in the ring and
10093  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
10094  * the function returns IOCB_BUSY.
10095  *
10096  * This function is called with hbalock held. The function will return success
10097  * after it successfully submit the iocb to firmware or after adding to the
10098  * txq.
10099  **/
10100 static int
10101 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
10102                     struct lpfc_iocbq *piocb, uint32_t flag)
10103 {
10104         struct lpfc_iocbq *nextiocb;
10105         IOCB_t *iocb;
10106         struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
10107
10108         lockdep_assert_held(&phba->hbalock);
10109
10110         if (piocb->iocb_cmpl && (!piocb->vport) &&
10111            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
10112            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
10113                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10114                                 "1807 IOCB x%x failed. No vport\n",
10115                                 piocb->iocb.ulpCommand);
10116                 dump_stack();
10117                 return IOCB_ERROR;
10118         }
10119
10120
10121         /* If the PCI channel is in offline state, do not post iocbs. */
10122         if (unlikely(pci_channel_offline(phba->pcidev)))
10123                 return IOCB_ERROR;
10124
10125         /* If HBA has a deferred error attention, fail the iocb. */
10126         if (unlikely(phba->hba_flag & DEFER_ERATT))
10127                 return IOCB_ERROR;
10128
10129         /*
10130          * We should never get an IOCB if we are in a < LINK_DOWN state
10131          */
10132         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10133                 return IOCB_ERROR;
10134
10135         /*
10136          * Check to see if we are blocking IOCB processing because of a
10137          * outstanding event.
10138          */
10139         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
10140                 goto iocb_busy;
10141
10142         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
10143                 /*
10144                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
10145                  * can be issued if the link is not up.
10146                  */
10147                 switch (piocb->iocb.ulpCommand) {
10148                 case CMD_GEN_REQUEST64_CR:
10149                 case CMD_GEN_REQUEST64_CX:
10150                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
10151                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
10152                                         FC_RCTL_DD_UNSOL_CMD) ||
10153                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
10154                                         MENLO_TRANSPORT_TYPE))
10155
10156                                 goto iocb_busy;
10157                         break;
10158                 case CMD_QUE_RING_BUF_CN:
10159                 case CMD_QUE_RING_BUF64_CN:
10160                         /*
10161                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
10162                          * completion, iocb_cmpl MUST be 0.
10163                          */
10164                         if (piocb->iocb_cmpl)
10165                                 piocb->iocb_cmpl = NULL;
10166                         fallthrough;
10167                 case CMD_CREATE_XRI_CR:
10168                 case CMD_CLOSE_XRI_CN:
10169                 case CMD_CLOSE_XRI_CX:
10170                         break;
10171                 default:
10172                         goto iocb_busy;
10173                 }
10174
10175         /*
10176          * For FCP commands, we must be in a state where we can process link
10177          * attention events.
10178          */
10179         } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
10180                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
10181                 goto iocb_busy;
10182         }
10183
10184         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
10185                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
10186                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
10187
10188         if (iocb)
10189                 lpfc_sli_update_ring(phba, pring);
10190         else
10191                 lpfc_sli_update_full_ring(phba, pring);
10192
10193         if (!piocb)
10194                 return IOCB_SUCCESS;
10195
10196         goto out_busy;
10197
10198  iocb_busy:
10199         pring->stats.iocb_cmd_delay++;
10200
10201  out_busy:
10202
10203         if (!(flag & SLI_IOCB_RET_IOCB)) {
10204                 __lpfc_sli_ringtx_put(phba, pring, piocb);
10205                 return IOCB_SUCCESS;
10206         }
10207
10208         return IOCB_BUSY;
10209 }
10210
10211 /**
10212  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
10213  * @phba: Pointer to HBA context object.
10214  * @piocbq: Pointer to command iocb.
10215  * @sglq: Pointer to the scatter gather queue object.
10216  *
10217  * This routine converts the bpl or bde that is in the IOCB
10218  * to a sgl list for the sli4 hardware. The physical address
10219  * of the bpl/bde is converted back to a virtual address.
10220  * If the IOCB contains a BPL then the list of BDE's is
10221  * converted to sli4_sge's. If the IOCB contains a single
10222  * BDE then it is converted to a single sli_sge.
10223  * The IOCB is still in cpu endianess so the contents of
10224  * the bpl can be used without byte swapping.
10225  *
10226  * Returns valid XRI = Success, NO_XRI = Failure.
10227 **/
10228 static uint16_t
10229 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
10230                 struct lpfc_sglq *sglq)
10231 {
10232         uint16_t xritag = NO_XRI;
10233         struct ulp_bde64 *bpl = NULL;
10234         struct ulp_bde64 bde;
10235         struct sli4_sge *sgl  = NULL;
10236         struct lpfc_dmabuf *dmabuf;
10237         IOCB_t *icmd;
10238         int numBdes = 0;
10239         int i = 0;
10240         uint32_t offset = 0; /* accumulated offset in the sg request list */
10241         int inbound = 0; /* number of sg reply entries inbound from firmware */
10242
10243         if (!piocbq || !sglq)
10244                 return xritag;
10245
10246         sgl  = (struct sli4_sge *)sglq->sgl;
10247         icmd = &piocbq->iocb;
10248         if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
10249                 return sglq->sli4_xritag;
10250         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
10251                 numBdes = icmd->un.genreq64.bdl.bdeSize /
10252                                 sizeof(struct ulp_bde64);
10253                 /* The addrHigh and addrLow fields within the IOCB
10254                  * have not been byteswapped yet so there is no
10255                  * need to swap them back.
10256                  */
10257                 if (piocbq->context3)
10258                         dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
10259                 else
10260                         return xritag;
10261
10262                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
10263                 if (!bpl)
10264                         return xritag;
10265
10266                 for (i = 0; i < numBdes; i++) {
10267                         /* Should already be byte swapped. */
10268                         sgl->addr_hi = bpl->addrHigh;
10269                         sgl->addr_lo = bpl->addrLow;
10270
10271                         sgl->word2 = le32_to_cpu(sgl->word2);
10272                         if ((i+1) == numBdes)
10273                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
10274                         else
10275                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
10276                         /* swap the size field back to the cpu so we
10277                          * can assign it to the sgl.
10278                          */
10279                         bde.tus.w = le32_to_cpu(bpl->tus.w);
10280                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
10281                         /* The offsets in the sgl need to be accumulated
10282                          * separately for the request and reply lists.
10283                          * The request is always first, the reply follows.
10284                          */
10285                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
10286                                 /* add up the reply sg entries */
10287                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
10288                                         inbound++;
10289                                 /* first inbound? reset the offset */
10290                                 if (inbound == 1)
10291                                         offset = 0;
10292                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
10293                                 bf_set(lpfc_sli4_sge_type, sgl,
10294                                         LPFC_SGE_TYPE_DATA);
10295                                 offset += bde.tus.f.bdeSize;
10296                         }
10297                         sgl->word2 = cpu_to_le32(sgl->word2);
10298                         bpl++;
10299                         sgl++;
10300                 }
10301         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
10302                         /* The addrHigh and addrLow fields of the BDE have not
10303                          * been byteswapped yet so they need to be swapped
10304                          * before putting them in the sgl.
10305                          */
10306                         sgl->addr_hi =
10307                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
10308                         sgl->addr_lo =
10309                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
10310                         sgl->word2 = le32_to_cpu(sgl->word2);
10311                         bf_set(lpfc_sli4_sge_last, sgl, 1);
10312                         sgl->word2 = cpu_to_le32(sgl->word2);
10313                         sgl->sge_len =
10314                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
10315         }
10316         return sglq->sli4_xritag;
10317 }
10318
10319 /**
10320  * lpfc_sli4_iocb2wqe - Convert the IOCB to a work queue entry.
10321  * @phba: Pointer to HBA context object.
10322  * @iocbq: Pointer to command iocb.
10323  * @wqe: Pointer to the work queue entry.
10324  *
10325  * This routine converts the iocb command to its Work Queue Entry
10326  * equivalent. The wqe pointer should not have any fields set when
10327  * this routine is called because it will memcpy over them.
10328  * This routine does not set the CQ_ID or the WQEC bits in the
10329  * wqe.
10330  *
10331  * Returns: 0 = Success, IOCB_ERROR = Failure.
10332  **/
10333 static int
10334 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
10335                 union lpfc_wqe128 *wqe)
10336 {
10337         uint32_t xmit_len = 0, total_len = 0;
10338         uint8_t ct = 0;
10339         uint32_t fip;
10340         uint32_t abort_tag;
10341         uint8_t command_type = ELS_COMMAND_NON_FIP;
10342         uint8_t cmnd;
10343         uint16_t xritag;
10344         uint16_t abrt_iotag;
10345         struct lpfc_iocbq *abrtiocbq;
10346         struct ulp_bde64 *bpl = NULL;
10347         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
10348         int numBdes, i;
10349         struct ulp_bde64 bde;
10350         struct lpfc_nodelist *ndlp;
10351         uint32_t *pcmd;
10352         uint32_t if_type;
10353
10354         fip = phba->hba_flag & HBA_FIP_SUPPORT;
10355         /* The fcp commands will set command type */
10356         if (iocbq->iocb_flag &  LPFC_IO_FCP)
10357                 command_type = FCP_COMMAND;
10358         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
10359                 command_type = ELS_COMMAND_FIP;
10360         else
10361                 command_type = ELS_COMMAND_NON_FIP;
10362
10363         if (phba->fcp_embed_io)
10364                 memset(wqe, 0, sizeof(union lpfc_wqe128));
10365         /* Some of the fields are in the right position already */
10366         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
10367         /* The ct field has moved so reset */
10368         wqe->generic.wqe_com.word7 = 0;
10369         wqe->generic.wqe_com.word10 = 0;
10370
10371         abort_tag = (uint32_t) iocbq->iotag;
10372         xritag = iocbq->sli4_xritag;
10373         /* words0-2 bpl convert bde */
10374         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
10375                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
10376                                 sizeof(struct ulp_bde64);
10377                 bpl  = (struct ulp_bde64 *)
10378                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
10379                 if (!bpl)
10380                         return IOCB_ERROR;
10381
10382                 /* Should already be byte swapped. */
10383                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
10384                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
10385                 /* swap the size field back to the cpu so we
10386                  * can assign it to the sgl.
10387                  */
10388                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
10389                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
10390                 total_len = 0;
10391                 for (i = 0; i < numBdes; i++) {
10392                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
10393                         total_len += bde.tus.f.bdeSize;
10394                 }
10395         } else
10396                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
10397
10398         iocbq->iocb.ulpIoTag = iocbq->iotag;
10399         cmnd = iocbq->iocb.ulpCommand;
10400
10401         switch (iocbq->iocb.ulpCommand) {
10402         case CMD_ELS_REQUEST64_CR:
10403                 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
10404                         ndlp = iocbq->context_un.ndlp;
10405                 else
10406                         ndlp = (struct lpfc_nodelist *)iocbq->context1;
10407                 if (!iocbq->iocb.ulpLe) {
10408                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10409                                 "2007 Only Limited Edition cmd Format"
10410                                 " supported 0x%x\n",
10411                                 iocbq->iocb.ulpCommand);
10412                         return IOCB_ERROR;
10413                 }
10414
10415                 wqe->els_req.payload_len = xmit_len;
10416                 /* Els_reguest64 has a TMO */
10417                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
10418                         iocbq->iocb.ulpTimeout);
10419                 /* Need a VF for word 4 set the vf bit*/
10420                 bf_set(els_req64_vf, &wqe->els_req, 0);
10421                 /* And a VFID for word 12 */
10422                 bf_set(els_req64_vfid, &wqe->els_req, 0);
10423                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
10424                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10425                        iocbq->iocb.ulpContext);
10426                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
10427                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
10428                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
10429                 if (command_type == ELS_COMMAND_FIP)
10430                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
10431                                         >> LPFC_FIP_ELS_ID_SHIFT);
10432                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
10433                                         iocbq->context2)->virt);
10434                 if_type = bf_get(lpfc_sli_intf_if_type,
10435                                         &phba->sli4_hba.sli_intf);
10436                 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
10437                         if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
10438                                 *pcmd == ELS_CMD_SCR ||
10439                                 *pcmd == ELS_CMD_RDF ||
10440                                 *pcmd == ELS_CMD_EDC ||
10441                                 *pcmd == ELS_CMD_RSCN_XMT ||
10442                                 *pcmd == ELS_CMD_FDISC ||
10443                                 *pcmd == ELS_CMD_LOGO ||
10444                                 *pcmd == ELS_CMD_QFPA ||
10445                                 *pcmd == ELS_CMD_UVEM ||
10446                                 *pcmd == ELS_CMD_PLOGI)) {
10447                                 bf_set(els_req64_sp, &wqe->els_req, 1);
10448                                 bf_set(els_req64_sid, &wqe->els_req,
10449                                         iocbq->vport->fc_myDID);
10450                                 if ((*pcmd == ELS_CMD_FLOGI) &&
10451                                         !(phba->fc_topology ==
10452                                                 LPFC_TOPOLOGY_LOOP))
10453                                         bf_set(els_req64_sid, &wqe->els_req, 0);
10454                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
10455                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10456                                         phba->vpi_ids[iocbq->vport->vpi]);
10457                         } else if (pcmd && iocbq->context1) {
10458                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
10459                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10460                                         phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
10461                         }
10462                 }
10463                 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
10464                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
10465                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
10466                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
10467                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
10468                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
10469                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
10470                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
10471                 wqe->els_req.max_response_payload_len = total_len - xmit_len;
10472                 break;
10473         case CMD_XMIT_SEQUENCE64_CX:
10474                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
10475                        iocbq->iocb.un.ulpWord[3]);
10476                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
10477                        iocbq->iocb.unsli3.rcvsli3.ox_id);
10478                 /* The entire sequence is transmitted for this IOCB */
10479                 xmit_len = total_len;
10480                 cmnd = CMD_XMIT_SEQUENCE64_CR;
10481                 if (phba->link_flag & LS_LOOPBACK_MODE)
10482                         bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
10483                 fallthrough;
10484         case CMD_XMIT_SEQUENCE64_CR:
10485                 /* word3 iocb=io_tag32 wqe=reserved */
10486                 wqe->xmit_sequence.rsvd3 = 0;
10487                 /* word4 relative_offset memcpy */
10488                 /* word5 r_ctl/df_ctl memcpy */
10489                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
10490                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
10491                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
10492                        LPFC_WQE_IOD_WRITE);
10493                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
10494                        LPFC_WQE_LENLOC_WORD12);
10495                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
10496                 wqe->xmit_sequence.xmit_len = xmit_len;
10497                 command_type = OTHER_COMMAND;
10498                 break;
10499         case CMD_XMIT_BCAST64_CN:
10500                 /* word3 iocb=iotag32 wqe=seq_payload_len */
10501                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
10502                 /* word4 iocb=rsvd wqe=rsvd */
10503                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
10504                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
10505                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
10506                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10507                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
10508                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
10509                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
10510                        LPFC_WQE_LENLOC_WORD3);
10511                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
10512                 break;
10513         case CMD_FCP_IWRITE64_CR:
10514                 command_type = FCP_COMMAND_DATA_OUT;
10515                 /* word3 iocb=iotag wqe=payload_offset_len */
10516                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
10517                 bf_set(payload_offset_len, &wqe->fcp_iwrite,
10518                        xmit_len + sizeof(struct fcp_rsp));
10519                 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
10520                        0);
10521                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
10522                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
10523                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
10524                        iocbq->iocb.ulpFCP2Rcvy);
10525                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
10526                 /* Always open the exchange */
10527                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
10528                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
10529                        LPFC_WQE_LENLOC_WORD4);
10530                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
10531                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
10532                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
10533                         bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
10534                         bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
10535                         if (iocbq->priority) {
10536                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10537                                        (iocbq->priority << 1));
10538                         } else {
10539                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10540                                        (phba->cfg_XLanePriority << 1));
10541                         }
10542                 }
10543                 /* Note, word 10 is already initialized to 0 */
10544
10545                 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
10546                 if (phba->cfg_enable_pbde)
10547                         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
10548                 else
10549                         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
10550
10551                 if (phba->fcp_embed_io) {
10552                         struct lpfc_io_buf *lpfc_cmd;
10553                         struct sli4_sge *sgl;
10554                         struct fcp_cmnd *fcp_cmnd;
10555                         uint32_t *ptr;
10556
10557                         /* 128 byte wqe support here */
10558
10559                         lpfc_cmd = iocbq->context1;
10560                         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
10561                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
10562
10563                         /* Word 0-2 - FCP_CMND */
10564                         wqe->generic.bde.tus.f.bdeFlags =
10565                                 BUFF_TYPE_BDE_IMMED;
10566                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10567                         wqe->generic.bde.addrHigh = 0;
10568                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
10569
10570                         bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10571                         bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10572
10573                         /* Word 22-29  FCP CMND Payload */
10574                         ptr = &wqe->words[22];
10575                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10576                 }
10577                 break;
10578         case CMD_FCP_IREAD64_CR:
10579                 /* word3 iocb=iotag wqe=payload_offset_len */
10580                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
10581                 bf_set(payload_offset_len, &wqe->fcp_iread,
10582                        xmit_len + sizeof(struct fcp_rsp));
10583                 bf_set(cmd_buff_len, &wqe->fcp_iread,
10584                        0);
10585                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
10586                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
10587                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
10588                        iocbq->iocb.ulpFCP2Rcvy);
10589                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
10590                 /* Always open the exchange */
10591                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
10592                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
10593                        LPFC_WQE_LENLOC_WORD4);
10594                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
10595                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
10596                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
10597                         bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
10598                         bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
10599                         if (iocbq->priority) {
10600                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
10601                                        (iocbq->priority << 1));
10602                         } else {
10603                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
10604                                        (phba->cfg_XLanePriority << 1));
10605                         }
10606                 }
10607                 /* Note, word 10 is already initialized to 0 */
10608
10609                 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
10610                 if (phba->cfg_enable_pbde)
10611                         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
10612                 else
10613                         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
10614
10615                 if (phba->fcp_embed_io) {
10616                         struct lpfc_io_buf *lpfc_cmd;
10617                         struct sli4_sge *sgl;
10618                         struct fcp_cmnd *fcp_cmnd;
10619                         uint32_t *ptr;
10620
10621                         /* 128 byte wqe support here */
10622
10623                         lpfc_cmd = iocbq->context1;
10624                         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
10625                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
10626
10627                         /* Word 0-2 - FCP_CMND */
10628                         wqe->generic.bde.tus.f.bdeFlags =
10629                                 BUFF_TYPE_BDE_IMMED;
10630                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10631                         wqe->generic.bde.addrHigh = 0;
10632                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
10633
10634                         bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
10635                         bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
10636
10637                         /* Word 22-29  FCP CMND Payload */
10638                         ptr = &wqe->words[22];
10639                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10640                 }
10641                 break;
10642         case CMD_FCP_ICMND64_CR:
10643                 /* word3 iocb=iotag wqe=payload_offset_len */
10644                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
10645                 bf_set(payload_offset_len, &wqe->fcp_icmd,
10646                        xmit_len + sizeof(struct fcp_rsp));
10647                 bf_set(cmd_buff_len, &wqe->fcp_icmd,
10648                        0);
10649                 /* word3 iocb=IO_TAG wqe=reserved */
10650                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
10651                 /* Always open the exchange */
10652                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
10653                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
10654                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
10655                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
10656                        LPFC_WQE_LENLOC_NONE);
10657                 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
10658                        iocbq->iocb.ulpFCP2Rcvy);
10659                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
10660                         bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
10661                         bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
10662                         if (iocbq->priority) {
10663                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
10664                                        (iocbq->priority << 1));
10665                         } else {
10666                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
10667                                        (phba->cfg_XLanePriority << 1));
10668                         }
10669                 }
10670                 /* Note, word 10 is already initialized to 0 */
10671
10672                 if (phba->fcp_embed_io) {
10673                         struct lpfc_io_buf *lpfc_cmd;
10674                         struct sli4_sge *sgl;
10675                         struct fcp_cmnd *fcp_cmnd;
10676                         uint32_t *ptr;
10677
10678                         /* 128 byte wqe support here */
10679
10680                         lpfc_cmd = iocbq->context1;
10681                         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
10682                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
10683
10684                         /* Word 0-2 - FCP_CMND */
10685                         wqe->generic.bde.tus.f.bdeFlags =
10686                                 BUFF_TYPE_BDE_IMMED;
10687                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10688                         wqe->generic.bde.addrHigh = 0;
10689                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
10690
10691                         bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
10692                         bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
10693
10694                         /* Word 22-29  FCP CMND Payload */
10695                         ptr = &wqe->words[22];
10696                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10697                 }
10698                 break;
10699         case CMD_GEN_REQUEST64_CR:
10700                 /* For this command calculate the xmit length of the
10701                  * request bde.
10702                  */
10703                 xmit_len = 0;
10704                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
10705                         sizeof(struct ulp_bde64);
10706                 for (i = 0; i < numBdes; i++) {
10707                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
10708                         if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
10709                                 break;
10710                         xmit_len += bde.tus.f.bdeSize;
10711                 }
10712                 /* word3 iocb=IO_TAG wqe=request_payload_len */
10713                 wqe->gen_req.request_payload_len = xmit_len;
10714                 /* word4 iocb=parameter wqe=relative_offset memcpy */
10715                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
10716                 /* word6 context tag copied in memcpy */
10717                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
10718                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
10719                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10720                                 "2015 Invalid CT %x command 0x%x\n",
10721                                 ct, iocbq->iocb.ulpCommand);
10722                         return IOCB_ERROR;
10723                 }
10724                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
10725                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
10726                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
10727                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
10728                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
10729                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
10730                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
10731                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
10732                 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
10733                 command_type = OTHER_COMMAND;
10734                 break;
10735         case CMD_XMIT_ELS_RSP64_CX:
10736                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
10737                 /* words0-2 BDE memcpy */
10738                 /* word3 iocb=iotag32 wqe=response_payload_len */
10739                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
10740                 /* word4 */
10741                 wqe->xmit_els_rsp.word4 = 0;
10742                 /* word5 iocb=rsvd wge=did */
10743                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
10744                          iocbq->iocb.un.xseq64.xmit_els_remoteID);
10745
10746                 if_type = bf_get(lpfc_sli_intf_if_type,
10747                                         &phba->sli4_hba.sli_intf);
10748                 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
10749                         if (iocbq->vport->fc_flag & FC_PT2PT) {
10750                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
10751                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
10752                                         iocbq->vport->fc_myDID);
10753                                 if (iocbq->vport->fc_myDID == Fabric_DID) {
10754                                         bf_set(wqe_els_did,
10755                                                 &wqe->xmit_els_rsp.wqe_dest, 0);
10756                                 }
10757                         }
10758                 }
10759                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
10760                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10761                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
10762                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
10763                        iocbq->iocb.unsli3.rcvsli3.ox_id);
10764                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
10765                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
10766                                phba->vpi_ids[iocbq->vport->vpi]);
10767                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
10768                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
10769                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
10770                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
10771                        LPFC_WQE_LENLOC_WORD3);
10772                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
10773                 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
10774                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
10775                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
10776                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
10777                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
10778                                         iocbq->vport->fc_myDID);
10779                                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
10780                                 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
10781                                         phba->vpi_ids[phba->pport->vpi]);
10782                 }
10783                 command_type = OTHER_COMMAND;
10784                 break;
10785         case CMD_CLOSE_XRI_CN:
10786         case CMD_ABORT_XRI_CN:
10787         case CMD_ABORT_XRI_CX:
10788                 /* words 0-2 memcpy should be 0 rserved */
10789                 /* port will send abts */
10790                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
10791                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
10792                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
10793                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
10794                 } else
10795                         fip = 0;
10796
10797                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
10798                         /*
10799                          * The link is down, or the command was ELS_FIP
10800                          * so the fw does not need to send abts
10801                          * on the wire.
10802                          */
10803                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
10804                 else
10805                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
10806                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
10807                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
10808                 wqe->abort_cmd.rsrvd5 = 0;
10809                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
10810                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10811                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
10812                 /*
10813                  * The abort handler will send us CMD_ABORT_XRI_CN or
10814                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
10815                  */
10816                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10817                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
10818                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
10819                        LPFC_WQE_LENLOC_NONE);
10820                 cmnd = CMD_ABORT_XRI_CX;
10821                 command_type = OTHER_COMMAND;
10822                 xritag = 0;
10823                 break;
10824         case CMD_XMIT_BLS_RSP64_CX:
10825                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
10826                 /* As BLS ABTS RSP WQE is very different from other WQEs,
10827                  * we re-construct this WQE here based on information in
10828                  * iocbq from scratch.
10829                  */
10830                 memset(wqe, 0, sizeof(*wqe));
10831                 /* OX_ID is invariable to who sent ABTS to CT exchange */
10832                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
10833                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
10834                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
10835                     LPFC_ABTS_UNSOL_INT) {
10836                         /* ABTS sent by initiator to CT exchange, the
10837                          * RX_ID field will be filled with the newly
10838                          * allocated responder XRI.
10839                          */
10840                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10841                                iocbq->sli4_xritag);
10842                 } else {
10843                         /* ABTS sent by responder to CT exchange, the
10844                          * RX_ID field will be filled with the responder
10845                          * RX_ID from ABTS.
10846                          */
10847                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10848                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
10849                 }
10850                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
10851                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
10852
10853                 /* Use CT=VPI */
10854                 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
10855                         ndlp->nlp_DID);
10856                 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
10857                         iocbq->iocb.ulpContext);
10858                 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
10859                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
10860                         phba->vpi_ids[phba->pport->vpi]);
10861                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
10862                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
10863                        LPFC_WQE_LENLOC_NONE);
10864                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
10865                 command_type = OTHER_COMMAND;
10866                 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
10867                         bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
10868                                bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
10869                         bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
10870                                bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
10871                         bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
10872                                bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
10873                 }
10874
10875                 break;
10876         case CMD_SEND_FRAME:
10877                 bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_SEND_FRAME);
10878                 bf_set(wqe_sof, &wqe->generic.wqe_com, 0x2E); /* SOF byte */
10879                 bf_set(wqe_eof, &wqe->generic.wqe_com, 0x41); /* EOF byte */
10880                 bf_set(wqe_lenloc, &wqe->generic.wqe_com, 1);
10881                 bf_set(wqe_xbl, &wqe->generic.wqe_com, 1);
10882                 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10883                 bf_set(wqe_xc, &wqe->generic.wqe_com, 1);
10884                 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, 0xA);
10885                 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10886                 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10887                 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10888                 return 0;
10889         case CMD_XRI_ABORTED_CX:
10890         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
10891         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
10892         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
10893         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
10894         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
10895         default:
10896                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10897                                 "2014 Invalid command 0x%x\n",
10898                                 iocbq->iocb.ulpCommand);
10899                 return IOCB_ERROR;
10900         }
10901
10902         if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
10903                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
10904         else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
10905                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
10906         else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
10907                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
10908         iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
10909                               LPFC_IO_DIF_INSERT);
10910         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10911         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10912         wqe->generic.wqe_com.abort_tag = abort_tag;
10913         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
10914         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
10915         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
10916         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10917         return 0;
10918 }
10919
10920 /**
10921  * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb
10922  * @phba: Pointer to HBA context object.
10923  * @ring_number: SLI ring number to issue wqe on.
10924  * @piocb: Pointer to command iocb.
10925  * @flag: Flag indicating if this command can be put into txq.
10926  *
10927  * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to
10928  * send  an iocb command to an HBA with SLI-4 interface spec.
10929  *
10930  * This function takes the hbalock before invoking the lockless version.
10931  * The function will return success after it successfully submit the wqe to
10932  * firmware or after adding to the txq.
10933  **/
10934 static int
10935 __lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,
10936                            struct lpfc_iocbq *piocb, uint32_t flag)
10937 {
10938         unsigned long iflags;
10939         int rc;
10940
10941         spin_lock_irqsave(&phba->hbalock, iflags);
10942         rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);
10943         spin_unlock_irqrestore(&phba->hbalock, iflags);
10944
10945         return rc;
10946 }
10947
10948 /**
10949  * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe
10950  * @phba: Pointer to HBA context object.
10951  * @ring_number: SLI ring number to issue wqe on.
10952  * @piocb: Pointer to command iocb.
10953  * @flag: Flag indicating if this command can be put into txq.
10954  *
10955  * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue
10956  * an wqe command to an HBA with SLI-4 interface spec.
10957  *
10958  * This function is a lockless version. The function will return success
10959  * after it successfully submit the wqe to firmware or after adding to the
10960  * txq.
10961  **/
10962 static int
10963 __lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,
10964                            struct lpfc_iocbq *piocb, uint32_t flag)
10965 {
10966         int rc;
10967         struct lpfc_io_buf *lpfc_cmd =
10968                 (struct lpfc_io_buf *)piocb->context1;
10969         union lpfc_wqe128 *wqe = &piocb->wqe;
10970         struct sli4_sge *sgl;
10971
10972         /* 128 byte wqe support here */
10973         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
10974
10975         if (phba->fcp_embed_io) {
10976                 struct fcp_cmnd *fcp_cmnd;
10977                 u32 *ptr;
10978
10979                 fcp_cmnd = lpfc_cmd->fcp_cmnd;
10980
10981                 /* Word 0-2 - FCP_CMND */
10982                 wqe->generic.bde.tus.f.bdeFlags =
10983                         BUFF_TYPE_BDE_IMMED;
10984                 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10985                 wqe->generic.bde.addrHigh = 0;
10986                 wqe->generic.bde.addrLow =  88;  /* Word 22 */
10987
10988                 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10989                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10990
10991                 /* Word 22-29  FCP CMND Payload */
10992                 ptr = &wqe->words[22];
10993                 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10994         } else {
10995                 /* Word 0-2 - Inline BDE */
10996                 wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
10997                 wqe->generic.bde.tus.f.bdeSize = sizeof(struct fcp_cmnd);
10998                 wqe->generic.bde.addrHigh = sgl->addr_hi;
10999                 wqe->generic.bde.addrLow =  sgl->addr_lo;
11000
11001                 /* Word 10 */
11002                 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
11003                 bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
11004         }
11005
11006         /* add the VMID tags as per switch response */
11007         if (unlikely(piocb->iocb_flag & LPFC_IO_VMID)) {
11008                 if (phba->pport->vmid_priority_tagging) {
11009                         bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
11010                         bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
11011                                         (piocb->vmid_tag.cs_ctl_vmid));
11012                 } else {
11013                         bf_set(wqe_appid, &wqe->fcp_iwrite.wqe_com, 1);
11014                         bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
11015                         wqe->words[31] = piocb->vmid_tag.app_id;
11016                 }
11017         }
11018         rc = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);
11019         return rc;
11020 }
11021
11022 /**
11023  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
11024  * @phba: Pointer to HBA context object.
11025  * @ring_number: SLI ring number to issue iocb on.
11026  * @piocb: Pointer to command iocb.
11027  * @flag: Flag indicating if this command can be put into txq.
11028  *
11029  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
11030  * an iocb command to an HBA with SLI-4 interface spec.
11031  *
11032  * This function is called with ringlock held. The function will return success
11033  * after it successfully submit the iocb to firmware or after adding to the
11034  * txq.
11035  **/
11036 static int
11037 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
11038                          struct lpfc_iocbq *piocb, uint32_t flag)
11039 {
11040         struct lpfc_sglq *sglq;
11041         union lpfc_wqe128 wqe;
11042         struct lpfc_queue *wq;
11043         struct lpfc_sli_ring *pring;
11044
11045         /* Get the WQ */
11046         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
11047             (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
11048                 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
11049         } else {
11050                 wq = phba->sli4_hba.els_wq;
11051         }
11052
11053         /* Get corresponding ring */
11054         pring = wq->pring;
11055
11056         /*
11057          * The WQE can be either 64 or 128 bytes,
11058          */
11059
11060         lockdep_assert_held(&pring->ring_lock);
11061
11062         if (piocb->sli4_xritag == NO_XRI) {
11063                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
11064                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
11065                         sglq = NULL;
11066                 else {
11067                         if (!list_empty(&pring->txq)) {
11068                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
11069                                         __lpfc_sli_ringtx_put(phba,
11070                                                 pring, piocb);
11071                                         return IOCB_SUCCESS;
11072                                 } else {
11073                                         return IOCB_BUSY;
11074                                 }
11075                         } else {
11076                                 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
11077                                 if (!sglq) {
11078                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
11079                                                 __lpfc_sli_ringtx_put(phba,
11080                                                                 pring,
11081                                                                 piocb);
11082                                                 return IOCB_SUCCESS;
11083                                         } else
11084                                                 return IOCB_BUSY;
11085                                 }
11086                         }
11087                 }
11088         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
11089                 /* These IO's already have an XRI and a mapped sgl. */
11090                 sglq = NULL;
11091         }
11092         else {
11093                 /*
11094                  * This is a continuation of a commandi,(CX) so this
11095                  * sglq is on the active list
11096                  */
11097                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
11098                 if (!sglq)
11099                         return IOCB_ERROR;
11100         }
11101
11102         if (sglq) {
11103                 piocb->sli4_lxritag = sglq->sli4_lxritag;
11104                 piocb->sli4_xritag = sglq->sli4_xritag;
11105                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
11106                         return IOCB_ERROR;
11107         }
11108
11109         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
11110                 return IOCB_ERROR;
11111
11112         if (lpfc_sli4_wq_put(wq, &wqe))
11113                 return IOCB_ERROR;
11114         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
11115
11116         return 0;
11117 }
11118
11119 /*
11120  * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o
11121  *
11122  * This routine wraps the actual fcp i/o function for issusing WQE for sli-4
11123  * or IOCB for sli-3  function.
11124  * pointer from the lpfc_hba struct.
11125  *
11126  * Return codes:
11127  * IOCB_ERROR - Error
11128  * IOCB_SUCCESS - Success
11129  * IOCB_BUSY - Busy
11130  **/
11131 int
11132 lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,
11133                       struct lpfc_iocbq *piocb, uint32_t flag)
11134 {
11135         return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);
11136 }
11137
11138 /*
11139  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
11140  *
11141  * This routine wraps the actual lockless version for issusing IOCB function
11142  * pointer from the lpfc_hba struct.
11143  *
11144  * Return codes:
11145  * IOCB_ERROR - Error
11146  * IOCB_SUCCESS - Success
11147  * IOCB_BUSY - Busy
11148  **/
11149 int
11150 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
11151                 struct lpfc_iocbq *piocb, uint32_t flag)
11152 {
11153         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11154 }
11155
11156 /**
11157  * lpfc_sli_api_table_setup - Set up sli api function jump table
11158  * @phba: The hba struct for which this call is being executed.
11159  * @dev_grp: The HBA PCI-Device group number.
11160  *
11161  * This routine sets up the SLI interface API function jump table in @phba
11162  * struct.
11163  * Returns: 0 - success, -ENODEV - failure.
11164  **/
11165 int
11166 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
11167 {
11168
11169         switch (dev_grp) {
11170         case LPFC_PCI_DEV_LP:
11171                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
11172                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
11173                 phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;
11174                 break;
11175         case LPFC_PCI_DEV_OC:
11176                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
11177                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
11178                 phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;
11179                 break;
11180         default:
11181                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11182                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
11183                                 dev_grp);
11184                 return -ENODEV;
11185         }
11186         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
11187         return 0;
11188 }
11189
11190 /**
11191  * lpfc_sli4_calc_ring - Calculates which ring to use
11192  * @phba: Pointer to HBA context object.
11193  * @piocb: Pointer to command iocb.
11194  *
11195  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
11196  * hba_wqidx, thus we need to calculate the corresponding ring.
11197  * Since ABORTS must go on the same WQ of the command they are
11198  * aborting, we use command's hba_wqidx.
11199  */
11200 struct lpfc_sli_ring *
11201 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
11202 {
11203         struct lpfc_io_buf *lpfc_cmd;
11204
11205         if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
11206                 if (unlikely(!phba->sli4_hba.hdwq))
11207                         return NULL;
11208                 /*
11209                  * for abort iocb hba_wqidx should already
11210                  * be setup based on what work queue we used.
11211                  */
11212                 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
11213                         lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
11214                         piocb->hba_wqidx = lpfc_cmd->hdwq_no;
11215                 }
11216                 return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
11217         } else {
11218                 if (unlikely(!phba->sli4_hba.els_wq))
11219                         return NULL;
11220                 piocb->hba_wqidx = 0;
11221                 return phba->sli4_hba.els_wq->pring;
11222         }
11223 }
11224
11225 /**
11226  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
11227  * @phba: Pointer to HBA context object.
11228  * @ring_number: Ring number
11229  * @piocb: Pointer to command iocb.
11230  * @flag: Flag indicating if this command can be put into txq.
11231  *
11232  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
11233  * function. This function gets the hbalock and calls
11234  * __lpfc_sli_issue_iocb function and will return the error returned
11235  * by __lpfc_sli_issue_iocb function. This wrapper is used by
11236  * functions which do not hold hbalock.
11237  **/
11238 int
11239 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
11240                     struct lpfc_iocbq *piocb, uint32_t flag)
11241 {
11242         struct lpfc_sli_ring *pring;
11243         struct lpfc_queue *eq;
11244         unsigned long iflags;
11245         int rc;
11246
11247         if (phba->sli_rev == LPFC_SLI_REV4) {
11248                 eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
11249
11250                 pring = lpfc_sli4_calc_ring(phba, piocb);
11251                 if (unlikely(pring == NULL))
11252                         return IOCB_ERROR;
11253
11254                 spin_lock_irqsave(&pring->ring_lock, iflags);
11255                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11256                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11257
11258                 lpfc_sli4_poll_eq(eq, LPFC_POLL_FASTPATH);
11259         } else {
11260                 /* For now, SLI2/3 will still use hbalock */
11261                 spin_lock_irqsave(&phba->hbalock, iflags);
11262                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11263                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11264         }
11265         return rc;
11266 }
11267
11268 /**
11269  * lpfc_extra_ring_setup - Extra ring setup function
11270  * @phba: Pointer to HBA context object.
11271  *
11272  * This function is called while driver attaches with the
11273  * HBA to setup the extra ring. The extra ring is used
11274  * only when driver needs to support target mode functionality
11275  * or IP over FC functionalities.
11276  *
11277  * This function is called with no lock held. SLI3 only.
11278  **/
11279 static int
11280 lpfc_extra_ring_setup( struct lpfc_hba *phba)
11281 {
11282         struct lpfc_sli *psli;
11283         struct lpfc_sli_ring *pring;
11284
11285         psli = &phba->sli;
11286
11287         /* Adjust cmd/rsp ring iocb entries more evenly */
11288
11289         /* Take some away from the FCP ring */
11290         pring = &psli->sli3_ring[LPFC_FCP_RING];
11291         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11292         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11293         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11294         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11295
11296         /* and give them to the extra ring */
11297         pring = &psli->sli3_ring[LPFC_EXTRA_RING];
11298
11299         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11300         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11301         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11302         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11303
11304         /* Setup default profile for this ring */
11305         pring->iotag_max = 4096;
11306         pring->num_mask = 1;
11307         pring->prt[0].profile = 0;      /* Mask 0 */
11308         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
11309         pring->prt[0].type = phba->cfg_multi_ring_type;
11310         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
11311         return 0;
11312 }
11313
11314 static void
11315 lpfc_sli_post_recovery_event(struct lpfc_hba *phba,
11316                              struct lpfc_nodelist *ndlp)
11317 {
11318         unsigned long iflags;
11319         struct lpfc_work_evt  *evtp = &ndlp->recovery_evt;
11320
11321         spin_lock_irqsave(&phba->hbalock, iflags);
11322         if (!list_empty(&evtp->evt_listp)) {
11323                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11324                 return;
11325         }
11326
11327         /* Incrementing the reference count until the queued work is done. */
11328         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
11329         if (!evtp->evt_arg1) {
11330                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11331                 return;
11332         }
11333         evtp->evt = LPFC_EVT_RECOVER_PORT;
11334         list_add_tail(&evtp->evt_listp, &phba->work_list);
11335         spin_unlock_irqrestore(&phba->hbalock, iflags);
11336
11337         lpfc_worker_wake_up(phba);
11338 }
11339
11340 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
11341  * @phba: Pointer to HBA context object.
11342  * @iocbq: Pointer to iocb object.
11343  *
11344  * The async_event handler calls this routine when it receives
11345  * an ASYNC_STATUS_CN event from the port.  The port generates
11346  * this event when an Abort Sequence request to an rport fails
11347  * twice in succession.  The abort could be originated by the
11348  * driver or by the port.  The ABTS could have been for an ELS
11349  * or FCP IO.  The port only generates this event when an ABTS
11350  * fails to complete after one retry.
11351  */
11352 static void
11353 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
11354                           struct lpfc_iocbq *iocbq)
11355 {
11356         struct lpfc_nodelist *ndlp = NULL;
11357         uint16_t rpi = 0, vpi = 0;
11358         struct lpfc_vport *vport = NULL;
11359
11360         /* The rpi in the ulpContext is vport-sensitive. */
11361         vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
11362         rpi = iocbq->iocb.ulpContext;
11363
11364         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11365                         "3092 Port generated ABTS async event "
11366                         "on vpi %d rpi %d status 0x%x\n",
11367                         vpi, rpi, iocbq->iocb.ulpStatus);
11368
11369         vport = lpfc_find_vport_by_vpid(phba, vpi);
11370         if (!vport)
11371                 goto err_exit;
11372         ndlp = lpfc_findnode_rpi(vport, rpi);
11373         if (!ndlp)
11374                 goto err_exit;
11375
11376         if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
11377                 lpfc_sli_abts_recover_port(vport, ndlp);
11378         return;
11379
11380  err_exit:
11381         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11382                         "3095 Event Context not found, no "
11383                         "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
11384                         iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
11385                         vpi, rpi);
11386 }
11387
11388 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
11389  * @phba: pointer to HBA context object.
11390  * @ndlp: nodelist pointer for the impacted rport.
11391  * @axri: pointer to the wcqe containing the failed exchange.
11392  *
11393  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
11394  * port.  The port generates this event when an abort exchange request to an
11395  * rport fails twice in succession with no reply.  The abort could be originated
11396  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
11397  */
11398 void
11399 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
11400                            struct lpfc_nodelist *ndlp,
11401                            struct sli4_wcqe_xri_aborted *axri)
11402 {
11403         uint32_t ext_status = 0;
11404
11405         if (!ndlp) {
11406                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11407                                 "3115 Node Context not found, driver "
11408                                 "ignoring abts err event\n");
11409                 return;
11410         }
11411
11412         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11413                         "3116 Port generated FCP XRI ABORT event on "
11414                         "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
11415                         ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
11416                         bf_get(lpfc_wcqe_xa_xri, axri),
11417                         bf_get(lpfc_wcqe_xa_status, axri),
11418                         axri->parameter);
11419
11420         /*
11421          * Catch the ABTS protocol failure case.  Older OCe FW releases returned
11422          * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
11423          * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
11424          */
11425         ext_status = axri->parameter & IOERR_PARAM_MASK;
11426         if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
11427             ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
11428                 lpfc_sli_post_recovery_event(phba, ndlp);
11429 }
11430
11431 /**
11432  * lpfc_sli_async_event_handler - ASYNC iocb handler function
11433  * @phba: Pointer to HBA context object.
11434  * @pring: Pointer to driver SLI ring object.
11435  * @iocbq: Pointer to iocb object.
11436  *
11437  * This function is called by the slow ring event handler
11438  * function when there is an ASYNC event iocb in the ring.
11439  * This function is called with no lock held.
11440  * Currently this function handles only temperature related
11441  * ASYNC events. The function decodes the temperature sensor
11442  * event message and posts events for the management applications.
11443  **/
11444 static void
11445 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
11446         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
11447 {
11448         IOCB_t *icmd;
11449         uint16_t evt_code;
11450         struct temp_event temp_event_data;
11451         struct Scsi_Host *shost;
11452         uint32_t *iocb_w;
11453
11454         icmd = &iocbq->iocb;
11455         evt_code = icmd->un.asyncstat.evt_code;
11456
11457         switch (evt_code) {
11458         case ASYNC_TEMP_WARN:
11459         case ASYNC_TEMP_SAFE:
11460                 temp_event_data.data = (uint32_t) icmd->ulpContext;
11461                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
11462                 if (evt_code == ASYNC_TEMP_WARN) {
11463                         temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
11464                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11465                                 "0347 Adapter is very hot, please take "
11466                                 "corrective action. temperature : %d Celsius\n",
11467                                 (uint32_t) icmd->ulpContext);
11468                 } else {
11469                         temp_event_data.event_code = LPFC_NORMAL_TEMP;
11470                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11471                                 "0340 Adapter temperature is OK now. "
11472                                 "temperature : %d Celsius\n",
11473                                 (uint32_t) icmd->ulpContext);
11474                 }
11475
11476                 /* Send temperature change event to applications */
11477                 shost = lpfc_shost_from_vport(phba->pport);
11478                 fc_host_post_vendor_event(shost, fc_get_event_number(),
11479                         sizeof(temp_event_data), (char *) &temp_event_data,
11480                         LPFC_NL_VENDOR_ID);
11481                 break;
11482         case ASYNC_STATUS_CN:
11483                 lpfc_sli_abts_err_handler(phba, iocbq);
11484                 break;
11485         default:
11486                 iocb_w = (uint32_t *) icmd;
11487                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11488                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
11489                         " evt_code 0x%x\n"
11490                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
11491                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
11492                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
11493                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
11494                         pring->ringno, icmd->un.asyncstat.evt_code,
11495                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
11496                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
11497                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
11498                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
11499
11500                 break;
11501         }
11502 }
11503
11504
11505 /**
11506  * lpfc_sli4_setup - SLI ring setup function
11507  * @phba: Pointer to HBA context object.
11508  *
11509  * lpfc_sli_setup sets up rings of the SLI interface with
11510  * number of iocbs per ring and iotags. This function is
11511  * called while driver attach to the HBA and before the
11512  * interrupts are enabled. So there is no need for locking.
11513  *
11514  * This function always returns 0.
11515  **/
11516 int
11517 lpfc_sli4_setup(struct lpfc_hba *phba)
11518 {
11519         struct lpfc_sli_ring *pring;
11520
11521         pring = phba->sli4_hba.els_wq->pring;
11522         pring->num_mask = LPFC_MAX_RING_MASK;
11523         pring->prt[0].profile = 0;      /* Mask 0 */
11524         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11525         pring->prt[0].type = FC_TYPE_ELS;
11526         pring->prt[0].lpfc_sli_rcv_unsol_event =
11527             lpfc_els_unsol_event;
11528         pring->prt[1].profile = 0;      /* Mask 1 */
11529         pring->prt[1].rctl = FC_RCTL_ELS_REP;
11530         pring->prt[1].type = FC_TYPE_ELS;
11531         pring->prt[1].lpfc_sli_rcv_unsol_event =
11532             lpfc_els_unsol_event;
11533         pring->prt[2].profile = 0;      /* Mask 2 */
11534         /* NameServer Inquiry */
11535         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11536         /* NameServer */
11537         pring->prt[2].type = FC_TYPE_CT;
11538         pring->prt[2].lpfc_sli_rcv_unsol_event =
11539             lpfc_ct_unsol_event;
11540         pring->prt[3].profile = 0;      /* Mask 3 */
11541         /* NameServer response */
11542         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11543         /* NameServer */
11544         pring->prt[3].type = FC_TYPE_CT;
11545         pring->prt[3].lpfc_sli_rcv_unsol_event =
11546             lpfc_ct_unsol_event;
11547         return 0;
11548 }
11549
11550 /**
11551  * lpfc_sli_setup - SLI ring setup function
11552  * @phba: Pointer to HBA context object.
11553  *
11554  * lpfc_sli_setup sets up rings of the SLI interface with
11555  * number of iocbs per ring and iotags. This function is
11556  * called while driver attach to the HBA and before the
11557  * interrupts are enabled. So there is no need for locking.
11558  *
11559  * This function always returns 0. SLI3 only.
11560  **/
11561 int
11562 lpfc_sli_setup(struct lpfc_hba *phba)
11563 {
11564         int i, totiocbsize = 0;
11565         struct lpfc_sli *psli = &phba->sli;
11566         struct lpfc_sli_ring *pring;
11567
11568         psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
11569         psli->sli_flag = 0;
11570
11571         psli->iocbq_lookup = NULL;
11572         psli->iocbq_lookup_len = 0;
11573         psli->last_iotag = 0;
11574
11575         for (i = 0; i < psli->num_rings; i++) {
11576                 pring = &psli->sli3_ring[i];
11577                 switch (i) {
11578                 case LPFC_FCP_RING:     /* ring 0 - FCP */
11579                         /* numCiocb and numRiocb are used in config_port */
11580                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
11581                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
11582                         pring->sli.sli3.numCiocb +=
11583                                 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11584                         pring->sli.sli3.numRiocb +=
11585                                 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11586                         pring->sli.sli3.numCiocb +=
11587                                 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11588                         pring->sli.sli3.numRiocb +=
11589                                 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11590                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11591                                                         SLI3_IOCB_CMD_SIZE :
11592                                                         SLI2_IOCB_CMD_SIZE;
11593                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11594                                                         SLI3_IOCB_RSP_SIZE :
11595                                                         SLI2_IOCB_RSP_SIZE;
11596                         pring->iotag_ctr = 0;
11597                         pring->iotag_max =
11598                             (phba->cfg_hba_queue_depth * 2);
11599                         pring->fast_iotag = pring->iotag_max;
11600                         pring->num_mask = 0;
11601                         break;
11602                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
11603                         /* numCiocb and numRiocb are used in config_port */
11604                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
11605                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
11606                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11607                                                         SLI3_IOCB_CMD_SIZE :
11608                                                         SLI2_IOCB_CMD_SIZE;
11609                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11610                                                         SLI3_IOCB_RSP_SIZE :
11611                                                         SLI2_IOCB_RSP_SIZE;
11612                         pring->iotag_max = phba->cfg_hba_queue_depth;
11613                         pring->num_mask = 0;
11614                         break;
11615                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
11616                         /* numCiocb and numRiocb are used in config_port */
11617                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
11618                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
11619                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11620                                                         SLI3_IOCB_CMD_SIZE :
11621                                                         SLI2_IOCB_CMD_SIZE;
11622                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11623                                                         SLI3_IOCB_RSP_SIZE :
11624                                                         SLI2_IOCB_RSP_SIZE;
11625                         pring->fast_iotag = 0;
11626                         pring->iotag_ctr = 0;
11627                         pring->iotag_max = 4096;
11628                         pring->lpfc_sli_rcv_async_status =
11629                                 lpfc_sli_async_event_handler;
11630                         pring->num_mask = LPFC_MAX_RING_MASK;
11631                         pring->prt[0].profile = 0;      /* Mask 0 */
11632                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11633                         pring->prt[0].type = FC_TYPE_ELS;
11634                         pring->prt[0].lpfc_sli_rcv_unsol_event =
11635                             lpfc_els_unsol_event;
11636                         pring->prt[1].profile = 0;      /* Mask 1 */
11637                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
11638                         pring->prt[1].type = FC_TYPE_ELS;
11639                         pring->prt[1].lpfc_sli_rcv_unsol_event =
11640                             lpfc_els_unsol_event;
11641                         pring->prt[2].profile = 0;      /* Mask 2 */
11642                         /* NameServer Inquiry */
11643                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11644                         /* NameServer */
11645                         pring->prt[2].type = FC_TYPE_CT;
11646                         pring->prt[2].lpfc_sli_rcv_unsol_event =
11647                             lpfc_ct_unsol_event;
11648                         pring->prt[3].profile = 0;      /* Mask 3 */
11649                         /* NameServer response */
11650                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11651                         /* NameServer */
11652                         pring->prt[3].type = FC_TYPE_CT;
11653                         pring->prt[3].lpfc_sli_rcv_unsol_event =
11654                             lpfc_ct_unsol_event;
11655                         break;
11656                 }
11657                 totiocbsize += (pring->sli.sli3.numCiocb *
11658                         pring->sli.sli3.sizeCiocb) +
11659                         (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
11660         }
11661         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
11662                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
11663                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
11664                        "SLI2 SLIM Data: x%x x%lx\n",
11665                        phba->brd_no, totiocbsize,
11666                        (unsigned long) MAX_SLIM_IOCB_SIZE);
11667         }
11668         if (phba->cfg_multi_ring_support == 2)
11669                 lpfc_extra_ring_setup(phba);
11670
11671         return 0;
11672 }
11673
11674 /**
11675  * lpfc_sli4_queue_init - Queue initialization function
11676  * @phba: Pointer to HBA context object.
11677  *
11678  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
11679  * ring. This function also initializes ring indices of each ring.
11680  * This function is called during the initialization of the SLI
11681  * interface of an HBA.
11682  * This function is called with no lock held and always returns
11683  * 1.
11684  **/
11685 void
11686 lpfc_sli4_queue_init(struct lpfc_hba *phba)
11687 {
11688         struct lpfc_sli *psli;
11689         struct lpfc_sli_ring *pring;
11690         int i;
11691
11692         psli = &phba->sli;
11693         spin_lock_irq(&phba->hbalock);
11694         INIT_LIST_HEAD(&psli->mboxq);
11695         INIT_LIST_HEAD(&psli->mboxq_cmpl);
11696         /* Initialize list headers for txq and txcmplq as double linked lists */
11697         for (i = 0; i < phba->cfg_hdw_queue; i++) {
11698                 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
11699                 pring->flag = 0;
11700                 pring->ringno = LPFC_FCP_RING;
11701                 pring->txcmplq_cnt = 0;
11702                 INIT_LIST_HEAD(&pring->txq);
11703                 INIT_LIST_HEAD(&pring->txcmplq);
11704                 INIT_LIST_HEAD(&pring->iocb_continueq);
11705                 spin_lock_init(&pring->ring_lock);
11706         }
11707         pring = phba->sli4_hba.els_wq->pring;
11708         pring->flag = 0;
11709         pring->ringno = LPFC_ELS_RING;
11710         pring->txcmplq_cnt = 0;
11711         INIT_LIST_HEAD(&pring->txq);
11712         INIT_LIST_HEAD(&pring->txcmplq);
11713         INIT_LIST_HEAD(&pring->iocb_continueq);
11714         spin_lock_init(&pring->ring_lock);
11715
11716         if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11717                 pring = phba->sli4_hba.nvmels_wq->pring;
11718                 pring->flag = 0;
11719                 pring->ringno = LPFC_ELS_RING;
11720                 pring->txcmplq_cnt = 0;
11721                 INIT_LIST_HEAD(&pring->txq);
11722                 INIT_LIST_HEAD(&pring->txcmplq);
11723                 INIT_LIST_HEAD(&pring->iocb_continueq);
11724                 spin_lock_init(&pring->ring_lock);
11725         }
11726
11727         spin_unlock_irq(&phba->hbalock);
11728 }
11729
11730 /**
11731  * lpfc_sli_queue_init - Queue initialization function
11732  * @phba: Pointer to HBA context object.
11733  *
11734  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
11735  * ring. This function also initializes ring indices of each ring.
11736  * This function is called during the initialization of the SLI
11737  * interface of an HBA.
11738  * This function is called with no lock held and always returns
11739  * 1.
11740  **/
11741 void
11742 lpfc_sli_queue_init(struct lpfc_hba *phba)
11743 {
11744         struct lpfc_sli *psli;
11745         struct lpfc_sli_ring *pring;
11746         int i;
11747
11748         psli = &phba->sli;
11749         spin_lock_irq(&phba->hbalock);
11750         INIT_LIST_HEAD(&psli->mboxq);
11751         INIT_LIST_HEAD(&psli->mboxq_cmpl);
11752         /* Initialize list headers for txq and txcmplq as double linked lists */
11753         for (i = 0; i < psli->num_rings; i++) {
11754                 pring = &psli->sli3_ring[i];
11755                 pring->ringno = i;
11756                 pring->sli.sli3.next_cmdidx  = 0;
11757                 pring->sli.sli3.local_getidx = 0;
11758                 pring->sli.sli3.cmdidx = 0;
11759                 INIT_LIST_HEAD(&pring->iocb_continueq);
11760                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
11761                 INIT_LIST_HEAD(&pring->postbufq);
11762                 pring->flag = 0;
11763                 INIT_LIST_HEAD(&pring->txq);
11764                 INIT_LIST_HEAD(&pring->txcmplq);
11765                 spin_lock_init(&pring->ring_lock);
11766         }
11767         spin_unlock_irq(&phba->hbalock);
11768 }
11769
11770 /**
11771  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
11772  * @phba: Pointer to HBA context object.
11773  *
11774  * This routine flushes the mailbox command subsystem. It will unconditionally
11775  * flush all the mailbox commands in the three possible stages in the mailbox
11776  * command sub-system: pending mailbox command queue; the outstanding mailbox
11777  * command; and completed mailbox command queue. It is caller's responsibility
11778  * to make sure that the driver is in the proper state to flush the mailbox
11779  * command sub-system. Namely, the posting of mailbox commands into the
11780  * pending mailbox command queue from the various clients must be stopped;
11781  * either the HBA is in a state that it will never works on the outstanding
11782  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
11783  * mailbox command has been completed.
11784  **/
11785 static void
11786 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
11787 {
11788         LIST_HEAD(completions);
11789         struct lpfc_sli *psli = &phba->sli;
11790         LPFC_MBOXQ_t *pmb;
11791         unsigned long iflag;
11792
11793         /* Disable softirqs, including timers from obtaining phba->hbalock */
11794         local_bh_disable();
11795
11796         /* Flush all the mailbox commands in the mbox system */
11797         spin_lock_irqsave(&phba->hbalock, iflag);
11798
11799         /* The pending mailbox command queue */
11800         list_splice_init(&phba->sli.mboxq, &completions);
11801         /* The outstanding active mailbox command */
11802         if (psli->mbox_active) {
11803                 list_add_tail(&psli->mbox_active->list, &completions);
11804                 psli->mbox_active = NULL;
11805                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11806         }
11807         /* The completed mailbox command queue */
11808         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
11809         spin_unlock_irqrestore(&phba->hbalock, iflag);
11810
11811         /* Enable softirqs again, done with phba->hbalock */
11812         local_bh_enable();
11813
11814         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
11815         while (!list_empty(&completions)) {
11816                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
11817                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
11818                 if (pmb->mbox_cmpl)
11819                         pmb->mbox_cmpl(phba, pmb);
11820         }
11821 }
11822
11823 /**
11824  * lpfc_sli_host_down - Vport cleanup function
11825  * @vport: Pointer to virtual port object.
11826  *
11827  * lpfc_sli_host_down is called to clean up the resources
11828  * associated with a vport before destroying virtual
11829  * port data structures.
11830  * This function does following operations:
11831  * - Free discovery resources associated with this virtual
11832  *   port.
11833  * - Free iocbs associated with this virtual port in
11834  *   the txq.
11835  * - Send abort for all iocb commands associated with this
11836  *   vport in txcmplq.
11837  *
11838  * This function is called with no lock held and always returns 1.
11839  **/
11840 int
11841 lpfc_sli_host_down(struct lpfc_vport *vport)
11842 {
11843         LIST_HEAD(completions);
11844         struct lpfc_hba *phba = vport->phba;
11845         struct lpfc_sli *psli = &phba->sli;
11846         struct lpfc_queue *qp = NULL;
11847         struct lpfc_sli_ring *pring;
11848         struct lpfc_iocbq *iocb, *next_iocb;
11849         int i;
11850         unsigned long flags = 0;
11851         uint16_t prev_pring_flag;
11852
11853         lpfc_cleanup_discovery_resources(vport);
11854
11855         spin_lock_irqsave(&phba->hbalock, flags);
11856
11857         /*
11858          * Error everything on the txq since these iocbs
11859          * have not been given to the FW yet.
11860          * Also issue ABTS for everything on the txcmplq
11861          */
11862         if (phba->sli_rev != LPFC_SLI_REV4) {
11863                 for (i = 0; i < psli->num_rings; i++) {
11864                         pring = &psli->sli3_ring[i];
11865                         prev_pring_flag = pring->flag;
11866                         /* Only slow rings */
11867                         if (pring->ringno == LPFC_ELS_RING) {
11868                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11869                                 /* Set the lpfc data pending flag */
11870                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
11871                         }
11872                         list_for_each_entry_safe(iocb, next_iocb,
11873                                                  &pring->txq, list) {
11874                                 if (iocb->vport != vport)
11875                                         continue;
11876                                 list_move_tail(&iocb->list, &completions);
11877                         }
11878                         list_for_each_entry_safe(iocb, next_iocb,
11879                                                  &pring->txcmplq, list) {
11880                                 if (iocb->vport != vport)
11881                                         continue;
11882                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11883                                                            NULL);
11884                         }
11885                         pring->flag = prev_pring_flag;
11886                 }
11887         } else {
11888                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11889                         pring = qp->pring;
11890                         if (!pring)
11891                                 continue;
11892                         if (pring == phba->sli4_hba.els_wq->pring) {
11893                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11894                                 /* Set the lpfc data pending flag */
11895                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
11896                         }
11897                         prev_pring_flag = pring->flag;
11898                         spin_lock(&pring->ring_lock);
11899                         list_for_each_entry_safe(iocb, next_iocb,
11900                                                  &pring->txq, list) {
11901                                 if (iocb->vport != vport)
11902                                         continue;
11903                                 list_move_tail(&iocb->list, &completions);
11904                         }
11905                         spin_unlock(&pring->ring_lock);
11906                         list_for_each_entry_safe(iocb, next_iocb,
11907                                                  &pring->txcmplq, list) {
11908                                 if (iocb->vport != vport)
11909                                         continue;
11910                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11911                                                            NULL);
11912                         }
11913                         pring->flag = prev_pring_flag;
11914                 }
11915         }
11916         spin_unlock_irqrestore(&phba->hbalock, flags);
11917
11918         /* Make sure HBA is alive */
11919         lpfc_issue_hb_tmo(phba);
11920
11921         /* Cancel all the IOCBs from the completions list */
11922         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11923                               IOERR_SLI_DOWN);
11924         return 1;
11925 }
11926
11927 /**
11928  * lpfc_sli_hba_down - Resource cleanup function for the HBA
11929  * @phba: Pointer to HBA context object.
11930  *
11931  * This function cleans up all iocb, buffers, mailbox commands
11932  * while shutting down the HBA. This function is called with no
11933  * lock held and always returns 1.
11934  * This function does the following to cleanup driver resources:
11935  * - Free discovery resources for each virtual port
11936  * - Cleanup any pending fabric iocbs
11937  * - Iterate through the iocb txq and free each entry
11938  *   in the list.
11939  * - Free up any buffer posted to the HBA
11940  * - Free mailbox commands in the mailbox queue.
11941  **/
11942 int
11943 lpfc_sli_hba_down(struct lpfc_hba *phba)
11944 {
11945         LIST_HEAD(completions);
11946         struct lpfc_sli *psli = &phba->sli;
11947         struct lpfc_queue *qp = NULL;
11948         struct lpfc_sli_ring *pring;
11949         struct lpfc_dmabuf *buf_ptr;
11950         unsigned long flags = 0;
11951         int i;
11952
11953         /* Shutdown the mailbox command sub-system */
11954         lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
11955
11956         lpfc_hba_down_prep(phba);
11957
11958         /* Disable softirqs, including timers from obtaining phba->hbalock */
11959         local_bh_disable();
11960
11961         lpfc_fabric_abort_hba(phba);
11962
11963         spin_lock_irqsave(&phba->hbalock, flags);
11964
11965         /*
11966          * Error everything on the txq since these iocbs
11967          * have not been given to the FW yet.
11968          */
11969         if (phba->sli_rev != LPFC_SLI_REV4) {
11970                 for (i = 0; i < psli->num_rings; i++) {
11971                         pring = &psli->sli3_ring[i];
11972                         /* Only slow rings */
11973                         if (pring->ringno == LPFC_ELS_RING) {
11974                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11975                                 /* Set the lpfc data pending flag */
11976                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
11977                         }
11978                         list_splice_init(&pring->txq, &completions);
11979                 }
11980         } else {
11981                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11982                         pring = qp->pring;
11983                         if (!pring)
11984                                 continue;
11985                         spin_lock(&pring->ring_lock);
11986                         list_splice_init(&pring->txq, &completions);
11987                         spin_unlock(&pring->ring_lock);
11988                         if (pring == phba->sli4_hba.els_wq->pring) {
11989                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11990                                 /* Set the lpfc data pending flag */
11991                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
11992                         }
11993                 }
11994         }
11995         spin_unlock_irqrestore(&phba->hbalock, flags);
11996
11997         /* Cancel all the IOCBs from the completions list */
11998         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11999                               IOERR_SLI_DOWN);
12000
12001         spin_lock_irqsave(&phba->hbalock, flags);
12002         list_splice_init(&phba->elsbuf, &completions);
12003         phba->elsbuf_cnt = 0;
12004         phba->elsbuf_prev_cnt = 0;
12005         spin_unlock_irqrestore(&phba->hbalock, flags);
12006
12007         while (!list_empty(&completions)) {
12008                 list_remove_head(&completions, buf_ptr,
12009                         struct lpfc_dmabuf, list);
12010                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
12011                 kfree(buf_ptr);
12012         }
12013
12014         /* Enable softirqs again, done with phba->hbalock */
12015         local_bh_enable();
12016
12017         /* Return any active mbox cmds */
12018         del_timer_sync(&psli->mbox_tmo);
12019
12020         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
12021         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12022         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
12023
12024         return 1;
12025 }
12026
12027 /**
12028  * lpfc_sli_pcimem_bcopy - SLI memory copy function
12029  * @srcp: Source memory pointer.
12030  * @destp: Destination memory pointer.
12031  * @cnt: Number of words required to be copied.
12032  *
12033  * This function is used for copying data between driver memory
12034  * and the SLI memory. This function also changes the endianness
12035  * of each word if native endianness is different from SLI
12036  * endianness. This function can be called with or without
12037  * lock.
12038  **/
12039 void
12040 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
12041 {
12042         uint32_t *src = srcp;
12043         uint32_t *dest = destp;
12044         uint32_t ldata;
12045         int i;
12046
12047         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
12048                 ldata = *src;
12049                 ldata = le32_to_cpu(ldata);
12050                 *dest = ldata;
12051                 src++;
12052                 dest++;
12053         }
12054 }
12055
12056
12057 /**
12058  * lpfc_sli_bemem_bcopy - SLI memory copy function
12059  * @srcp: Source memory pointer.
12060  * @destp: Destination memory pointer.
12061  * @cnt: Number of words required to be copied.
12062  *
12063  * This function is used for copying data between a data structure
12064  * with big endian representation to local endianness.
12065  * This function can be called with or without lock.
12066  **/
12067 void
12068 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
12069 {
12070         uint32_t *src = srcp;
12071         uint32_t *dest = destp;
12072         uint32_t ldata;
12073         int i;
12074
12075         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
12076                 ldata = *src;
12077                 ldata = be32_to_cpu(ldata);
12078                 *dest = ldata;
12079                 src++;
12080                 dest++;
12081         }
12082 }
12083
12084 /**
12085  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
12086  * @phba: Pointer to HBA context object.
12087  * @pring: Pointer to driver SLI ring object.
12088  * @mp: Pointer to driver buffer object.
12089  *
12090  * This function is called with no lock held.
12091  * It always return zero after adding the buffer to the postbufq
12092  * buffer list.
12093  **/
12094 int
12095 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12096                          struct lpfc_dmabuf *mp)
12097 {
12098         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
12099            later */
12100         spin_lock_irq(&phba->hbalock);
12101         list_add_tail(&mp->list, &pring->postbufq);
12102         pring->postbufq_cnt++;
12103         spin_unlock_irq(&phba->hbalock);
12104         return 0;
12105 }
12106
12107 /**
12108  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
12109  * @phba: Pointer to HBA context object.
12110  *
12111  * When HBQ is enabled, buffers are searched based on tags. This function
12112  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
12113  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
12114  * does not conflict with tags of buffer posted for unsolicited events.
12115  * The function returns the allocated tag. The function is called with
12116  * no locks held.
12117  **/
12118 uint32_t
12119 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
12120 {
12121         spin_lock_irq(&phba->hbalock);
12122         phba->buffer_tag_count++;
12123         /*
12124          * Always set the QUE_BUFTAG_BIT to distiguish between
12125          * a tag assigned by HBQ.
12126          */
12127         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
12128         spin_unlock_irq(&phba->hbalock);
12129         return phba->buffer_tag_count;
12130 }
12131
12132 /**
12133  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
12134  * @phba: Pointer to HBA context object.
12135  * @pring: Pointer to driver SLI ring object.
12136  * @tag: Buffer tag.
12137  *
12138  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
12139  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
12140  * iocb is posted to the response ring with the tag of the buffer.
12141  * This function searches the pring->postbufq list using the tag
12142  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
12143  * iocb. If the buffer is found then lpfc_dmabuf object of the
12144  * buffer is returned to the caller else NULL is returned.
12145  * This function is called with no lock held.
12146  **/
12147 struct lpfc_dmabuf *
12148 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12149                         uint32_t tag)
12150 {
12151         struct lpfc_dmabuf *mp, *next_mp;
12152         struct list_head *slp = &pring->postbufq;
12153
12154         /* Search postbufq, from the beginning, looking for a match on tag */
12155         spin_lock_irq(&phba->hbalock);
12156         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12157                 if (mp->buffer_tag == tag) {
12158                         list_del_init(&mp->list);
12159                         pring->postbufq_cnt--;
12160                         spin_unlock_irq(&phba->hbalock);
12161                         return mp;
12162                 }
12163         }
12164
12165         spin_unlock_irq(&phba->hbalock);
12166         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12167                         "0402 Cannot find virtual addr for buffer tag on "
12168                         "ring %d Data x%lx x%px x%px x%x\n",
12169                         pring->ringno, (unsigned long) tag,
12170                         slp->next, slp->prev, pring->postbufq_cnt);
12171
12172         return NULL;
12173 }
12174
12175 /**
12176  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
12177  * @phba: Pointer to HBA context object.
12178  * @pring: Pointer to driver SLI ring object.
12179  * @phys: DMA address of the buffer.
12180  *
12181  * This function searches the buffer list using the dma_address
12182  * of unsolicited event to find the driver's lpfc_dmabuf object
12183  * corresponding to the dma_address. The function returns the
12184  * lpfc_dmabuf object if a buffer is found else it returns NULL.
12185  * This function is called by the ct and els unsolicited event
12186  * handlers to get the buffer associated with the unsolicited
12187  * event.
12188  *
12189  * This function is called with no lock held.
12190  **/
12191 struct lpfc_dmabuf *
12192 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12193                          dma_addr_t phys)
12194 {
12195         struct lpfc_dmabuf *mp, *next_mp;
12196         struct list_head *slp = &pring->postbufq;
12197
12198         /* Search postbufq, from the beginning, looking for a match on phys */
12199         spin_lock_irq(&phba->hbalock);
12200         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12201                 if (mp->phys == phys) {
12202                         list_del_init(&mp->list);
12203                         pring->postbufq_cnt--;
12204                         spin_unlock_irq(&phba->hbalock);
12205                         return mp;
12206                 }
12207         }
12208
12209         spin_unlock_irq(&phba->hbalock);
12210         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12211                         "0410 Cannot find virtual addr for mapped buf on "
12212                         "ring %d Data x%llx x%px x%px x%x\n",
12213                         pring->ringno, (unsigned long long)phys,
12214                         slp->next, slp->prev, pring->postbufq_cnt);
12215         return NULL;
12216 }
12217
12218 /**
12219  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
12220  * @phba: Pointer to HBA context object.
12221  * @cmdiocb: Pointer to driver command iocb object.
12222  * @rspiocb: Pointer to driver response iocb object.
12223  *
12224  * This function is the completion handler for the abort iocbs for
12225  * ELS commands. This function is called from the ELS ring event
12226  * handler with no lock held. This function frees memory resources
12227  * associated with the abort iocb.
12228  **/
12229 static void
12230 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12231                         struct lpfc_iocbq *rspiocb)
12232 {
12233         IOCB_t *irsp = &rspiocb->iocb;
12234         uint16_t abort_iotag, abort_context;
12235         struct lpfc_iocbq *abort_iocb = NULL;
12236
12237         if (irsp->ulpStatus) {
12238
12239                 /*
12240                  * Assume that the port already completed and returned, or
12241                  * will return the iocb. Just Log the message.
12242                  */
12243                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
12244                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
12245
12246                 spin_lock_irq(&phba->hbalock);
12247                 if (phba->sli_rev < LPFC_SLI_REV4) {
12248                         if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
12249                             irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
12250                             irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
12251                                 spin_unlock_irq(&phba->hbalock);
12252                                 goto release_iocb;
12253                         }
12254                         if (abort_iotag != 0 &&
12255                                 abort_iotag <= phba->sli.last_iotag)
12256                                 abort_iocb =
12257                                         phba->sli.iocbq_lookup[abort_iotag];
12258                 } else
12259                         /* For sli4 the abort_tag is the XRI,
12260                          * so the abort routine puts the iotag  of the iocb
12261                          * being aborted in the context field of the abort
12262                          * IOCB.
12263                          */
12264                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
12265
12266                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
12267                                 "0327 Cannot abort els iocb x%px "
12268                                 "with tag %x context %x, abort status %x, "
12269                                 "abort code %x\n",
12270                                 abort_iocb, abort_iotag, abort_context,
12271                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
12272
12273                 spin_unlock_irq(&phba->hbalock);
12274         }
12275 release_iocb:
12276         lpfc_sli_release_iocbq(phba, cmdiocb);
12277         return;
12278 }
12279
12280 /**
12281  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
12282  * @phba: Pointer to HBA context object.
12283  * @cmdiocb: Pointer to driver command iocb object.
12284  * @rspiocb: Pointer to driver response iocb object.
12285  *
12286  * The function is called from SLI ring event handler with no
12287  * lock held. This function is the completion handler for ELS commands
12288  * which are aborted. The function frees memory resources used for
12289  * the aborted ELS commands.
12290  **/
12291 void
12292 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12293                      struct lpfc_iocbq *rspiocb)
12294 {
12295         struct lpfc_nodelist *ndlp = NULL;
12296         IOCB_t *irsp = &rspiocb->iocb;
12297
12298         /* ELS cmd tag <ulpIoTag> completes */
12299         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
12300                         "0139 Ignoring ELS cmd code x%x completion Data: "
12301                         "x%x x%x x%x\n",
12302                         irsp->ulpIoTag, irsp->ulpStatus,
12303                         irsp->un.ulpWord[4], irsp->ulpTimeout);
12304         /*
12305          * Deref the ndlp after free_iocb. sli_release_iocb will access the ndlp
12306          * if exchange is busy.
12307          */
12308         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
12309                 ndlp = cmdiocb->context_un.ndlp;
12310                 lpfc_ct_free_iocb(phba, cmdiocb);
12311         } else {
12312                 ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
12313                 lpfc_els_free_iocb(phba, cmdiocb);
12314         }
12315
12316         lpfc_nlp_put(ndlp);
12317 }
12318
12319 /**
12320  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
12321  * @phba: Pointer to HBA context object.
12322  * @pring: Pointer to driver SLI ring object.
12323  * @cmdiocb: Pointer to driver command iocb object.
12324  * @cmpl: completion function.
12325  *
12326  * This function issues an abort iocb for the provided command iocb. In case
12327  * of unloading, the abort iocb will not be issued to commands on the ELS
12328  * ring. Instead, the callback function shall be changed to those commands
12329  * so that nothing happens when them finishes. This function is called with
12330  * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS
12331  * when the command iocb is an abort request.
12332  *
12333  **/
12334 int
12335 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12336                            struct lpfc_iocbq *cmdiocb, void *cmpl)
12337 {
12338         struct lpfc_vport *vport = cmdiocb->vport;
12339         struct lpfc_iocbq *abtsiocbp;
12340         IOCB_t *icmd = NULL;
12341         IOCB_t *iabt = NULL;
12342         int retval = IOCB_ERROR;
12343         unsigned long iflags;
12344         struct lpfc_nodelist *ndlp;
12345
12346         /*
12347          * There are certain command types we don't want to abort.  And we
12348          * don't want to abort commands that are already in the process of
12349          * being aborted.
12350          */
12351         icmd = &cmdiocb->iocb;
12352         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
12353             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
12354             cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED)
12355                 return IOCB_ABORTING;
12356
12357         if (!pring) {
12358                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
12359                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
12360                 else
12361                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
12362                 return retval;
12363         }
12364
12365         /*
12366          * If we're unloading, don't abort iocb on the ELS ring, but change
12367          * the callback so that nothing happens when it finishes.
12368          */
12369         if ((vport->load_flag & FC_UNLOADING) &&
12370             pring->ringno == LPFC_ELS_RING) {
12371                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
12372                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
12373                 else
12374                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
12375                 return retval;
12376         }
12377
12378         /* issue ABTS for this IOCB based on iotag */
12379         abtsiocbp = __lpfc_sli_get_iocbq(phba);
12380         if (abtsiocbp == NULL)
12381                 return IOCB_NORESOURCE;
12382
12383         /* This signals the response to set the correct status
12384          * before calling the completion handler
12385          */
12386         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
12387
12388         iabt = &abtsiocbp->iocb;
12389         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
12390         iabt->un.acxri.abortContextTag = icmd->ulpContext;
12391         if (phba->sli_rev == LPFC_SLI_REV4) {
12392                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
12393                 if (pring->ringno == LPFC_ELS_RING)
12394                         iabt->un.acxri.abortContextTag = cmdiocb->iotag;
12395         } else {
12396                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
12397                 if (pring->ringno == LPFC_ELS_RING) {
12398                         ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
12399                         iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
12400                 }
12401         }
12402         iabt->ulpLe = 1;
12403         iabt->ulpClass = icmd->ulpClass;
12404
12405         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
12406         abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
12407         if (cmdiocb->iocb_flag & LPFC_IO_FCP) {
12408                 abtsiocbp->iocb_flag |= LPFC_IO_FCP;
12409                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
12410         }
12411         if (cmdiocb->iocb_flag & LPFC_IO_FOF)
12412                 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
12413
12414         if (phba->link_state >= LPFC_LINK_UP)
12415                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
12416         else
12417                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
12418
12419         if (cmpl)
12420                 abtsiocbp->iocb_cmpl = cmpl;
12421         else
12422                 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
12423         abtsiocbp->vport = vport;
12424
12425         if (phba->sli_rev == LPFC_SLI_REV4) {
12426                 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
12427                 if (unlikely(pring == NULL))
12428                         goto abort_iotag_exit;
12429                 /* Note: both hbalock and ring_lock need to be set here */
12430                 spin_lock_irqsave(&pring->ring_lock, iflags);
12431                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12432                         abtsiocbp, 0);
12433                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12434         } else {
12435                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12436                         abtsiocbp, 0);
12437         }
12438
12439 abort_iotag_exit:
12440
12441         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
12442                          "0339 Abort xri x%x, original iotag x%x, "
12443                          "abort cmd iotag x%x retval x%x\n",
12444                          iabt->un.acxri.abortIoTag,
12445                          iabt->un.acxri.abortContextTag,
12446                          abtsiocbp->iotag, retval);
12447
12448         if (retval) {
12449                 cmdiocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12450                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
12451         }
12452
12453         /*
12454          * Caller to this routine should check for IOCB_ERROR
12455          * and handle it properly.  This routine no longer removes
12456          * iocb off txcmplq and call compl in case of IOCB_ERROR.
12457          */
12458         return retval;
12459 }
12460
12461 /**
12462  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
12463  * @phba: pointer to lpfc HBA data structure.
12464  *
12465  * This routine will abort all pending and outstanding iocbs to an HBA.
12466  **/
12467 void
12468 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
12469 {
12470         struct lpfc_sli *psli = &phba->sli;
12471         struct lpfc_sli_ring *pring;
12472         struct lpfc_queue *qp = NULL;
12473         int i;
12474
12475         if (phba->sli_rev != LPFC_SLI_REV4) {
12476                 for (i = 0; i < psli->num_rings; i++) {
12477                         pring = &psli->sli3_ring[i];
12478                         lpfc_sli_abort_iocb_ring(phba, pring);
12479                 }
12480                 return;
12481         }
12482         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12483                 pring = qp->pring;
12484                 if (!pring)
12485                         continue;
12486                 lpfc_sli_abort_iocb_ring(phba, pring);
12487         }
12488 }
12489
12490 /**
12491  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
12492  * @iocbq: Pointer to driver iocb object.
12493  * @vport: Pointer to driver virtual port object.
12494  * @tgt_id: SCSI ID of the target.
12495  * @lun_id: LUN ID of the scsi device.
12496  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
12497  *
12498  * This function acts as an iocb filter for functions which abort or count
12499  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
12500  * 0 if the filtering criteria is met for the given iocb and will return
12501  * 1 if the filtering criteria is not met.
12502  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
12503  * given iocb is for the SCSI device specified by vport, tgt_id and
12504  * lun_id parameter.
12505  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
12506  * given iocb is for the SCSI target specified by vport and tgt_id
12507  * parameters.
12508  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
12509  * given iocb is for the SCSI host associated with the given vport.
12510  * This function is called with no locks held.
12511  **/
12512 static int
12513 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
12514                            uint16_t tgt_id, uint64_t lun_id,
12515                            lpfc_ctx_cmd ctx_cmd)
12516 {
12517         struct lpfc_io_buf *lpfc_cmd;
12518         IOCB_t *icmd = NULL;
12519         int rc = 1;
12520
12521         if (!iocbq || iocbq->vport != vport)
12522                 return rc;
12523
12524         if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
12525             !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ) ||
12526               iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
12527                 return rc;
12528
12529         icmd = &iocbq->iocb;
12530         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
12531             icmd->ulpCommand == CMD_CLOSE_XRI_CN)
12532                 return rc;
12533
12534         lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12535
12536         if (lpfc_cmd->pCmd == NULL)
12537                 return rc;
12538
12539         switch (ctx_cmd) {
12540         case LPFC_CTX_LUN:
12541                 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12542                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
12543                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
12544                         rc = 0;
12545                 break;
12546         case LPFC_CTX_TGT:
12547                 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12548                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
12549                         rc = 0;
12550                 break;
12551         case LPFC_CTX_HOST:
12552                 rc = 0;
12553                 break;
12554         default:
12555                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
12556                         __func__, ctx_cmd);
12557                 break;
12558         }
12559
12560         return rc;
12561 }
12562
12563 /**
12564  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
12565  * @vport: Pointer to virtual port.
12566  * @tgt_id: SCSI ID of the target.
12567  * @lun_id: LUN ID of the scsi device.
12568  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12569  *
12570  * This function returns number of FCP commands pending for the vport.
12571  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
12572  * commands pending on the vport associated with SCSI device specified
12573  * by tgt_id and lun_id parameters.
12574  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
12575  * commands pending on the vport associated with SCSI target specified
12576  * by tgt_id parameter.
12577  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
12578  * commands pending on the vport.
12579  * This function returns the number of iocbs which satisfy the filter.
12580  * This function is called without any lock held.
12581  **/
12582 int
12583 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
12584                   lpfc_ctx_cmd ctx_cmd)
12585 {
12586         struct lpfc_hba *phba = vport->phba;
12587         struct lpfc_iocbq *iocbq;
12588         int sum, i;
12589
12590         spin_lock_irq(&phba->hbalock);
12591         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
12592                 iocbq = phba->sli.iocbq_lookup[i];
12593
12594                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
12595                                                 ctx_cmd) == 0)
12596                         sum++;
12597         }
12598         spin_unlock_irq(&phba->hbalock);
12599
12600         return sum;
12601 }
12602
12603 /**
12604  * lpfc_sli4_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
12605  * @phba: Pointer to HBA context object
12606  * @cmdiocb: Pointer to command iocb object.
12607  * @wcqe: pointer to the complete wcqe
12608  *
12609  * This function is called when an aborted FCP iocb completes. This
12610  * function is called by the ring event handler with no lock held.
12611  * This function frees the iocb. It is called for sli-4 adapters.
12612  **/
12613 void
12614 lpfc_sli4_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12615                          struct lpfc_wcqe_complete *wcqe)
12616 {
12617         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12618                         "3017 ABORT_XRI_CN completing on rpi x%x "
12619                         "original iotag x%x, abort cmd iotag x%x "
12620                         "status 0x%x, reason 0x%x\n",
12621                         cmdiocb->iocb.un.acxri.abortContextTag,
12622                         cmdiocb->iocb.un.acxri.abortIoTag,
12623                         cmdiocb->iotag,
12624                         (bf_get(lpfc_wcqe_c_status, wcqe)
12625                         & LPFC_IOCB_STATUS_MASK),
12626                         wcqe->parameter);
12627         lpfc_sli_release_iocbq(phba, cmdiocb);
12628 }
12629
12630 /**
12631  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
12632  * @phba: Pointer to HBA context object
12633  * @cmdiocb: Pointer to command iocb object.
12634  * @rspiocb: Pointer to response iocb object.
12635  *
12636  * This function is called when an aborted FCP iocb completes. This
12637  * function is called by the ring event handler with no lock held.
12638  * This function frees the iocb.
12639  **/
12640 void
12641 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12642                         struct lpfc_iocbq *rspiocb)
12643 {
12644         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12645                         "3096 ABORT_XRI_CN completing on rpi x%x "
12646                         "original iotag x%x, abort cmd iotag x%x "
12647                         "status 0x%x, reason 0x%x\n",
12648                         cmdiocb->iocb.un.acxri.abortContextTag,
12649                         cmdiocb->iocb.un.acxri.abortIoTag,
12650                         cmdiocb->iotag, rspiocb->iocb.ulpStatus,
12651                         rspiocb->iocb.un.ulpWord[4]);
12652         lpfc_sli_release_iocbq(phba, cmdiocb);
12653         return;
12654 }
12655
12656 /**
12657  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
12658  * @vport: Pointer to virtual port.
12659  * @tgt_id: SCSI ID of the target.
12660  * @lun_id: LUN ID of the scsi device.
12661  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12662  *
12663  * This function sends an abort command for every SCSI command
12664  * associated with the given virtual port pending on the ring
12665  * filtered by lpfc_sli_validate_fcp_iocb function.
12666  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
12667  * FCP iocbs associated with lun specified by tgt_id and lun_id
12668  * parameters
12669  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
12670  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12671  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
12672  * FCP iocbs associated with virtual port.
12673  * The pring used for SLI3 is sli3_ring[LPFC_FCP_RING], for SLI4
12674  * lpfc_sli4_calc_ring is used.
12675  * This function returns number of iocbs it failed to abort.
12676  * This function is called with no locks held.
12677  **/
12678 int
12679 lpfc_sli_abort_iocb(struct lpfc_vport *vport, u16 tgt_id, u64 lun_id,
12680                     lpfc_ctx_cmd abort_cmd)
12681 {
12682         struct lpfc_hba *phba = vport->phba;
12683         struct lpfc_sli_ring *pring = NULL;
12684         struct lpfc_iocbq *iocbq;
12685         int errcnt = 0, ret_val = 0;
12686         unsigned long iflags;
12687         int i;
12688         void *fcp_cmpl = NULL;
12689
12690         /* all I/Os are in process of being flushed */
12691         if (phba->hba_flag & HBA_IOQ_FLUSH)
12692                 return errcnt;
12693
12694         for (i = 1; i <= phba->sli.last_iotag; i++) {
12695                 iocbq = phba->sli.iocbq_lookup[i];
12696
12697                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12698                                                abort_cmd) != 0)
12699                         continue;
12700
12701                 spin_lock_irqsave(&phba->hbalock, iflags);
12702                 if (phba->sli_rev == LPFC_SLI_REV3) {
12703                         pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12704                         fcp_cmpl = lpfc_sli_abort_fcp_cmpl;
12705                 } else if (phba->sli_rev == LPFC_SLI_REV4) {
12706                         pring = lpfc_sli4_calc_ring(phba, iocbq);
12707                         fcp_cmpl = lpfc_sli4_abort_fcp_cmpl;
12708                 }
12709                 ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,
12710                                                      fcp_cmpl);
12711                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12712                 if (ret_val != IOCB_SUCCESS)
12713                         errcnt++;
12714         }
12715
12716         return errcnt;
12717 }
12718
12719 /**
12720  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
12721  * @vport: Pointer to virtual port.
12722  * @pring: Pointer to driver SLI ring object.
12723  * @tgt_id: SCSI ID of the target.
12724  * @lun_id: LUN ID of the scsi device.
12725  * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12726  *
12727  * This function sends an abort command for every SCSI command
12728  * associated with the given virtual port pending on the ring
12729  * filtered by lpfc_sli_validate_fcp_iocb function.
12730  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
12731  * FCP iocbs associated with lun specified by tgt_id and lun_id
12732  * parameters
12733  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
12734  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12735  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
12736  * FCP iocbs associated with virtual port.
12737  * This function returns number of iocbs it aborted .
12738  * This function is called with no locks held right after a taskmgmt
12739  * command is sent.
12740  **/
12741 int
12742 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
12743                         uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
12744 {
12745         struct lpfc_hba *phba = vport->phba;
12746         struct lpfc_io_buf *lpfc_cmd;
12747         struct lpfc_iocbq *abtsiocbq;
12748         struct lpfc_nodelist *ndlp;
12749         struct lpfc_iocbq *iocbq;
12750         IOCB_t *icmd;
12751         int sum, i, ret_val;
12752         unsigned long iflags;
12753         struct lpfc_sli_ring *pring_s4 = NULL;
12754
12755         spin_lock_irqsave(&phba->hbalock, iflags);
12756
12757         /* all I/Os are in process of being flushed */
12758         if (phba->hba_flag & HBA_IOQ_FLUSH) {
12759                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12760                 return 0;
12761         }
12762         sum = 0;
12763
12764         for (i = 1; i <= phba->sli.last_iotag; i++) {
12765                 iocbq = phba->sli.iocbq_lookup[i];
12766
12767                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12768                                                cmd) != 0)
12769                         continue;
12770
12771                 /* Guard against IO completion being called at same time */
12772                 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12773                 spin_lock(&lpfc_cmd->buf_lock);
12774
12775                 if (!lpfc_cmd->pCmd) {
12776                         spin_unlock(&lpfc_cmd->buf_lock);
12777                         continue;
12778                 }
12779
12780                 if (phba->sli_rev == LPFC_SLI_REV4) {
12781                         pring_s4 =
12782                             phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
12783                         if (!pring_s4) {
12784                                 spin_unlock(&lpfc_cmd->buf_lock);
12785                                 continue;
12786                         }
12787                         /* Note: both hbalock and ring_lock must be set here */
12788                         spin_lock(&pring_s4->ring_lock);
12789                 }
12790
12791                 /*
12792                  * If the iocbq is already being aborted, don't take a second
12793                  * action, but do count it.
12794                  */
12795                 if ((iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
12796                     !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
12797                         if (phba->sli_rev == LPFC_SLI_REV4)
12798                                 spin_unlock(&pring_s4->ring_lock);
12799                         spin_unlock(&lpfc_cmd->buf_lock);
12800                         continue;
12801                 }
12802
12803                 /* issue ABTS for this IOCB based on iotag */
12804                 abtsiocbq = __lpfc_sli_get_iocbq(phba);
12805                 if (!abtsiocbq) {
12806                         if (phba->sli_rev == LPFC_SLI_REV4)
12807                                 spin_unlock(&pring_s4->ring_lock);
12808                         spin_unlock(&lpfc_cmd->buf_lock);
12809                         continue;
12810                 }
12811
12812                 icmd = &iocbq->iocb;
12813                 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
12814                 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
12815                 if (phba->sli_rev == LPFC_SLI_REV4)
12816                         abtsiocbq->iocb.un.acxri.abortIoTag =
12817                                                          iocbq->sli4_xritag;
12818                 else
12819                         abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
12820                 abtsiocbq->iocb.ulpLe = 1;
12821                 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
12822                 abtsiocbq->vport = vport;
12823
12824                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
12825                 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
12826                 if (iocbq->iocb_flag & LPFC_IO_FCP)
12827                         abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
12828                 if (iocbq->iocb_flag & LPFC_IO_FOF)
12829                         abtsiocbq->iocb_flag |= LPFC_IO_FOF;
12830
12831                 ndlp = lpfc_cmd->rdata->pnode;
12832
12833                 if (lpfc_is_link_up(phba) &&
12834                     (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
12835                         abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
12836                 else
12837                         abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
12838
12839                 /* Setup callback routine and issue the command. */
12840                 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
12841
12842                 /*
12843                  * Indicate the IO is being aborted by the driver and set
12844                  * the caller's flag into the aborted IO.
12845                  */
12846                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
12847
12848                 if (phba->sli_rev == LPFC_SLI_REV4) {
12849                         ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
12850                                                         abtsiocbq, 0);
12851                         spin_unlock(&pring_s4->ring_lock);
12852                 } else {
12853                         ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
12854                                                         abtsiocbq, 0);
12855                 }
12856
12857                 spin_unlock(&lpfc_cmd->buf_lock);
12858
12859                 if (ret_val == IOCB_ERROR)
12860                         __lpfc_sli_release_iocbq(phba, abtsiocbq);
12861                 else
12862                         sum++;
12863         }
12864         spin_unlock_irqrestore(&phba->hbalock, iflags);
12865         return sum;
12866 }
12867
12868 /**
12869  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
12870  * @phba: Pointer to HBA context object.
12871  * @cmdiocbq: Pointer to command iocb.
12872  * @rspiocbq: Pointer to response iocb.
12873  *
12874  * This function is the completion handler for iocbs issued using
12875  * lpfc_sli_issue_iocb_wait function. This function is called by the
12876  * ring event handler function without any lock held. This function
12877  * can be called from both worker thread context and interrupt
12878  * context. This function also can be called from other thread which
12879  * cleans up the SLI layer objects.
12880  * This function copy the contents of the response iocb to the
12881  * response iocb memory object provided by the caller of
12882  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
12883  * sleeps for the iocb completion.
12884  **/
12885 static void
12886 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
12887                         struct lpfc_iocbq *cmdiocbq,
12888                         struct lpfc_iocbq *rspiocbq)
12889 {
12890         wait_queue_head_t *pdone_q;
12891         unsigned long iflags;
12892         struct lpfc_io_buf *lpfc_cmd;
12893
12894         spin_lock_irqsave(&phba->hbalock, iflags);
12895         if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
12896
12897                 /*
12898                  * A time out has occurred for the iocb.  If a time out
12899                  * completion handler has been supplied, call it.  Otherwise,
12900                  * just free the iocbq.
12901                  */
12902
12903                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12904                 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
12905                 cmdiocbq->wait_iocb_cmpl = NULL;
12906                 if (cmdiocbq->iocb_cmpl)
12907                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
12908                 else
12909                         lpfc_sli_release_iocbq(phba, cmdiocbq);
12910                 return;
12911         }
12912
12913         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
12914         if (cmdiocbq->context2 && rspiocbq)
12915                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
12916                        &rspiocbq->iocb, sizeof(IOCB_t));
12917
12918         /* Set the exchange busy flag for task management commands */
12919         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
12920                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
12921                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
12922                         cur_iocbq);
12923                 if (rspiocbq && (rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY))
12924                         lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
12925                 else
12926                         lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
12927         }
12928
12929         pdone_q = cmdiocbq->context_un.wait_queue;
12930         if (pdone_q)
12931                 wake_up(pdone_q);
12932         spin_unlock_irqrestore(&phba->hbalock, iflags);
12933         return;
12934 }
12935
12936 /**
12937  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
12938  * @phba: Pointer to HBA context object..
12939  * @piocbq: Pointer to command iocb.
12940  * @flag: Flag to test.
12941  *
12942  * This routine grabs the hbalock and then test the iocb_flag to
12943  * see if the passed in flag is set.
12944  * Returns:
12945  * 1 if flag is set.
12946  * 0 if flag is not set.
12947  **/
12948 static int
12949 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
12950                  struct lpfc_iocbq *piocbq, uint32_t flag)
12951 {
12952         unsigned long iflags;
12953         int ret;
12954
12955         spin_lock_irqsave(&phba->hbalock, iflags);
12956         ret = piocbq->iocb_flag & flag;
12957         spin_unlock_irqrestore(&phba->hbalock, iflags);
12958         return ret;
12959
12960 }
12961
12962 /**
12963  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
12964  * @phba: Pointer to HBA context object..
12965  * @ring_number: Ring number
12966  * @piocb: Pointer to command iocb.
12967  * @prspiocbq: Pointer to response iocb.
12968  * @timeout: Timeout in number of seconds.
12969  *
12970  * This function issues the iocb to firmware and waits for the
12971  * iocb to complete. The iocb_cmpl field of the shall be used
12972  * to handle iocbs which time out. If the field is NULL, the
12973  * function shall free the iocbq structure.  If more clean up is
12974  * needed, the caller is expected to provide a completion function
12975  * that will provide the needed clean up.  If the iocb command is
12976  * not completed within timeout seconds, the function will either
12977  * free the iocbq structure (if iocb_cmpl == NULL) or execute the
12978  * completion function set in the iocb_cmpl field and then return
12979  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
12980  * resources if this function returns IOCB_TIMEDOUT.
12981  * The function waits for the iocb completion using an
12982  * non-interruptible wait.
12983  * This function will sleep while waiting for iocb completion.
12984  * So, this function should not be called from any context which
12985  * does not allow sleeping. Due to the same reason, this function
12986  * cannot be called with interrupt disabled.
12987  * This function assumes that the iocb completions occur while
12988  * this function sleep. So, this function cannot be called from
12989  * the thread which process iocb completion for this ring.
12990  * This function clears the iocb_flag of the iocb object before
12991  * issuing the iocb and the iocb completion handler sets this
12992  * flag and wakes this thread when the iocb completes.
12993  * The contents of the response iocb will be copied to prspiocbq
12994  * by the completion handler when the command completes.
12995  * This function returns IOCB_SUCCESS when success.
12996  * This function is called with no lock held.
12997  **/
12998 int
12999 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
13000                          uint32_t ring_number,
13001                          struct lpfc_iocbq *piocb,
13002                          struct lpfc_iocbq *prspiocbq,
13003                          uint32_t timeout)
13004 {
13005         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
13006         long timeleft, timeout_req = 0;
13007         int retval = IOCB_SUCCESS;
13008         uint32_t creg_val;
13009         struct lpfc_iocbq *iocb;
13010         int txq_cnt = 0;
13011         int txcmplq_cnt = 0;
13012         struct lpfc_sli_ring *pring;
13013         unsigned long iflags;
13014         bool iocb_completed = true;
13015
13016         if (phba->sli_rev >= LPFC_SLI_REV4)
13017                 pring = lpfc_sli4_calc_ring(phba, piocb);
13018         else
13019                 pring = &phba->sli.sli3_ring[ring_number];
13020         /*
13021          * If the caller has provided a response iocbq buffer, then context2
13022          * is NULL or its an error.
13023          */
13024         if (prspiocbq) {
13025                 if (piocb->context2)
13026                         return IOCB_ERROR;
13027                 piocb->context2 = prspiocbq;
13028         }
13029
13030         piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
13031         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
13032         piocb->context_un.wait_queue = &done_q;
13033         piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
13034
13035         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13036                 if (lpfc_readl(phba->HCregaddr, &creg_val))
13037                         return IOCB_ERROR;
13038                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
13039                 writel(creg_val, phba->HCregaddr);
13040                 readl(phba->HCregaddr); /* flush */
13041         }
13042
13043         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
13044                                      SLI_IOCB_RET_IOCB);
13045         if (retval == IOCB_SUCCESS) {
13046                 timeout_req = msecs_to_jiffies(timeout * 1000);
13047                 timeleft = wait_event_timeout(done_q,
13048                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
13049                                 timeout_req);
13050                 spin_lock_irqsave(&phba->hbalock, iflags);
13051                 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
13052
13053                         /*
13054                          * IOCB timed out.  Inform the wake iocb wait
13055                          * completion function and set local status
13056                          */
13057
13058                         iocb_completed = false;
13059                         piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
13060                 }
13061                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13062                 if (iocb_completed) {
13063                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13064                                         "0331 IOCB wake signaled\n");
13065                         /* Note: we are not indicating if the IOCB has a success
13066                          * status or not - that's for the caller to check.
13067                          * IOCB_SUCCESS means just that the command was sent and
13068                          * completed. Not that it completed successfully.
13069                          * */
13070                 } else if (timeleft == 0) {
13071                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13072                                         "0338 IOCB wait timeout error - no "
13073                                         "wake response Data x%x\n", timeout);
13074                         retval = IOCB_TIMEDOUT;
13075                 } else {
13076                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13077                                         "0330 IOCB wake NOT set, "
13078                                         "Data x%x x%lx\n",
13079                                         timeout, (timeleft / jiffies));
13080                         retval = IOCB_TIMEDOUT;
13081                 }
13082         } else if (retval == IOCB_BUSY) {
13083                 if (phba->cfg_log_verbose & LOG_SLI) {
13084                         list_for_each_entry(iocb, &pring->txq, list) {
13085                                 txq_cnt++;
13086                         }
13087                         list_for_each_entry(iocb, &pring->txcmplq, list) {
13088                                 txcmplq_cnt++;
13089                         }
13090                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13091                                 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
13092                                 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
13093                 }
13094                 return retval;
13095         } else {
13096                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13097                                 "0332 IOCB wait issue failed, Data x%x\n",
13098                                 retval);
13099                 retval = IOCB_ERROR;
13100         }
13101
13102         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13103                 if (lpfc_readl(phba->HCregaddr, &creg_val))
13104                         return IOCB_ERROR;
13105                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
13106                 writel(creg_val, phba->HCregaddr);
13107                 readl(phba->HCregaddr); /* flush */
13108         }
13109
13110         if (prspiocbq)
13111                 piocb->context2 = NULL;
13112
13113         piocb->context_un.wait_queue = NULL;
13114         piocb->iocb_cmpl = NULL;
13115         return retval;
13116 }
13117
13118 /**
13119  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
13120  * @phba: Pointer to HBA context object.
13121  * @pmboxq: Pointer to driver mailbox object.
13122  * @timeout: Timeout in number of seconds.
13123  *
13124  * This function issues the mailbox to firmware and waits for the
13125  * mailbox command to complete. If the mailbox command is not
13126  * completed within timeout seconds, it returns MBX_TIMEOUT.
13127  * The function waits for the mailbox completion using an
13128  * interruptible wait. If the thread is woken up due to a
13129  * signal, MBX_TIMEOUT error is returned to the caller. Caller
13130  * should not free the mailbox resources, if this function returns
13131  * MBX_TIMEOUT.
13132  * This function will sleep while waiting for mailbox completion.
13133  * So, this function should not be called from any context which
13134  * does not allow sleeping. Due to the same reason, this function
13135  * cannot be called with interrupt disabled.
13136  * This function assumes that the mailbox completion occurs while
13137  * this function sleep. So, this function cannot be called from
13138  * the worker thread which processes mailbox completion.
13139  * This function is called in the context of HBA management
13140  * applications.
13141  * This function returns MBX_SUCCESS when successful.
13142  * This function is called with no lock held.
13143  **/
13144 int
13145 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
13146                          uint32_t timeout)
13147 {
13148         struct completion mbox_done;
13149         int retval;
13150         unsigned long flag;
13151
13152         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
13153         /* setup wake call as IOCB callback */
13154         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
13155
13156         /* setup context3 field to pass wait_queue pointer to wake function  */
13157         init_completion(&mbox_done);
13158         pmboxq->context3 = &mbox_done;
13159         /* now issue the command */
13160         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
13161         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
13162                 wait_for_completion_timeout(&mbox_done,
13163                                             msecs_to_jiffies(timeout * 1000));
13164
13165                 spin_lock_irqsave(&phba->hbalock, flag);
13166                 pmboxq->context3 = NULL;
13167                 /*
13168                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
13169                  * else do not free the resources.
13170                  */
13171                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
13172                         retval = MBX_SUCCESS;
13173                 } else {
13174                         retval = MBX_TIMEOUT;
13175                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13176                 }
13177                 spin_unlock_irqrestore(&phba->hbalock, flag);
13178         }
13179         return retval;
13180 }
13181
13182 /**
13183  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
13184  * @phba: Pointer to HBA context.
13185  * @mbx_action: Mailbox shutdown options.
13186  *
13187  * This function is called to shutdown the driver's mailbox sub-system.
13188  * It first marks the mailbox sub-system is in a block state to prevent
13189  * the asynchronous mailbox command from issued off the pending mailbox
13190  * command queue. If the mailbox command sub-system shutdown is due to
13191  * HBA error conditions such as EEH or ERATT, this routine shall invoke
13192  * the mailbox sub-system flush routine to forcefully bring down the
13193  * mailbox sub-system. Otherwise, if it is due to normal condition (such
13194  * as with offline or HBA function reset), this routine will wait for the
13195  * outstanding mailbox command to complete before invoking the mailbox
13196  * sub-system flush routine to gracefully bring down mailbox sub-system.
13197  **/
13198 void
13199 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
13200 {
13201         struct lpfc_sli *psli = &phba->sli;
13202         unsigned long timeout;
13203
13204         if (mbx_action == LPFC_MBX_NO_WAIT) {
13205                 /* delay 100ms for port state */
13206                 msleep(100);
13207                 lpfc_sli_mbox_sys_flush(phba);
13208                 return;
13209         }
13210         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
13211
13212         /* Disable softirqs, including timers from obtaining phba->hbalock */
13213         local_bh_disable();
13214
13215         spin_lock_irq(&phba->hbalock);
13216         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
13217
13218         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
13219                 /* Determine how long we might wait for the active mailbox
13220                  * command to be gracefully completed by firmware.
13221                  */
13222                 if (phba->sli.mbox_active)
13223                         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
13224                                                 phba->sli.mbox_active) *
13225                                                 1000) + jiffies;
13226                 spin_unlock_irq(&phba->hbalock);
13227
13228                 /* Enable softirqs again, done with phba->hbalock */
13229                 local_bh_enable();
13230
13231                 while (phba->sli.mbox_active) {
13232                         /* Check active mailbox complete status every 2ms */
13233                         msleep(2);
13234                         if (time_after(jiffies, timeout))
13235                                 /* Timeout, let the mailbox flush routine to
13236                                  * forcefully release active mailbox command
13237                                  */
13238                                 break;
13239                 }
13240         } else {
13241                 spin_unlock_irq(&phba->hbalock);
13242
13243                 /* Enable softirqs again, done with phba->hbalock */
13244                 local_bh_enable();
13245         }
13246
13247         lpfc_sli_mbox_sys_flush(phba);
13248 }
13249
13250 /**
13251  * lpfc_sli_eratt_read - read sli-3 error attention events
13252  * @phba: Pointer to HBA context.
13253  *
13254  * This function is called to read the SLI3 device error attention registers
13255  * for possible error attention events. The caller must hold the hostlock
13256  * with spin_lock_irq().
13257  *
13258  * This function returns 1 when there is Error Attention in the Host Attention
13259  * Register and returns 0 otherwise.
13260  **/
13261 static int
13262 lpfc_sli_eratt_read(struct lpfc_hba *phba)
13263 {
13264         uint32_t ha_copy;
13265
13266         /* Read chip Host Attention (HA) register */
13267         if (lpfc_readl(phba->HAregaddr, &ha_copy))
13268                 goto unplug_err;
13269
13270         if (ha_copy & HA_ERATT) {
13271                 /* Read host status register to retrieve error event */
13272                 if (lpfc_sli_read_hs(phba))
13273                         goto unplug_err;
13274
13275                 /* Check if there is a deferred error condition is active */
13276                 if ((HS_FFER1 & phba->work_hs) &&
13277                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13278                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
13279                         phba->hba_flag |= DEFER_ERATT;
13280                         /* Clear all interrupt enable conditions */
13281                         writel(0, phba->HCregaddr);
13282                         readl(phba->HCregaddr);
13283                 }
13284
13285                 /* Set the driver HA work bitmap */
13286                 phba->work_ha |= HA_ERATT;
13287                 /* Indicate polling handles this ERATT */
13288                 phba->hba_flag |= HBA_ERATT_HANDLED;
13289                 return 1;
13290         }
13291         return 0;
13292
13293 unplug_err:
13294         /* Set the driver HS work bitmap */
13295         phba->work_hs |= UNPLUG_ERR;
13296         /* Set the driver HA work bitmap */
13297         phba->work_ha |= HA_ERATT;
13298         /* Indicate polling handles this ERATT */
13299         phba->hba_flag |= HBA_ERATT_HANDLED;
13300         return 1;
13301 }
13302
13303 /**
13304  * lpfc_sli4_eratt_read - read sli-4 error attention events
13305  * @phba: Pointer to HBA context.
13306  *
13307  * This function is called to read the SLI4 device error attention registers
13308  * for possible error attention events. The caller must hold the hostlock
13309  * with spin_lock_irq().
13310  *
13311  * This function returns 1 when there is Error Attention in the Host Attention
13312  * Register and returns 0 otherwise.
13313  **/
13314 static int
13315 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
13316 {
13317         uint32_t uerr_sta_hi, uerr_sta_lo;
13318         uint32_t if_type, portsmphr;
13319         struct lpfc_register portstat_reg;
13320
13321         /*
13322          * For now, use the SLI4 device internal unrecoverable error
13323          * registers for error attention. This can be changed later.
13324          */
13325         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
13326         switch (if_type) {
13327         case LPFC_SLI_INTF_IF_TYPE_0:
13328                 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
13329                         &uerr_sta_lo) ||
13330                         lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
13331                         &uerr_sta_hi)) {
13332                         phba->work_hs |= UNPLUG_ERR;
13333                         phba->work_ha |= HA_ERATT;
13334                         phba->hba_flag |= HBA_ERATT_HANDLED;
13335                         return 1;
13336                 }
13337                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
13338                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
13339                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13340                                         "1423 HBA Unrecoverable error: "
13341                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
13342                                         "ue_mask_lo_reg=0x%x, "
13343                                         "ue_mask_hi_reg=0x%x\n",
13344                                         uerr_sta_lo, uerr_sta_hi,
13345                                         phba->sli4_hba.ue_mask_lo,
13346                                         phba->sli4_hba.ue_mask_hi);
13347                         phba->work_status[0] = uerr_sta_lo;
13348                         phba->work_status[1] = uerr_sta_hi;
13349                         phba->work_ha |= HA_ERATT;
13350                         phba->hba_flag |= HBA_ERATT_HANDLED;
13351                         return 1;
13352                 }
13353                 break;
13354         case LPFC_SLI_INTF_IF_TYPE_2:
13355         case LPFC_SLI_INTF_IF_TYPE_6:
13356                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
13357                         &portstat_reg.word0) ||
13358                         lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
13359                         &portsmphr)){
13360                         phba->work_hs |= UNPLUG_ERR;
13361                         phba->work_ha |= HA_ERATT;
13362                         phba->hba_flag |= HBA_ERATT_HANDLED;
13363                         return 1;
13364                 }
13365                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
13366                         phba->work_status[0] =
13367                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
13368                         phba->work_status[1] =
13369                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
13370                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13371                                         "2885 Port Status Event: "
13372                                         "port status reg 0x%x, "
13373                                         "port smphr reg 0x%x, "
13374                                         "error 1=0x%x, error 2=0x%x\n",
13375                                         portstat_reg.word0,
13376                                         portsmphr,
13377                                         phba->work_status[0],
13378                                         phba->work_status[1]);
13379                         phba->work_ha |= HA_ERATT;
13380                         phba->hba_flag |= HBA_ERATT_HANDLED;
13381                         return 1;
13382                 }
13383                 break;
13384         case LPFC_SLI_INTF_IF_TYPE_1:
13385         default:
13386                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13387                                 "2886 HBA Error Attention on unsupported "
13388                                 "if type %d.", if_type);
13389                 return 1;
13390         }
13391
13392         return 0;
13393 }
13394
13395 /**
13396  * lpfc_sli_check_eratt - check error attention events
13397  * @phba: Pointer to HBA context.
13398  *
13399  * This function is called from timer soft interrupt context to check HBA's
13400  * error attention register bit for error attention events.
13401  *
13402  * This function returns 1 when there is Error Attention in the Host Attention
13403  * Register and returns 0 otherwise.
13404  **/
13405 int
13406 lpfc_sli_check_eratt(struct lpfc_hba *phba)
13407 {
13408         uint32_t ha_copy;
13409
13410         /* If somebody is waiting to handle an eratt, don't process it
13411          * here. The brdkill function will do this.
13412          */
13413         if (phba->link_flag & LS_IGNORE_ERATT)
13414                 return 0;
13415
13416         /* Check if interrupt handler handles this ERATT */
13417         spin_lock_irq(&phba->hbalock);
13418         if (phba->hba_flag & HBA_ERATT_HANDLED) {
13419                 /* Interrupt handler has handled ERATT */
13420                 spin_unlock_irq(&phba->hbalock);
13421                 return 0;
13422         }
13423
13424         /*
13425          * If there is deferred error attention, do not check for error
13426          * attention
13427          */
13428         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13429                 spin_unlock_irq(&phba->hbalock);
13430                 return 0;
13431         }
13432
13433         /* If PCI channel is offline, don't process it */
13434         if (unlikely(pci_channel_offline(phba->pcidev))) {
13435                 spin_unlock_irq(&phba->hbalock);
13436                 return 0;
13437         }
13438
13439         switch (phba->sli_rev) {
13440         case LPFC_SLI_REV2:
13441         case LPFC_SLI_REV3:
13442                 /* Read chip Host Attention (HA) register */
13443                 ha_copy = lpfc_sli_eratt_read(phba);
13444                 break;
13445         case LPFC_SLI_REV4:
13446                 /* Read device Uncoverable Error (UERR) registers */
13447                 ha_copy = lpfc_sli4_eratt_read(phba);
13448                 break;
13449         default:
13450                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13451                                 "0299 Invalid SLI revision (%d)\n",
13452                                 phba->sli_rev);
13453                 ha_copy = 0;
13454                 break;
13455         }
13456         spin_unlock_irq(&phba->hbalock);
13457
13458         return ha_copy;
13459 }
13460
13461 /**
13462  * lpfc_intr_state_check - Check device state for interrupt handling
13463  * @phba: Pointer to HBA context.
13464  *
13465  * This inline routine checks whether a device or its PCI slot is in a state
13466  * that the interrupt should be handled.
13467  *
13468  * This function returns 0 if the device or the PCI slot is in a state that
13469  * interrupt should be handled, otherwise -EIO.
13470  */
13471 static inline int
13472 lpfc_intr_state_check(struct lpfc_hba *phba)
13473 {
13474         /* If the pci channel is offline, ignore all the interrupts */
13475         if (unlikely(pci_channel_offline(phba->pcidev)))
13476                 return -EIO;
13477
13478         /* Update device level interrupt statistics */
13479         phba->sli.slistat.sli_intr++;
13480
13481         /* Ignore all interrupts during initialization. */
13482         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
13483                 return -EIO;
13484
13485         return 0;
13486 }
13487
13488 /**
13489  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
13490  * @irq: Interrupt number.
13491  * @dev_id: The device context pointer.
13492  *
13493  * This function is directly called from the PCI layer as an interrupt
13494  * service routine when device with SLI-3 interface spec is enabled with
13495  * MSI-X multi-message interrupt mode and there are slow-path events in
13496  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
13497  * interrupt mode, this function is called as part of the device-level
13498  * interrupt handler. When the PCI slot is in error recovery or the HBA
13499  * is undergoing initialization, the interrupt handler will not process
13500  * the interrupt. The link attention and ELS ring attention events are
13501  * handled by the worker thread. The interrupt handler signals the worker
13502  * thread and returns for these events. This function is called without
13503  * any lock held. It gets the hbalock to access and update SLI data
13504  * structures.
13505  *
13506  * This function returns IRQ_HANDLED when interrupt is handled else it
13507  * returns IRQ_NONE.
13508  **/
13509 irqreturn_t
13510 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
13511 {
13512         struct lpfc_hba  *phba;
13513         uint32_t ha_copy, hc_copy;
13514         uint32_t work_ha_copy;
13515         unsigned long status;
13516         unsigned long iflag;
13517         uint32_t control;
13518
13519         MAILBOX_t *mbox, *pmbox;
13520         struct lpfc_vport *vport;
13521         struct lpfc_nodelist *ndlp;
13522         struct lpfc_dmabuf *mp;
13523         LPFC_MBOXQ_t *pmb;
13524         int rc;
13525
13526         /*
13527          * Get the driver's phba structure from the dev_id and
13528          * assume the HBA is not interrupting.
13529          */
13530         phba = (struct lpfc_hba *)dev_id;
13531
13532         if (unlikely(!phba))
13533                 return IRQ_NONE;
13534
13535         /*
13536          * Stuff needs to be attented to when this function is invoked as an
13537          * individual interrupt handler in MSI-X multi-message interrupt mode
13538          */
13539         if (phba->intr_type == MSIX) {
13540                 /* Check device state for handling interrupt */
13541                 if (lpfc_intr_state_check(phba))
13542                         return IRQ_NONE;
13543                 /* Need to read HA REG for slow-path events */
13544                 spin_lock_irqsave(&phba->hbalock, iflag);
13545                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
13546                         goto unplug_error;
13547                 /* If somebody is waiting to handle an eratt don't process it
13548                  * here. The brdkill function will do this.
13549                  */
13550                 if (phba->link_flag & LS_IGNORE_ERATT)
13551                         ha_copy &= ~HA_ERATT;
13552                 /* Check the need for handling ERATT in interrupt handler */
13553                 if (ha_copy & HA_ERATT) {
13554                         if (phba->hba_flag & HBA_ERATT_HANDLED)
13555                                 /* ERATT polling has handled ERATT */
13556                                 ha_copy &= ~HA_ERATT;
13557                         else
13558                                 /* Indicate interrupt handler handles ERATT */
13559                                 phba->hba_flag |= HBA_ERATT_HANDLED;
13560                 }
13561
13562                 /*
13563                  * If there is deferred error attention, do not check for any
13564                  * interrupt.
13565                  */
13566                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13567                         spin_unlock_irqrestore(&phba->hbalock, iflag);
13568                         return IRQ_NONE;
13569                 }
13570
13571                 /* Clear up only attention source related to slow-path */
13572                 if (lpfc_readl(phba->HCregaddr, &hc_copy))
13573                         goto unplug_error;
13574
13575                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
13576                         HC_LAINT_ENA | HC_ERINT_ENA),
13577                         phba->HCregaddr);
13578                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
13579                         phba->HAregaddr);
13580                 writel(hc_copy, phba->HCregaddr);
13581                 readl(phba->HAregaddr); /* flush */
13582                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13583         } else
13584                 ha_copy = phba->ha_copy;
13585
13586         work_ha_copy = ha_copy & phba->work_ha_mask;
13587
13588         if (work_ha_copy) {
13589                 if (work_ha_copy & HA_LATT) {
13590                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
13591                                 /*
13592                                  * Turn off Link Attention interrupts
13593                                  * until CLEAR_LA done
13594                                  */
13595                                 spin_lock_irqsave(&phba->hbalock, iflag);
13596                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
13597                                 if (lpfc_readl(phba->HCregaddr, &control))
13598                                         goto unplug_error;
13599                                 control &= ~HC_LAINT_ENA;
13600                                 writel(control, phba->HCregaddr);
13601                                 readl(phba->HCregaddr); /* flush */
13602                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13603                         }
13604                         else
13605                                 work_ha_copy &= ~HA_LATT;
13606                 }
13607
13608                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
13609                         /*
13610                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
13611                          * the only slow ring.
13612                          */
13613                         status = (work_ha_copy &
13614                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
13615                         status >>= (4*LPFC_ELS_RING);
13616                         if (status & HA_RXMASK) {
13617                                 spin_lock_irqsave(&phba->hbalock, iflag);
13618                                 if (lpfc_readl(phba->HCregaddr, &control))
13619                                         goto unplug_error;
13620
13621                                 lpfc_debugfs_slow_ring_trc(phba,
13622                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
13623                                 control, status,
13624                                 (uint32_t)phba->sli.slistat.sli_intr);
13625
13626                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
13627                                         lpfc_debugfs_slow_ring_trc(phba,
13628                                                 "ISR Disable ring:"
13629                                                 "pwork:x%x hawork:x%x wait:x%x",
13630                                                 phba->work_ha, work_ha_copy,
13631                                                 (uint32_t)((unsigned long)
13632                                                 &phba->work_waitq));
13633
13634                                         control &=
13635                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
13636                                         writel(control, phba->HCregaddr);
13637                                         readl(phba->HCregaddr); /* flush */
13638                                 }
13639                                 else {
13640                                         lpfc_debugfs_slow_ring_trc(phba,
13641                                                 "ISR slow ring:   pwork:"
13642                                                 "x%x hawork:x%x wait:x%x",
13643                                                 phba->work_ha, work_ha_copy,
13644                                                 (uint32_t)((unsigned long)
13645                                                 &phba->work_waitq));
13646                                 }
13647                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13648                         }
13649                 }
13650                 spin_lock_irqsave(&phba->hbalock, iflag);
13651                 if (work_ha_copy & HA_ERATT) {
13652                         if (lpfc_sli_read_hs(phba))
13653                                 goto unplug_error;
13654                         /*
13655                          * Check if there is a deferred error condition
13656                          * is active
13657                          */
13658                         if ((HS_FFER1 & phba->work_hs) &&
13659                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13660                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
13661                                   phba->work_hs)) {
13662                                 phba->hba_flag |= DEFER_ERATT;
13663                                 /* Clear all interrupt enable conditions */
13664                                 writel(0, phba->HCregaddr);
13665                                 readl(phba->HCregaddr);
13666                         }
13667                 }
13668
13669                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
13670                         pmb = phba->sli.mbox_active;
13671                         pmbox = &pmb->u.mb;
13672                         mbox = phba->mbox;
13673                         vport = pmb->vport;
13674
13675                         /* First check out the status word */
13676                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
13677                         if (pmbox->mbxOwner != OWN_HOST) {
13678                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13679                                 /*
13680                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
13681                                  * mbxStatus <status>
13682                                  */
13683                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13684                                                 "(%d):0304 Stray Mailbox "
13685                                                 "Interrupt mbxCommand x%x "
13686                                                 "mbxStatus x%x\n",
13687                                                 (vport ? vport->vpi : 0),
13688                                                 pmbox->mbxCommand,
13689                                                 pmbox->mbxStatus);
13690                                 /* clear mailbox attention bit */
13691                                 work_ha_copy &= ~HA_MBATT;
13692                         } else {
13693                                 phba->sli.mbox_active = NULL;
13694                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13695                                 phba->last_completion_time = jiffies;
13696                                 del_timer(&phba->sli.mbox_tmo);
13697                                 if (pmb->mbox_cmpl) {
13698                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
13699                                                         MAILBOX_CMD_SIZE);
13700                                         if (pmb->out_ext_byte_len &&
13701                                                 pmb->ctx_buf)
13702                                                 lpfc_sli_pcimem_bcopy(
13703                                                 phba->mbox_ext,
13704                                                 pmb->ctx_buf,
13705                                                 pmb->out_ext_byte_len);
13706                                 }
13707                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13708                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13709
13710                                         lpfc_debugfs_disc_trc(vport,
13711                                                 LPFC_DISC_TRC_MBOX_VPORT,
13712                                                 "MBOX dflt rpi: : "
13713                                                 "status:x%x rpi:x%x",
13714                                                 (uint32_t)pmbox->mbxStatus,
13715                                                 pmbox->un.varWords[0], 0);
13716
13717                                         if (!pmbox->mbxStatus) {
13718                                                 mp = (struct lpfc_dmabuf *)
13719                                                         (pmb->ctx_buf);
13720                                                 ndlp = (struct lpfc_nodelist *)
13721                                                         pmb->ctx_ndlp;
13722
13723                                                 /* Reg_LOGIN of dflt RPI was
13724                                                  * successful. new lets get
13725                                                  * rid of the RPI using the
13726                                                  * same mbox buffer.
13727                                                  */
13728                                                 lpfc_unreg_login(phba,
13729                                                         vport->vpi,
13730                                                         pmbox->un.varWords[0],
13731                                                         pmb);
13732                                                 pmb->mbox_cmpl =
13733                                                         lpfc_mbx_cmpl_dflt_rpi;
13734                                                 pmb->ctx_buf = mp;
13735                                                 pmb->ctx_ndlp = ndlp;
13736                                                 pmb->vport = vport;
13737                                                 rc = lpfc_sli_issue_mbox(phba,
13738                                                                 pmb,
13739                                                                 MBX_NOWAIT);
13740                                                 if (rc != MBX_BUSY)
13741                                                         lpfc_printf_log(phba,
13742                                                         KERN_ERR,
13743                                                         LOG_TRACE_EVENT,
13744                                                         "0350 rc should have"
13745                                                         "been MBX_BUSY\n");
13746                                                 if (rc != MBX_NOT_FINISHED)
13747                                                         goto send_current_mbox;
13748                                         }
13749                                 }
13750                                 spin_lock_irqsave(
13751                                                 &phba->pport->work_port_lock,
13752                                                 iflag);
13753                                 phba->pport->work_port_events &=
13754                                         ~WORKER_MBOX_TMO;
13755                                 spin_unlock_irqrestore(
13756                                                 &phba->pport->work_port_lock,
13757                                                 iflag);
13758
13759                                 /* Do NOT queue MBX_HEARTBEAT to the worker
13760                                  * thread for processing.
13761                                  */
13762                                 if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13763                                         /* Process mbox now */
13764                                         phba->sli.mbox_active = NULL;
13765                                         phba->sli.sli_flag &=
13766                                                 ~LPFC_SLI_MBOX_ACTIVE;
13767                                         if (pmb->mbox_cmpl)
13768                                                 pmb->mbox_cmpl(phba, pmb);
13769                                 } else {
13770                                         /* Queue to worker thread to process */
13771                                         lpfc_mbox_cmpl_put(phba, pmb);
13772                                 }
13773                         }
13774                 } else
13775                         spin_unlock_irqrestore(&phba->hbalock, iflag);
13776
13777                 if ((work_ha_copy & HA_MBATT) &&
13778                     (phba->sli.mbox_active == NULL)) {
13779 send_current_mbox:
13780                         /* Process next mailbox command if there is one */
13781                         do {
13782                                 rc = lpfc_sli_issue_mbox(phba, NULL,
13783                                                          MBX_NOWAIT);
13784                         } while (rc == MBX_NOT_FINISHED);
13785                         if (rc != MBX_SUCCESS)
13786                                 lpfc_printf_log(phba, KERN_ERR,
13787                                                 LOG_TRACE_EVENT,
13788                                                 "0349 rc should be "
13789                                                 "MBX_SUCCESS\n");
13790                 }
13791
13792                 spin_lock_irqsave(&phba->hbalock, iflag);
13793                 phba->work_ha |= work_ha_copy;
13794                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13795                 lpfc_worker_wake_up(phba);
13796         }
13797         return IRQ_HANDLED;
13798 unplug_error:
13799         spin_unlock_irqrestore(&phba->hbalock, iflag);
13800         return IRQ_HANDLED;
13801
13802 } /* lpfc_sli_sp_intr_handler */
13803
13804 /**
13805  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
13806  * @irq: Interrupt number.
13807  * @dev_id: The device context pointer.
13808  *
13809  * This function is directly called from the PCI layer as an interrupt
13810  * service routine when device with SLI-3 interface spec is enabled with
13811  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13812  * ring event in the HBA. However, when the device is enabled with either
13813  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13814  * device-level interrupt handler. When the PCI slot is in error recovery
13815  * or the HBA is undergoing initialization, the interrupt handler will not
13816  * process the interrupt. The SCSI FCP fast-path ring event are handled in
13817  * the intrrupt context. This function is called without any lock held.
13818  * It gets the hbalock to access and update SLI data structures.
13819  *
13820  * This function returns IRQ_HANDLED when interrupt is handled else it
13821  * returns IRQ_NONE.
13822  **/
13823 irqreturn_t
13824 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
13825 {
13826         struct lpfc_hba  *phba;
13827         uint32_t ha_copy;
13828         unsigned long status;
13829         unsigned long iflag;
13830         struct lpfc_sli_ring *pring;
13831
13832         /* Get the driver's phba structure from the dev_id and
13833          * assume the HBA is not interrupting.
13834          */
13835         phba = (struct lpfc_hba *) dev_id;
13836
13837         if (unlikely(!phba))
13838                 return IRQ_NONE;
13839
13840         /*
13841          * Stuff needs to be attented to when this function is invoked as an
13842          * individual interrupt handler in MSI-X multi-message interrupt mode
13843          */
13844         if (phba->intr_type == MSIX) {
13845                 /* Check device state for handling interrupt */
13846                 if (lpfc_intr_state_check(phba))
13847                         return IRQ_NONE;
13848                 /* Need to read HA REG for FCP ring and other ring events */
13849                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
13850                         return IRQ_HANDLED;
13851                 /* Clear up only attention source related to fast-path */
13852                 spin_lock_irqsave(&phba->hbalock, iflag);
13853                 /*
13854                  * If there is deferred error attention, do not check for
13855                  * any interrupt.
13856                  */
13857                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13858                         spin_unlock_irqrestore(&phba->hbalock, iflag);
13859                         return IRQ_NONE;
13860                 }
13861                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
13862                         phba->HAregaddr);
13863                 readl(phba->HAregaddr); /* flush */
13864                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13865         } else
13866                 ha_copy = phba->ha_copy;
13867
13868         /*
13869          * Process all events on FCP ring. Take the optimized path for FCP IO.
13870          */
13871         ha_copy &= ~(phba->work_ha_mask);
13872
13873         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13874         status >>= (4*LPFC_FCP_RING);
13875         pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
13876         if (status & HA_RXMASK)
13877                 lpfc_sli_handle_fast_ring_event(phba, pring, status);
13878
13879         if (phba->cfg_multi_ring_support == 2) {
13880                 /*
13881                  * Process all events on extra ring. Take the optimized path
13882                  * for extra ring IO.
13883                  */
13884                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
13885                 status >>= (4*LPFC_EXTRA_RING);
13886                 if (status & HA_RXMASK) {
13887                         lpfc_sli_handle_fast_ring_event(phba,
13888                                         &phba->sli.sli3_ring[LPFC_EXTRA_RING],
13889                                         status);
13890                 }
13891         }
13892         return IRQ_HANDLED;
13893 }  /* lpfc_sli_fp_intr_handler */
13894
13895 /**
13896  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
13897  * @irq: Interrupt number.
13898  * @dev_id: The device context pointer.
13899  *
13900  * This function is the HBA device-level interrupt handler to device with
13901  * SLI-3 interface spec, called from the PCI layer when either MSI or
13902  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
13903  * requires driver attention. This function invokes the slow-path interrupt
13904  * attention handling function and fast-path interrupt attention handling
13905  * function in turn to process the relevant HBA attention events. This
13906  * function is called without any lock held. It gets the hbalock to access
13907  * and update SLI data structures.
13908  *
13909  * This function returns IRQ_HANDLED when interrupt is handled, else it
13910  * returns IRQ_NONE.
13911  **/
13912 irqreturn_t
13913 lpfc_sli_intr_handler(int irq, void *dev_id)
13914 {
13915         struct lpfc_hba  *phba;
13916         irqreturn_t sp_irq_rc, fp_irq_rc;
13917         unsigned long status1, status2;
13918         uint32_t hc_copy;
13919
13920         /*
13921          * Get the driver's phba structure from the dev_id and
13922          * assume the HBA is not interrupting.
13923          */
13924         phba = (struct lpfc_hba *) dev_id;
13925
13926         if (unlikely(!phba))
13927                 return IRQ_NONE;
13928
13929         /* Check device state for handling interrupt */
13930         if (lpfc_intr_state_check(phba))
13931                 return IRQ_NONE;
13932
13933         spin_lock(&phba->hbalock);
13934         if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
13935                 spin_unlock(&phba->hbalock);
13936                 return IRQ_HANDLED;
13937         }
13938
13939         if (unlikely(!phba->ha_copy)) {
13940                 spin_unlock(&phba->hbalock);
13941                 return IRQ_NONE;
13942         } else if (phba->ha_copy & HA_ERATT) {
13943                 if (phba->hba_flag & HBA_ERATT_HANDLED)
13944                         /* ERATT polling has handled ERATT */
13945                         phba->ha_copy &= ~HA_ERATT;
13946                 else
13947                         /* Indicate interrupt handler handles ERATT */
13948                         phba->hba_flag |= HBA_ERATT_HANDLED;
13949         }
13950
13951         /*
13952          * If there is deferred error attention, do not check for any interrupt.
13953          */
13954         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13955                 spin_unlock(&phba->hbalock);
13956                 return IRQ_NONE;
13957         }
13958
13959         /* Clear attention sources except link and error attentions */
13960         if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
13961                 spin_unlock(&phba->hbalock);
13962                 return IRQ_HANDLED;
13963         }
13964         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
13965                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
13966                 phba->HCregaddr);
13967         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
13968         writel(hc_copy, phba->HCregaddr);
13969         readl(phba->HAregaddr); /* flush */
13970         spin_unlock(&phba->hbalock);
13971
13972         /*
13973          * Invokes slow-path host attention interrupt handling as appropriate.
13974          */
13975
13976         /* status of events with mailbox and link attention */
13977         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
13978
13979         /* status of events with ELS ring */
13980         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
13981         status2 >>= (4*LPFC_ELS_RING);
13982
13983         if (status1 || (status2 & HA_RXMASK))
13984                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
13985         else
13986                 sp_irq_rc = IRQ_NONE;
13987
13988         /*
13989          * Invoke fast-path host attention interrupt handling as appropriate.
13990          */
13991
13992         /* status of events with FCP ring */
13993         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13994         status1 >>= (4*LPFC_FCP_RING);
13995
13996         /* status of events with extra ring */
13997         if (phba->cfg_multi_ring_support == 2) {
13998                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
13999                 status2 >>= (4*LPFC_EXTRA_RING);
14000         } else
14001                 status2 = 0;
14002
14003         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
14004                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
14005         else
14006                 fp_irq_rc = IRQ_NONE;
14007
14008         /* Return device-level interrupt handling status */
14009         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
14010 }  /* lpfc_sli_intr_handler */
14011
14012 /**
14013  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
14014  * @phba: pointer to lpfc hba data structure.
14015  *
14016  * This routine is invoked by the worker thread to process all the pending
14017  * SLI4 els abort xri events.
14018  **/
14019 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
14020 {
14021         struct lpfc_cq_event *cq_event;
14022         unsigned long iflags;
14023
14024         /* First, declare the els xri abort event has been handled */
14025         spin_lock_irqsave(&phba->hbalock, iflags);
14026         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
14027         spin_unlock_irqrestore(&phba->hbalock, iflags);
14028
14029         /* Now, handle all the els xri abort events */
14030         spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14031         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
14032                 /* Get the first event from the head of the event queue */
14033                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
14034                                  cq_event, struct lpfc_cq_event, list);
14035                 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14036                                        iflags);
14037                 /* Notify aborted XRI for ELS work queue */
14038                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
14039
14040                 /* Free the event processed back to the free pool */
14041                 lpfc_sli4_cq_event_release(phba, cq_event);
14042                 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14043                                   iflags);
14044         }
14045         spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14046 }
14047
14048 /**
14049  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
14050  * @phba: pointer to lpfc hba data structure
14051  * @pIocbIn: pointer to the rspiocbq
14052  * @pIocbOut: pointer to the cmdiocbq
14053  * @wcqe: pointer to the complete wcqe
14054  *
14055  * This routine transfers the fields of a command iocbq to a response iocbq
14056  * by copying all the IOCB fields from command iocbq and transferring the
14057  * completion status information from the complete wcqe.
14058  **/
14059 static void
14060 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
14061                               struct lpfc_iocbq *pIocbIn,
14062                               struct lpfc_iocbq *pIocbOut,
14063                               struct lpfc_wcqe_complete *wcqe)
14064 {
14065         int numBdes, i;
14066         unsigned long iflags;
14067         uint32_t status, max_response;
14068         struct lpfc_dmabuf *dmabuf;
14069         struct ulp_bde64 *bpl, bde;
14070         size_t offset = offsetof(struct lpfc_iocbq, iocb);
14071
14072         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
14073                sizeof(struct lpfc_iocbq) - offset);
14074         /* Map WCQE parameters into irspiocb parameters */
14075         status = bf_get(lpfc_wcqe_c_status, wcqe);
14076         pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
14077         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
14078                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
14079                         pIocbIn->iocb.un.fcpi.fcpi_parm =
14080                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
14081                                         wcqe->total_data_placed;
14082                 else
14083                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
14084         else {
14085                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
14086                 switch (pIocbOut->iocb.ulpCommand) {
14087                 case CMD_ELS_REQUEST64_CR:
14088                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
14089                         bpl  = (struct ulp_bde64 *)dmabuf->virt;
14090                         bde.tus.w = le32_to_cpu(bpl[1].tus.w);
14091                         max_response = bde.tus.f.bdeSize;
14092                         break;
14093                 case CMD_GEN_REQUEST64_CR:
14094                         max_response = 0;
14095                         if (!pIocbOut->context3)
14096                                 break;
14097                         numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
14098                                         sizeof(struct ulp_bde64);
14099                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
14100                         bpl = (struct ulp_bde64 *)dmabuf->virt;
14101                         for (i = 0; i < numBdes; i++) {
14102                                 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
14103                                 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
14104                                         max_response += bde.tus.f.bdeSize;
14105                         }
14106                         break;
14107                 default:
14108                         max_response = wcqe->total_data_placed;
14109                         break;
14110                 }
14111                 if (max_response < wcqe->total_data_placed)
14112                         pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
14113                 else
14114                         pIocbIn->iocb.un.genreq64.bdl.bdeSize =
14115                                 wcqe->total_data_placed;
14116         }
14117
14118         /* Convert BG errors for completion status */
14119         if (status == CQE_STATUS_DI_ERROR) {
14120                 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
14121
14122                 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
14123                         pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
14124                 else
14125                         pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
14126
14127                 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
14128                 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
14129                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14130                                 BGS_GUARD_ERR_MASK;
14131                 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
14132                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14133                                 BGS_APPTAG_ERR_MASK;
14134                 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
14135                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14136                                 BGS_REFTAG_ERR_MASK;
14137
14138                 /* Check to see if there was any good data before the error */
14139                 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
14140                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14141                                 BGS_HI_WATER_MARK_PRESENT_MASK;
14142                         pIocbIn->iocb.unsli3.sli3_bg.bghm =
14143                                 wcqe->total_data_placed;
14144                 }
14145
14146                 /*
14147                 * Set ALL the error bits to indicate we don't know what
14148                 * type of error it is.
14149                 */
14150                 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
14151                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14152                                 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
14153                                 BGS_GUARD_ERR_MASK);
14154         }
14155
14156         /* Pick up HBA exchange busy condition */
14157         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
14158                 spin_lock_irqsave(&phba->hbalock, iflags);
14159                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
14160                 spin_unlock_irqrestore(&phba->hbalock, iflags);
14161         }
14162 }
14163
14164 /**
14165  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
14166  * @phba: Pointer to HBA context object.
14167  * @irspiocbq: Pointer to work-queue completion queue entry.
14168  *
14169  * This routine handles an ELS work-queue completion event and construct
14170  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
14171  * discovery engine to handle.
14172  *
14173  * Return: Pointer to the receive IOCBQ, NULL otherwise.
14174  **/
14175 static struct lpfc_iocbq *
14176 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
14177                                struct lpfc_iocbq *irspiocbq)
14178 {
14179         struct lpfc_sli_ring *pring;
14180         struct lpfc_iocbq *cmdiocbq;
14181         struct lpfc_wcqe_complete *wcqe;
14182         unsigned long iflags;
14183
14184         pring = lpfc_phba_elsring(phba);
14185         if (unlikely(!pring))
14186                 return NULL;
14187
14188         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
14189         pring->stats.iocb_event++;
14190         /* Look up the ELS command IOCB and create pseudo response IOCB */
14191         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14192                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
14193         if (unlikely(!cmdiocbq)) {
14194                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14195                                 "0386 ELS complete with no corresponding "
14196                                 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
14197                                 wcqe->word0, wcqe->total_data_placed,
14198                                 wcqe->parameter, wcqe->word3);
14199                 lpfc_sli_release_iocbq(phba, irspiocbq);
14200                 return NULL;
14201         }
14202
14203         spin_lock_irqsave(&pring->ring_lock, iflags);
14204         /* Put the iocb back on the txcmplq */
14205         lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
14206         spin_unlock_irqrestore(&pring->ring_lock, iflags);
14207
14208         /* Fake the irspiocbq and copy necessary response information */
14209         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
14210
14211         return irspiocbq;
14212 }
14213
14214 inline struct lpfc_cq_event *
14215 lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
14216 {
14217         struct lpfc_cq_event *cq_event;
14218
14219         /* Allocate a new internal CQ_EVENT entry */
14220         cq_event = lpfc_sli4_cq_event_alloc(phba);
14221         if (!cq_event) {
14222                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14223                                 "0602 Failed to alloc CQ_EVENT entry\n");
14224                 return NULL;
14225         }
14226
14227         /* Move the CQE into the event */
14228         memcpy(&cq_event->cqe, entry, size);
14229         return cq_event;
14230 }
14231
14232 /**
14233  * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
14234  * @phba: Pointer to HBA context object.
14235  * @mcqe: Pointer to mailbox completion queue entry.
14236  *
14237  * This routine process a mailbox completion queue entry with asynchronous
14238  * event.
14239  *
14240  * Return: true if work posted to worker thread, otherwise false.
14241  **/
14242 static bool
14243 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14244 {
14245         struct lpfc_cq_event *cq_event;
14246         unsigned long iflags;
14247
14248         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14249                         "0392 Async Event: word0:x%x, word1:x%x, "
14250                         "word2:x%x, word3:x%x\n", mcqe->word0,
14251                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
14252
14253         cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
14254         if (!cq_event)
14255                 return false;
14256
14257         spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
14258         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
14259         spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
14260
14261         /* Set the async event flag */
14262         spin_lock_irqsave(&phba->hbalock, iflags);
14263         phba->hba_flag |= ASYNC_EVENT;
14264         spin_unlock_irqrestore(&phba->hbalock, iflags);
14265
14266         return true;
14267 }
14268
14269 /**
14270  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
14271  * @phba: Pointer to HBA context object.
14272  * @mcqe: Pointer to mailbox completion queue entry.
14273  *
14274  * This routine process a mailbox completion queue entry with mailbox
14275  * completion event.
14276  *
14277  * Return: true if work posted to worker thread, otherwise false.
14278  **/
14279 static bool
14280 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14281 {
14282         uint32_t mcqe_status;
14283         MAILBOX_t *mbox, *pmbox;
14284         struct lpfc_mqe *mqe;
14285         struct lpfc_vport *vport;
14286         struct lpfc_nodelist *ndlp;
14287         struct lpfc_dmabuf *mp;
14288         unsigned long iflags;
14289         LPFC_MBOXQ_t *pmb;
14290         bool workposted = false;
14291         int rc;
14292
14293         /* If not a mailbox complete MCQE, out by checking mailbox consume */
14294         if (!bf_get(lpfc_trailer_completed, mcqe))
14295                 goto out_no_mqe_complete;
14296
14297         /* Get the reference to the active mbox command */
14298         spin_lock_irqsave(&phba->hbalock, iflags);
14299         pmb = phba->sli.mbox_active;
14300         if (unlikely(!pmb)) {
14301                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14302                                 "1832 No pending MBOX command to handle\n");
14303                 spin_unlock_irqrestore(&phba->hbalock, iflags);
14304                 goto out_no_mqe_complete;
14305         }
14306         spin_unlock_irqrestore(&phba->hbalock, iflags);
14307         mqe = &pmb->u.mqe;
14308         pmbox = (MAILBOX_t *)&pmb->u.mqe;
14309         mbox = phba->mbox;
14310         vport = pmb->vport;
14311
14312         /* Reset heartbeat timer */
14313         phba->last_completion_time = jiffies;
14314         del_timer(&phba->sli.mbox_tmo);
14315
14316         /* Move mbox data to caller's mailbox region, do endian swapping */
14317         if (pmb->mbox_cmpl && mbox)
14318                 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
14319
14320         /*
14321          * For mcqe errors, conditionally move a modified error code to
14322          * the mbox so that the error will not be missed.
14323          */
14324         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
14325         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
14326                 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
14327                         bf_set(lpfc_mqe_status, mqe,
14328                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
14329         }
14330         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
14331                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
14332                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
14333                                       "MBOX dflt rpi: status:x%x rpi:x%x",
14334                                       mcqe_status,
14335                                       pmbox->un.varWords[0], 0);
14336                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
14337                         mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
14338                         ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
14339
14340                         /* Reg_LOGIN of dflt RPI was successful. Mark the
14341                          * node as having an UNREG_LOGIN in progress to stop
14342                          * an unsolicited PLOGI from the same NPortId from
14343                          * starting another mailbox transaction.
14344                          */
14345                         spin_lock_irqsave(&ndlp->lock, iflags);
14346                         ndlp->nlp_flag |= NLP_UNREG_INP;
14347                         spin_unlock_irqrestore(&ndlp->lock, iflags);
14348                         lpfc_unreg_login(phba, vport->vpi,
14349                                          pmbox->un.varWords[0], pmb);
14350                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
14351                         pmb->ctx_buf = mp;
14352
14353                         /* No reference taken here.  This is a default
14354                          * RPI reg/immediate unreg cycle. The reference was
14355                          * taken in the reg rpi path and is released when
14356                          * this mailbox completes.
14357                          */
14358                         pmb->ctx_ndlp = ndlp;
14359                         pmb->vport = vport;
14360                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
14361                         if (rc != MBX_BUSY)
14362                                 lpfc_printf_log(phba, KERN_ERR,
14363                                                 LOG_TRACE_EVENT,
14364                                                 "0385 rc should "
14365                                                 "have been MBX_BUSY\n");
14366                         if (rc != MBX_NOT_FINISHED)
14367                                 goto send_current_mbox;
14368                 }
14369         }
14370         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
14371         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
14372         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
14373
14374         /* Do NOT queue MBX_HEARTBEAT to the worker thread for processing. */
14375         if (pmbox->mbxCommand == MBX_HEARTBEAT) {
14376                 spin_lock_irqsave(&phba->hbalock, iflags);
14377                 /* Release the mailbox command posting token */
14378                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14379                 phba->sli.mbox_active = NULL;
14380                 if (bf_get(lpfc_trailer_consumed, mcqe))
14381                         lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14382                 spin_unlock_irqrestore(&phba->hbalock, iflags);
14383
14384                 /* Post the next mbox command, if there is one */
14385                 lpfc_sli4_post_async_mbox(phba);
14386
14387                 /* Process cmpl now */
14388                 if (pmb->mbox_cmpl)
14389                         pmb->mbox_cmpl(phba, pmb);
14390                 return false;
14391         }
14392
14393         /* There is mailbox completion work to queue to the worker thread */
14394         spin_lock_irqsave(&phba->hbalock, iflags);
14395         __lpfc_mbox_cmpl_put(phba, pmb);
14396         phba->work_ha |= HA_MBATT;
14397         spin_unlock_irqrestore(&phba->hbalock, iflags);
14398         workposted = true;
14399
14400 send_current_mbox:
14401         spin_lock_irqsave(&phba->hbalock, iflags);
14402         /* Release the mailbox command posting token */
14403         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14404         /* Setting active mailbox pointer need to be in sync to flag clear */
14405         phba->sli.mbox_active = NULL;
14406         if (bf_get(lpfc_trailer_consumed, mcqe))
14407                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14408         spin_unlock_irqrestore(&phba->hbalock, iflags);
14409         /* Wake up worker thread to post the next pending mailbox command */
14410         lpfc_worker_wake_up(phba);
14411         return workposted;
14412
14413 out_no_mqe_complete:
14414         spin_lock_irqsave(&phba->hbalock, iflags);
14415         if (bf_get(lpfc_trailer_consumed, mcqe))
14416                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14417         spin_unlock_irqrestore(&phba->hbalock, iflags);
14418         return false;
14419 }
14420
14421 /**
14422  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
14423  * @phba: Pointer to HBA context object.
14424  * @cq: Pointer to associated CQ
14425  * @cqe: Pointer to mailbox completion queue entry.
14426  *
14427  * This routine process a mailbox completion queue entry, it invokes the
14428  * proper mailbox complete handling or asynchronous event handling routine
14429  * according to the MCQE's async bit.
14430  *
14431  * Return: true if work posted to worker thread, otherwise false.
14432  **/
14433 static bool
14434 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14435                          struct lpfc_cqe *cqe)
14436 {
14437         struct lpfc_mcqe mcqe;
14438         bool workposted;
14439
14440         cq->CQ_mbox++;
14441
14442         /* Copy the mailbox MCQE and convert endian order as needed */
14443         lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
14444
14445         /* Invoke the proper event handling routine */
14446         if (!bf_get(lpfc_trailer_async, &mcqe))
14447                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
14448         else
14449                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
14450         return workposted;
14451 }
14452
14453 /**
14454  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
14455  * @phba: Pointer to HBA context object.
14456  * @cq: Pointer to associated CQ
14457  * @wcqe: Pointer to work-queue completion queue entry.
14458  *
14459  * This routine handles an ELS work-queue completion event.
14460  *
14461  * Return: true if work posted to worker thread, otherwise false.
14462  **/
14463 static bool
14464 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14465                              struct lpfc_wcqe_complete *wcqe)
14466 {
14467         struct lpfc_iocbq *irspiocbq;
14468         unsigned long iflags;
14469         struct lpfc_sli_ring *pring = cq->pring;
14470         int txq_cnt = 0;
14471         int txcmplq_cnt = 0;
14472
14473         /* Check for response status */
14474         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14475                 /* Log the error status */
14476                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14477                                 "0357 ELS CQE error: status=x%x: "
14478                                 "CQE: %08x %08x %08x %08x\n",
14479                                 bf_get(lpfc_wcqe_c_status, wcqe),
14480                                 wcqe->word0, wcqe->total_data_placed,
14481                                 wcqe->parameter, wcqe->word3);
14482         }
14483
14484         /* Get an irspiocbq for later ELS response processing use */
14485         irspiocbq = lpfc_sli_get_iocbq(phba);
14486         if (!irspiocbq) {
14487                 if (!list_empty(&pring->txq))
14488                         txq_cnt++;
14489                 if (!list_empty(&pring->txcmplq))
14490                         txcmplq_cnt++;
14491                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14492                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
14493                         "els_txcmplq_cnt=%d\n",
14494                         txq_cnt, phba->iocb_cnt,
14495                         txcmplq_cnt);
14496                 return false;
14497         }
14498
14499         /* Save off the slow-path queue event for work thread to process */
14500         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
14501         spin_lock_irqsave(&phba->hbalock, iflags);
14502         list_add_tail(&irspiocbq->cq_event.list,
14503                       &phba->sli4_hba.sp_queue_event);
14504         phba->hba_flag |= HBA_SP_QUEUE_EVT;
14505         spin_unlock_irqrestore(&phba->hbalock, iflags);
14506
14507         return true;
14508 }
14509
14510 /**
14511  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
14512  * @phba: Pointer to HBA context object.
14513  * @wcqe: Pointer to work-queue completion queue entry.
14514  *
14515  * This routine handles slow-path WQ entry consumed event by invoking the
14516  * proper WQ release routine to the slow-path WQ.
14517  **/
14518 static void
14519 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
14520                              struct lpfc_wcqe_release *wcqe)
14521 {
14522         /* sanity check on queue memory */
14523         if (unlikely(!phba->sli4_hba.els_wq))
14524                 return;
14525         /* Check for the slow-path ELS work queue */
14526         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
14527                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
14528                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
14529         else
14530                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14531                                 "2579 Slow-path wqe consume event carries "
14532                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
14533                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
14534                                 phba->sli4_hba.els_wq->queue_id);
14535 }
14536
14537 /**
14538  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
14539  * @phba: Pointer to HBA context object.
14540  * @cq: Pointer to a WQ completion queue.
14541  * @wcqe: Pointer to work-queue completion queue entry.
14542  *
14543  * This routine handles an XRI abort event.
14544  *
14545  * Return: true if work posted to worker thread, otherwise false.
14546  **/
14547 static bool
14548 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
14549                                    struct lpfc_queue *cq,
14550                                    struct sli4_wcqe_xri_aborted *wcqe)
14551 {
14552         bool workposted = false;
14553         struct lpfc_cq_event *cq_event;
14554         unsigned long iflags;
14555
14556         switch (cq->subtype) {
14557         case LPFC_IO:
14558                 lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
14559                 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
14560                         /* Notify aborted XRI for NVME work queue */
14561                         if (phba->nvmet_support)
14562                                 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
14563                 }
14564                 workposted = false;
14565                 break;
14566         case LPFC_NVME_LS: /* NVME LS uses ELS resources */
14567         case LPFC_ELS:
14568                 cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));
14569                 if (!cq_event) {
14570                         workposted = false;
14571                         break;
14572                 }
14573                 cq_event->hdwq = cq->hdwq;
14574                 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14575                                   iflags);
14576                 list_add_tail(&cq_event->list,
14577                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
14578                 /* Set the els xri abort event flag */
14579                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
14580                 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14581                                        iflags);
14582                 workposted = true;
14583                 break;
14584         default:
14585                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14586                                 "0603 Invalid CQ subtype %d: "
14587                                 "%08x %08x %08x %08x\n",
14588                                 cq->subtype, wcqe->word0, wcqe->parameter,
14589                                 wcqe->word2, wcqe->word3);
14590                 workposted = false;
14591                 break;
14592         }
14593         return workposted;
14594 }
14595
14596 #define FC_RCTL_MDS_DIAGS       0xF4
14597
14598 /**
14599  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
14600  * @phba: Pointer to HBA context object.
14601  * @rcqe: Pointer to receive-queue completion queue entry.
14602  *
14603  * This routine process a receive-queue completion queue entry.
14604  *
14605  * Return: true if work posted to worker thread, otherwise false.
14606  **/
14607 static bool
14608 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
14609 {
14610         bool workposted = false;
14611         struct fc_frame_header *fc_hdr;
14612         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
14613         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
14614         struct lpfc_nvmet_tgtport *tgtp;
14615         struct hbq_dmabuf *dma_buf;
14616         uint32_t status, rq_id;
14617         unsigned long iflags;
14618
14619         /* sanity check on queue memory */
14620         if (unlikely(!hrq) || unlikely(!drq))
14621                 return workposted;
14622
14623         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14624                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14625         else
14626                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14627         if (rq_id != hrq->queue_id)
14628                 goto out;
14629
14630         status = bf_get(lpfc_rcqe_status, rcqe);
14631         switch (status) {
14632         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
14633                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14634                                 "2537 Receive Frame Truncated!!\n");
14635                 fallthrough;
14636         case FC_STATUS_RQ_SUCCESS:
14637                 spin_lock_irqsave(&phba->hbalock, iflags);
14638                 lpfc_sli4_rq_release(hrq, drq);
14639                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14640                 if (!dma_buf) {
14641                         hrq->RQ_no_buf_found++;
14642                         spin_unlock_irqrestore(&phba->hbalock, iflags);
14643                         goto out;
14644                 }
14645                 hrq->RQ_rcv_buf++;
14646                 hrq->RQ_buf_posted--;
14647                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
14648
14649                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14650
14651                 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
14652                     fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
14653                         spin_unlock_irqrestore(&phba->hbalock, iflags);
14654                         /* Handle MDS Loopback frames */
14655                         if  (!(phba->pport->load_flag & FC_UNLOADING))
14656                                 lpfc_sli4_handle_mds_loopback(phba->pport,
14657                                                               dma_buf);
14658                         else
14659                                 lpfc_in_buf_free(phba, &dma_buf->dbuf);
14660                         break;
14661                 }
14662
14663                 /* save off the frame for the work thread to process */
14664                 list_add_tail(&dma_buf->cq_event.list,
14665                               &phba->sli4_hba.sp_queue_event);
14666                 /* Frame received */
14667                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
14668                 spin_unlock_irqrestore(&phba->hbalock, iflags);
14669                 workposted = true;
14670                 break;
14671         case FC_STATUS_INSUFF_BUF_FRM_DISC:
14672                 if (phba->nvmet_support) {
14673                         tgtp = phba->targetport->private;
14674                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14675                                         "6402 RQE Error x%x, posted %d err_cnt "
14676                                         "%d: %x %x %x\n",
14677                                         status, hrq->RQ_buf_posted,
14678                                         hrq->RQ_no_posted_buf,
14679                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
14680                                         atomic_read(&tgtp->rcv_fcp_cmd_out),
14681                                         atomic_read(&tgtp->xmt_fcp_release));
14682                 }
14683                 fallthrough;
14684
14685         case FC_STATUS_INSUFF_BUF_NEED_BUF:
14686                 hrq->RQ_no_posted_buf++;
14687                 /* Post more buffers if possible */
14688                 spin_lock_irqsave(&phba->hbalock, iflags);
14689                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
14690                 spin_unlock_irqrestore(&phba->hbalock, iflags);
14691                 workposted = true;
14692                 break;
14693         }
14694 out:
14695         return workposted;
14696 }
14697
14698 /**
14699  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
14700  * @phba: Pointer to HBA context object.
14701  * @cq: Pointer to the completion queue.
14702  * @cqe: Pointer to a completion queue entry.
14703  *
14704  * This routine process a slow-path work-queue or receive queue completion queue
14705  * entry.
14706  *
14707  * Return: true if work posted to worker thread, otherwise false.
14708  **/
14709 static bool
14710 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14711                          struct lpfc_cqe *cqe)
14712 {
14713         struct lpfc_cqe cqevt;
14714         bool workposted = false;
14715
14716         /* Copy the work queue CQE and convert endian order if needed */
14717         lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
14718
14719         /* Check and process for different type of WCQE and dispatch */
14720         switch (bf_get(lpfc_cqe_code, &cqevt)) {
14721         case CQE_CODE_COMPL_WQE:
14722                 /* Process the WQ/RQ complete event */
14723                 phba->last_completion_time = jiffies;
14724                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
14725                                 (struct lpfc_wcqe_complete *)&cqevt);
14726                 break;
14727         case CQE_CODE_RELEASE_WQE:
14728                 /* Process the WQ release event */
14729                 lpfc_sli4_sp_handle_rel_wcqe(phba,
14730                                 (struct lpfc_wcqe_release *)&cqevt);
14731                 break;
14732         case CQE_CODE_XRI_ABORTED:
14733                 /* Process the WQ XRI abort event */
14734                 phba->last_completion_time = jiffies;
14735                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14736                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
14737                 break;
14738         case CQE_CODE_RECEIVE:
14739         case CQE_CODE_RECEIVE_V1:
14740                 /* Process the RQ event */
14741                 phba->last_completion_time = jiffies;
14742                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
14743                                 (struct lpfc_rcqe *)&cqevt);
14744                 break;
14745         default:
14746                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14747                                 "0388 Not a valid WCQE code: x%x\n",
14748                                 bf_get(lpfc_cqe_code, &cqevt));
14749                 break;
14750         }
14751         return workposted;
14752 }
14753
14754 /**
14755  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
14756  * @phba: Pointer to HBA context object.
14757  * @eqe: Pointer to fast-path event queue entry.
14758  * @speq: Pointer to slow-path event queue.
14759  *
14760  * This routine process a event queue entry from the slow-path event queue.
14761  * It will check the MajorCode and MinorCode to determine this is for a
14762  * completion event on a completion queue, if not, an error shall be logged
14763  * and just return. Otherwise, it will get to the corresponding completion
14764  * queue and process all the entries on that completion queue, rearm the
14765  * completion queue, and then return.
14766  *
14767  **/
14768 static void
14769 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14770         struct lpfc_queue *speq)
14771 {
14772         struct lpfc_queue *cq = NULL, *childq;
14773         uint16_t cqid;
14774         int ret = 0;
14775
14776         /* Get the reference to the corresponding CQ */
14777         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14778
14779         list_for_each_entry(childq, &speq->child_list, list) {
14780                 if (childq->queue_id == cqid) {
14781                         cq = childq;
14782                         break;
14783                 }
14784         }
14785         if (unlikely(!cq)) {
14786                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
14787                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14788                                         "0365 Slow-path CQ identifier "
14789                                         "(%d) does not exist\n", cqid);
14790                 return;
14791         }
14792
14793         /* Save EQ associated with this CQ */
14794         cq->assoc_qp = speq;
14795
14796         if (is_kdump_kernel())
14797                 ret = queue_work(phba->wq, &cq->spwork);
14798         else
14799                 ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
14800
14801         if (!ret)
14802                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14803                                 "0390 Cannot schedule queue work "
14804                                 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14805                                 cqid, cq->queue_id, raw_smp_processor_id());
14806 }
14807
14808 /**
14809  * __lpfc_sli4_process_cq - Process elements of a CQ
14810  * @phba: Pointer to HBA context object.
14811  * @cq: Pointer to CQ to be processed
14812  * @handler: Routine to process each cqe
14813  * @delay: Pointer to usdelay to set in case of rescheduling of the handler
14814  * @poll_mode: Polling mode we were called from
14815  *
14816  * This routine processes completion queue entries in a CQ. While a valid
14817  * queue element is found, the handler is called. During processing checks
14818  * are made for periodic doorbell writes to let the hardware know of
14819  * element consumption.
14820  *
14821  * If the max limit on cqes to process is hit, or there are no more valid
14822  * entries, the loop stops. If we processed a sufficient number of elements,
14823  * meaning there is sufficient load, rather than rearming and generating
14824  * another interrupt, a cq rescheduling delay will be set. A delay of 0
14825  * indicates no rescheduling.
14826  *
14827  * Returns True if work scheduled, False otherwise.
14828  **/
14829 static bool
14830 __lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
14831         bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
14832                         struct lpfc_cqe *), unsigned long *delay,
14833                         enum lpfc_poll_mode poll_mode)
14834 {
14835         struct lpfc_cqe *cqe;
14836         bool workposted = false;
14837         int count = 0, consumed = 0;
14838         bool arm = true;
14839
14840         /* default - no reschedule */
14841         *delay = 0;
14842
14843         if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
14844                 goto rearm_and_exit;
14845
14846         /* Process all the entries to the CQ */
14847         cq->q_flag = 0;
14848         cqe = lpfc_sli4_cq_get(cq);
14849         while (cqe) {
14850                 workposted |= handler(phba, cq, cqe);
14851                 __lpfc_sli4_consume_cqe(phba, cq, cqe);
14852
14853                 consumed++;
14854                 if (!(++count % cq->max_proc_limit))
14855                         break;
14856
14857                 if (!(count % cq->notify_interval)) {
14858                         phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14859                                                 LPFC_QUEUE_NOARM);
14860                         consumed = 0;
14861                         cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
14862                 }
14863
14864                 if (count == LPFC_NVMET_CQ_NOTIFY)
14865                         cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
14866
14867                 cqe = lpfc_sli4_cq_get(cq);
14868         }
14869         if (count >= phba->cfg_cq_poll_threshold) {
14870                 *delay = 1;
14871                 arm = false;
14872         }
14873
14874         /* Note: complete the irq_poll softirq before rearming CQ */
14875         if (poll_mode == LPFC_IRQ_POLL)
14876                 irq_poll_complete(&cq->iop);
14877
14878         /* Track the max number of CQEs processed in 1 EQ */
14879         if (count > cq->CQ_max_cqe)
14880                 cq->CQ_max_cqe = count;
14881
14882         cq->assoc_qp->EQ_cqe_cnt += count;
14883
14884         /* Catch the no cq entry condition */
14885         if (unlikely(count == 0))
14886                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14887                                 "0369 No entry from completion queue "
14888                                 "qid=%d\n", cq->queue_id);
14889
14890         xchg(&cq->queue_claimed, 0);
14891
14892 rearm_and_exit:
14893         phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14894                         arm ?  LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
14895
14896         return workposted;
14897 }
14898
14899 /**
14900  * __lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
14901  * @cq: pointer to CQ to process
14902  *
14903  * This routine calls the cq processing routine with a handler specific
14904  * to the type of queue bound to it.
14905  *
14906  * The CQ routine returns two values: the first is the calling status,
14907  * which indicates whether work was queued to the  background discovery
14908  * thread. If true, the routine should wakeup the discovery thread;
14909  * the second is the delay parameter. If non-zero, rather than rearming
14910  * the CQ and yet another interrupt, the CQ handler should be queued so
14911  * that it is processed in a subsequent polling action. The value of
14912  * the delay indicates when to reschedule it.
14913  **/
14914 static void
14915 __lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
14916 {
14917         struct lpfc_hba *phba = cq->phba;
14918         unsigned long delay;
14919         bool workposted = false;
14920         int ret = 0;
14921
14922         /* Process and rearm the CQ */
14923         switch (cq->type) {
14924         case LPFC_MCQ:
14925                 workposted |= __lpfc_sli4_process_cq(phba, cq,
14926                                                 lpfc_sli4_sp_handle_mcqe,
14927                                                 &delay, LPFC_QUEUE_WORK);
14928                 break;
14929         case LPFC_WCQ:
14930                 if (cq->subtype == LPFC_IO)
14931                         workposted |= __lpfc_sli4_process_cq(phba, cq,
14932                                                 lpfc_sli4_fp_handle_cqe,
14933                                                 &delay, LPFC_QUEUE_WORK);
14934                 else
14935                         workposted |= __lpfc_sli4_process_cq(phba, cq,
14936                                                 lpfc_sli4_sp_handle_cqe,
14937                                                 &delay, LPFC_QUEUE_WORK);
14938                 break;
14939         default:
14940                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14941                                 "0370 Invalid completion queue type (%d)\n",
14942                                 cq->type);
14943                 return;
14944         }
14945
14946         if (delay) {
14947                 if (is_kdump_kernel())
14948                         ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
14949                                                 delay);
14950                 else
14951                         ret = queue_delayed_work_on(cq->chann, phba->wq,
14952                                                 &cq->sched_spwork, delay);
14953                 if (!ret)
14954                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14955                                 "0394 Cannot schedule queue work "
14956                                 "for cqid=%d on CPU %d\n",
14957                                 cq->queue_id, cq->chann);
14958         }
14959
14960         /* wake up worker thread if there are works to be done */
14961         if (workposted)
14962                 lpfc_worker_wake_up(phba);
14963 }
14964
14965 /**
14966  * lpfc_sli4_sp_process_cq - slow-path work handler when started by
14967  *   interrupt
14968  * @work: pointer to work element
14969  *
14970  * translates from the work handler and calls the slow-path handler.
14971  **/
14972 static void
14973 lpfc_sli4_sp_process_cq(struct work_struct *work)
14974 {
14975         struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
14976
14977         __lpfc_sli4_sp_process_cq(cq);
14978 }
14979
14980 /**
14981  * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
14982  * @work: pointer to work element
14983  *
14984  * translates from the work handler and calls the slow-path handler.
14985  **/
14986 static void
14987 lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
14988 {
14989         struct lpfc_queue *cq = container_of(to_delayed_work(work),
14990                                         struct lpfc_queue, sched_spwork);
14991
14992         __lpfc_sli4_sp_process_cq(cq);
14993 }
14994
14995 /**
14996  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
14997  * @phba: Pointer to HBA context object.
14998  * @cq: Pointer to associated CQ
14999  * @wcqe: Pointer to work-queue completion queue entry.
15000  *
15001  * This routine process a fast-path work queue completion entry from fast-path
15002  * event queue for FCP command response completion.
15003  **/
15004 static void
15005 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15006                              struct lpfc_wcqe_complete *wcqe)
15007 {
15008         struct lpfc_sli_ring *pring = cq->pring;
15009         struct lpfc_iocbq *cmdiocbq;
15010         struct lpfc_iocbq irspiocbq;
15011         unsigned long iflags;
15012
15013         /* Check for response status */
15014         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
15015                 /* If resource errors reported from HBA, reduce queue
15016                  * depth of the SCSI device.
15017                  */
15018                 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
15019                      IOSTAT_LOCAL_REJECT)) &&
15020                     ((wcqe->parameter & IOERR_PARAM_MASK) ==
15021                      IOERR_NO_RESOURCES))
15022                         phba->lpfc_rampdown_queue_depth(phba);
15023
15024                 /* Log the cmpl status */
15025                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15026                                 "0373 FCP CQE cmpl: status=x%x: "
15027                                 "CQE: %08x %08x %08x %08x\n",
15028                                 bf_get(lpfc_wcqe_c_status, wcqe),
15029                                 wcqe->word0, wcqe->total_data_placed,
15030                                 wcqe->parameter, wcqe->word3);
15031         }
15032
15033         /* Look up the FCP command IOCB and create pseudo response IOCB */
15034         spin_lock_irqsave(&pring->ring_lock, iflags);
15035         pring->stats.iocb_event++;
15036         spin_unlock_irqrestore(&pring->ring_lock, iflags);
15037         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
15038                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
15039         if (unlikely(!cmdiocbq)) {
15040                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15041                                 "0374 FCP complete with no corresponding "
15042                                 "cmdiocb: iotag (%d)\n",
15043                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
15044                 return;
15045         }
15046 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
15047         cmdiocbq->isr_timestamp = cq->isr_timestamp;
15048 #endif
15049         if (cmdiocbq->iocb_cmpl == NULL) {
15050                 if (cmdiocbq->wqe_cmpl) {
15051                         /* For FCP the flag is cleared in wqe_cmpl */
15052                         if (!(cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
15053                             cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
15054                                 spin_lock_irqsave(&phba->hbalock, iflags);
15055                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
15056                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
15057                         }
15058
15059                         /* Pass the cmd_iocb and the wcqe to the upper layer */
15060                         (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
15061                         return;
15062                 }
15063                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15064                                 "0375 FCP cmdiocb not callback function "
15065                                 "iotag: (%d)\n",
15066                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
15067                 return;
15068         }
15069
15070         /* Only SLI4 non-IO commands stil use IOCB */
15071         /* Fake the irspiocb and copy necessary response information */
15072         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
15073
15074         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
15075                 spin_lock_irqsave(&phba->hbalock, iflags);
15076                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
15077                 spin_unlock_irqrestore(&phba->hbalock, iflags);
15078         }
15079
15080         /* Pass the cmd_iocb and the rsp state to the upper layer */
15081         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
15082 }
15083
15084 /**
15085  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
15086  * @phba: Pointer to HBA context object.
15087  * @cq: Pointer to completion queue.
15088  * @wcqe: Pointer to work-queue completion queue entry.
15089  *
15090  * This routine handles an fast-path WQ entry consumed event by invoking the
15091  * proper WQ release routine to the slow-path WQ.
15092  **/
15093 static void
15094 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15095                              struct lpfc_wcqe_release *wcqe)
15096 {
15097         struct lpfc_queue *childwq;
15098         bool wqid_matched = false;
15099         uint16_t hba_wqid;
15100
15101         /* Check for fast-path FCP work queue release */
15102         hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
15103         list_for_each_entry(childwq, &cq->child_list, list) {
15104                 if (childwq->queue_id == hba_wqid) {
15105                         lpfc_sli4_wq_release(childwq,
15106                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
15107                         if (childwq->q_flag & HBA_NVMET_WQFULL)
15108                                 lpfc_nvmet_wqfull_process(phba, childwq);
15109                         wqid_matched = true;
15110                         break;
15111                 }
15112         }
15113         /* Report warning log message if no match found */
15114         if (wqid_matched != true)
15115                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15116                                 "2580 Fast-path wqe consume event carries "
15117                                 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
15118 }
15119
15120 /**
15121  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
15122  * @phba: Pointer to HBA context object.
15123  * @cq: Pointer to completion queue.
15124  * @rcqe: Pointer to receive-queue completion queue entry.
15125  *
15126  * This routine process a receive-queue completion queue entry.
15127  *
15128  * Return: true if work posted to worker thread, otherwise false.
15129  **/
15130 static bool
15131 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15132                             struct lpfc_rcqe *rcqe)
15133 {
15134         bool workposted = false;
15135         struct lpfc_queue *hrq;
15136         struct lpfc_queue *drq;
15137         struct rqb_dmabuf *dma_buf;
15138         struct fc_frame_header *fc_hdr;
15139         struct lpfc_nvmet_tgtport *tgtp;
15140         uint32_t status, rq_id;
15141         unsigned long iflags;
15142         uint32_t fctl, idx;
15143
15144         if ((phba->nvmet_support == 0) ||
15145             (phba->sli4_hba.nvmet_cqset == NULL))
15146                 return workposted;
15147
15148         idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
15149         hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
15150         drq = phba->sli4_hba.nvmet_mrq_data[idx];
15151
15152         /* sanity check on queue memory */
15153         if (unlikely(!hrq) || unlikely(!drq))
15154                 return workposted;
15155
15156         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
15157                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
15158         else
15159                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
15160
15161         if ((phba->nvmet_support == 0) ||
15162             (rq_id != hrq->queue_id))
15163                 return workposted;
15164
15165         status = bf_get(lpfc_rcqe_status, rcqe);
15166         switch (status) {
15167         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
15168                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15169                                 "6126 Receive Frame Truncated!!\n");
15170                 fallthrough;
15171         case FC_STATUS_RQ_SUCCESS:
15172                 spin_lock_irqsave(&phba->hbalock, iflags);
15173                 lpfc_sli4_rq_release(hrq, drq);
15174                 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15175                 if (!dma_buf) {
15176                         hrq->RQ_no_buf_found++;
15177                         spin_unlock_irqrestore(&phba->hbalock, iflags);
15178                         goto out;
15179                 }
15180                 spin_unlock_irqrestore(&phba->hbalock, iflags);
15181                 hrq->RQ_rcv_buf++;
15182                 hrq->RQ_buf_posted--;
15183                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
15184
15185                 /* Just some basic sanity checks on FCP Command frame */
15186                 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
15187                         fc_hdr->fh_f_ctl[1] << 8 |
15188                         fc_hdr->fh_f_ctl[2]);
15189                 if (((fctl &
15190                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
15191                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
15192                     (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
15193                         goto drop;
15194
15195                 if (fc_hdr->fh_type == FC_TYPE_FCP) {
15196                         dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
15197                         lpfc_nvmet_unsol_fcp_event(
15198                                 phba, idx, dma_buf, cq->isr_timestamp,
15199                                 cq->q_flag & HBA_NVMET_CQ_NOTIFY);
15200                         return false;
15201                 }
15202 drop:
15203                 lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15204                 break;
15205         case FC_STATUS_INSUFF_BUF_FRM_DISC:
15206                 if (phba->nvmet_support) {
15207                         tgtp = phba->targetport->private;
15208                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15209                                         "6401 RQE Error x%x, posted %d err_cnt "
15210                                         "%d: %x %x %x\n",
15211                                         status, hrq->RQ_buf_posted,
15212                                         hrq->RQ_no_posted_buf,
15213                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
15214                                         atomic_read(&tgtp->rcv_fcp_cmd_out),
15215                                         atomic_read(&tgtp->xmt_fcp_release));
15216                 }
15217                 fallthrough;
15218
15219         case FC_STATUS_INSUFF_BUF_NEED_BUF:
15220                 hrq->RQ_no_posted_buf++;
15221                 /* Post more buffers if possible */
15222                 break;
15223         }
15224 out:
15225         return workposted;
15226 }
15227
15228 /**
15229  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
15230  * @phba: adapter with cq
15231  * @cq: Pointer to the completion queue.
15232  * @cqe: Pointer to fast-path completion queue entry.
15233  *
15234  * This routine process a fast-path work queue completion entry from fast-path
15235  * event queue for FCP command response completion.
15236  *
15237  * Return: true if work posted to worker thread, otherwise false.
15238  **/
15239 static bool
15240 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15241                          struct lpfc_cqe *cqe)
15242 {
15243         struct lpfc_wcqe_release wcqe;
15244         bool workposted = false;
15245
15246         /* Copy the work queue CQE and convert endian order if needed */
15247         lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
15248
15249         /* Check and process for different type of WCQE and dispatch */
15250         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
15251         case CQE_CODE_COMPL_WQE:
15252         case CQE_CODE_NVME_ERSP:
15253                 cq->CQ_wq++;
15254                 /* Process the WQ complete event */
15255                 phba->last_completion_time = jiffies;
15256                 if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
15257                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
15258                                 (struct lpfc_wcqe_complete *)&wcqe);
15259                 break;
15260         case CQE_CODE_RELEASE_WQE:
15261                 cq->CQ_release_wqe++;
15262                 /* Process the WQ release event */
15263                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
15264                                 (struct lpfc_wcqe_release *)&wcqe);
15265                 break;
15266         case CQE_CODE_XRI_ABORTED:
15267                 cq->CQ_xri_aborted++;
15268                 /* Process the WQ XRI abort event */
15269                 phba->last_completion_time = jiffies;
15270                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
15271                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
15272                 break;
15273         case CQE_CODE_RECEIVE_V1:
15274         case CQE_CODE_RECEIVE:
15275                 phba->last_completion_time = jiffies;
15276                 if (cq->subtype == LPFC_NVMET) {
15277                         workposted = lpfc_sli4_nvmet_handle_rcqe(
15278                                 phba, cq, (struct lpfc_rcqe *)&wcqe);
15279                 }
15280                 break;
15281         default:
15282                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15283                                 "0144 Not a valid CQE code: x%x\n",
15284                                 bf_get(lpfc_wcqe_c_code, &wcqe));
15285                 break;
15286         }
15287         return workposted;
15288 }
15289
15290 /**
15291  * lpfc_sli4_sched_cq_work - Schedules cq work
15292  * @phba: Pointer to HBA context object.
15293  * @cq: Pointer to CQ
15294  * @cqid: CQ ID
15295  *
15296  * This routine checks the poll mode of the CQ corresponding to
15297  * cq->chann, then either schedules a softirq or queue_work to complete
15298  * cq work.
15299  *
15300  * queue_work path is taken if in NVMET mode, or if poll_mode is in
15301  * LPFC_QUEUE_WORK mode.  Otherwise, softirq path is taken.
15302  *
15303  **/
15304 static void lpfc_sli4_sched_cq_work(struct lpfc_hba *phba,
15305                                     struct lpfc_queue *cq, uint16_t cqid)
15306 {
15307         int ret = 0;
15308
15309         switch (cq->poll_mode) {
15310         case LPFC_IRQ_POLL:
15311                 /* CGN mgmt is mutually exclusive from softirq processing */
15312                 if (phba->cmf_active_mode == LPFC_CFG_OFF) {
15313                         irq_poll_sched(&cq->iop);
15314                         break;
15315                 }
15316                 fallthrough;
15317         case LPFC_QUEUE_WORK:
15318         default:
15319                 if (is_kdump_kernel())
15320                         ret = queue_work(phba->wq, &cq->irqwork);
15321                 else
15322                         ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
15323                 if (!ret)
15324                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15325                                         "0383 Cannot schedule queue work "
15326                                         "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
15327                                         cqid, cq->queue_id,
15328                                         raw_smp_processor_id());
15329         }
15330 }
15331
15332 /**
15333  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
15334  * @phba: Pointer to HBA context object.
15335  * @eq: Pointer to the queue structure.
15336  * @eqe: Pointer to fast-path event queue entry.
15337  *
15338  * This routine process a event queue entry from the fast-path event queue.
15339  * It will check the MajorCode and MinorCode to determine this is for a
15340  * completion event on a completion queue, if not, an error shall be logged
15341  * and just return. Otherwise, it will get to the corresponding completion
15342  * queue and process all the entries on the completion queue, rearm the
15343  * completion queue, and then return.
15344  **/
15345 static void
15346 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
15347                          struct lpfc_eqe *eqe)
15348 {
15349         struct lpfc_queue *cq = NULL;
15350         uint32_t qidx = eq->hdwq;
15351         uint16_t cqid, id;
15352
15353         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
15354                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15355                                 "0366 Not a valid completion "
15356                                 "event: majorcode=x%x, minorcode=x%x\n",
15357                                 bf_get_le32(lpfc_eqe_major_code, eqe),
15358                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
15359                 return;
15360         }
15361
15362         /* Get the reference to the corresponding CQ */
15363         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
15364
15365         /* Use the fast lookup method first */
15366         if (cqid <= phba->sli4_hba.cq_max) {
15367                 cq = phba->sli4_hba.cq_lookup[cqid];
15368                 if (cq)
15369                         goto  work_cq;
15370         }
15371
15372         /* Next check for NVMET completion */
15373         if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
15374                 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
15375                 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
15376                         /* Process NVMET unsol rcv */
15377                         cq = phba->sli4_hba.nvmet_cqset[cqid - id];
15378                         goto  process_cq;
15379                 }
15380         }
15381
15382         if (phba->sli4_hba.nvmels_cq &&
15383             (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
15384                 /* Process NVME unsol rcv */
15385                 cq = phba->sli4_hba.nvmels_cq;
15386         }
15387
15388         /* Otherwise this is a Slow path event */
15389         if (cq == NULL) {
15390                 lpfc_sli4_sp_handle_eqe(phba, eqe,
15391                                         phba->sli4_hba.hdwq[qidx].hba_eq);
15392                 return;
15393         }
15394
15395 process_cq:
15396         if (unlikely(cqid != cq->queue_id)) {
15397                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15398                                 "0368 Miss-matched fast-path completion "
15399                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
15400                                 cqid, cq->queue_id);
15401                 return;
15402         }
15403
15404 work_cq:
15405 #if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
15406         if (phba->ktime_on)
15407                 cq->isr_timestamp = ktime_get_ns();
15408         else
15409                 cq->isr_timestamp = 0;
15410 #endif
15411         lpfc_sli4_sched_cq_work(phba, cq, cqid);
15412 }
15413
15414 /**
15415  * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
15416  * @cq: Pointer to CQ to be processed
15417  * @poll_mode: Enum lpfc_poll_state to determine poll mode
15418  *
15419  * This routine calls the cq processing routine with the handler for
15420  * fast path CQEs.
15421  *
15422  * The CQ routine returns two values: the first is the calling status,
15423  * which indicates whether work was queued to the  background discovery
15424  * thread. If true, the routine should wakeup the discovery thread;
15425  * the second is the delay parameter. If non-zero, rather than rearming
15426  * the CQ and yet another interrupt, the CQ handler should be queued so
15427  * that it is processed in a subsequent polling action. The value of
15428  * the delay indicates when to reschedule it.
15429  **/
15430 static void
15431 __lpfc_sli4_hba_process_cq(struct lpfc_queue *cq,
15432                            enum lpfc_poll_mode poll_mode)
15433 {
15434         struct lpfc_hba *phba = cq->phba;
15435         unsigned long delay;
15436         bool workposted = false;
15437         int ret = 0;
15438
15439         /* process and rearm the CQ */
15440         workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
15441                                              &delay, poll_mode);
15442
15443         if (delay) {
15444                 if (is_kdump_kernel())
15445                         ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
15446                                                 delay);
15447                 else
15448                         ret = queue_delayed_work_on(cq->chann, phba->wq,
15449                                                 &cq->sched_irqwork, delay);
15450                 if (!ret)
15451                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15452                                         "0367 Cannot schedule queue work "
15453                                         "for cqid=%d on CPU %d\n",
15454                                         cq->queue_id, cq->chann);
15455         }
15456
15457         /* wake up worker thread if there are works to be done */
15458         if (workposted)
15459                 lpfc_worker_wake_up(phba);
15460 }
15461
15462 /**
15463  * lpfc_sli4_hba_process_cq - fast-path work handler when started by
15464  *   interrupt
15465  * @work: pointer to work element
15466  *
15467  * translates from the work handler and calls the fast-path handler.
15468  **/
15469 static void
15470 lpfc_sli4_hba_process_cq(struct work_struct *work)
15471 {
15472         struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
15473
15474         __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
15475 }
15476
15477 /**
15478  * lpfc_sli4_dly_hba_process_cq - fast-path work handler when started by timer
15479  * @work: pointer to work element
15480  *
15481  * translates from the work handler and calls the fast-path handler.
15482  **/
15483 static void
15484 lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
15485 {
15486         struct lpfc_queue *cq = container_of(to_delayed_work(work),
15487                                         struct lpfc_queue, sched_irqwork);
15488
15489         __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
15490 }
15491
15492 /**
15493  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
15494  * @irq: Interrupt number.
15495  * @dev_id: The device context pointer.
15496  *
15497  * This function is directly called from the PCI layer as an interrupt
15498  * service routine when device with SLI-4 interface spec is enabled with
15499  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
15500  * ring event in the HBA. However, when the device is enabled with either
15501  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
15502  * device-level interrupt handler. When the PCI slot is in error recovery
15503  * or the HBA is undergoing initialization, the interrupt handler will not
15504  * process the interrupt. The SCSI FCP fast-path ring event are handled in
15505  * the intrrupt context. This function is called without any lock held.
15506  * It gets the hbalock to access and update SLI data structures. Note that,
15507  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
15508  * equal to that of FCP CQ index.
15509  *
15510  * The link attention and ELS ring attention events are handled
15511  * by the worker thread. The interrupt handler signals the worker thread
15512  * and returns for these events. This function is called without any lock
15513  * held. It gets the hbalock to access and update SLI data structures.
15514  *
15515  * This function returns IRQ_HANDLED when interrupt is handled else it
15516  * returns IRQ_NONE.
15517  **/
15518 irqreturn_t
15519 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
15520 {
15521         struct lpfc_hba *phba;
15522         struct lpfc_hba_eq_hdl *hba_eq_hdl;
15523         struct lpfc_queue *fpeq;
15524         unsigned long iflag;
15525         int ecount = 0;
15526         int hba_eqidx;
15527         struct lpfc_eq_intr_info *eqi;
15528
15529         /* Get the driver's phba structure from the dev_id */
15530         hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
15531         phba = hba_eq_hdl->phba;
15532         hba_eqidx = hba_eq_hdl->idx;
15533
15534         if (unlikely(!phba))
15535                 return IRQ_NONE;
15536         if (unlikely(!phba->sli4_hba.hdwq))
15537                 return IRQ_NONE;
15538
15539         /* Get to the EQ struct associated with this vector */
15540         fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
15541         if (unlikely(!fpeq))
15542                 return IRQ_NONE;
15543
15544         /* Check device state for handling interrupt */
15545         if (unlikely(lpfc_intr_state_check(phba))) {
15546                 /* Check again for link_state with lock held */
15547                 spin_lock_irqsave(&phba->hbalock, iflag);
15548                 if (phba->link_state < LPFC_LINK_DOWN)
15549                         /* Flush, clear interrupt, and rearm the EQ */
15550                         lpfc_sli4_eqcq_flush(phba, fpeq);
15551                 spin_unlock_irqrestore(&phba->hbalock, iflag);
15552                 return IRQ_NONE;
15553         }
15554
15555         eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
15556         eqi->icnt++;
15557
15558         fpeq->last_cpu = raw_smp_processor_id();
15559
15560         if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
15561             fpeq->q_flag & HBA_EQ_DELAY_CHK &&
15562             phba->cfg_auto_imax &&
15563             fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
15564             phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
15565                 lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
15566
15567         /* process and rearm the EQ */
15568         ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
15569
15570         if (unlikely(ecount == 0)) {
15571                 fpeq->EQ_no_entry++;
15572                 if (phba->intr_type == MSIX)
15573                         /* MSI-X treated interrupt served as no EQ share INT */
15574                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15575                                         "0358 MSI-X interrupt with no EQE\n");
15576                 else
15577                         /* Non MSI-X treated on interrupt as EQ share INT */
15578                         return IRQ_NONE;
15579         }
15580
15581         return IRQ_HANDLED;
15582 } /* lpfc_sli4_hba_intr_handler */
15583
15584 /**
15585  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
15586  * @irq: Interrupt number.
15587  * @dev_id: The device context pointer.
15588  *
15589  * This function is the device-level interrupt handler to device with SLI-4
15590  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
15591  * interrupt mode is enabled and there is an event in the HBA which requires
15592  * driver attention. This function invokes the slow-path interrupt attention
15593  * handling function and fast-path interrupt attention handling function in
15594  * turn to process the relevant HBA attention events. This function is called
15595  * without any lock held. It gets the hbalock to access and update SLI data
15596  * structures.
15597  *
15598  * This function returns IRQ_HANDLED when interrupt is handled, else it
15599  * returns IRQ_NONE.
15600  **/
15601 irqreturn_t
15602 lpfc_sli4_intr_handler(int irq, void *dev_id)
15603 {
15604         struct lpfc_hba  *phba;
15605         irqreturn_t hba_irq_rc;
15606         bool hba_handled = false;
15607         int qidx;
15608
15609         /* Get the driver's phba structure from the dev_id */
15610         phba = (struct lpfc_hba *)dev_id;
15611
15612         if (unlikely(!phba))
15613                 return IRQ_NONE;
15614
15615         /*
15616          * Invoke fast-path host attention interrupt handling as appropriate.
15617          */
15618         for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
15619                 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
15620                                         &phba->sli4_hba.hba_eq_hdl[qidx]);
15621                 if (hba_irq_rc == IRQ_HANDLED)
15622                         hba_handled |= true;
15623         }
15624
15625         return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
15626 } /* lpfc_sli4_intr_handler */
15627
15628 void lpfc_sli4_poll_hbtimer(struct timer_list *t)
15629 {
15630         struct lpfc_hba *phba = from_timer(phba, t, cpuhp_poll_timer);
15631         struct lpfc_queue *eq;
15632         int i = 0;
15633
15634         rcu_read_lock();
15635
15636         list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
15637                 i += lpfc_sli4_poll_eq(eq, LPFC_POLL_SLOWPATH);
15638         if (!list_empty(&phba->poll_list))
15639                 mod_timer(&phba->cpuhp_poll_timer,
15640                           jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15641
15642         rcu_read_unlock();
15643 }
15644
15645 inline int lpfc_sli4_poll_eq(struct lpfc_queue *eq, uint8_t path)
15646 {
15647         struct lpfc_hba *phba = eq->phba;
15648         int i = 0;
15649
15650         /*
15651          * Unlocking an irq is one of the entry point to check
15652          * for re-schedule, but we are good for io submission
15653          * path as midlayer does a get_cpu to glue us in. Flush
15654          * out the invalidate queue so we can see the updated
15655          * value for flag.
15656          */
15657         smp_rmb();
15658
15659         if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
15660                 /* We will not likely get the completion for the caller
15661                  * during this iteration but i guess that's fine.
15662                  * Future io's coming on this eq should be able to
15663                  * pick it up.  As for the case of single io's, they
15664                  * will be handled through a sched from polling timer
15665                  * function which is currently triggered every 1msec.
15666                  */
15667                 i = lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM);
15668
15669         return i;
15670 }
15671
15672 static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
15673 {
15674         struct lpfc_hba *phba = eq->phba;
15675
15676         /* kickstart slowpath processing if needed */
15677         if (list_empty(&phba->poll_list))
15678                 mod_timer(&phba->cpuhp_poll_timer,
15679                           jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15680
15681         list_add_rcu(&eq->_poll_list, &phba->poll_list);
15682         synchronize_rcu();
15683 }
15684
15685 static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
15686 {
15687         struct lpfc_hba *phba = eq->phba;
15688
15689         /* Disable slowpath processing for this eq.  Kick start the eq
15690          * by RE-ARMING the eq's ASAP
15691          */
15692         list_del_rcu(&eq->_poll_list);
15693         synchronize_rcu();
15694
15695         if (list_empty(&phba->poll_list))
15696                 del_timer_sync(&phba->cpuhp_poll_timer);
15697 }
15698
15699 void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
15700 {
15701         struct lpfc_queue *eq, *next;
15702
15703         list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
15704                 list_del(&eq->_poll_list);
15705
15706         INIT_LIST_HEAD(&phba->poll_list);
15707         synchronize_rcu();
15708 }
15709
15710 static inline void
15711 __lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
15712 {
15713         if (mode == eq->mode)
15714                 return;
15715         /*
15716          * currently this function is only called during a hotplug
15717          * event and the cpu on which this function is executing
15718          * is going offline.  By now the hotplug has instructed
15719          * the scheduler to remove this cpu from cpu active mask.
15720          * So we don't need to work about being put aside by the
15721          * scheduler for a high priority process.  Yes, the inte-
15722          * rrupts could come but they are known to retire ASAP.
15723          */
15724
15725         /* Disable polling in the fastpath */
15726         WRITE_ONCE(eq->mode, mode);
15727         /* flush out the store buffer */
15728         smp_wmb();
15729
15730         /*
15731          * Add this eq to the polling list and start polling. For
15732          * a grace period both interrupt handler and poller will
15733          * try to process the eq _but_ that's fine.  We have a
15734          * synchronization mechanism in place (queue_claimed) to
15735          * deal with it.  This is just a draining phase for int-
15736          * errupt handler (not eq's) as we have guranteed through
15737          * barrier that all the CPUs have seen the new CQ_POLLED
15738          * state. which will effectively disable the REARMING of
15739          * the EQ.  The whole idea is eq's die off eventually as
15740          * we are not rearming EQ's anymore.
15741          */
15742         mode ? lpfc_sli4_add_to_poll_list(eq) :
15743                lpfc_sli4_remove_from_poll_list(eq);
15744 }
15745
15746 void lpfc_sli4_start_polling(struct lpfc_queue *eq)
15747 {
15748         __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
15749 }
15750
15751 void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
15752 {
15753         struct lpfc_hba *phba = eq->phba;
15754
15755         __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
15756
15757         /* Kick start for the pending io's in h/w.
15758          * Once we switch back to interrupt processing on a eq
15759          * the io path completion will only arm eq's when it
15760          * receives a completion.  But since eq's are in disa-
15761          * rmed state it doesn't receive a completion.  This
15762          * creates a deadlock scenaro.
15763          */
15764         phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
15765 }
15766
15767 /**
15768  * lpfc_sli4_queue_free - free a queue structure and associated memory
15769  * @queue: The queue structure to free.
15770  *
15771  * This function frees a queue structure and the DMAable memory used for
15772  * the host resident queue. This function must be called after destroying the
15773  * queue on the HBA.
15774  **/
15775 void
15776 lpfc_sli4_queue_free(struct lpfc_queue *queue)
15777 {
15778         struct lpfc_dmabuf *dmabuf;
15779
15780         if (!queue)
15781                 return;
15782
15783         if (!list_empty(&queue->wq_list))
15784                 list_del(&queue->wq_list);
15785
15786         while (!list_empty(&queue->page_list)) {
15787                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
15788                                  list);
15789                 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
15790                                   dmabuf->virt, dmabuf->phys);
15791                 kfree(dmabuf);
15792         }
15793         if (queue->rqbp) {
15794                 lpfc_free_rq_buffer(queue->phba, queue);
15795                 kfree(queue->rqbp);
15796         }
15797
15798         if (!list_empty(&queue->cpu_list))
15799                 list_del(&queue->cpu_list);
15800
15801         kfree(queue);
15802         return;
15803 }
15804
15805 /**
15806  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
15807  * @phba: The HBA that this queue is being created on.
15808  * @page_size: The size of a queue page
15809  * @entry_size: The size of each queue entry for this queue.
15810  * @entry_count: The number of entries that this queue will handle.
15811  * @cpu: The cpu that will primarily utilize this queue.
15812  *
15813  * This function allocates a queue structure and the DMAable memory used for
15814  * the host resident queue. This function must be called before creating the
15815  * queue on the HBA.
15816  **/
15817 struct lpfc_queue *
15818 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
15819                       uint32_t entry_size, uint32_t entry_count, int cpu)
15820 {
15821         struct lpfc_queue *queue;
15822         struct lpfc_dmabuf *dmabuf;
15823         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15824         uint16_t x, pgcnt;
15825
15826         if (!phba->sli4_hba.pc_sli4_params.supported)
15827                 hw_page_size = page_size;
15828
15829         pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
15830
15831         /* If needed, Adjust page count to match the max the adapter supports */
15832         if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
15833                 pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
15834
15835         queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
15836                              GFP_KERNEL, cpu_to_node(cpu));
15837         if (!queue)
15838                 return NULL;
15839
15840         INIT_LIST_HEAD(&queue->list);
15841         INIT_LIST_HEAD(&queue->_poll_list);
15842         INIT_LIST_HEAD(&queue->wq_list);
15843         INIT_LIST_HEAD(&queue->wqfull_list);
15844         INIT_LIST_HEAD(&queue->page_list);
15845         INIT_LIST_HEAD(&queue->child_list);
15846         INIT_LIST_HEAD(&queue->cpu_list);
15847
15848         /* Set queue parameters now.  If the system cannot provide memory
15849          * resources, the free routine needs to know what was allocated.
15850          */
15851         queue->page_count = pgcnt;
15852         queue->q_pgs = (void **)&queue[1];
15853         queue->entry_cnt_per_pg = hw_page_size / entry_size;
15854         queue->entry_size = entry_size;
15855         queue->entry_count = entry_count;
15856         queue->page_size = hw_page_size;
15857         queue->phba = phba;
15858
15859         for (x = 0; x < queue->page_count; x++) {
15860                 dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
15861                                       dev_to_node(&phba->pcidev->dev));
15862                 if (!dmabuf)
15863                         goto out_fail;
15864                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
15865                                                   hw_page_size, &dmabuf->phys,
15866                                                   GFP_KERNEL);
15867                 if (!dmabuf->virt) {
15868                         kfree(dmabuf);
15869                         goto out_fail;
15870                 }
15871                 dmabuf->buffer_tag = x;
15872                 list_add_tail(&dmabuf->list, &queue->page_list);
15873                 /* use lpfc_sli4_qe to index a paritcular entry in this page */
15874                 queue->q_pgs[x] = dmabuf->virt;
15875         }
15876         INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
15877         INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
15878         INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
15879         INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
15880
15881         /* notify_interval will be set during q creation */
15882
15883         return queue;
15884 out_fail:
15885         lpfc_sli4_queue_free(queue);
15886         return NULL;
15887 }
15888
15889 /**
15890  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
15891  * @phba: HBA structure that indicates port to create a queue on.
15892  * @pci_barset: PCI BAR set flag.
15893  *
15894  * This function shall perform iomap of the specified PCI BAR address to host
15895  * memory address if not already done so and return it. The returned host
15896  * memory address can be NULL.
15897  */
15898 static void __iomem *
15899 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
15900 {
15901         if (!phba->pcidev)
15902                 return NULL;
15903
15904         switch (pci_barset) {
15905         case WQ_PCI_BAR_0_AND_1:
15906                 return phba->pci_bar0_memmap_p;
15907         case WQ_PCI_BAR_2_AND_3:
15908                 return phba->pci_bar2_memmap_p;
15909         case WQ_PCI_BAR_4_AND_5:
15910                 return phba->pci_bar4_memmap_p;
15911         default:
15912                 break;
15913         }
15914         return NULL;
15915 }
15916
15917 /**
15918  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
15919  * @phba: HBA structure that EQs are on.
15920  * @startq: The starting EQ index to modify
15921  * @numq: The number of EQs (consecutive indexes) to modify
15922  * @usdelay: amount of delay
15923  *
15924  * This function revises the EQ delay on 1 or more EQs. The EQ delay
15925  * is set either by writing to a register (if supported by the SLI Port)
15926  * or by mailbox command. The mailbox command allows several EQs to be
15927  * updated at once.
15928  *
15929  * The @phba struct is used to send a mailbox command to HBA. The @startq
15930  * is used to get the starting EQ index to change. The @numq value is
15931  * used to specify how many consecutive EQ indexes, starting at EQ index,
15932  * are to be changed. This function is asynchronous and will wait for any
15933  * mailbox commands to finish before returning.
15934  *
15935  * On success this function will return a zero. If unable to allocate
15936  * enough memory this function will return -ENOMEM. If a mailbox command
15937  * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
15938  * have had their delay multipler changed.
15939  **/
15940 void
15941 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
15942                          uint32_t numq, uint32_t usdelay)
15943 {
15944         struct lpfc_mbx_modify_eq_delay *eq_delay;
15945         LPFC_MBOXQ_t *mbox;
15946         struct lpfc_queue *eq;
15947         int cnt = 0, rc, length;
15948         uint32_t shdr_status, shdr_add_status;
15949         uint32_t dmult;
15950         int qidx;
15951         union lpfc_sli4_cfg_shdr *shdr;
15952
15953         if (startq >= phba->cfg_irq_chann)
15954                 return;
15955
15956         if (usdelay > 0xFFFF) {
15957                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
15958                                 "6429 usdelay %d too large. Scaled down to "
15959                                 "0xFFFF.\n", usdelay);
15960                 usdelay = 0xFFFF;
15961         }
15962
15963         /* set values by EQ_DELAY register if supported */
15964         if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
15965                 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
15966                         eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
15967                         if (!eq)
15968                                 continue;
15969
15970                         lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
15971
15972                         if (++cnt >= numq)
15973                                 break;
15974                 }
15975                 return;
15976         }
15977
15978         /* Otherwise, set values by mailbox cmd */
15979
15980         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15981         if (!mbox) {
15982                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15983                                 "6428 Failed allocating mailbox cmd buffer."
15984                                 " EQ delay was not set.\n");
15985                 return;
15986         }
15987         length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
15988                   sizeof(struct lpfc_sli4_cfg_mhdr));
15989         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15990                          LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
15991                          length, LPFC_SLI4_MBX_EMBED);
15992         eq_delay = &mbox->u.mqe.un.eq_delay;
15993
15994         /* Calculate delay multiper from maximum interrupt per second */
15995         dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
15996         if (dmult)
15997                 dmult--;
15998         if (dmult > LPFC_DMULT_MAX)
15999                 dmult = LPFC_DMULT_MAX;
16000
16001         for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
16002                 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
16003                 if (!eq)
16004                         continue;
16005                 eq->q_mode = usdelay;
16006                 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
16007                 eq_delay->u.request.eq[cnt].phase = 0;
16008                 eq_delay->u.request.eq[cnt].delay_multi = dmult;
16009
16010                 if (++cnt >= numq)
16011                         break;
16012         }
16013         eq_delay->u.request.num_eq = cnt;
16014
16015         mbox->vport = phba->pport;
16016         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16017         mbox->ctx_buf = NULL;
16018         mbox->ctx_ndlp = NULL;
16019         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16020         shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
16021         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16022         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16023         if (shdr_status || shdr_add_status || rc) {
16024                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16025                                 "2512 MODIFY_EQ_DELAY mailbox failed with "
16026                                 "status x%x add_status x%x, mbx status x%x\n",
16027                                 shdr_status, shdr_add_status, rc);
16028         }
16029         mempool_free(mbox, phba->mbox_mem_pool);
16030         return;
16031 }
16032
16033 /**
16034  * lpfc_eq_create - Create an Event Queue on the HBA
16035  * @phba: HBA structure that indicates port to create a queue on.
16036  * @eq: The queue structure to use to create the event queue.
16037  * @imax: The maximum interrupt per second limit.
16038  *
16039  * This function creates an event queue, as detailed in @eq, on a port,
16040  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
16041  *
16042  * The @phba struct is used to send mailbox command to HBA. The @eq struct
16043  * is used to get the entry count and entry size that are necessary to
16044  * determine the number of pages to allocate and use for this queue. This
16045  * function will send the EQ_CREATE mailbox command to the HBA to setup the
16046  * event queue. This function is asynchronous and will wait for the mailbox
16047  * command to finish before continuing.
16048  *
16049  * On success this function will return a zero. If unable to allocate enough
16050  * memory this function will return -ENOMEM. If the queue create mailbox command
16051  * fails this function will return -ENXIO.
16052  **/
16053 int
16054 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
16055 {
16056         struct lpfc_mbx_eq_create *eq_create;
16057         LPFC_MBOXQ_t *mbox;
16058         int rc, length, status = 0;
16059         struct lpfc_dmabuf *dmabuf;
16060         uint32_t shdr_status, shdr_add_status;
16061         union lpfc_sli4_cfg_shdr *shdr;
16062         uint16_t dmult;
16063         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16064
16065         /* sanity check on queue memory */
16066         if (!eq)
16067                 return -ENODEV;
16068         if (!phba->sli4_hba.pc_sli4_params.supported)
16069                 hw_page_size = SLI4_PAGE_SIZE;
16070
16071         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16072         if (!mbox)
16073                 return -ENOMEM;
16074         length = (sizeof(struct lpfc_mbx_eq_create) -
16075                   sizeof(struct lpfc_sli4_cfg_mhdr));
16076         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16077                          LPFC_MBOX_OPCODE_EQ_CREATE,
16078                          length, LPFC_SLI4_MBX_EMBED);
16079         eq_create = &mbox->u.mqe.un.eq_create;
16080         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
16081         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
16082                eq->page_count);
16083         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
16084                LPFC_EQE_SIZE);
16085         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
16086
16087         /* Use version 2 of CREATE_EQ if eqav is set */
16088         if (phba->sli4_hba.pc_sli4_params.eqav) {
16089                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16090                        LPFC_Q_CREATE_VERSION_2);
16091                 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
16092                        phba->sli4_hba.pc_sli4_params.eqav);
16093         }
16094
16095         /* don't setup delay multiplier using EQ_CREATE */
16096         dmult = 0;
16097         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
16098                dmult);
16099         switch (eq->entry_count) {
16100         default:
16101                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16102                                 "0360 Unsupported EQ count. (%d)\n",
16103                                 eq->entry_count);
16104                 if (eq->entry_count < 256) {
16105                         status = -EINVAL;
16106                         goto out;
16107                 }
16108                 fallthrough;    /* otherwise default to smallest count */
16109         case 256:
16110                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16111                        LPFC_EQ_CNT_256);
16112                 break;
16113         case 512:
16114                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16115                        LPFC_EQ_CNT_512);
16116                 break;
16117         case 1024:
16118                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16119                        LPFC_EQ_CNT_1024);
16120                 break;
16121         case 2048:
16122                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16123                        LPFC_EQ_CNT_2048);
16124                 break;
16125         case 4096:
16126                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16127                        LPFC_EQ_CNT_4096);
16128                 break;
16129         }
16130         list_for_each_entry(dmabuf, &eq->page_list, list) {
16131                 memset(dmabuf->virt, 0, hw_page_size);
16132                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16133                                         putPaddrLow(dmabuf->phys);
16134                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16135                                         putPaddrHigh(dmabuf->phys);
16136         }
16137         mbox->vport = phba->pport;
16138         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16139         mbox->ctx_buf = NULL;
16140         mbox->ctx_ndlp = NULL;
16141         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16142         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16143         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16144         if (shdr_status || shdr_add_status || rc) {
16145                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16146                                 "2500 EQ_CREATE mailbox failed with "
16147                                 "status x%x add_status x%x, mbx status x%x\n",
16148                                 shdr_status, shdr_add_status, rc);
16149                 status = -ENXIO;
16150         }
16151         eq->type = LPFC_EQ;
16152         eq->subtype = LPFC_NONE;
16153         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
16154         if (eq->queue_id == 0xFFFF)
16155                 status = -ENXIO;
16156         eq->host_index = 0;
16157         eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
16158         eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
16159 out:
16160         mempool_free(mbox, phba->mbox_mem_pool);
16161         return status;
16162 }
16163
16164 static int lpfc_cq_poll_hdler(struct irq_poll *iop, int budget)
16165 {
16166         struct lpfc_queue *cq = container_of(iop, struct lpfc_queue, iop);
16167
16168         __lpfc_sli4_hba_process_cq(cq, LPFC_IRQ_POLL);
16169
16170         return 1;
16171 }
16172
16173 /**
16174  * lpfc_cq_create - Create a Completion Queue on the HBA
16175  * @phba: HBA structure that indicates port to create a queue on.
16176  * @cq: The queue structure to use to create the completion queue.
16177  * @eq: The event queue to bind this completion queue to.
16178  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16179  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16180  *
16181  * This function creates a completion queue, as detailed in @wq, on a port,
16182  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
16183  *
16184  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16185  * is used to get the entry count and entry size that are necessary to
16186  * determine the number of pages to allocate and use for this queue. The @eq
16187  * is used to indicate which event queue to bind this completion queue to. This
16188  * function will send the CQ_CREATE mailbox command to the HBA to setup the
16189  * completion queue. This function is asynchronous and will wait for the mailbox
16190  * command to finish before continuing.
16191  *
16192  * On success this function will return a zero. If unable to allocate enough
16193  * memory this function will return -ENOMEM. If the queue create mailbox command
16194  * fails this function will return -ENXIO.
16195  **/
16196 int
16197 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
16198                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
16199 {
16200         struct lpfc_mbx_cq_create *cq_create;
16201         struct lpfc_dmabuf *dmabuf;
16202         LPFC_MBOXQ_t *mbox;
16203         int rc, length, status = 0;
16204         uint32_t shdr_status, shdr_add_status;
16205         union lpfc_sli4_cfg_shdr *shdr;
16206
16207         /* sanity check on queue memory */
16208         if (!cq || !eq)
16209                 return -ENODEV;
16210
16211         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16212         if (!mbox)
16213                 return -ENOMEM;
16214         length = (sizeof(struct lpfc_mbx_cq_create) -
16215                   sizeof(struct lpfc_sli4_cfg_mhdr));
16216         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16217                          LPFC_MBOX_OPCODE_CQ_CREATE,
16218                          length, LPFC_SLI4_MBX_EMBED);
16219         cq_create = &mbox->u.mqe.un.cq_create;
16220         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
16221         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
16222                     cq->page_count);
16223         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
16224         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
16225         bf_set(lpfc_mbox_hdr_version, &shdr->request,
16226                phba->sli4_hba.pc_sli4_params.cqv);
16227         if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
16228                 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
16229                        (cq->page_size / SLI4_PAGE_SIZE));
16230                 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
16231                        eq->queue_id);
16232                 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
16233                        phba->sli4_hba.pc_sli4_params.cqav);
16234         } else {
16235                 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
16236                        eq->queue_id);
16237         }
16238         switch (cq->entry_count) {
16239         case 2048:
16240         case 4096:
16241                 if (phba->sli4_hba.pc_sli4_params.cqv ==
16242                     LPFC_Q_CREATE_VERSION_2) {
16243                         cq_create->u.request.context.lpfc_cq_context_count =
16244                                 cq->entry_count;
16245                         bf_set(lpfc_cq_context_count,
16246                                &cq_create->u.request.context,
16247                                LPFC_CQ_CNT_WORD7);
16248                         break;
16249                 }
16250                 fallthrough;
16251         default:
16252                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16253                                 "0361 Unsupported CQ count: "
16254                                 "entry cnt %d sz %d pg cnt %d\n",
16255                                 cq->entry_count, cq->entry_size,
16256                                 cq->page_count);
16257                 if (cq->entry_count < 256) {
16258                         status = -EINVAL;
16259                         goto out;
16260                 }
16261                 fallthrough;    /* otherwise default to smallest count */
16262         case 256:
16263                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16264                        LPFC_CQ_CNT_256);
16265                 break;
16266         case 512:
16267                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16268                        LPFC_CQ_CNT_512);
16269                 break;
16270         case 1024:
16271                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16272                        LPFC_CQ_CNT_1024);
16273                 break;
16274         }
16275         list_for_each_entry(dmabuf, &cq->page_list, list) {
16276                 memset(dmabuf->virt, 0, cq->page_size);
16277                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16278                                         putPaddrLow(dmabuf->phys);
16279                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16280                                         putPaddrHigh(dmabuf->phys);
16281         }
16282         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16283
16284         /* The IOCTL status is embedded in the mailbox subheader. */
16285         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16286         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16287         if (shdr_status || shdr_add_status || rc) {
16288                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16289                                 "2501 CQ_CREATE mailbox failed with "
16290                                 "status x%x add_status x%x, mbx status x%x\n",
16291                                 shdr_status, shdr_add_status, rc);
16292                 status = -ENXIO;
16293                 goto out;
16294         }
16295         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16296         if (cq->queue_id == 0xFFFF) {
16297                 status = -ENXIO;
16298                 goto out;
16299         }
16300         /* link the cq onto the parent eq child list */
16301         list_add_tail(&cq->list, &eq->child_list);
16302         /* Set up completion queue's type and subtype */
16303         cq->type = type;
16304         cq->subtype = subtype;
16305         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16306         cq->assoc_qid = eq->queue_id;
16307         cq->assoc_qp = eq;
16308         cq->host_index = 0;
16309         cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16310         cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
16311
16312         if (cq->queue_id > phba->sli4_hba.cq_max)
16313                 phba->sli4_hba.cq_max = cq->queue_id;
16314
16315         irq_poll_init(&cq->iop, LPFC_IRQ_POLL_WEIGHT, lpfc_cq_poll_hdler);
16316 out:
16317         mempool_free(mbox, phba->mbox_mem_pool);
16318         return status;
16319 }
16320
16321 /**
16322  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
16323  * @phba: HBA structure that indicates port to create a queue on.
16324  * @cqp: The queue structure array to use to create the completion queues.
16325  * @hdwq: The hardware queue array  with the EQ to bind completion queues to.
16326  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16327  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16328  *
16329  * This function creates a set of  completion queue, s to support MRQ
16330  * as detailed in @cqp, on a port,
16331  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
16332  *
16333  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16334  * is used to get the entry count and entry size that are necessary to
16335  * determine the number of pages to allocate and use for this queue. The @eq
16336  * is used to indicate which event queue to bind this completion queue to. This
16337  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
16338  * completion queue. This function is asynchronous and will wait for the mailbox
16339  * command to finish before continuing.
16340  *
16341  * On success this function will return a zero. If unable to allocate enough
16342  * memory this function will return -ENOMEM. If the queue create mailbox command
16343  * fails this function will return -ENXIO.
16344  **/
16345 int
16346 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
16347                    struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
16348                    uint32_t subtype)
16349 {
16350         struct lpfc_queue *cq;
16351         struct lpfc_queue *eq;
16352         struct lpfc_mbx_cq_create_set *cq_set;
16353         struct lpfc_dmabuf *dmabuf;
16354         LPFC_MBOXQ_t *mbox;
16355         int rc, length, alloclen, status = 0;
16356         int cnt, idx, numcq, page_idx = 0;
16357         uint32_t shdr_status, shdr_add_status;
16358         union lpfc_sli4_cfg_shdr *shdr;
16359         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16360
16361         /* sanity check on queue memory */
16362         numcq = phba->cfg_nvmet_mrq;
16363         if (!cqp || !hdwq || !numcq)
16364                 return -ENODEV;
16365
16366         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16367         if (!mbox)
16368                 return -ENOMEM;
16369
16370         length = sizeof(struct lpfc_mbx_cq_create_set);
16371         length += ((numcq * cqp[0]->page_count) *
16372                    sizeof(struct dma_address));
16373         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16374                         LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
16375                         LPFC_SLI4_MBX_NEMBED);
16376         if (alloclen < length) {
16377                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16378                                 "3098 Allocated DMA memory size (%d) is "
16379                                 "less than the requested DMA memory size "
16380                                 "(%d)\n", alloclen, length);
16381                 status = -ENOMEM;
16382                 goto out;
16383         }
16384         cq_set = mbox->sge_array->addr[0];
16385         shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
16386         bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
16387
16388         for (idx = 0; idx < numcq; idx++) {
16389                 cq = cqp[idx];
16390                 eq = hdwq[idx].hba_eq;
16391                 if (!cq || !eq) {
16392                         status = -ENOMEM;
16393                         goto out;
16394                 }
16395                 if (!phba->sli4_hba.pc_sli4_params.supported)
16396                         hw_page_size = cq->page_size;
16397
16398                 switch (idx) {
16399                 case 0:
16400                         bf_set(lpfc_mbx_cq_create_set_page_size,
16401                                &cq_set->u.request,
16402                                (hw_page_size / SLI4_PAGE_SIZE));
16403                         bf_set(lpfc_mbx_cq_create_set_num_pages,
16404                                &cq_set->u.request, cq->page_count);
16405                         bf_set(lpfc_mbx_cq_create_set_evt,
16406                                &cq_set->u.request, 1);
16407                         bf_set(lpfc_mbx_cq_create_set_valid,
16408                                &cq_set->u.request, 1);
16409                         bf_set(lpfc_mbx_cq_create_set_cqe_size,
16410                                &cq_set->u.request, 0);
16411                         bf_set(lpfc_mbx_cq_create_set_num_cq,
16412                                &cq_set->u.request, numcq);
16413                         bf_set(lpfc_mbx_cq_create_set_autovalid,
16414                                &cq_set->u.request,
16415                                phba->sli4_hba.pc_sli4_params.cqav);
16416                         switch (cq->entry_count) {
16417                         case 2048:
16418                         case 4096:
16419                                 if (phba->sli4_hba.pc_sli4_params.cqv ==
16420                                     LPFC_Q_CREATE_VERSION_2) {
16421                                         bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16422                                                &cq_set->u.request,
16423                                                 cq->entry_count);
16424                                         bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16425                                                &cq_set->u.request,
16426                                                LPFC_CQ_CNT_WORD7);
16427                                         break;
16428                                 }
16429                                 fallthrough;
16430                         default:
16431                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16432                                                 "3118 Bad CQ count. (%d)\n",
16433                                                 cq->entry_count);
16434                                 if (cq->entry_count < 256) {
16435                                         status = -EINVAL;
16436                                         goto out;
16437                                 }
16438                                 fallthrough;    /* otherwise default to smallest */
16439                         case 256:
16440                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16441                                        &cq_set->u.request, LPFC_CQ_CNT_256);
16442                                 break;
16443                         case 512:
16444                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16445                                        &cq_set->u.request, LPFC_CQ_CNT_512);
16446                                 break;
16447                         case 1024:
16448                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16449                                        &cq_set->u.request, LPFC_CQ_CNT_1024);
16450                                 break;
16451                         }
16452                         bf_set(lpfc_mbx_cq_create_set_eq_id0,
16453                                &cq_set->u.request, eq->queue_id);
16454                         break;
16455                 case 1:
16456                         bf_set(lpfc_mbx_cq_create_set_eq_id1,
16457                                &cq_set->u.request, eq->queue_id);
16458                         break;
16459                 case 2:
16460                         bf_set(lpfc_mbx_cq_create_set_eq_id2,
16461                                &cq_set->u.request, eq->queue_id);
16462                         break;
16463                 case 3:
16464                         bf_set(lpfc_mbx_cq_create_set_eq_id3,
16465                                &cq_set->u.request, eq->queue_id);
16466                         break;
16467                 case 4:
16468                         bf_set(lpfc_mbx_cq_create_set_eq_id4,
16469                                &cq_set->u.request, eq->queue_id);
16470                         break;
16471                 case 5:
16472                         bf_set(lpfc_mbx_cq_create_set_eq_id5,
16473                                &cq_set->u.request, eq->queue_id);
16474                         break;
16475                 case 6:
16476                         bf_set(lpfc_mbx_cq_create_set_eq_id6,
16477                                &cq_set->u.request, eq->queue_id);
16478                         break;
16479                 case 7:
16480                         bf_set(lpfc_mbx_cq_create_set_eq_id7,
16481                                &cq_set->u.request, eq->queue_id);
16482                         break;
16483                 case 8:
16484                         bf_set(lpfc_mbx_cq_create_set_eq_id8,
16485                                &cq_set->u.request, eq->queue_id);
16486                         break;
16487                 case 9:
16488                         bf_set(lpfc_mbx_cq_create_set_eq_id9,
16489                                &cq_set->u.request, eq->queue_id);
16490                         break;
16491                 case 10:
16492                         bf_set(lpfc_mbx_cq_create_set_eq_id10,
16493                                &cq_set->u.request, eq->queue_id);
16494                         break;
16495                 case 11:
16496                         bf_set(lpfc_mbx_cq_create_set_eq_id11,
16497                                &cq_set->u.request, eq->queue_id);
16498                         break;
16499                 case 12:
16500                         bf_set(lpfc_mbx_cq_create_set_eq_id12,
16501                                &cq_set->u.request, eq->queue_id);
16502                         break;
16503                 case 13:
16504                         bf_set(lpfc_mbx_cq_create_set_eq_id13,
16505                                &cq_set->u.request, eq->queue_id);
16506                         break;
16507                 case 14:
16508                         bf_set(lpfc_mbx_cq_create_set_eq_id14,
16509                                &cq_set->u.request, eq->queue_id);
16510                         break;
16511                 case 15:
16512                         bf_set(lpfc_mbx_cq_create_set_eq_id15,
16513                                &cq_set->u.request, eq->queue_id);
16514                         break;
16515                 }
16516
16517                 /* link the cq onto the parent eq child list */
16518                 list_add_tail(&cq->list, &eq->child_list);
16519                 /* Set up completion queue's type and subtype */
16520                 cq->type = type;
16521                 cq->subtype = subtype;
16522                 cq->assoc_qid = eq->queue_id;
16523                 cq->assoc_qp = eq;
16524                 cq->host_index = 0;
16525                 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16526                 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
16527                                          cq->entry_count);
16528                 cq->chann = idx;
16529
16530                 rc = 0;
16531                 list_for_each_entry(dmabuf, &cq->page_list, list) {
16532                         memset(dmabuf->virt, 0, hw_page_size);
16533                         cnt = page_idx + dmabuf->buffer_tag;
16534                         cq_set->u.request.page[cnt].addr_lo =
16535                                         putPaddrLow(dmabuf->phys);
16536                         cq_set->u.request.page[cnt].addr_hi =
16537                                         putPaddrHigh(dmabuf->phys);
16538                         rc++;
16539                 }
16540                 page_idx += rc;
16541         }
16542
16543         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16544
16545         /* The IOCTL status is embedded in the mailbox subheader. */
16546         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16547         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16548         if (shdr_status || shdr_add_status || rc) {
16549                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16550                                 "3119 CQ_CREATE_SET mailbox failed with "
16551                                 "status x%x add_status x%x, mbx status x%x\n",
16552                                 shdr_status, shdr_add_status, rc);
16553                 status = -ENXIO;
16554                 goto out;
16555         }
16556         rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
16557         if (rc == 0xFFFF) {
16558                 status = -ENXIO;
16559                 goto out;
16560         }
16561
16562         for (idx = 0; idx < numcq; idx++) {
16563                 cq = cqp[idx];
16564                 cq->queue_id = rc + idx;
16565                 if (cq->queue_id > phba->sli4_hba.cq_max)
16566                         phba->sli4_hba.cq_max = cq->queue_id;
16567         }
16568
16569 out:
16570         lpfc_sli4_mbox_cmd_free(phba, mbox);
16571         return status;
16572 }
16573
16574 /**
16575  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
16576  * @phba: HBA structure that indicates port to create a queue on.
16577  * @mq: The queue structure to use to create the mailbox queue.
16578  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
16579  * @cq: The completion queue to associate with this cq.
16580  *
16581  * This function provides failback (fb) functionality when the
16582  * mq_create_ext fails on older FW generations.  It's purpose is identical
16583  * to mq_create_ext otherwise.
16584  *
16585  * This routine cannot fail as all attributes were previously accessed and
16586  * initialized in mq_create_ext.
16587  **/
16588 static void
16589 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
16590                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
16591 {
16592         struct lpfc_mbx_mq_create *mq_create;
16593         struct lpfc_dmabuf *dmabuf;
16594         int length;
16595
16596         length = (sizeof(struct lpfc_mbx_mq_create) -
16597                   sizeof(struct lpfc_sli4_cfg_mhdr));
16598         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16599                          LPFC_MBOX_OPCODE_MQ_CREATE,
16600                          length, LPFC_SLI4_MBX_EMBED);
16601         mq_create = &mbox->u.mqe.un.mq_create;
16602         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
16603                mq->page_count);
16604         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
16605                cq->queue_id);
16606         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
16607         switch (mq->entry_count) {
16608         case 16:
16609                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16610                        LPFC_MQ_RING_SIZE_16);
16611                 break;
16612         case 32:
16613                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16614                        LPFC_MQ_RING_SIZE_32);
16615                 break;
16616         case 64:
16617                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16618                        LPFC_MQ_RING_SIZE_64);
16619                 break;
16620         case 128:
16621                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16622                        LPFC_MQ_RING_SIZE_128);
16623                 break;
16624         }
16625         list_for_each_entry(dmabuf, &mq->page_list, list) {
16626                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16627                         putPaddrLow(dmabuf->phys);
16628                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16629                         putPaddrHigh(dmabuf->phys);
16630         }
16631 }
16632
16633 /**
16634  * lpfc_mq_create - Create a mailbox Queue on the HBA
16635  * @phba: HBA structure that indicates port to create a queue on.
16636  * @mq: The queue structure to use to create the mailbox queue.
16637  * @cq: The completion queue to associate with this cq.
16638  * @subtype: The queue's subtype.
16639  *
16640  * This function creates a mailbox queue, as detailed in @mq, on a port,
16641  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
16642  *
16643  * The @phba struct is used to send mailbox command to HBA. The @cq struct
16644  * is used to get the entry count and entry size that are necessary to
16645  * determine the number of pages to allocate and use for this queue. This
16646  * function will send the MQ_CREATE mailbox command to the HBA to setup the
16647  * mailbox queue. This function is asynchronous and will wait for the mailbox
16648  * command to finish before continuing.
16649  *
16650  * On success this function will return a zero. If unable to allocate enough
16651  * memory this function will return -ENOMEM. If the queue create mailbox command
16652  * fails this function will return -ENXIO.
16653  **/
16654 int32_t
16655 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
16656                struct lpfc_queue *cq, uint32_t subtype)
16657 {
16658         struct lpfc_mbx_mq_create *mq_create;
16659         struct lpfc_mbx_mq_create_ext *mq_create_ext;
16660         struct lpfc_dmabuf *dmabuf;
16661         LPFC_MBOXQ_t *mbox;
16662         int rc, length, status = 0;
16663         uint32_t shdr_status, shdr_add_status;
16664         union lpfc_sli4_cfg_shdr *shdr;
16665         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16666
16667         /* sanity check on queue memory */
16668         if (!mq || !cq)
16669                 return -ENODEV;
16670         if (!phba->sli4_hba.pc_sli4_params.supported)
16671                 hw_page_size = SLI4_PAGE_SIZE;
16672
16673         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16674         if (!mbox)
16675                 return -ENOMEM;
16676         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
16677                   sizeof(struct lpfc_sli4_cfg_mhdr));
16678         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16679                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
16680                          length, LPFC_SLI4_MBX_EMBED);
16681
16682         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
16683         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
16684         bf_set(lpfc_mbx_mq_create_ext_num_pages,
16685                &mq_create_ext->u.request, mq->page_count);
16686         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
16687                &mq_create_ext->u.request, 1);
16688         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
16689                &mq_create_ext->u.request, 1);
16690         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
16691                &mq_create_ext->u.request, 1);
16692         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
16693                &mq_create_ext->u.request, 1);
16694         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
16695                &mq_create_ext->u.request, 1);
16696         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
16697         bf_set(lpfc_mbox_hdr_version, &shdr->request,
16698                phba->sli4_hba.pc_sli4_params.mqv);
16699         if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
16700                 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
16701                        cq->queue_id);
16702         else
16703                 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
16704                        cq->queue_id);
16705         switch (mq->entry_count) {
16706         default:
16707                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16708                                 "0362 Unsupported MQ count. (%d)\n",
16709                                 mq->entry_count);
16710                 if (mq->entry_count < 16) {
16711                         status = -EINVAL;
16712                         goto out;
16713                 }
16714                 fallthrough;    /* otherwise default to smallest count */
16715         case 16:
16716                 bf_set(lpfc_mq_context_ring_size,
16717                        &mq_create_ext->u.request.context,
16718                        LPFC_MQ_RING_SIZE_16);
16719                 break;
16720         case 32:
16721                 bf_set(lpfc_mq_context_ring_size,
16722                        &mq_create_ext->u.request.context,
16723                        LPFC_MQ_RING_SIZE_32);
16724                 break;
16725         case 64:
16726                 bf_set(lpfc_mq_context_ring_size,
16727                        &mq_create_ext->u.request.context,
16728                        LPFC_MQ_RING_SIZE_64);
16729                 break;
16730         case 128:
16731                 bf_set(lpfc_mq_context_ring_size,
16732                        &mq_create_ext->u.request.context,
16733                        LPFC_MQ_RING_SIZE_128);
16734                 break;
16735         }
16736         list_for_each_entry(dmabuf, &mq->page_list, list) {
16737                 memset(dmabuf->virt, 0, hw_page_size);
16738                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
16739                                         putPaddrLow(dmabuf->phys);
16740                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
16741                                         putPaddrHigh(dmabuf->phys);
16742         }
16743         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16744         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16745                               &mq_create_ext->u.response);
16746         if (rc != MBX_SUCCESS) {
16747                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16748                                 "2795 MQ_CREATE_EXT failed with "
16749                                 "status x%x. Failback to MQ_CREATE.\n",
16750                                 rc);
16751                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
16752                 mq_create = &mbox->u.mqe.un.mq_create;
16753                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16754                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
16755                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16756                                       &mq_create->u.response);
16757         }
16758
16759         /* The IOCTL status is embedded in the mailbox subheader. */
16760         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16761         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16762         if (shdr_status || shdr_add_status || rc) {
16763                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16764                                 "2502 MQ_CREATE mailbox failed with "
16765                                 "status x%x add_status x%x, mbx status x%x\n",
16766                                 shdr_status, shdr_add_status, rc);
16767                 status = -ENXIO;
16768                 goto out;
16769         }
16770         if (mq->queue_id == 0xFFFF) {
16771                 status = -ENXIO;
16772                 goto out;
16773         }
16774         mq->type = LPFC_MQ;
16775         mq->assoc_qid = cq->queue_id;
16776         mq->subtype = subtype;
16777         mq->host_index = 0;
16778         mq->hba_index = 0;
16779
16780         /* link the mq onto the parent cq child list */
16781         list_add_tail(&mq->list, &cq->child_list);
16782 out:
16783         mempool_free(mbox, phba->mbox_mem_pool);
16784         return status;
16785 }
16786
16787 /**
16788  * lpfc_wq_create - Create a Work Queue on the HBA
16789  * @phba: HBA structure that indicates port to create a queue on.
16790  * @wq: The queue structure to use to create the work queue.
16791  * @cq: The completion queue to bind this work queue to.
16792  * @subtype: The subtype of the work queue indicating its functionality.
16793  *
16794  * This function creates a work queue, as detailed in @wq, on a port, described
16795  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
16796  *
16797  * The @phba struct is used to send mailbox command to HBA. The @wq struct
16798  * is used to get the entry count and entry size that are necessary to
16799  * determine the number of pages to allocate and use for this queue. The @cq
16800  * is used to indicate which completion queue to bind this work queue to. This
16801  * function will send the WQ_CREATE mailbox command to the HBA to setup the
16802  * work queue. This function is asynchronous and will wait for the mailbox
16803  * command to finish before continuing.
16804  *
16805  * On success this function will return a zero. If unable to allocate enough
16806  * memory this function will return -ENOMEM. If the queue create mailbox command
16807  * fails this function will return -ENXIO.
16808  **/
16809 int
16810 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
16811                struct lpfc_queue *cq, uint32_t subtype)
16812 {
16813         struct lpfc_mbx_wq_create *wq_create;
16814         struct lpfc_dmabuf *dmabuf;
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         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16820         struct dma_address *page;
16821         void __iomem *bar_memmap_p;
16822         uint32_t db_offset;
16823         uint16_t pci_barset;
16824         uint8_t dpp_barset;
16825         uint32_t dpp_offset;
16826         uint8_t wq_create_version;
16827 #ifdef CONFIG_X86
16828         unsigned long pg_addr;
16829 #endif
16830
16831         /* sanity check on queue memory */
16832         if (!wq || !cq)
16833                 return -ENODEV;
16834         if (!phba->sli4_hba.pc_sli4_params.supported)
16835                 hw_page_size = wq->page_size;
16836
16837         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16838         if (!mbox)
16839                 return -ENOMEM;
16840         length = (sizeof(struct lpfc_mbx_wq_create) -
16841                   sizeof(struct lpfc_sli4_cfg_mhdr));
16842         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16843                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
16844                          length, LPFC_SLI4_MBX_EMBED);
16845         wq_create = &mbox->u.mqe.un.wq_create;
16846         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
16847         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
16848                     wq->page_count);
16849         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
16850                     cq->queue_id);
16851
16852         /* wqv is the earliest version supported, NOT the latest */
16853         bf_set(lpfc_mbox_hdr_version, &shdr->request,
16854                phba->sli4_hba.pc_sli4_params.wqv);
16855
16856         if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
16857             (wq->page_size > SLI4_PAGE_SIZE))
16858                 wq_create_version = LPFC_Q_CREATE_VERSION_1;
16859         else
16860                 wq_create_version = LPFC_Q_CREATE_VERSION_0;
16861
16862         switch (wq_create_version) {
16863         case LPFC_Q_CREATE_VERSION_1:
16864                 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
16865                        wq->entry_count);
16866                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16867                        LPFC_Q_CREATE_VERSION_1);
16868
16869                 switch (wq->entry_size) {
16870                 default:
16871                 case 64:
16872                         bf_set(lpfc_mbx_wq_create_wqe_size,
16873                                &wq_create->u.request_1,
16874                                LPFC_WQ_WQE_SIZE_64);
16875                         break;
16876                 case 128:
16877                         bf_set(lpfc_mbx_wq_create_wqe_size,
16878                                &wq_create->u.request_1,
16879                                LPFC_WQ_WQE_SIZE_128);
16880                         break;
16881                 }
16882                 /* Request DPP by default */
16883                 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
16884                 bf_set(lpfc_mbx_wq_create_page_size,
16885                        &wq_create->u.request_1,
16886                        (wq->page_size / SLI4_PAGE_SIZE));
16887                 page = wq_create->u.request_1.page;
16888                 break;
16889         default:
16890                 page = wq_create->u.request.page;
16891                 break;
16892         }
16893
16894         list_for_each_entry(dmabuf, &wq->page_list, list) {
16895                 memset(dmabuf->virt, 0, hw_page_size);
16896                 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
16897                 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
16898         }
16899
16900         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16901                 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
16902
16903         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16904         /* The IOCTL status is embedded in the mailbox subheader. */
16905         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16906         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16907         if (shdr_status || shdr_add_status || rc) {
16908                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16909                                 "2503 WQ_CREATE mailbox failed with "
16910                                 "status x%x add_status x%x, mbx status x%x\n",
16911                                 shdr_status, shdr_add_status, rc);
16912                 status = -ENXIO;
16913                 goto out;
16914         }
16915
16916         if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
16917                 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
16918                                         &wq_create->u.response);
16919         else
16920                 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
16921                                         &wq_create->u.response_1);
16922
16923         if (wq->queue_id == 0xFFFF) {
16924                 status = -ENXIO;
16925                 goto out;
16926         }
16927
16928         wq->db_format = LPFC_DB_LIST_FORMAT;
16929         if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
16930                 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16931                         wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
16932                                                &wq_create->u.response);
16933                         if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
16934                             (wq->db_format != LPFC_DB_RING_FORMAT)) {
16935                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16936                                                 "3265 WQ[%d] doorbell format "
16937                                                 "not supported: x%x\n",
16938                                                 wq->queue_id, wq->db_format);
16939                                 status = -EINVAL;
16940                                 goto out;
16941                         }
16942                         pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
16943                                             &wq_create->u.response);
16944                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16945                                                                    pci_barset);
16946                         if (!bar_memmap_p) {
16947                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16948                                                 "3263 WQ[%d] failed to memmap "
16949                                                 "pci barset:x%x\n",
16950                                                 wq->queue_id, pci_barset);
16951                                 status = -ENOMEM;
16952                                 goto out;
16953                         }
16954                         db_offset = wq_create->u.response.doorbell_offset;
16955                         if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
16956                             (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
16957                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16958                                                 "3252 WQ[%d] doorbell offset "
16959                                                 "not supported: x%x\n",
16960                                                 wq->queue_id, db_offset);
16961                                 status = -EINVAL;
16962                                 goto out;
16963                         }
16964                         wq->db_regaddr = bar_memmap_p + db_offset;
16965                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16966                                         "3264 WQ[%d]: barset:x%x, offset:x%x, "
16967                                         "format:x%x\n", wq->queue_id,
16968                                         pci_barset, db_offset, wq->db_format);
16969                 } else
16970                         wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
16971         } else {
16972                 /* Check if DPP was honored by the firmware */
16973                 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
16974                                     &wq_create->u.response_1);
16975                 if (wq->dpp_enable) {
16976                         pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
16977                                             &wq_create->u.response_1);
16978                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16979                                                                    pci_barset);
16980                         if (!bar_memmap_p) {
16981                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16982                                                 "3267 WQ[%d] failed to memmap "
16983                                                 "pci barset:x%x\n",
16984                                                 wq->queue_id, pci_barset);
16985                                 status = -ENOMEM;
16986                                 goto out;
16987                         }
16988                         db_offset = wq_create->u.response_1.doorbell_offset;
16989                         wq->db_regaddr = bar_memmap_p + db_offset;
16990                         wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
16991                                             &wq_create->u.response_1);
16992                         dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
16993                                             &wq_create->u.response_1);
16994                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16995                                                                    dpp_barset);
16996                         if (!bar_memmap_p) {
16997                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16998                                                 "3268 WQ[%d] failed to memmap "
16999                                                 "pci barset:x%x\n",
17000                                                 wq->queue_id, dpp_barset);
17001                                 status = -ENOMEM;
17002                                 goto out;
17003                         }
17004                         dpp_offset = wq_create->u.response_1.dpp_offset;
17005                         wq->dpp_regaddr = bar_memmap_p + dpp_offset;
17006                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17007                                         "3271 WQ[%d]: barset:x%x, offset:x%x, "
17008                                         "dpp_id:x%x dpp_barset:x%x "
17009                                         "dpp_offset:x%x\n",
17010                                         wq->queue_id, pci_barset, db_offset,
17011                                         wq->dpp_id, dpp_barset, dpp_offset);
17012
17013 #ifdef CONFIG_X86
17014                         /* Enable combined writes for DPP aperture */
17015                         pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
17016                         rc = set_memory_wc(pg_addr, 1);
17017                         if (rc) {
17018                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17019                                         "3272 Cannot setup Combined "
17020                                         "Write on WQ[%d] - disable DPP\n",
17021                                         wq->queue_id);
17022                                 phba->cfg_enable_dpp = 0;
17023                         }
17024 #else
17025                         phba->cfg_enable_dpp = 0;
17026 #endif
17027                 } else
17028                         wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17029         }
17030         wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
17031         if (wq->pring == NULL) {
17032                 status = -ENOMEM;
17033                 goto out;
17034         }
17035         wq->type = LPFC_WQ;
17036         wq->assoc_qid = cq->queue_id;
17037         wq->subtype = subtype;
17038         wq->host_index = 0;
17039         wq->hba_index = 0;
17040         wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
17041
17042         /* link the wq onto the parent cq child list */
17043         list_add_tail(&wq->list, &cq->child_list);
17044 out:
17045         mempool_free(mbox, phba->mbox_mem_pool);
17046         return status;
17047 }
17048
17049 /**
17050  * lpfc_rq_create - Create a Receive Queue on the HBA
17051  * @phba: HBA structure that indicates port to create a queue on.
17052  * @hrq: The queue structure to use to create the header receive queue.
17053  * @drq: The queue structure to use to create the data receive queue.
17054  * @cq: The completion queue to bind this work queue to.
17055  * @subtype: The subtype of the work queue indicating its functionality.
17056  *
17057  * This function creates a receive buffer queue pair , as detailed in @hrq and
17058  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17059  * to the HBA.
17060  *
17061  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17062  * struct is used to get the entry count that is necessary to determine the
17063  * number of pages to use for this queue. The @cq is used to indicate which
17064  * completion queue to bind received buffers that are posted to these queues to.
17065  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17066  * receive queue pair. This function is asynchronous and will wait for the
17067  * mailbox command to finish before continuing.
17068  *
17069  * On success this function will return a zero. If unable to allocate enough
17070  * memory this function will return -ENOMEM. If the queue create mailbox command
17071  * fails this function will return -ENXIO.
17072  **/
17073 int
17074 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17075                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
17076 {
17077         struct lpfc_mbx_rq_create *rq_create;
17078         struct lpfc_dmabuf *dmabuf;
17079         LPFC_MBOXQ_t *mbox;
17080         int rc, length, status = 0;
17081         uint32_t shdr_status, shdr_add_status;
17082         union lpfc_sli4_cfg_shdr *shdr;
17083         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17084         void __iomem *bar_memmap_p;
17085         uint32_t db_offset;
17086         uint16_t pci_barset;
17087
17088         /* sanity check on queue memory */
17089         if (!hrq || !drq || !cq)
17090                 return -ENODEV;
17091         if (!phba->sli4_hba.pc_sli4_params.supported)
17092                 hw_page_size = SLI4_PAGE_SIZE;
17093
17094         if (hrq->entry_count != drq->entry_count)
17095                 return -EINVAL;
17096         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17097         if (!mbox)
17098                 return -ENOMEM;
17099         length = (sizeof(struct lpfc_mbx_rq_create) -
17100                   sizeof(struct lpfc_sli4_cfg_mhdr));
17101         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17102                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17103                          length, LPFC_SLI4_MBX_EMBED);
17104         rq_create = &mbox->u.mqe.un.rq_create;
17105         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17106         bf_set(lpfc_mbox_hdr_version, &shdr->request,
17107                phba->sli4_hba.pc_sli4_params.rqv);
17108         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17109                 bf_set(lpfc_rq_context_rqe_count_1,
17110                        &rq_create->u.request.context,
17111                        hrq->entry_count);
17112                 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
17113                 bf_set(lpfc_rq_context_rqe_size,
17114                        &rq_create->u.request.context,
17115                        LPFC_RQE_SIZE_8);
17116                 bf_set(lpfc_rq_context_page_size,
17117                        &rq_create->u.request.context,
17118                        LPFC_RQ_PAGE_SIZE_4096);
17119         } else {
17120                 switch (hrq->entry_count) {
17121                 default:
17122                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17123                                         "2535 Unsupported RQ count. (%d)\n",
17124                                         hrq->entry_count);
17125                         if (hrq->entry_count < 512) {
17126                                 status = -EINVAL;
17127                                 goto out;
17128                         }
17129                         fallthrough;    /* otherwise default to smallest count */
17130                 case 512:
17131                         bf_set(lpfc_rq_context_rqe_count,
17132                                &rq_create->u.request.context,
17133                                LPFC_RQ_RING_SIZE_512);
17134                         break;
17135                 case 1024:
17136                         bf_set(lpfc_rq_context_rqe_count,
17137                                &rq_create->u.request.context,
17138                                LPFC_RQ_RING_SIZE_1024);
17139                         break;
17140                 case 2048:
17141                         bf_set(lpfc_rq_context_rqe_count,
17142                                &rq_create->u.request.context,
17143                                LPFC_RQ_RING_SIZE_2048);
17144                         break;
17145                 case 4096:
17146                         bf_set(lpfc_rq_context_rqe_count,
17147                                &rq_create->u.request.context,
17148                                LPFC_RQ_RING_SIZE_4096);
17149                         break;
17150                 }
17151                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
17152                        LPFC_HDR_BUF_SIZE);
17153         }
17154         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17155                cq->queue_id);
17156         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17157                hrq->page_count);
17158         list_for_each_entry(dmabuf, &hrq->page_list, list) {
17159                 memset(dmabuf->virt, 0, hw_page_size);
17160                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17161                                         putPaddrLow(dmabuf->phys);
17162                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17163                                         putPaddrHigh(dmabuf->phys);
17164         }
17165         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17166                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17167
17168         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17169         /* The IOCTL status is embedded in the mailbox subheader. */
17170         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17171         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17172         if (shdr_status || shdr_add_status || rc) {
17173                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17174                                 "2504 RQ_CREATE mailbox failed with "
17175                                 "status x%x add_status x%x, mbx status x%x\n",
17176                                 shdr_status, shdr_add_status, rc);
17177                 status = -ENXIO;
17178                 goto out;
17179         }
17180         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17181         if (hrq->queue_id == 0xFFFF) {
17182                 status = -ENXIO;
17183                 goto out;
17184         }
17185
17186         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
17187                 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
17188                                         &rq_create->u.response);
17189                 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
17190                     (hrq->db_format != LPFC_DB_RING_FORMAT)) {
17191                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17192                                         "3262 RQ [%d] doorbell format not "
17193                                         "supported: x%x\n", hrq->queue_id,
17194                                         hrq->db_format);
17195                         status = -EINVAL;
17196                         goto out;
17197                 }
17198
17199                 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
17200                                     &rq_create->u.response);
17201                 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
17202                 if (!bar_memmap_p) {
17203                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17204                                         "3269 RQ[%d] failed to memmap pci "
17205                                         "barset:x%x\n", hrq->queue_id,
17206                                         pci_barset);
17207                         status = -ENOMEM;
17208                         goto out;
17209                 }
17210
17211                 db_offset = rq_create->u.response.doorbell_offset;
17212                 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
17213                     (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
17214                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17215                                         "3270 RQ[%d] doorbell offset not "
17216                                         "supported: x%x\n", hrq->queue_id,
17217                                         db_offset);
17218                         status = -EINVAL;
17219                         goto out;
17220                 }
17221                 hrq->db_regaddr = bar_memmap_p + db_offset;
17222                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17223                                 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
17224                                 "format:x%x\n", hrq->queue_id, pci_barset,
17225                                 db_offset, hrq->db_format);
17226         } else {
17227                 hrq->db_format = LPFC_DB_RING_FORMAT;
17228                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17229         }
17230         hrq->type = LPFC_HRQ;
17231         hrq->assoc_qid = cq->queue_id;
17232         hrq->subtype = subtype;
17233         hrq->host_index = 0;
17234         hrq->hba_index = 0;
17235         hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17236
17237         /* now create the data queue */
17238         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17239                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17240                          length, LPFC_SLI4_MBX_EMBED);
17241         bf_set(lpfc_mbox_hdr_version, &shdr->request,
17242                phba->sli4_hba.pc_sli4_params.rqv);
17243         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17244                 bf_set(lpfc_rq_context_rqe_count_1,
17245                        &rq_create->u.request.context, hrq->entry_count);
17246                 if (subtype == LPFC_NVMET)
17247                         rq_create->u.request.context.buffer_size =
17248                                 LPFC_NVMET_DATA_BUF_SIZE;
17249                 else
17250                         rq_create->u.request.context.buffer_size =
17251                                 LPFC_DATA_BUF_SIZE;
17252                 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
17253                        LPFC_RQE_SIZE_8);
17254                 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
17255                        (PAGE_SIZE/SLI4_PAGE_SIZE));
17256         } else {
17257                 switch (drq->entry_count) {
17258                 default:
17259                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17260                                         "2536 Unsupported RQ count. (%d)\n",
17261                                         drq->entry_count);
17262                         if (drq->entry_count < 512) {
17263                                 status = -EINVAL;
17264                                 goto out;
17265                         }
17266                         fallthrough;    /* otherwise default to smallest count */
17267                 case 512:
17268                         bf_set(lpfc_rq_context_rqe_count,
17269                                &rq_create->u.request.context,
17270                                LPFC_RQ_RING_SIZE_512);
17271                         break;
17272                 case 1024:
17273                         bf_set(lpfc_rq_context_rqe_count,
17274                                &rq_create->u.request.context,
17275                                LPFC_RQ_RING_SIZE_1024);
17276                         break;
17277                 case 2048:
17278                         bf_set(lpfc_rq_context_rqe_count,
17279                                &rq_create->u.request.context,
17280                                LPFC_RQ_RING_SIZE_2048);
17281                         break;
17282                 case 4096:
17283                         bf_set(lpfc_rq_context_rqe_count,
17284                                &rq_create->u.request.context,
17285                                LPFC_RQ_RING_SIZE_4096);
17286                         break;
17287                 }
17288                 if (subtype == LPFC_NVMET)
17289                         bf_set(lpfc_rq_context_buf_size,
17290                                &rq_create->u.request.context,
17291                                LPFC_NVMET_DATA_BUF_SIZE);
17292                 else
17293                         bf_set(lpfc_rq_context_buf_size,
17294                                &rq_create->u.request.context,
17295                                LPFC_DATA_BUF_SIZE);
17296         }
17297         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17298                cq->queue_id);
17299         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17300                drq->page_count);
17301         list_for_each_entry(dmabuf, &drq->page_list, list) {
17302                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17303                                         putPaddrLow(dmabuf->phys);
17304                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17305                                         putPaddrHigh(dmabuf->phys);
17306         }
17307         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17308                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17309         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17310         /* The IOCTL status is embedded in the mailbox subheader. */
17311         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17312         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17313         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17314         if (shdr_status || shdr_add_status || rc) {
17315                 status = -ENXIO;
17316                 goto out;
17317         }
17318         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17319         if (drq->queue_id == 0xFFFF) {
17320                 status = -ENXIO;
17321                 goto out;
17322         }
17323         drq->type = LPFC_DRQ;
17324         drq->assoc_qid = cq->queue_id;
17325         drq->subtype = subtype;
17326         drq->host_index = 0;
17327         drq->hba_index = 0;
17328         drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17329
17330         /* link the header and data RQs onto the parent cq child list */
17331         list_add_tail(&hrq->list, &cq->child_list);
17332         list_add_tail(&drq->list, &cq->child_list);
17333
17334 out:
17335         mempool_free(mbox, phba->mbox_mem_pool);
17336         return status;
17337 }
17338
17339 /**
17340  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
17341  * @phba: HBA structure that indicates port to create a queue on.
17342  * @hrqp: The queue structure array to use to create the header receive queues.
17343  * @drqp: The queue structure array to use to create the data receive queues.
17344  * @cqp: The completion queue array to bind these receive queues to.
17345  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
17346  *
17347  * This function creates a receive buffer queue pair , as detailed in @hrq and
17348  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17349  * to the HBA.
17350  *
17351  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17352  * struct is used to get the entry count that is necessary to determine the
17353  * number of pages to use for this queue. The @cq is used to indicate which
17354  * completion queue to bind received buffers that are posted to these queues to.
17355  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17356  * receive queue pair. This function is asynchronous and will wait for the
17357  * mailbox command to finish before continuing.
17358  *
17359  * On success this function will return a zero. If unable to allocate enough
17360  * memory this function will return -ENOMEM. If the queue create mailbox command
17361  * fails this function will return -ENXIO.
17362  **/
17363 int
17364 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
17365                 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
17366                 uint32_t subtype)
17367 {
17368         struct lpfc_queue *hrq, *drq, *cq;
17369         struct lpfc_mbx_rq_create_v2 *rq_create;
17370         struct lpfc_dmabuf *dmabuf;
17371         LPFC_MBOXQ_t *mbox;
17372         int rc, length, alloclen, status = 0;
17373         int cnt, idx, numrq, page_idx = 0;
17374         uint32_t shdr_status, shdr_add_status;
17375         union lpfc_sli4_cfg_shdr *shdr;
17376         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17377
17378         numrq = phba->cfg_nvmet_mrq;
17379         /* sanity check on array memory */
17380         if (!hrqp || !drqp || !cqp || !numrq)
17381                 return -ENODEV;
17382         if (!phba->sli4_hba.pc_sli4_params.supported)
17383                 hw_page_size = SLI4_PAGE_SIZE;
17384
17385         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17386         if (!mbox)
17387                 return -ENOMEM;
17388
17389         length = sizeof(struct lpfc_mbx_rq_create_v2);
17390         length += ((2 * numrq * hrqp[0]->page_count) *
17391                    sizeof(struct dma_address));
17392
17393         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17394                                     LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
17395                                     LPFC_SLI4_MBX_NEMBED);
17396         if (alloclen < length) {
17397                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17398                                 "3099 Allocated DMA memory size (%d) is "
17399                                 "less than the requested DMA memory size "
17400                                 "(%d)\n", alloclen, length);
17401                 status = -ENOMEM;
17402                 goto out;
17403         }
17404
17405
17406
17407         rq_create = mbox->sge_array->addr[0];
17408         shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
17409
17410         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
17411         cnt = 0;
17412
17413         for (idx = 0; idx < numrq; idx++) {
17414                 hrq = hrqp[idx];
17415                 drq = drqp[idx];
17416                 cq  = cqp[idx];
17417
17418                 /* sanity check on queue memory */
17419                 if (!hrq || !drq || !cq) {
17420                         status = -ENODEV;
17421                         goto out;
17422                 }
17423
17424                 if (hrq->entry_count != drq->entry_count) {
17425                         status = -EINVAL;
17426                         goto out;
17427                 }
17428
17429                 if (idx == 0) {
17430                         bf_set(lpfc_mbx_rq_create_num_pages,
17431                                &rq_create->u.request,
17432                                hrq->page_count);
17433                         bf_set(lpfc_mbx_rq_create_rq_cnt,
17434                                &rq_create->u.request, (numrq * 2));
17435                         bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
17436                                1);
17437                         bf_set(lpfc_rq_context_base_cq,
17438                                &rq_create->u.request.context,
17439                                cq->queue_id);
17440                         bf_set(lpfc_rq_context_data_size,
17441                                &rq_create->u.request.context,
17442                                LPFC_NVMET_DATA_BUF_SIZE);
17443                         bf_set(lpfc_rq_context_hdr_size,
17444                                &rq_create->u.request.context,
17445                                LPFC_HDR_BUF_SIZE);
17446                         bf_set(lpfc_rq_context_rqe_count_1,
17447                                &rq_create->u.request.context,
17448                                hrq->entry_count);
17449                         bf_set(lpfc_rq_context_rqe_size,
17450                                &rq_create->u.request.context,
17451                                LPFC_RQE_SIZE_8);
17452                         bf_set(lpfc_rq_context_page_size,
17453                                &rq_create->u.request.context,
17454                                (PAGE_SIZE/SLI4_PAGE_SIZE));
17455                 }
17456                 rc = 0;
17457                 list_for_each_entry(dmabuf, &hrq->page_list, list) {
17458                         memset(dmabuf->virt, 0, hw_page_size);
17459                         cnt = page_idx + dmabuf->buffer_tag;
17460                         rq_create->u.request.page[cnt].addr_lo =
17461                                         putPaddrLow(dmabuf->phys);
17462                         rq_create->u.request.page[cnt].addr_hi =
17463                                         putPaddrHigh(dmabuf->phys);
17464                         rc++;
17465                 }
17466                 page_idx += rc;
17467
17468                 rc = 0;
17469                 list_for_each_entry(dmabuf, &drq->page_list, list) {
17470                         memset(dmabuf->virt, 0, hw_page_size);
17471                         cnt = page_idx + dmabuf->buffer_tag;
17472                         rq_create->u.request.page[cnt].addr_lo =
17473                                         putPaddrLow(dmabuf->phys);
17474                         rq_create->u.request.page[cnt].addr_hi =
17475                                         putPaddrHigh(dmabuf->phys);
17476                         rc++;
17477                 }
17478                 page_idx += rc;
17479
17480                 hrq->db_format = LPFC_DB_RING_FORMAT;
17481                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17482                 hrq->type = LPFC_HRQ;
17483                 hrq->assoc_qid = cq->queue_id;
17484                 hrq->subtype = subtype;
17485                 hrq->host_index = 0;
17486                 hrq->hba_index = 0;
17487                 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17488
17489                 drq->db_format = LPFC_DB_RING_FORMAT;
17490                 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17491                 drq->type = LPFC_DRQ;
17492                 drq->assoc_qid = cq->queue_id;
17493                 drq->subtype = subtype;
17494                 drq->host_index = 0;
17495                 drq->hba_index = 0;
17496                 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17497
17498                 list_add_tail(&hrq->list, &cq->child_list);
17499                 list_add_tail(&drq->list, &cq->child_list);
17500         }
17501
17502         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17503         /* The IOCTL status is embedded in the mailbox subheader. */
17504         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17505         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17506         if (shdr_status || shdr_add_status || rc) {
17507                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17508                                 "3120 RQ_CREATE mailbox failed with "
17509                                 "status x%x add_status x%x, mbx status x%x\n",
17510                                 shdr_status, shdr_add_status, rc);
17511                 status = -ENXIO;
17512                 goto out;
17513         }
17514         rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17515         if (rc == 0xFFFF) {
17516                 status = -ENXIO;
17517                 goto out;
17518         }
17519
17520         /* Initialize all RQs with associated queue id */
17521         for (idx = 0; idx < numrq; idx++) {
17522                 hrq = hrqp[idx];
17523                 hrq->queue_id = rc + (2 * idx);
17524                 drq = drqp[idx];
17525                 drq->queue_id = rc + (2 * idx) + 1;
17526         }
17527
17528 out:
17529         lpfc_sli4_mbox_cmd_free(phba, mbox);
17530         return status;
17531 }
17532
17533 /**
17534  * lpfc_eq_destroy - Destroy an event Queue on the HBA
17535  * @phba: HBA structure that indicates port to destroy a queue on.
17536  * @eq: The queue structure associated with the queue to destroy.
17537  *
17538  * This function destroys a queue, as detailed in @eq by sending an mailbox
17539  * command, specific to the type of queue, to the HBA.
17540  *
17541  * The @eq struct is used to get the queue ID of the queue to destroy.
17542  *
17543  * On success this function will return a zero. If the queue destroy mailbox
17544  * command fails this function will return -ENXIO.
17545  **/
17546 int
17547 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
17548 {
17549         LPFC_MBOXQ_t *mbox;
17550         int rc, length, status = 0;
17551         uint32_t shdr_status, shdr_add_status;
17552         union lpfc_sli4_cfg_shdr *shdr;
17553
17554         /* sanity check on queue memory */
17555         if (!eq)
17556                 return -ENODEV;
17557
17558         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
17559         if (!mbox)
17560                 return -ENOMEM;
17561         length = (sizeof(struct lpfc_mbx_eq_destroy) -
17562                   sizeof(struct lpfc_sli4_cfg_mhdr));
17563         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17564                          LPFC_MBOX_OPCODE_EQ_DESTROY,
17565                          length, LPFC_SLI4_MBX_EMBED);
17566         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
17567                eq->queue_id);
17568         mbox->vport = eq->phba->pport;
17569         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17570
17571         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
17572         /* The IOCTL status is embedded in the mailbox subheader. */
17573         shdr = (union lpfc_sli4_cfg_shdr *)
17574                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
17575         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17576         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17577         if (shdr_status || shdr_add_status || rc) {
17578                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17579                                 "2505 EQ_DESTROY mailbox failed with "
17580                                 "status x%x add_status x%x, mbx status x%x\n",
17581                                 shdr_status, shdr_add_status, rc);
17582                 status = -ENXIO;
17583         }
17584
17585         /* Remove eq from any list */
17586         list_del_init(&eq->list);
17587         mempool_free(mbox, eq->phba->mbox_mem_pool);
17588         return status;
17589 }
17590
17591 /**
17592  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
17593  * @phba: HBA structure that indicates port to destroy a queue on.
17594  * @cq: The queue structure associated with the queue to destroy.
17595  *
17596  * This function destroys a queue, as detailed in @cq by sending an mailbox
17597  * command, specific to the type of queue, to the HBA.
17598  *
17599  * The @cq struct is used to get the queue ID of the queue to destroy.
17600  *
17601  * On success this function will return a zero. If the queue destroy mailbox
17602  * command fails this function will return -ENXIO.
17603  **/
17604 int
17605 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
17606 {
17607         LPFC_MBOXQ_t *mbox;
17608         int rc, length, status = 0;
17609         uint32_t shdr_status, shdr_add_status;
17610         union lpfc_sli4_cfg_shdr *shdr;
17611
17612         /* sanity check on queue memory */
17613         if (!cq)
17614                 return -ENODEV;
17615         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
17616         if (!mbox)
17617                 return -ENOMEM;
17618         length = (sizeof(struct lpfc_mbx_cq_destroy) -
17619                   sizeof(struct lpfc_sli4_cfg_mhdr));
17620         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17621                          LPFC_MBOX_OPCODE_CQ_DESTROY,
17622                          length, LPFC_SLI4_MBX_EMBED);
17623         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
17624                cq->queue_id);
17625         mbox->vport = cq->phba->pport;
17626         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17627         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
17628         /* The IOCTL status is embedded in the mailbox subheader. */
17629         shdr = (union lpfc_sli4_cfg_shdr *)
17630                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
17631         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17632         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17633         if (shdr_status || shdr_add_status || rc) {
17634                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17635                                 "2506 CQ_DESTROY mailbox failed with "
17636                                 "status x%x add_status x%x, mbx status x%x\n",
17637                                 shdr_status, shdr_add_status, rc);
17638                 status = -ENXIO;
17639         }
17640         /* Remove cq from any list */
17641         list_del_init(&cq->list);
17642         mempool_free(mbox, cq->phba->mbox_mem_pool);
17643         return status;
17644 }
17645
17646 /**
17647  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
17648  * @phba: HBA structure that indicates port to destroy a queue on.
17649  * @mq: The queue structure associated with the queue to destroy.
17650  *
17651  * This function destroys a queue, as detailed in @mq by sending an mailbox
17652  * command, specific to the type of queue, to the HBA.
17653  *
17654  * The @mq struct is used to get the queue ID of the queue to destroy.
17655  *
17656  * On success this function will return a zero. If the queue destroy mailbox
17657  * command fails this function will return -ENXIO.
17658  **/
17659 int
17660 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
17661 {
17662         LPFC_MBOXQ_t *mbox;
17663         int rc, length, status = 0;
17664         uint32_t shdr_status, shdr_add_status;
17665         union lpfc_sli4_cfg_shdr *shdr;
17666
17667         /* sanity check on queue memory */
17668         if (!mq)
17669                 return -ENODEV;
17670         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
17671         if (!mbox)
17672                 return -ENOMEM;
17673         length = (sizeof(struct lpfc_mbx_mq_destroy) -
17674                   sizeof(struct lpfc_sli4_cfg_mhdr));
17675         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17676                          LPFC_MBOX_OPCODE_MQ_DESTROY,
17677                          length, LPFC_SLI4_MBX_EMBED);
17678         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
17679                mq->queue_id);
17680         mbox->vport = mq->phba->pport;
17681         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17682         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
17683         /* The IOCTL status is embedded in the mailbox subheader. */
17684         shdr = (union lpfc_sli4_cfg_shdr *)
17685                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
17686         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17687         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17688         if (shdr_status || shdr_add_status || rc) {
17689                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17690                                 "2507 MQ_DESTROY mailbox failed with "
17691                                 "status x%x add_status x%x, mbx status x%x\n",
17692                                 shdr_status, shdr_add_status, rc);
17693                 status = -ENXIO;
17694         }
17695         /* Remove mq from any list */
17696         list_del_init(&mq->list);
17697         mempool_free(mbox, mq->phba->mbox_mem_pool);
17698         return status;
17699 }
17700
17701 /**
17702  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
17703  * @phba: HBA structure that indicates port to destroy a queue on.
17704  * @wq: The queue structure associated with the queue to destroy.
17705  *
17706  * This function destroys a queue, as detailed in @wq by sending an mailbox
17707  * command, specific to the type of queue, to the HBA.
17708  *
17709  * The @wq struct is used to get the queue ID of the queue to destroy.
17710  *
17711  * On success this function will return a zero. If the queue destroy mailbox
17712  * command fails this function will return -ENXIO.
17713  **/
17714 int
17715 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
17716 {
17717         LPFC_MBOXQ_t *mbox;
17718         int rc, length, status = 0;
17719         uint32_t shdr_status, shdr_add_status;
17720         union lpfc_sli4_cfg_shdr *shdr;
17721
17722         /* sanity check on queue memory */
17723         if (!wq)
17724                 return -ENODEV;
17725         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
17726         if (!mbox)
17727                 return -ENOMEM;
17728         length = (sizeof(struct lpfc_mbx_wq_destroy) -
17729                   sizeof(struct lpfc_sli4_cfg_mhdr));
17730         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17731                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
17732                          length, LPFC_SLI4_MBX_EMBED);
17733         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
17734                wq->queue_id);
17735         mbox->vport = wq->phba->pport;
17736         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17737         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
17738         shdr = (union lpfc_sli4_cfg_shdr *)
17739                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
17740         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17741         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17742         if (shdr_status || shdr_add_status || rc) {
17743                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17744                                 "2508 WQ_DESTROY mailbox failed with "
17745                                 "status x%x add_status x%x, mbx status x%x\n",
17746                                 shdr_status, shdr_add_status, rc);
17747                 status = -ENXIO;
17748         }
17749         /* Remove wq from any list */
17750         list_del_init(&wq->list);
17751         kfree(wq->pring);
17752         wq->pring = NULL;
17753         mempool_free(mbox, wq->phba->mbox_mem_pool);
17754         return status;
17755 }
17756
17757 /**
17758  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
17759  * @phba: HBA structure that indicates port to destroy a queue on.
17760  * @hrq: The queue structure associated with the queue to destroy.
17761  * @drq: The queue structure associated with the queue to destroy.
17762  *
17763  * This function destroys a queue, as detailed in @rq by sending an mailbox
17764  * command, specific to the type of queue, to the HBA.
17765  *
17766  * The @rq struct is used to get the queue ID of the queue to destroy.
17767  *
17768  * On success this function will return a zero. If the queue destroy mailbox
17769  * command fails this function will return -ENXIO.
17770  **/
17771 int
17772 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17773                 struct lpfc_queue *drq)
17774 {
17775         LPFC_MBOXQ_t *mbox;
17776         int rc, length, status = 0;
17777         uint32_t shdr_status, shdr_add_status;
17778         union lpfc_sli4_cfg_shdr *shdr;
17779
17780         /* sanity check on queue memory */
17781         if (!hrq || !drq)
17782                 return -ENODEV;
17783         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
17784         if (!mbox)
17785                 return -ENOMEM;
17786         length = (sizeof(struct lpfc_mbx_rq_destroy) -
17787                   sizeof(struct lpfc_sli4_cfg_mhdr));
17788         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17789                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
17790                          length, LPFC_SLI4_MBX_EMBED);
17791         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17792                hrq->queue_id);
17793         mbox->vport = hrq->phba->pport;
17794         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17795         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
17796         /* The IOCTL status is embedded in the mailbox subheader. */
17797         shdr = (union lpfc_sli4_cfg_shdr *)
17798                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17799         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17800         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17801         if (shdr_status || shdr_add_status || rc) {
17802                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17803                                 "2509 RQ_DESTROY mailbox failed with "
17804                                 "status x%x add_status x%x, mbx status x%x\n",
17805                                 shdr_status, shdr_add_status, rc);
17806                 mempool_free(mbox, hrq->phba->mbox_mem_pool);
17807                 return -ENXIO;
17808         }
17809         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17810                drq->queue_id);
17811         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
17812         shdr = (union lpfc_sli4_cfg_shdr *)
17813                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17814         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17815         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17816         if (shdr_status || shdr_add_status || rc) {
17817                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17818                                 "2510 RQ_DESTROY mailbox failed with "
17819                                 "status x%x add_status x%x, mbx status x%x\n",
17820                                 shdr_status, shdr_add_status, rc);
17821                 status = -ENXIO;
17822         }
17823         list_del_init(&hrq->list);
17824         list_del_init(&drq->list);
17825         mempool_free(mbox, hrq->phba->mbox_mem_pool);
17826         return status;
17827 }
17828
17829 /**
17830  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
17831  * @phba: The virtual port for which this call being executed.
17832  * @pdma_phys_addr0: Physical address of the 1st SGL page.
17833  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
17834  * @xritag: the xritag that ties this io to the SGL pages.
17835  *
17836  * This routine will post the sgl pages for the IO that has the xritag
17837  * that is in the iocbq structure. The xritag is assigned during iocbq
17838  * creation and persists for as long as the driver is loaded.
17839  * if the caller has fewer than 256 scatter gather segments to map then
17840  * pdma_phys_addr1 should be 0.
17841  * If the caller needs to map more than 256 scatter gather segment then
17842  * pdma_phys_addr1 should be a valid physical address.
17843  * physical address for SGLs must be 64 byte aligned.
17844  * If you are going to map 2 SGL's then the first one must have 256 entries
17845  * the second sgl can have between 1 and 256 entries.
17846  *
17847  * Return codes:
17848  *      0 - Success
17849  *      -ENXIO, -ENOMEM - Failure
17850  **/
17851 int
17852 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
17853                 dma_addr_t pdma_phys_addr0,
17854                 dma_addr_t pdma_phys_addr1,
17855                 uint16_t xritag)
17856 {
17857         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
17858         LPFC_MBOXQ_t *mbox;
17859         int rc;
17860         uint32_t shdr_status, shdr_add_status;
17861         uint32_t mbox_tmo;
17862         union lpfc_sli4_cfg_shdr *shdr;
17863
17864         if (xritag == NO_XRI) {
17865                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17866                                 "0364 Invalid param:\n");
17867                 return -EINVAL;
17868         }
17869
17870         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17871         if (!mbox)
17872                 return -ENOMEM;
17873
17874         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17875                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17876                         sizeof(struct lpfc_mbx_post_sgl_pages) -
17877                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17878
17879         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
17880                                 &mbox->u.mqe.un.post_sgl_pages;
17881         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
17882         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
17883
17884         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
17885                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
17886         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
17887                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
17888
17889         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
17890                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
17891         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
17892                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
17893         if (!phba->sli4_hba.intr_enable)
17894                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17895         else {
17896                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
17897                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17898         }
17899         /* The IOCTL status is embedded in the mailbox subheader. */
17900         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
17901         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17902         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17903         if (!phba->sli4_hba.intr_enable)
17904                 mempool_free(mbox, phba->mbox_mem_pool);
17905         else if (rc != MBX_TIMEOUT)
17906                 mempool_free(mbox, phba->mbox_mem_pool);
17907         if (shdr_status || shdr_add_status || rc) {
17908                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17909                                 "2511 POST_SGL mailbox failed with "
17910                                 "status x%x add_status x%x, mbx status x%x\n",
17911                                 shdr_status, shdr_add_status, rc);
17912         }
17913         return 0;
17914 }
17915
17916 /**
17917  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
17918  * @phba: pointer to lpfc hba data structure.
17919  *
17920  * This routine is invoked to post rpi header templates to the
17921  * HBA consistent with the SLI-4 interface spec.  This routine
17922  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17923  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17924  *
17925  * Returns
17926  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
17927  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
17928  **/
17929 static uint16_t
17930 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
17931 {
17932         unsigned long xri;
17933
17934         /*
17935          * Fetch the next logical xri.  Because this index is logical,
17936          * the driver starts at 0 each time.
17937          */
17938         spin_lock_irq(&phba->hbalock);
17939         xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
17940                                  phba->sli4_hba.max_cfg_param.max_xri, 0);
17941         if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
17942                 spin_unlock_irq(&phba->hbalock);
17943                 return NO_XRI;
17944         } else {
17945                 set_bit(xri, phba->sli4_hba.xri_bmask);
17946                 phba->sli4_hba.max_cfg_param.xri_used++;
17947         }
17948         spin_unlock_irq(&phba->hbalock);
17949         return xri;
17950 }
17951
17952 /**
17953  * __lpfc_sli4_free_xri - Release an xri for reuse.
17954  * @phba: pointer to lpfc hba data structure.
17955  * @xri: xri to release.
17956  *
17957  * This routine is invoked to release an xri to the pool of
17958  * available rpis maintained by the driver.
17959  **/
17960 static void
17961 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
17962 {
17963         if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
17964                 phba->sli4_hba.max_cfg_param.xri_used--;
17965         }
17966 }
17967
17968 /**
17969  * lpfc_sli4_free_xri - Release an xri for reuse.
17970  * @phba: pointer to lpfc hba data structure.
17971  * @xri: xri to release.
17972  *
17973  * This routine is invoked to release an xri to the pool of
17974  * available rpis maintained by the driver.
17975  **/
17976 void
17977 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
17978 {
17979         spin_lock_irq(&phba->hbalock);
17980         __lpfc_sli4_free_xri(phba, xri);
17981         spin_unlock_irq(&phba->hbalock);
17982 }
17983
17984 /**
17985  * lpfc_sli4_next_xritag - Get an xritag for the io
17986  * @phba: Pointer to HBA context object.
17987  *
17988  * This function gets an xritag for the iocb. If there is no unused xritag
17989  * it will return 0xffff.
17990  * The function returns the allocated xritag if successful, else returns zero.
17991  * Zero is not a valid xritag.
17992  * The caller is not required to hold any lock.
17993  **/
17994 uint16_t
17995 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
17996 {
17997         uint16_t xri_index;
17998
17999         xri_index = lpfc_sli4_alloc_xri(phba);
18000         if (xri_index == NO_XRI)
18001                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18002                                 "2004 Failed to allocate XRI.last XRITAG is %d"
18003                                 " Max XRI is %d, Used XRI is %d\n",
18004                                 xri_index,
18005                                 phba->sli4_hba.max_cfg_param.max_xri,
18006                                 phba->sli4_hba.max_cfg_param.xri_used);
18007         return xri_index;
18008 }
18009
18010 /**
18011  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
18012  * @phba: pointer to lpfc hba data structure.
18013  * @post_sgl_list: pointer to els sgl entry list.
18014  * @post_cnt: number of els sgl entries on the list.
18015  *
18016  * This routine is invoked to post a block of driver's sgl pages to the
18017  * HBA using non-embedded mailbox command. No Lock is held. This routine
18018  * is only called when the driver is loading and after all IO has been
18019  * stopped.
18020  **/
18021 static int
18022 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
18023                             struct list_head *post_sgl_list,
18024                             int post_cnt)
18025 {
18026         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
18027         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18028         struct sgl_page_pairs *sgl_pg_pairs;
18029         void *viraddr;
18030         LPFC_MBOXQ_t *mbox;
18031         uint32_t reqlen, alloclen, pg_pairs;
18032         uint32_t mbox_tmo;
18033         uint16_t xritag_start = 0;
18034         int rc = 0;
18035         uint32_t shdr_status, shdr_add_status;
18036         union lpfc_sli4_cfg_shdr *shdr;
18037
18038         reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
18039                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18040         if (reqlen > SLI4_PAGE_SIZE) {
18041                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18042                                 "2559 Block sgl registration required DMA "
18043                                 "size (%d) great than a page\n", reqlen);
18044                 return -ENOMEM;
18045         }
18046
18047         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18048         if (!mbox)
18049                 return -ENOMEM;
18050
18051         /* Allocate DMA memory and set up the non-embedded mailbox command */
18052         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18053                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
18054                          LPFC_SLI4_MBX_NEMBED);
18055
18056         if (alloclen < reqlen) {
18057                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18058                                 "0285 Allocated DMA memory size (%d) is "
18059                                 "less than the requested DMA memory "
18060                                 "size (%d)\n", alloclen, reqlen);
18061                 lpfc_sli4_mbox_cmd_free(phba, mbox);
18062                 return -ENOMEM;
18063         }
18064         /* Set up the SGL pages in the non-embedded DMA pages */
18065         viraddr = mbox->sge_array->addr[0];
18066         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18067         sgl_pg_pairs = &sgl->sgl_pg_pairs;
18068
18069         pg_pairs = 0;
18070         list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
18071                 /* Set up the sge entry */
18072                 sgl_pg_pairs->sgl_pg0_addr_lo =
18073                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
18074                 sgl_pg_pairs->sgl_pg0_addr_hi =
18075                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
18076                 sgl_pg_pairs->sgl_pg1_addr_lo =
18077                                 cpu_to_le32(putPaddrLow(0));
18078                 sgl_pg_pairs->sgl_pg1_addr_hi =
18079                                 cpu_to_le32(putPaddrHigh(0));
18080
18081                 /* Keep the first xritag on the list */
18082                 if (pg_pairs == 0)
18083                         xritag_start = sglq_entry->sli4_xritag;
18084                 sgl_pg_pairs++;
18085                 pg_pairs++;
18086         }
18087
18088         /* Complete initialization and perform endian conversion. */
18089         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18090         bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
18091         sgl->word0 = cpu_to_le32(sgl->word0);
18092
18093         if (!phba->sli4_hba.intr_enable)
18094                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18095         else {
18096                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18097                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18098         }
18099         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
18100         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18101         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18102         if (!phba->sli4_hba.intr_enable)
18103                 lpfc_sli4_mbox_cmd_free(phba, mbox);
18104         else if (rc != MBX_TIMEOUT)
18105                 lpfc_sli4_mbox_cmd_free(phba, mbox);
18106         if (shdr_status || shdr_add_status || rc) {
18107                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18108                                 "2513 POST_SGL_BLOCK mailbox command failed "
18109                                 "status x%x add_status x%x mbx status x%x\n",
18110                                 shdr_status, shdr_add_status, rc);
18111                 rc = -ENXIO;
18112         }
18113         return rc;
18114 }
18115
18116 /**
18117  * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
18118  * @phba: pointer to lpfc hba data structure.
18119  * @nblist: pointer to nvme buffer list.
18120  * @count: number of scsi buffers on the list.
18121  *
18122  * This routine is invoked to post a block of @count scsi sgl pages from a
18123  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
18124  * No Lock is held.
18125  *
18126  **/
18127 static int
18128 lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
18129                             int count)
18130 {
18131         struct lpfc_io_buf *lpfc_ncmd;
18132         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18133         struct sgl_page_pairs *sgl_pg_pairs;
18134         void *viraddr;
18135         LPFC_MBOXQ_t *mbox;
18136         uint32_t reqlen, alloclen, pg_pairs;
18137         uint32_t mbox_tmo;
18138         uint16_t xritag_start = 0;
18139         int rc = 0;
18140         uint32_t shdr_status, shdr_add_status;
18141         dma_addr_t pdma_phys_bpl1;
18142         union lpfc_sli4_cfg_shdr *shdr;
18143
18144         /* Calculate the requested length of the dma memory */
18145         reqlen = count * sizeof(struct sgl_page_pairs) +
18146                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18147         if (reqlen > SLI4_PAGE_SIZE) {
18148                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
18149                                 "6118 Block sgl registration required DMA "
18150                                 "size (%d) great than a page\n", reqlen);
18151                 return -ENOMEM;
18152         }
18153         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18154         if (!mbox) {
18155                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18156                                 "6119 Failed to allocate mbox cmd memory\n");
18157                 return -ENOMEM;
18158         }
18159
18160         /* Allocate DMA memory and set up the non-embedded mailbox command */
18161         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18162                                     LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
18163                                     reqlen, LPFC_SLI4_MBX_NEMBED);
18164
18165         if (alloclen < reqlen) {
18166                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18167                                 "6120 Allocated DMA memory size (%d) is "
18168                                 "less than the requested DMA memory "
18169                                 "size (%d)\n", alloclen, reqlen);
18170                 lpfc_sli4_mbox_cmd_free(phba, mbox);
18171                 return -ENOMEM;
18172         }
18173
18174         /* Get the first SGE entry from the non-embedded DMA memory */
18175         viraddr = mbox->sge_array->addr[0];
18176
18177         /* Set up the SGL pages in the non-embedded DMA pages */
18178         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18179         sgl_pg_pairs = &sgl->sgl_pg_pairs;
18180
18181         pg_pairs = 0;
18182         list_for_each_entry(lpfc_ncmd, nblist, list) {
18183                 /* Set up the sge entry */
18184                 sgl_pg_pairs->sgl_pg0_addr_lo =
18185                         cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
18186                 sgl_pg_pairs->sgl_pg0_addr_hi =
18187                         cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
18188                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
18189                         pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
18190                                                 SGL_PAGE_SIZE;
18191                 else
18192                         pdma_phys_bpl1 = 0;
18193                 sgl_pg_pairs->sgl_pg1_addr_lo =
18194                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
18195                 sgl_pg_pairs->sgl_pg1_addr_hi =
18196                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
18197                 /* Keep the first xritag on the list */
18198                 if (pg_pairs == 0)
18199                         xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
18200                 sgl_pg_pairs++;
18201                 pg_pairs++;
18202         }
18203         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18204         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
18205         /* Perform endian conversion if necessary */
18206         sgl->word0 = cpu_to_le32(sgl->word0);
18207
18208         if (!phba->sli4_hba.intr_enable) {
18209                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18210         } else {
18211                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18212                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18213         }
18214         shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
18215         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18216         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18217         if (!phba->sli4_hba.intr_enable)
18218                 lpfc_sli4_mbox_cmd_free(phba, mbox);
18219         else if (rc != MBX_TIMEOUT)
18220                 lpfc_sli4_mbox_cmd_free(phba, mbox);
18221         if (shdr_status || shdr_add_status || rc) {
18222                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18223                                 "6125 POST_SGL_BLOCK mailbox command failed "
18224                                 "status x%x add_status x%x mbx status x%x\n",
18225                                 shdr_status, shdr_add_status, rc);
18226                 rc = -ENXIO;
18227         }
18228         return rc;
18229 }
18230
18231 /**
18232  * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
18233  * @phba: pointer to lpfc hba data structure.
18234  * @post_nblist: pointer to the nvme buffer list.
18235  * @sb_count: number of nvme buffers.
18236  *
18237  * This routine walks a list of nvme buffers that was passed in. It attempts
18238  * to construct blocks of nvme buffer sgls which contains contiguous xris and
18239  * uses the non-embedded SGL block post mailbox commands to post to the port.
18240  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
18241  * embedded SGL post mailbox command for posting. The @post_nblist passed in
18242  * must be local list, thus no lock is needed when manipulate the list.
18243  *
18244  * Returns: 0 = failure, non-zero number of successfully posted buffers.
18245  **/
18246 int
18247 lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
18248                            struct list_head *post_nblist, int sb_count)
18249 {
18250         struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
18251         int status, sgl_size;
18252         int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
18253         dma_addr_t pdma_phys_sgl1;
18254         int last_xritag = NO_XRI;
18255         int cur_xritag;
18256         LIST_HEAD(prep_nblist);
18257         LIST_HEAD(blck_nblist);
18258         LIST_HEAD(nvme_nblist);
18259
18260         /* sanity check */
18261         if (sb_count <= 0)
18262                 return -EINVAL;
18263
18264         sgl_size = phba->cfg_sg_dma_buf_size;
18265         list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
18266                 list_del_init(&lpfc_ncmd->list);
18267                 block_cnt++;
18268                 if ((last_xritag != NO_XRI) &&
18269                     (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
18270                         /* a hole in xri block, form a sgl posting block */
18271                         list_splice_init(&prep_nblist, &blck_nblist);
18272                         post_cnt = block_cnt - 1;
18273                         /* prepare list for next posting block */
18274                         list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18275                         block_cnt = 1;
18276                 } else {
18277                         /* prepare list for next posting block */
18278                         list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18279                         /* enough sgls for non-embed sgl mbox command */
18280                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
18281                                 list_splice_init(&prep_nblist, &blck_nblist);
18282                                 post_cnt = block_cnt;
18283                                 block_cnt = 0;
18284                         }
18285                 }
18286                 num_posting++;
18287                 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18288
18289                 /* end of repost sgl list condition for NVME buffers */
18290                 if (num_posting == sb_count) {
18291                         if (post_cnt == 0) {
18292                                 /* last sgl posting block */
18293                                 list_splice_init(&prep_nblist, &blck_nblist);
18294                                 post_cnt = block_cnt;
18295                         } else if (block_cnt == 1) {
18296                                 /* last single sgl with non-contiguous xri */
18297                                 if (sgl_size > SGL_PAGE_SIZE)
18298                                         pdma_phys_sgl1 =
18299                                                 lpfc_ncmd->dma_phys_sgl +
18300                                                 SGL_PAGE_SIZE;
18301                                 else
18302                                         pdma_phys_sgl1 = 0;
18303                                 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18304                                 status = lpfc_sli4_post_sgl(
18305                                                 phba, lpfc_ncmd->dma_phys_sgl,
18306                                                 pdma_phys_sgl1, cur_xritag);
18307                                 if (status) {
18308                                         /* Post error.  Buffer unavailable. */
18309                                         lpfc_ncmd->flags |=
18310                                                 LPFC_SBUF_NOT_POSTED;
18311                                 } else {
18312                                         /* Post success. Bffer available. */
18313                                         lpfc_ncmd->flags &=
18314                                                 ~LPFC_SBUF_NOT_POSTED;
18315                                         lpfc_ncmd->status = IOSTAT_SUCCESS;
18316                                         num_posted++;
18317                                 }
18318                                 /* success, put on NVME buffer sgl list */
18319                                 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18320                         }
18321                 }
18322
18323                 /* continue until a nembed page worth of sgls */
18324                 if (post_cnt == 0)
18325                         continue;
18326
18327                 /* post block of NVME buffer list sgls */
18328                 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
18329                                                      post_cnt);
18330
18331                 /* don't reset xirtag due to hole in xri block */
18332                 if (block_cnt == 0)
18333                         last_xritag = NO_XRI;
18334
18335                 /* reset NVME buffer post count for next round of posting */
18336                 post_cnt = 0;
18337
18338                 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
18339                 while (!list_empty(&blck_nblist)) {
18340                         list_remove_head(&blck_nblist, lpfc_ncmd,
18341                                          struct lpfc_io_buf, list);
18342                         if (status) {
18343                                 /* Post error.  Mark buffer unavailable. */
18344                                 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
18345                         } else {
18346                                 /* Post success, Mark buffer available. */
18347                                 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
18348                                 lpfc_ncmd->status = IOSTAT_SUCCESS;
18349                                 num_posted++;
18350                         }
18351                         list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18352                 }
18353         }
18354         /* Push NVME buffers with sgl posted to the available list */
18355         lpfc_io_buf_replenish(phba, &nvme_nblist);
18356
18357         return num_posted;
18358 }
18359
18360 /**
18361  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
18362  * @phba: pointer to lpfc_hba struct that the frame was received on
18363  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18364  *
18365  * This function checks the fields in the @fc_hdr to see if the FC frame is a
18366  * valid type of frame that the LPFC driver will handle. This function will
18367  * return a zero if the frame is a valid frame or a non zero value when the
18368  * frame does not pass the check.
18369  **/
18370 static int
18371 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
18372 {
18373         /*  make rctl_names static to save stack space */
18374         struct fc_vft_header *fc_vft_hdr;
18375         uint32_t *header = (uint32_t *) fc_hdr;
18376
18377 #define FC_RCTL_MDS_DIAGS       0xF4
18378
18379         switch (fc_hdr->fh_r_ctl) {
18380         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
18381         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
18382         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
18383         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
18384         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
18385         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
18386         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
18387         case FC_RCTL_DD_CMD_STATUS:     /* command status */
18388         case FC_RCTL_ELS_REQ:   /* extended link services request */
18389         case FC_RCTL_ELS_REP:   /* extended link services reply */
18390         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
18391         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
18392         case FC_RCTL_BA_NOP:    /* basic link service NOP */
18393         case FC_RCTL_BA_ABTS:   /* basic link service abort */
18394         case FC_RCTL_BA_RMC:    /* remove connection */
18395         case FC_RCTL_BA_ACC:    /* basic accept */
18396         case FC_RCTL_BA_RJT:    /* basic reject */
18397         case FC_RCTL_BA_PRMT:
18398         case FC_RCTL_ACK_1:     /* acknowledge_1 */
18399         case FC_RCTL_ACK_0:     /* acknowledge_0 */
18400         case FC_RCTL_P_RJT:     /* port reject */
18401         case FC_RCTL_F_RJT:     /* fabric reject */
18402         case FC_RCTL_P_BSY:     /* port busy */
18403         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
18404         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
18405         case FC_RCTL_LCR:       /* link credit reset */
18406         case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
18407         case FC_RCTL_END:       /* end */
18408                 break;
18409         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
18410                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18411                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
18412                 return lpfc_fc_frame_check(phba, fc_hdr);
18413         default:
18414                 goto drop;
18415         }
18416
18417         switch (fc_hdr->fh_type) {
18418         case FC_TYPE_BLS:
18419         case FC_TYPE_ELS:
18420         case FC_TYPE_FCP:
18421         case FC_TYPE_CT:
18422         case FC_TYPE_NVME:
18423                 break;
18424         case FC_TYPE_IP:
18425         case FC_TYPE_ILS:
18426         default:
18427                 goto drop;
18428         }
18429
18430         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
18431                         "2538 Received frame rctl:x%x, type:x%x, "
18432                         "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
18433                         fc_hdr->fh_r_ctl, fc_hdr->fh_type,
18434                         be32_to_cpu(header[0]), be32_to_cpu(header[1]),
18435                         be32_to_cpu(header[2]), be32_to_cpu(header[3]),
18436                         be32_to_cpu(header[4]), be32_to_cpu(header[5]),
18437                         be32_to_cpu(header[6]));
18438         return 0;
18439 drop:
18440         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
18441                         "2539 Dropped frame rctl:x%x type:x%x\n",
18442                         fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18443         return 1;
18444 }
18445
18446 /**
18447  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
18448  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18449  *
18450  * This function processes the FC header to retrieve the VFI from the VF
18451  * header, if one exists. This function will return the VFI if one exists
18452  * or 0 if no VSAN Header exists.
18453  **/
18454 static uint32_t
18455 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
18456 {
18457         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18458
18459         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
18460                 return 0;
18461         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
18462 }
18463
18464 /**
18465  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
18466  * @phba: Pointer to the HBA structure to search for the vport on
18467  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18468  * @fcfi: The FC Fabric ID that the frame came from
18469  * @did: Destination ID to match against
18470  *
18471  * This function searches the @phba for a vport that matches the content of the
18472  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
18473  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
18474  * returns the matching vport pointer or NULL if unable to match frame to a
18475  * vport.
18476  **/
18477 static struct lpfc_vport *
18478 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
18479                        uint16_t fcfi, uint32_t did)
18480 {
18481         struct lpfc_vport **vports;
18482         struct lpfc_vport *vport = NULL;
18483         int i;
18484
18485         if (did == Fabric_DID)
18486                 return phba->pport;
18487         if ((phba->pport->fc_flag & FC_PT2PT) &&
18488                 !(phba->link_state == LPFC_HBA_READY))
18489                 return phba->pport;
18490
18491         vports = lpfc_create_vport_work_array(phba);
18492         if (vports != NULL) {
18493                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
18494                         if (phba->fcf.fcfi == fcfi &&
18495                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
18496                             vports[i]->fc_myDID == did) {
18497                                 vport = vports[i];
18498                                 break;
18499                         }
18500                 }
18501         }
18502         lpfc_destroy_vport_work_array(phba, vports);
18503         return vport;
18504 }
18505
18506 /**
18507  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
18508  * @vport: The vport to work on.
18509  *
18510  * This function updates the receive sequence time stamp for this vport. The
18511  * receive sequence time stamp indicates the time that the last frame of the
18512  * the sequence that has been idle for the longest amount of time was received.
18513  * the driver uses this time stamp to indicate if any received sequences have
18514  * timed out.
18515  **/
18516 static void
18517 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
18518 {
18519         struct lpfc_dmabuf *h_buf;
18520         struct hbq_dmabuf *dmabuf = NULL;
18521
18522         /* get the oldest sequence on the rcv list */
18523         h_buf = list_get_first(&vport->rcv_buffer_list,
18524                                struct lpfc_dmabuf, list);
18525         if (!h_buf)
18526                 return;
18527         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18528         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
18529 }
18530
18531 /**
18532  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
18533  * @vport: The vport that the received sequences were sent to.
18534  *
18535  * This function cleans up all outstanding received sequences. This is called
18536  * by the driver when a link event or user action invalidates all the received
18537  * sequences.
18538  **/
18539 void
18540 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
18541 {
18542         struct lpfc_dmabuf *h_buf, *hnext;
18543         struct lpfc_dmabuf *d_buf, *dnext;
18544         struct hbq_dmabuf *dmabuf = NULL;
18545
18546         /* start with the oldest sequence on the rcv list */
18547         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18548                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18549                 list_del_init(&dmabuf->hbuf.list);
18550                 list_for_each_entry_safe(d_buf, dnext,
18551                                          &dmabuf->dbuf.list, list) {
18552                         list_del_init(&d_buf->list);
18553                         lpfc_in_buf_free(vport->phba, d_buf);
18554                 }
18555                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18556         }
18557 }
18558
18559 /**
18560  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
18561  * @vport: The vport that the received sequences were sent to.
18562  *
18563  * This function determines whether any received sequences have timed out by
18564  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
18565  * indicates that there is at least one timed out sequence this routine will
18566  * go through the received sequences one at a time from most inactive to most
18567  * active to determine which ones need to be cleaned up. Once it has determined
18568  * that a sequence needs to be cleaned up it will simply free up the resources
18569  * without sending an abort.
18570  **/
18571 void
18572 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
18573 {
18574         struct lpfc_dmabuf *h_buf, *hnext;
18575         struct lpfc_dmabuf *d_buf, *dnext;
18576         struct hbq_dmabuf *dmabuf = NULL;
18577         unsigned long timeout;
18578         int abort_count = 0;
18579
18580         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18581                    vport->rcv_buffer_time_stamp);
18582         if (list_empty(&vport->rcv_buffer_list) ||
18583             time_before(jiffies, timeout))
18584                 return;
18585         /* start with the oldest sequence on the rcv list */
18586         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18587                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18588                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18589                            dmabuf->time_stamp);
18590                 if (time_before(jiffies, timeout))
18591                         break;
18592                 abort_count++;
18593                 list_del_init(&dmabuf->hbuf.list);
18594                 list_for_each_entry_safe(d_buf, dnext,
18595                                          &dmabuf->dbuf.list, list) {
18596                         list_del_init(&d_buf->list);
18597                         lpfc_in_buf_free(vport->phba, d_buf);
18598                 }
18599                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18600         }
18601         if (abort_count)
18602                 lpfc_update_rcv_time_stamp(vport);
18603 }
18604
18605 /**
18606  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
18607  * @vport: pointer to a vitural port
18608  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
18609  *
18610  * This function searches through the existing incomplete sequences that have
18611  * been sent to this @vport. If the frame matches one of the incomplete
18612  * sequences then the dbuf in the @dmabuf is added to the list of frames that
18613  * make up that sequence. If no sequence is found that matches this frame then
18614  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
18615  * This function returns a pointer to the first dmabuf in the sequence list that
18616  * the frame was linked to.
18617  **/
18618 static struct hbq_dmabuf *
18619 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18620 {
18621         struct fc_frame_header *new_hdr;
18622         struct fc_frame_header *temp_hdr;
18623         struct lpfc_dmabuf *d_buf;
18624         struct lpfc_dmabuf *h_buf;
18625         struct hbq_dmabuf *seq_dmabuf = NULL;
18626         struct hbq_dmabuf *temp_dmabuf = NULL;
18627         uint8_t found = 0;
18628
18629         INIT_LIST_HEAD(&dmabuf->dbuf.list);
18630         dmabuf->time_stamp = jiffies;
18631         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18632
18633         /* Use the hdr_buf to find the sequence that this frame belongs to */
18634         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18635                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
18636                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18637                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18638                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18639                         continue;
18640                 /* found a pending sequence that matches this frame */
18641                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18642                 break;
18643         }
18644         if (!seq_dmabuf) {
18645                 /*
18646                  * This indicates first frame received for this sequence.
18647                  * Queue the buffer on the vport's rcv_buffer_list.
18648                  */
18649                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18650                 lpfc_update_rcv_time_stamp(vport);
18651                 return dmabuf;
18652         }
18653         temp_hdr = seq_dmabuf->hbuf.virt;
18654         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
18655                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18656                 list_del_init(&seq_dmabuf->hbuf.list);
18657                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18658                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18659                 lpfc_update_rcv_time_stamp(vport);
18660                 return dmabuf;
18661         }
18662         /* move this sequence to the tail to indicate a young sequence */
18663         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
18664         seq_dmabuf->time_stamp = jiffies;
18665         lpfc_update_rcv_time_stamp(vport);
18666         if (list_empty(&seq_dmabuf->dbuf.list)) {
18667                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18668                 return seq_dmabuf;
18669         }
18670         /* find the correct place in the sequence to insert this frame */
18671         d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
18672         while (!found) {
18673                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18674                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
18675                 /*
18676                  * If the frame's sequence count is greater than the frame on
18677                  * the list then insert the frame right after this frame
18678                  */
18679                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
18680                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18681                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
18682                         found = 1;
18683                         break;
18684                 }
18685
18686                 if (&d_buf->list == &seq_dmabuf->dbuf.list)
18687                         break;
18688                 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
18689         }
18690
18691         if (found)
18692                 return seq_dmabuf;
18693         return NULL;
18694 }
18695
18696 /**
18697  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
18698  * @vport: pointer to a vitural port
18699  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18700  *
18701  * This function tries to abort from the partially assembed sequence, described
18702  * by the information from basic abbort @dmabuf. It checks to see whether such
18703  * partially assembled sequence held by the driver. If so, it shall free up all
18704  * the frames from the partially assembled sequence.
18705  *
18706  * Return
18707  * true  -- if there is matching partially assembled sequence present and all
18708  *          the frames freed with the sequence;
18709  * false -- if there is no matching partially assembled sequence present so
18710  *          nothing got aborted in the lower layer driver
18711  **/
18712 static bool
18713 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
18714                             struct hbq_dmabuf *dmabuf)
18715 {
18716         struct fc_frame_header *new_hdr;
18717         struct fc_frame_header *temp_hdr;
18718         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
18719         struct hbq_dmabuf *seq_dmabuf = NULL;
18720
18721         /* Use the hdr_buf to find the sequence that matches this frame */
18722         INIT_LIST_HEAD(&dmabuf->dbuf.list);
18723         INIT_LIST_HEAD(&dmabuf->hbuf.list);
18724         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18725         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18726                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
18727                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18728                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18729                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18730                         continue;
18731                 /* found a pending sequence that matches this frame */
18732                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18733                 break;
18734         }
18735
18736         /* Free up all the frames from the partially assembled sequence */
18737         if (seq_dmabuf) {
18738                 list_for_each_entry_safe(d_buf, n_buf,
18739                                          &seq_dmabuf->dbuf.list, list) {
18740                         list_del_init(&d_buf->list);
18741                         lpfc_in_buf_free(vport->phba, d_buf);
18742                 }
18743                 return true;
18744         }
18745         return false;
18746 }
18747
18748 /**
18749  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
18750  * @vport: pointer to a vitural port
18751  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18752  *
18753  * This function tries to abort from the assembed sequence from upper level
18754  * protocol, described by the information from basic abbort @dmabuf. It
18755  * checks to see whether such pending context exists at upper level protocol.
18756  * If so, it shall clean up the pending context.
18757  *
18758  * Return
18759  * true  -- if there is matching pending context of the sequence cleaned
18760  *          at ulp;
18761  * false -- if there is no matching pending context of the sequence present
18762  *          at ulp.
18763  **/
18764 static bool
18765 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18766 {
18767         struct lpfc_hba *phba = vport->phba;
18768         int handled;
18769
18770         /* Accepting abort at ulp with SLI4 only */
18771         if (phba->sli_rev < LPFC_SLI_REV4)
18772                 return false;
18773
18774         /* Register all caring upper level protocols to attend abort */
18775         handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
18776         if (handled)
18777                 return true;
18778
18779         return false;
18780 }
18781
18782 /**
18783  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
18784  * @phba: Pointer to HBA context object.
18785  * @cmd_iocbq: pointer to the command iocbq structure.
18786  * @rsp_iocbq: pointer to the response iocbq structure.
18787  *
18788  * This function handles the sequence abort response iocb command complete
18789  * event. It properly releases the memory allocated to the sequence abort
18790  * accept iocb.
18791  **/
18792 static void
18793 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
18794                              struct lpfc_iocbq *cmd_iocbq,
18795                              struct lpfc_iocbq *rsp_iocbq)
18796 {
18797         struct lpfc_nodelist *ndlp;
18798
18799         if (cmd_iocbq) {
18800                 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
18801                 lpfc_nlp_put(ndlp);
18802                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
18803         }
18804
18805         /* Failure means BLS ABORT RSP did not get delivered to remote node*/
18806         if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
18807                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18808                         "3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
18809                         rsp_iocbq->iocb.ulpStatus,
18810                         rsp_iocbq->iocb.un.ulpWord[4]);
18811 }
18812
18813 /**
18814  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
18815  * @phba: Pointer to HBA context object.
18816  * @xri: xri id in transaction.
18817  *
18818  * This function validates the xri maps to the known range of XRIs allocated an
18819  * used by the driver.
18820  **/
18821 uint16_t
18822 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
18823                       uint16_t xri)
18824 {
18825         uint16_t i;
18826
18827         for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
18828                 if (xri == phba->sli4_hba.xri_ids[i])
18829                         return i;
18830         }
18831         return NO_XRI;
18832 }
18833
18834 /**
18835  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
18836  * @vport: pointer to a virtual port.
18837  * @fc_hdr: pointer to a FC frame header.
18838  * @aborted: was the partially assembled receive sequence successfully aborted
18839  *
18840  * This function sends a basic response to a previous unsol sequence abort
18841  * event after aborting the sequence handling.
18842  **/
18843 void
18844 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
18845                         struct fc_frame_header *fc_hdr, bool aborted)
18846 {
18847         struct lpfc_hba *phba = vport->phba;
18848         struct lpfc_iocbq *ctiocb = NULL;
18849         struct lpfc_nodelist *ndlp;
18850         uint16_t oxid, rxid, xri, lxri;
18851         uint32_t sid, fctl;
18852         IOCB_t *icmd;
18853         int rc;
18854
18855         if (!lpfc_is_link_up(phba))
18856                 return;
18857
18858         sid = sli4_sid_from_fc_hdr(fc_hdr);
18859         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
18860         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
18861
18862         ndlp = lpfc_findnode_did(vport, sid);
18863         if (!ndlp) {
18864                 ndlp = lpfc_nlp_init(vport, sid);
18865                 if (!ndlp) {
18866                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
18867                                          "1268 Failed to allocate ndlp for "
18868                                          "oxid:x%x SID:x%x\n", oxid, sid);
18869                         return;
18870                 }
18871                 /* Put ndlp onto pport node list */
18872                 lpfc_enqueue_node(vport, ndlp);
18873         }
18874
18875         /* Allocate buffer for rsp iocb */
18876         ctiocb = lpfc_sli_get_iocbq(phba);
18877         if (!ctiocb)
18878                 return;
18879
18880         /* Extract the F_CTL field from FC_HDR */
18881         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
18882
18883         icmd = &ctiocb->iocb;
18884         icmd->un.xseq64.bdl.bdeSize = 0;
18885         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
18886         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
18887         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
18888         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
18889
18890         /* Fill in the rest of iocb fields */
18891         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
18892         icmd->ulpBdeCount = 0;
18893         icmd->ulpLe = 1;
18894         icmd->ulpClass = CLASS3;
18895         icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
18896         ctiocb->context1 = lpfc_nlp_get(ndlp);
18897         if (!ctiocb->context1) {
18898                 lpfc_sli_release_iocbq(phba, ctiocb);
18899                 return;
18900         }
18901
18902         ctiocb->vport = phba->pport;
18903         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
18904         ctiocb->sli4_lxritag = NO_XRI;
18905         ctiocb->sli4_xritag = NO_XRI;
18906
18907         if (fctl & FC_FC_EX_CTX)
18908                 /* Exchange responder sent the abort so we
18909                  * own the oxid.
18910                  */
18911                 xri = oxid;
18912         else
18913                 xri = rxid;
18914         lxri = lpfc_sli4_xri_inrange(phba, xri);
18915         if (lxri != NO_XRI)
18916                 lpfc_set_rrq_active(phba, ndlp, lxri,
18917                         (xri == oxid) ? rxid : oxid, 0);
18918         /* For BA_ABTS from exchange responder, if the logical xri with
18919          * the oxid maps to the FCP XRI range, the port no longer has
18920          * that exchange context, send a BLS_RJT. Override the IOCB for
18921          * a BA_RJT.
18922          */
18923         if ((fctl & FC_FC_EX_CTX) &&
18924             (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
18925                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
18926                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
18927                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
18928                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
18929         }
18930
18931         /* If BA_ABTS failed to abort a partially assembled receive sequence,
18932          * the driver no longer has that exchange, send a BLS_RJT. Override
18933          * the IOCB for a BA_RJT.
18934          */
18935         if (aborted == false) {
18936                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
18937                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
18938                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
18939                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
18940         }
18941
18942         if (fctl & FC_FC_EX_CTX) {
18943                 /* ABTS sent by responder to CT exchange, construction
18944                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
18945                  * field and RX_ID from ABTS for RX_ID field.
18946                  */
18947                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
18948         } else {
18949                 /* ABTS sent by initiator to CT exchange, construction
18950                  * of BA_ACC will need to allocate a new XRI as for the
18951                  * XRI_TAG field.
18952                  */
18953                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
18954         }
18955         bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
18956         bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
18957
18958         /* Xmit CT abts response on exchange <xid> */
18959         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
18960                          "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
18961                          icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
18962
18963         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
18964         if (rc == IOCB_ERROR) {
18965                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
18966                                  "2925 Failed to issue CT ABTS RSP x%x on "
18967                                  "xri x%x, Data x%x\n",
18968                                  icmd->un.xseq64.w5.hcsw.Rctl, oxid,
18969                                  phba->link_state);
18970                 lpfc_nlp_put(ndlp);
18971                 ctiocb->context1 = NULL;
18972                 lpfc_sli_release_iocbq(phba, ctiocb);
18973         }
18974 }
18975
18976 /**
18977  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
18978  * @vport: Pointer to the vport on which this sequence was received
18979  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18980  *
18981  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
18982  * receive sequence is only partially assembed by the driver, it shall abort
18983  * the partially assembled frames for the sequence. Otherwise, if the
18984  * unsolicited receive sequence has been completely assembled and passed to
18985  * the Upper Layer Protocol (ULP), it then mark the per oxid status for the
18986  * unsolicited sequence has been aborted. After that, it will issue a basic
18987  * accept to accept the abort.
18988  **/
18989 static void
18990 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
18991                              struct hbq_dmabuf *dmabuf)
18992 {
18993         struct lpfc_hba *phba = vport->phba;
18994         struct fc_frame_header fc_hdr;
18995         uint32_t fctl;
18996         bool aborted;
18997
18998         /* Make a copy of fc_hdr before the dmabuf being released */
18999         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
19000         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
19001
19002         if (fctl & FC_FC_EX_CTX) {
19003                 /* ABTS by responder to exchange, no cleanup needed */
19004                 aborted = true;
19005         } else {
19006                 /* ABTS by initiator to exchange, need to do cleanup */
19007                 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
19008                 if (aborted == false)
19009                         aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
19010         }
19011         lpfc_in_buf_free(phba, &dmabuf->dbuf);
19012
19013         if (phba->nvmet_support) {
19014                 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
19015                 return;
19016         }
19017
19018         /* Respond with BA_ACC or BA_RJT accordingly */
19019         lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
19020 }
19021
19022 /**
19023  * lpfc_seq_complete - Indicates if a sequence is complete
19024  * @dmabuf: pointer to a dmabuf that describes the FC sequence
19025  *
19026  * This function checks the sequence, starting with the frame described by
19027  * @dmabuf, to see if all the frames associated with this sequence are present.
19028  * the frames associated with this sequence are linked to the @dmabuf using the
19029  * dbuf list. This function looks for two major things. 1) That the first frame
19030  * has a sequence count of zero. 2) There is a frame with last frame of sequence
19031  * set. 3) That there are no holes in the sequence count. The function will
19032  * return 1 when the sequence is complete, otherwise it will return 0.
19033  **/
19034 static int
19035 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
19036 {
19037         struct fc_frame_header *hdr;
19038         struct lpfc_dmabuf *d_buf;
19039         struct hbq_dmabuf *seq_dmabuf;
19040         uint32_t fctl;
19041         int seq_count = 0;
19042
19043         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19044         /* make sure first fame of sequence has a sequence count of zero */
19045         if (hdr->fh_seq_cnt != seq_count)
19046                 return 0;
19047         fctl = (hdr->fh_f_ctl[0] << 16 |
19048                 hdr->fh_f_ctl[1] << 8 |
19049                 hdr->fh_f_ctl[2]);
19050         /* If last frame of sequence we can return success. */
19051         if (fctl & FC_FC_END_SEQ)
19052                 return 1;
19053         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
19054                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19055                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19056                 /* If there is a hole in the sequence count then fail. */
19057                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
19058                         return 0;
19059                 fctl = (hdr->fh_f_ctl[0] << 16 |
19060                         hdr->fh_f_ctl[1] << 8 |
19061                         hdr->fh_f_ctl[2]);
19062                 /* If last frame of sequence we can return success. */
19063                 if (fctl & FC_FC_END_SEQ)
19064                         return 1;
19065         }
19066         return 0;
19067 }
19068
19069 /**
19070  * lpfc_prep_seq - Prep sequence for ULP processing
19071  * @vport: Pointer to the vport on which this sequence was received
19072  * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence
19073  *
19074  * This function takes a sequence, described by a list of frames, and creates
19075  * a list of iocbq structures to describe the sequence. This iocbq list will be
19076  * used to issue to the generic unsolicited sequence handler. This routine
19077  * returns a pointer to the first iocbq in the list. If the function is unable
19078  * to allocate an iocbq then it throw out the received frames that were not
19079  * able to be described and return a pointer to the first iocbq. If unable to
19080  * allocate any iocbqs (including the first) this function will return NULL.
19081  **/
19082 static struct lpfc_iocbq *
19083 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
19084 {
19085         struct hbq_dmabuf *hbq_buf;
19086         struct lpfc_dmabuf *d_buf, *n_buf;
19087         struct lpfc_iocbq *first_iocbq, *iocbq;
19088         struct fc_frame_header *fc_hdr;
19089         uint32_t sid;
19090         uint32_t len, tot_len;
19091         struct ulp_bde64 *pbde;
19092
19093         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19094         /* remove from receive buffer list */
19095         list_del_init(&seq_dmabuf->hbuf.list);
19096         lpfc_update_rcv_time_stamp(vport);
19097         /* get the Remote Port's SID */
19098         sid = sli4_sid_from_fc_hdr(fc_hdr);
19099         tot_len = 0;
19100         /* Get an iocbq struct to fill in. */
19101         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
19102         if (first_iocbq) {
19103                 /* Initialize the first IOCB. */
19104                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
19105                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
19106                 first_iocbq->vport = vport;
19107
19108                 /* Check FC Header to see what TYPE of frame we are rcv'ing */
19109                 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
19110                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
19111                         first_iocbq->iocb.un.rcvels.parmRo =
19112                                 sli4_did_from_fc_hdr(fc_hdr);
19113                         first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
19114                 } else
19115                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
19116                 first_iocbq->iocb.ulpContext = NO_XRI;
19117                 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
19118                         be16_to_cpu(fc_hdr->fh_ox_id);
19119                 /* iocbq is prepped for internal consumption.  Physical vpi. */
19120                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
19121                         vport->phba->vpi_ids[vport->vpi];
19122                 /* put the first buffer into the first IOCBq */
19123                 tot_len = bf_get(lpfc_rcqe_length,
19124                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
19125
19126                 first_iocbq->context2 = &seq_dmabuf->dbuf;
19127                 first_iocbq->context3 = NULL;
19128                 first_iocbq->iocb.ulpBdeCount = 1;
19129                 if (tot_len > LPFC_DATA_BUF_SIZE)
19130                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
19131                                                         LPFC_DATA_BUF_SIZE;
19132                 else
19133                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
19134
19135                 first_iocbq->iocb.un.rcvels.remoteID = sid;
19136
19137                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
19138         }
19139         iocbq = first_iocbq;
19140         /*
19141          * Each IOCBq can have two Buffers assigned, so go through the list
19142          * of buffers for this sequence and save two buffers in each IOCBq
19143          */
19144         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
19145                 if (!iocbq) {
19146                         lpfc_in_buf_free(vport->phba, d_buf);
19147                         continue;
19148                 }
19149                 if (!iocbq->context3) {
19150                         iocbq->context3 = d_buf;
19151                         iocbq->iocb.ulpBdeCount++;
19152                         /* We need to get the size out of the right CQE */
19153                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19154                         len = bf_get(lpfc_rcqe_length,
19155                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
19156                         pbde = (struct ulp_bde64 *)
19157                                         &iocbq->iocb.unsli3.sli3Words[4];
19158                         if (len > LPFC_DATA_BUF_SIZE)
19159                                 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
19160                         else
19161                                 pbde->tus.f.bdeSize = len;
19162
19163                         iocbq->iocb.unsli3.rcvsli3.acc_len += len;
19164                         tot_len += len;
19165                 } else {
19166                         iocbq = lpfc_sli_get_iocbq(vport->phba);
19167                         if (!iocbq) {
19168                                 if (first_iocbq) {
19169                                         first_iocbq->iocb.ulpStatus =
19170                                                         IOSTAT_FCP_RSP_ERROR;
19171                                         first_iocbq->iocb.un.ulpWord[4] =
19172                                                         IOERR_NO_RESOURCES;
19173                                 }
19174                                 lpfc_in_buf_free(vport->phba, d_buf);
19175                                 continue;
19176                         }
19177                         /* We need to get the size out of the right CQE */
19178                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19179                         len = bf_get(lpfc_rcqe_length,
19180                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
19181                         iocbq->context2 = d_buf;
19182                         iocbq->context3 = NULL;
19183                         iocbq->iocb.ulpBdeCount = 1;
19184                         if (len > LPFC_DATA_BUF_SIZE)
19185                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
19186                                                         LPFC_DATA_BUF_SIZE;
19187                         else
19188                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
19189
19190                         tot_len += len;
19191                         iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
19192
19193                         iocbq->iocb.un.rcvels.remoteID = sid;
19194                         list_add_tail(&iocbq->list, &first_iocbq->list);
19195                 }
19196         }
19197         /* Free the sequence's header buffer */
19198         if (!first_iocbq)
19199                 lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
19200
19201         return first_iocbq;
19202 }
19203
19204 static void
19205 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
19206                           struct hbq_dmabuf *seq_dmabuf)
19207 {
19208         struct fc_frame_header *fc_hdr;
19209         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
19210         struct lpfc_hba *phba = vport->phba;
19211
19212         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19213         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
19214         if (!iocbq) {
19215                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19216                                 "2707 Ring %d handler: Failed to allocate "
19217                                 "iocb Rctl x%x Type x%x received\n",
19218                                 LPFC_ELS_RING,
19219                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19220                 return;
19221         }
19222         if (!lpfc_complete_unsol_iocb(phba,
19223                                       phba->sli4_hba.els_wq->pring,
19224                                       iocbq, fc_hdr->fh_r_ctl,
19225                                       fc_hdr->fh_type))
19226                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19227                                 "2540 Ring %d handler: unexpected Rctl "
19228                                 "x%x Type x%x received\n",
19229                                 LPFC_ELS_RING,
19230                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19231
19232         /* Free iocb created in lpfc_prep_seq */
19233         list_for_each_entry_safe(curr_iocb, next_iocb,
19234                 &iocbq->list, list) {
19235                 list_del_init(&curr_iocb->list);
19236                 lpfc_sli_release_iocbq(phba, curr_iocb);
19237         }
19238         lpfc_sli_release_iocbq(phba, iocbq);
19239 }
19240
19241 static void
19242 lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
19243                             struct lpfc_iocbq *rspiocb)
19244 {
19245         struct lpfc_dmabuf *pcmd = cmdiocb->context2;
19246
19247         if (pcmd && pcmd->virt)
19248                 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19249         kfree(pcmd);
19250         lpfc_sli_release_iocbq(phba, cmdiocb);
19251         lpfc_drain_txq(phba);
19252 }
19253
19254 static void
19255 lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
19256                               struct hbq_dmabuf *dmabuf)
19257 {
19258         struct fc_frame_header *fc_hdr;
19259         struct lpfc_hba *phba = vport->phba;
19260         struct lpfc_iocbq *iocbq = NULL;
19261         union  lpfc_wqe *wqe;
19262         struct lpfc_dmabuf *pcmd = NULL;
19263         uint32_t frame_len;
19264         int rc;
19265         unsigned long iflags;
19266
19267         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19268         frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
19269
19270         /* Send the received frame back */
19271         iocbq = lpfc_sli_get_iocbq(phba);
19272         if (!iocbq) {
19273                 /* Queue cq event and wakeup worker thread to process it */
19274                 spin_lock_irqsave(&phba->hbalock, iflags);
19275                 list_add_tail(&dmabuf->cq_event.list,
19276                               &phba->sli4_hba.sp_queue_event);
19277                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
19278                 spin_unlock_irqrestore(&phba->hbalock, iflags);
19279                 lpfc_worker_wake_up(phba);
19280                 return;
19281         }
19282
19283         /* Allocate buffer for command payload */
19284         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
19285         if (pcmd)
19286                 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
19287                                             &pcmd->phys);
19288         if (!pcmd || !pcmd->virt)
19289                 goto exit;
19290
19291         INIT_LIST_HEAD(&pcmd->list);
19292
19293         /* copyin the payload */
19294         memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
19295
19296         /* fill in BDE's for command */
19297         iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
19298         iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
19299         iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
19300         iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
19301
19302         iocbq->context2 = pcmd;
19303         iocbq->vport = vport;
19304         iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
19305         iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
19306
19307         /*
19308          * Setup rest of the iocb as though it were a WQE
19309          * Build the SEND_FRAME WQE
19310          */
19311         wqe = (union lpfc_wqe *)&iocbq->iocb;
19312
19313         wqe->send_frame.frame_len = frame_len;
19314         wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
19315         wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
19316         wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
19317         wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
19318         wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
19319         wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
19320
19321         iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
19322         iocbq->iocb.ulpLe = 1;
19323         iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
19324         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
19325         if (rc == IOCB_ERROR)
19326                 goto exit;
19327
19328         lpfc_in_buf_free(phba, &dmabuf->dbuf);
19329         return;
19330
19331 exit:
19332         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
19333                         "2023 Unable to process MDS loopback frame\n");
19334         if (pcmd && pcmd->virt)
19335                 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19336         kfree(pcmd);
19337         if (iocbq)
19338                 lpfc_sli_release_iocbq(phba, iocbq);
19339         lpfc_in_buf_free(phba, &dmabuf->dbuf);
19340 }
19341
19342 /**
19343  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
19344  * @phba: Pointer to HBA context object.
19345  * @dmabuf: Pointer to a dmabuf that describes the FC sequence.
19346  *
19347  * This function is called with no lock held. This function processes all
19348  * the received buffers and gives it to upper layers when a received buffer
19349  * indicates that it is the final frame in the sequence. The interrupt
19350  * service routine processes received buffers at interrupt contexts.
19351  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
19352  * appropriate receive function when the final frame in a sequence is received.
19353  **/
19354 void
19355 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
19356                                  struct hbq_dmabuf *dmabuf)
19357 {
19358         struct hbq_dmabuf *seq_dmabuf;
19359         struct fc_frame_header *fc_hdr;
19360         struct lpfc_vport *vport;
19361         uint32_t fcfi;
19362         uint32_t did;
19363
19364         /* Process each received buffer */
19365         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19366
19367         if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
19368             fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
19369                 vport = phba->pport;
19370                 /* Handle MDS Loopback frames */
19371                 if  (!(phba->pport->load_flag & FC_UNLOADING))
19372                         lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19373                 else
19374                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
19375                 return;
19376         }
19377
19378         /* check to see if this a valid type of frame */
19379         if (lpfc_fc_frame_check(phba, fc_hdr)) {
19380                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19381                 return;
19382         }
19383
19384         if ((bf_get(lpfc_cqe_code,
19385                     &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
19386                 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
19387                               &dmabuf->cq_event.cqe.rcqe_cmpl);
19388         else
19389                 fcfi = bf_get(lpfc_rcqe_fcf_id,
19390                               &dmabuf->cq_event.cqe.rcqe_cmpl);
19391
19392         if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
19393                 vport = phba->pport;
19394                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
19395                                 "2023 MDS Loopback %d bytes\n",
19396                                 bf_get(lpfc_rcqe_length,
19397                                        &dmabuf->cq_event.cqe.rcqe_cmpl));
19398                 /* Handle MDS Loopback frames */
19399                 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19400                 return;
19401         }
19402
19403         /* d_id this frame is directed to */
19404         did = sli4_did_from_fc_hdr(fc_hdr);
19405
19406         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
19407         if (!vport) {
19408                 /* throw out the frame */
19409                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19410                 return;
19411         }
19412
19413         /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
19414         if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
19415                 (did != Fabric_DID)) {
19416                 /*
19417                  * Throw out the frame if we are not pt2pt.
19418                  * The pt2pt protocol allows for discovery frames
19419                  * to be received without a registered VPI.
19420                  */
19421                 if (!(vport->fc_flag & FC_PT2PT) ||
19422                         (phba->link_state == LPFC_HBA_READY)) {
19423                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
19424                         return;
19425                 }
19426         }
19427
19428         /* Handle the basic abort sequence (BA_ABTS) event */
19429         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
19430                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
19431                 return;
19432         }
19433
19434         /* Link this frame */
19435         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
19436         if (!seq_dmabuf) {
19437                 /* unable to add frame to vport - throw it out */
19438                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19439                 return;
19440         }
19441         /* If not last frame in sequence continue processing frames. */
19442         if (!lpfc_seq_complete(seq_dmabuf))
19443                 return;
19444
19445         /* Send the complete sequence to the upper layer protocol */
19446         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
19447 }
19448
19449 /**
19450  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
19451  * @phba: pointer to lpfc hba data structure.
19452  *
19453  * This routine is invoked to post rpi header templates to the
19454  * HBA consistent with the SLI-4 interface spec.  This routine
19455  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19456  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19457  *
19458  * This routine does not require any locks.  It's usage is expected
19459  * to be driver load or reset recovery when the driver is
19460  * sequential.
19461  *
19462  * Return codes
19463  *      0 - successful
19464  *      -EIO - The mailbox failed to complete successfully.
19465  *      When this error occurs, the driver is not guaranteed
19466  *      to have any rpi regions posted to the device and
19467  *      must either attempt to repost the regions or take a
19468  *      fatal error.
19469  **/
19470 int
19471 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
19472 {
19473         struct lpfc_rpi_hdr *rpi_page;
19474         uint32_t rc = 0;
19475         uint16_t lrpi = 0;
19476
19477         /* SLI4 ports that support extents do not require RPI headers. */
19478         if (!phba->sli4_hba.rpi_hdrs_in_use)
19479                 goto exit;
19480         if (phba->sli4_hba.extents_in_use)
19481                 return -EIO;
19482
19483         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
19484                 /*
19485                  * Assign the rpi headers a physical rpi only if the driver
19486                  * has not initialized those resources.  A port reset only
19487                  * needs the headers posted.
19488                  */
19489                 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
19490                     LPFC_RPI_RSRC_RDY)
19491                         rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19492
19493                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
19494                 if (rc != MBX_SUCCESS) {
19495                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19496                                         "2008 Error %d posting all rpi "
19497                                         "headers\n", rc);
19498                         rc = -EIO;
19499                         break;
19500                 }
19501         }
19502
19503  exit:
19504         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
19505                LPFC_RPI_RSRC_RDY);
19506         return rc;
19507 }
19508
19509 /**
19510  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
19511  * @phba: pointer to lpfc hba data structure.
19512  * @rpi_page:  pointer to the rpi memory region.
19513  *
19514  * This routine is invoked to post a single rpi header to the
19515  * HBA consistent with the SLI-4 interface spec.  This memory region
19516  * maps up to 64 rpi context regions.
19517  *
19518  * Return codes
19519  *      0 - successful
19520  *      -ENOMEM - No available memory
19521  *      -EIO - The mailbox failed to complete successfully.
19522  **/
19523 int
19524 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
19525 {
19526         LPFC_MBOXQ_t *mboxq;
19527         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
19528         uint32_t rc = 0;
19529         uint32_t shdr_status, shdr_add_status;
19530         union lpfc_sli4_cfg_shdr *shdr;
19531
19532         /* SLI4 ports that support extents do not require RPI headers. */
19533         if (!phba->sli4_hba.rpi_hdrs_in_use)
19534                 return rc;
19535         if (phba->sli4_hba.extents_in_use)
19536                 return -EIO;
19537
19538         /* The port is notified of the header region via a mailbox command. */
19539         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19540         if (!mboxq) {
19541                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19542                                 "2001 Unable to allocate memory for issuing "
19543                                 "SLI_CONFIG_SPECIAL mailbox command\n");
19544                 return -ENOMEM;
19545         }
19546
19547         /* Post all rpi memory regions to the port. */
19548         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
19549         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19550                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
19551                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
19552                          sizeof(struct lpfc_sli4_cfg_mhdr),
19553                          LPFC_SLI4_MBX_EMBED);
19554
19555
19556         /* Post the physical rpi to the port for this rpi header. */
19557         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
19558                rpi_page->start_rpi);
19559         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
19560                hdr_tmpl, rpi_page->page_count);
19561
19562         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
19563         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
19564         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19565         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
19566         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19567         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19568         mempool_free(mboxq, phba->mbox_mem_pool);
19569         if (shdr_status || shdr_add_status || rc) {
19570                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19571                                 "2514 POST_RPI_HDR mailbox failed with "
19572                                 "status x%x add_status x%x, mbx status x%x\n",
19573                                 shdr_status, shdr_add_status, rc);
19574                 rc = -ENXIO;
19575         } else {
19576                 /*
19577                  * The next_rpi stores the next logical module-64 rpi value used
19578                  * to post physical rpis in subsequent rpi postings.
19579                  */
19580                 spin_lock_irq(&phba->hbalock);
19581                 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
19582                 spin_unlock_irq(&phba->hbalock);
19583         }
19584         return rc;
19585 }
19586
19587 /**
19588  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
19589  * @phba: pointer to lpfc hba data structure.
19590  *
19591  * This routine is invoked to post rpi header templates to the
19592  * HBA consistent with the SLI-4 interface spec.  This routine
19593  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19594  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19595  *
19596  * Returns
19597  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
19598  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
19599  **/
19600 int
19601 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
19602 {
19603         unsigned long rpi;
19604         uint16_t max_rpi, rpi_limit;
19605         uint16_t rpi_remaining, lrpi = 0;
19606         struct lpfc_rpi_hdr *rpi_hdr;
19607         unsigned long iflag;
19608
19609         /*
19610          * Fetch the next logical rpi.  Because this index is logical,
19611          * the  driver starts at 0 each time.
19612          */
19613         spin_lock_irqsave(&phba->hbalock, iflag);
19614         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
19615         rpi_limit = phba->sli4_hba.next_rpi;
19616
19617         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
19618         if (rpi >= rpi_limit)
19619                 rpi = LPFC_RPI_ALLOC_ERROR;
19620         else {
19621                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
19622                 phba->sli4_hba.max_cfg_param.rpi_used++;
19623                 phba->sli4_hba.rpi_count++;
19624         }
19625         lpfc_printf_log(phba, KERN_INFO,
19626                         LOG_NODE | LOG_DISCOVERY,
19627                         "0001 Allocated rpi:x%x max:x%x lim:x%x\n",
19628                         (int) rpi, max_rpi, rpi_limit);
19629
19630         /*
19631          * Don't try to allocate more rpi header regions if the device limit
19632          * has been exhausted.
19633          */
19634         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
19635             (phba->sli4_hba.rpi_count >= max_rpi)) {
19636                 spin_unlock_irqrestore(&phba->hbalock, iflag);
19637                 return rpi;
19638         }
19639
19640         /*
19641          * RPI header postings are not required for SLI4 ports capable of
19642          * extents.
19643          */
19644         if (!phba->sli4_hba.rpi_hdrs_in_use) {
19645                 spin_unlock_irqrestore(&phba->hbalock, iflag);
19646                 return rpi;
19647         }
19648
19649         /*
19650          * If the driver is running low on rpi resources, allocate another
19651          * page now.  Note that the next_rpi value is used because
19652          * it represents how many are actually in use whereas max_rpi notes
19653          * how many are supported max by the device.
19654          */
19655         rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
19656         spin_unlock_irqrestore(&phba->hbalock, iflag);
19657         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
19658                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
19659                 if (!rpi_hdr) {
19660                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19661                                         "2002 Error Could not grow rpi "
19662                                         "count\n");
19663                 } else {
19664                         lrpi = rpi_hdr->start_rpi;
19665                         rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19666                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
19667                 }
19668         }
19669
19670         return rpi;
19671 }
19672
19673 /**
19674  * __lpfc_sli4_free_rpi - Release an rpi for reuse.
19675  * @phba: pointer to lpfc hba data structure.
19676  * @rpi: rpi to free
19677  *
19678  * This routine is invoked to release an rpi to the pool of
19679  * available rpis maintained by the driver.
19680  **/
19681 static void
19682 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19683 {
19684         /*
19685          * if the rpi value indicates a prior unreg has already
19686          * been done, skip the unreg.
19687          */
19688         if (rpi == LPFC_RPI_ALLOC_ERROR)
19689                 return;
19690
19691         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
19692                 phba->sli4_hba.rpi_count--;
19693                 phba->sli4_hba.max_cfg_param.rpi_used--;
19694         } else {
19695                 lpfc_printf_log(phba, KERN_INFO,
19696                                 LOG_NODE | LOG_DISCOVERY,
19697                                 "2016 rpi %x not inuse\n",
19698                                 rpi);
19699         }
19700 }
19701
19702 /**
19703  * lpfc_sli4_free_rpi - Release an rpi for reuse.
19704  * @phba: pointer to lpfc hba data structure.
19705  * @rpi: rpi to free
19706  *
19707  * This routine is invoked to release an rpi to the pool of
19708  * available rpis maintained by the driver.
19709  **/
19710 void
19711 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19712 {
19713         spin_lock_irq(&phba->hbalock);
19714         __lpfc_sli4_free_rpi(phba, rpi);
19715         spin_unlock_irq(&phba->hbalock);
19716 }
19717
19718 /**
19719  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
19720  * @phba: pointer to lpfc hba data structure.
19721  *
19722  * This routine is invoked to remove the memory region that
19723  * provided rpi via a bitmask.
19724  **/
19725 void
19726 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
19727 {
19728         kfree(phba->sli4_hba.rpi_bmask);
19729         kfree(phba->sli4_hba.rpi_ids);
19730         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
19731 }
19732
19733 /**
19734  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
19735  * @ndlp: pointer to lpfc nodelist data structure.
19736  * @cmpl: completion call-back.
19737  * @arg: data to load as MBox 'caller buffer information'
19738  *
19739  * This routine is invoked to remove the memory region that
19740  * provided rpi via a bitmask.
19741  **/
19742 int
19743 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
19744         void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
19745 {
19746         LPFC_MBOXQ_t *mboxq;
19747         struct lpfc_hba *phba = ndlp->phba;
19748         int rc;
19749
19750         /* The port is notified of the header region via a mailbox command. */
19751         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19752         if (!mboxq)
19753                 return -ENOMEM;
19754
19755         /* If cmpl assigned, then this nlp_get pairs with
19756          * lpfc_mbx_cmpl_resume_rpi.
19757          *
19758          * Else cmpl is NULL, then this nlp_get pairs with
19759          * lpfc_sli_def_mbox_cmpl.
19760          */
19761         if (!lpfc_nlp_get(ndlp)) {
19762                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19763                                 "2122 %s: Failed to get nlp ref\n",
19764                                 __func__);
19765                 mempool_free(mboxq, phba->mbox_mem_pool);
19766                 return -EIO;
19767         }
19768
19769         /* Post all rpi memory regions to the port. */
19770         lpfc_resume_rpi(mboxq, ndlp);
19771         if (cmpl) {
19772                 mboxq->mbox_cmpl = cmpl;
19773                 mboxq->ctx_buf = arg;
19774         } else
19775                 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19776         mboxq->ctx_ndlp = ndlp;
19777         mboxq->vport = ndlp->vport;
19778         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19779         if (rc == MBX_NOT_FINISHED) {
19780                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19781                                 "2010 Resume RPI Mailbox failed "
19782                                 "status %d, mbxStatus x%x\n", rc,
19783                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19784                 lpfc_nlp_put(ndlp);
19785                 mempool_free(mboxq, phba->mbox_mem_pool);
19786                 return -EIO;
19787         }
19788         return 0;
19789 }
19790
19791 /**
19792  * lpfc_sli4_init_vpi - Initialize a vpi with the port
19793  * @vport: Pointer to the vport for which the vpi is being initialized
19794  *
19795  * This routine is invoked to activate a vpi with the port.
19796  *
19797  * Returns:
19798  *    0 success
19799  *    -Evalue otherwise
19800  **/
19801 int
19802 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
19803 {
19804         LPFC_MBOXQ_t *mboxq;
19805         int rc = 0;
19806         int retval = MBX_SUCCESS;
19807         uint32_t mbox_tmo;
19808         struct lpfc_hba *phba = vport->phba;
19809         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19810         if (!mboxq)
19811                 return -ENOMEM;
19812         lpfc_init_vpi(phba, mboxq, vport->vpi);
19813         mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
19814         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
19815         if (rc != MBX_SUCCESS) {
19816                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19817                                 "2022 INIT VPI Mailbox failed "
19818                                 "status %d, mbxStatus x%x\n", rc,
19819                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19820                 retval = -EIO;
19821         }
19822         if (rc != MBX_TIMEOUT)
19823                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
19824
19825         return retval;
19826 }
19827
19828 /**
19829  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
19830  * @phba: pointer to lpfc hba data structure.
19831  * @mboxq: Pointer to mailbox object.
19832  *
19833  * This routine is invoked to manually add a single FCF record. The caller
19834  * must pass a completely initialized FCF_Record.  This routine takes
19835  * care of the nonembedded mailbox operations.
19836  **/
19837 static void
19838 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
19839 {
19840         void *virt_addr;
19841         union lpfc_sli4_cfg_shdr *shdr;
19842         uint32_t shdr_status, shdr_add_status;
19843
19844         virt_addr = mboxq->sge_array->addr[0];
19845         /* The IOCTL status is embedded in the mailbox subheader. */
19846         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
19847         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19848         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19849
19850         if ((shdr_status || shdr_add_status) &&
19851                 (shdr_status != STATUS_FCF_IN_USE))
19852                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19853                         "2558 ADD_FCF_RECORD mailbox failed with "
19854                         "status x%x add_status x%x\n",
19855                         shdr_status, shdr_add_status);
19856
19857         lpfc_sli4_mbox_cmd_free(phba, mboxq);
19858 }
19859
19860 /**
19861  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
19862  * @phba: pointer to lpfc hba data structure.
19863  * @fcf_record:  pointer to the initialized fcf record to add.
19864  *
19865  * This routine is invoked to manually add a single FCF record. The caller
19866  * must pass a completely initialized FCF_Record.  This routine takes
19867  * care of the nonembedded mailbox operations.
19868  **/
19869 int
19870 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
19871 {
19872         int rc = 0;
19873         LPFC_MBOXQ_t *mboxq;
19874         uint8_t *bytep;
19875         void *virt_addr;
19876         struct lpfc_mbx_sge sge;
19877         uint32_t alloc_len, req_len;
19878         uint32_t fcfindex;
19879
19880         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19881         if (!mboxq) {
19882                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19883                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
19884                 return -ENOMEM;
19885         }
19886
19887         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
19888                   sizeof(uint32_t);
19889
19890         /* Allocate DMA memory and set up the non-embedded mailbox command */
19891         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19892                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
19893                                      req_len, LPFC_SLI4_MBX_NEMBED);
19894         if (alloc_len < req_len) {
19895                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19896                         "2523 Allocated DMA memory size (x%x) is "
19897                         "less than the requested DMA memory "
19898                         "size (x%x)\n", alloc_len, req_len);
19899                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19900                 return -ENOMEM;
19901         }
19902
19903         /*
19904          * Get the first SGE entry from the non-embedded DMA memory.  This
19905          * routine only uses a single SGE.
19906          */
19907         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
19908         virt_addr = mboxq->sge_array->addr[0];
19909         /*
19910          * Configure the FCF record for FCFI 0.  This is the driver's
19911          * hardcoded default and gets used in nonFIP mode.
19912          */
19913         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
19914         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
19915         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
19916
19917         /*
19918          * Copy the fcf_index and the FCF Record Data. The data starts after
19919          * the FCoE header plus word10. The data copy needs to be endian
19920          * correct.
19921          */
19922         bytep += sizeof(uint32_t);
19923         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
19924         mboxq->vport = phba->pport;
19925         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
19926         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19927         if (rc == MBX_NOT_FINISHED) {
19928                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19929                         "2515 ADD_FCF_RECORD mailbox failed with "
19930                         "status 0x%x\n", rc);
19931                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19932                 rc = -EIO;
19933         } else
19934                 rc = 0;
19935
19936         return rc;
19937 }
19938
19939 /**
19940  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
19941  * @phba: pointer to lpfc hba data structure.
19942  * @fcf_record:  pointer to the fcf record to write the default data.
19943  * @fcf_index: FCF table entry index.
19944  *
19945  * This routine is invoked to build the driver's default FCF record.  The
19946  * values used are hardcoded.  This routine handles memory initialization.
19947  *
19948  **/
19949 void
19950 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
19951                                 struct fcf_record *fcf_record,
19952                                 uint16_t fcf_index)
19953 {
19954         memset(fcf_record, 0, sizeof(struct fcf_record));
19955         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
19956         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
19957         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
19958         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
19959         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
19960         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
19961         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
19962         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
19963         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
19964         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
19965         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
19966         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
19967         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
19968         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
19969         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
19970         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
19971                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
19972         /* Set the VLAN bit map */
19973         if (phba->valid_vlan) {
19974                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
19975                         = 1 << (phba->vlan_id % 8);
19976         }
19977 }
19978
19979 /**
19980  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
19981  * @phba: pointer to lpfc hba data structure.
19982  * @fcf_index: FCF table entry offset.
19983  *
19984  * This routine is invoked to scan the entire FCF table by reading FCF
19985  * record and processing it one at a time starting from the @fcf_index
19986  * for initial FCF discovery or fast FCF failover rediscovery.
19987  *
19988  * Return 0 if the mailbox command is submitted successfully, none 0
19989  * otherwise.
19990  **/
19991 int
19992 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
19993 {
19994         int rc = 0, error;
19995         LPFC_MBOXQ_t *mboxq;
19996
19997         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
19998         phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
19999         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20000         if (!mboxq) {
20001                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20002                                 "2000 Failed to allocate mbox for "
20003                                 "READ_FCF cmd\n");
20004                 error = -ENOMEM;
20005                 goto fail_fcf_scan;
20006         }
20007         /* Construct the read FCF record mailbox command */
20008         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20009         if (rc) {
20010                 error = -EINVAL;
20011                 goto fail_fcf_scan;
20012         }
20013         /* Issue the mailbox command asynchronously */
20014         mboxq->vport = phba->pport;
20015         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
20016
20017         spin_lock_irq(&phba->hbalock);
20018         phba->hba_flag |= FCF_TS_INPROG;
20019         spin_unlock_irq(&phba->hbalock);
20020
20021         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20022         if (rc == MBX_NOT_FINISHED)
20023                 error = -EIO;
20024         else {
20025                 /* Reset eligible FCF count for new scan */
20026                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
20027                         phba->fcf.eligible_fcf_cnt = 0;
20028                 error = 0;
20029         }
20030 fail_fcf_scan:
20031         if (error) {
20032                 if (mboxq)
20033                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
20034                 /* FCF scan failed, clear FCF_TS_INPROG flag */
20035                 spin_lock_irq(&phba->hbalock);
20036                 phba->hba_flag &= ~FCF_TS_INPROG;
20037                 spin_unlock_irq(&phba->hbalock);
20038         }
20039         return error;
20040 }
20041
20042 /**
20043  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
20044  * @phba: pointer to lpfc hba data structure.
20045  * @fcf_index: FCF table entry offset.
20046  *
20047  * This routine is invoked to read an FCF record indicated by @fcf_index
20048  * and to use it for FLOGI roundrobin FCF failover.
20049  *
20050  * Return 0 if the mailbox command is submitted successfully, none 0
20051  * otherwise.
20052  **/
20053 int
20054 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20055 {
20056         int rc = 0, error;
20057         LPFC_MBOXQ_t *mboxq;
20058
20059         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20060         if (!mboxq) {
20061                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20062                                 "2763 Failed to allocate mbox for "
20063                                 "READ_FCF cmd\n");
20064                 error = -ENOMEM;
20065                 goto fail_fcf_read;
20066         }
20067         /* Construct the read FCF record mailbox command */
20068         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20069         if (rc) {
20070                 error = -EINVAL;
20071                 goto fail_fcf_read;
20072         }
20073         /* Issue the mailbox command asynchronously */
20074         mboxq->vport = phba->pport;
20075         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
20076         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20077         if (rc == MBX_NOT_FINISHED)
20078                 error = -EIO;
20079         else
20080                 error = 0;
20081
20082 fail_fcf_read:
20083         if (error && mboxq)
20084                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
20085         return error;
20086 }
20087
20088 /**
20089  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
20090  * @phba: pointer to lpfc hba data structure.
20091  * @fcf_index: FCF table entry offset.
20092  *
20093  * This routine is invoked to read an FCF record indicated by @fcf_index to
20094  * determine whether it's eligible for FLOGI roundrobin failover list.
20095  *
20096  * Return 0 if the mailbox command is submitted successfully, none 0
20097  * otherwise.
20098  **/
20099 int
20100 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20101 {
20102         int rc = 0, error;
20103         LPFC_MBOXQ_t *mboxq;
20104
20105         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20106         if (!mboxq) {
20107                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20108                                 "2758 Failed to allocate mbox for "
20109                                 "READ_FCF cmd\n");
20110                                 error = -ENOMEM;
20111                                 goto fail_fcf_read;
20112         }
20113         /* Construct the read FCF record mailbox command */
20114         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20115         if (rc) {
20116                 error = -EINVAL;
20117                 goto fail_fcf_read;
20118         }
20119         /* Issue the mailbox command asynchronously */
20120         mboxq->vport = phba->pport;
20121         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
20122         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20123         if (rc == MBX_NOT_FINISHED)
20124                 error = -EIO;
20125         else
20126                 error = 0;
20127
20128 fail_fcf_read:
20129         if (error && mboxq)
20130                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
20131         return error;
20132 }
20133
20134 /**
20135  * lpfc_check_next_fcf_pri_level
20136  * @phba: pointer to the lpfc_hba struct for this port.
20137  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
20138  * routine when the rr_bmask is empty. The FCF indecies are put into the
20139  * rr_bmask based on their priority level. Starting from the highest priority
20140  * to the lowest. The most likely FCF candidate will be in the highest
20141  * priority group. When this routine is called it searches the fcf_pri list for
20142  * next lowest priority group and repopulates the rr_bmask with only those
20143  * fcf_indexes.
20144  * returns:
20145  * 1=success 0=failure
20146  **/
20147 static int
20148 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
20149 {
20150         uint16_t next_fcf_pri;
20151         uint16_t last_index;
20152         struct lpfc_fcf_pri *fcf_pri;
20153         int rc;
20154         int ret = 0;
20155
20156         last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20157                         LPFC_SLI4_FCF_TBL_INDX_MAX);
20158         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20159                         "3060 Last IDX %d\n", last_index);
20160
20161         /* Verify the priority list has 2 or more entries */
20162         spin_lock_irq(&phba->hbalock);
20163         if (list_empty(&phba->fcf.fcf_pri_list) ||
20164             list_is_singular(&phba->fcf.fcf_pri_list)) {
20165                 spin_unlock_irq(&phba->hbalock);
20166                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20167                         "3061 Last IDX %d\n", last_index);
20168                 return 0; /* Empty rr list */
20169         }
20170         spin_unlock_irq(&phba->hbalock);
20171
20172         next_fcf_pri = 0;
20173         /*
20174          * Clear the rr_bmask and set all of the bits that are at this
20175          * priority.
20176          */
20177         memset(phba->fcf.fcf_rr_bmask, 0,
20178                         sizeof(*phba->fcf.fcf_rr_bmask));
20179         spin_lock_irq(&phba->hbalock);
20180         list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20181                 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
20182                         continue;
20183                 /*
20184                  * the 1st priority that has not FLOGI failed
20185                  * will be the highest.
20186                  */
20187                 if (!next_fcf_pri)
20188                         next_fcf_pri = fcf_pri->fcf_rec.priority;
20189                 spin_unlock_irq(&phba->hbalock);
20190                 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20191                         rc = lpfc_sli4_fcf_rr_index_set(phba,
20192                                                 fcf_pri->fcf_rec.fcf_index);
20193                         if (rc)
20194                                 return 0;
20195                 }
20196                 spin_lock_irq(&phba->hbalock);
20197         }
20198         /*
20199          * if next_fcf_pri was not set above and the list is not empty then
20200          * we have failed flogis on all of them. So reset flogi failed
20201          * and start at the beginning.
20202          */
20203         if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
20204                 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20205                         fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
20206                         /*
20207                          * the 1st priority that has not FLOGI failed
20208                          * will be the highest.
20209                          */
20210                         if (!next_fcf_pri)
20211                                 next_fcf_pri = fcf_pri->fcf_rec.priority;
20212                         spin_unlock_irq(&phba->hbalock);
20213                         if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20214                                 rc = lpfc_sli4_fcf_rr_index_set(phba,
20215                                                 fcf_pri->fcf_rec.fcf_index);
20216                                 if (rc)
20217                                         return 0;
20218                         }
20219                         spin_lock_irq(&phba->hbalock);
20220                 }
20221         } else
20222                 ret = 1;
20223         spin_unlock_irq(&phba->hbalock);
20224
20225         return ret;
20226 }
20227 /**
20228  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
20229  * @phba: pointer to lpfc hba data structure.
20230  *
20231  * This routine is to get the next eligible FCF record index in a round
20232  * robin fashion. If the next eligible FCF record index equals to the
20233  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
20234  * shall be returned, otherwise, the next eligible FCF record's index
20235  * shall be returned.
20236  **/
20237 uint16_t
20238 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
20239 {
20240         uint16_t next_fcf_index;
20241
20242 initial_priority:
20243         /* Search start from next bit of currently registered FCF index */
20244         next_fcf_index = phba->fcf.current_rec.fcf_indx;
20245
20246 next_priority:
20247         /* Determine the next fcf index to check */
20248         next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
20249         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
20250                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
20251                                        next_fcf_index);
20252
20253         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
20254         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20255                 /*
20256                  * If we have wrapped then we need to clear the bits that
20257                  * have been tested so that we can detect when we should
20258                  * change the priority level.
20259                  */
20260                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
20261                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
20262         }
20263
20264
20265         /* Check roundrobin failover list empty condition */
20266         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
20267                 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
20268                 /*
20269                  * If next fcf index is not found check if there are lower
20270                  * Priority level fcf's in the fcf_priority list.
20271                  * Set up the rr_bmask with all of the avaiable fcf bits
20272                  * at that level and continue the selection process.
20273                  */
20274                 if (lpfc_check_next_fcf_pri_level(phba))
20275                         goto initial_priority;
20276                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
20277                                 "2844 No roundrobin failover FCF available\n");
20278
20279                 return LPFC_FCOE_FCF_NEXT_NONE;
20280         }
20281
20282         if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
20283                 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
20284                 LPFC_FCF_FLOGI_FAILED) {
20285                 if (list_is_singular(&phba->fcf.fcf_pri_list))
20286                         return LPFC_FCOE_FCF_NEXT_NONE;
20287
20288                 goto next_priority;
20289         }
20290
20291         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20292                         "2845 Get next roundrobin failover FCF (x%x)\n",
20293                         next_fcf_index);
20294
20295         return next_fcf_index;
20296 }
20297
20298 /**
20299  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
20300  * @phba: pointer to lpfc hba data structure.
20301  * @fcf_index: index into the FCF table to 'set'
20302  *
20303  * This routine sets the FCF record index in to the eligible bmask for
20304  * roundrobin failover search. It checks to make sure that the index
20305  * does not go beyond the range of the driver allocated bmask dimension
20306  * before setting the bit.
20307  *
20308  * Returns 0 if the index bit successfully set, otherwise, it returns
20309  * -EINVAL.
20310  **/
20311 int
20312 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
20313 {
20314         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20315                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20316                                 "2610 FCF (x%x) reached driver's book "
20317                                 "keeping dimension:x%x\n",
20318                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20319                 return -EINVAL;
20320         }
20321         /* Set the eligible FCF record index bmask */
20322         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20323
20324         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20325                         "2790 Set FCF (x%x) to roundrobin FCF failover "
20326                         "bmask\n", fcf_index);
20327
20328         return 0;
20329 }
20330
20331 /**
20332  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
20333  * @phba: pointer to lpfc hba data structure.
20334  * @fcf_index: index into the FCF table to 'clear'
20335  *
20336  * This routine clears the FCF record index from the eligible bmask for
20337  * roundrobin failover search. It checks to make sure that the index
20338  * does not go beyond the range of the driver allocated bmask dimension
20339  * before clearing the bit.
20340  **/
20341 void
20342 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
20343 {
20344         struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
20345         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20346                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20347                                 "2762 FCF (x%x) reached driver's book "
20348                                 "keeping dimension:x%x\n",
20349                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20350                 return;
20351         }
20352         /* Clear the eligible FCF record index bmask */
20353         spin_lock_irq(&phba->hbalock);
20354         list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
20355                                  list) {
20356                 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
20357                         list_del_init(&fcf_pri->list);
20358                         break;
20359                 }
20360         }
20361         spin_unlock_irq(&phba->hbalock);
20362         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20363
20364         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20365                         "2791 Clear FCF (x%x) from roundrobin failover "
20366                         "bmask\n", fcf_index);
20367 }
20368
20369 /**
20370  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
20371  * @phba: pointer to lpfc hba data structure.
20372  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
20373  *
20374  * This routine is the completion routine for the rediscover FCF table mailbox
20375  * command. If the mailbox command returned failure, it will try to stop the
20376  * FCF rediscover wait timer.
20377  **/
20378 static void
20379 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
20380 {
20381         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20382         uint32_t shdr_status, shdr_add_status;
20383
20384         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20385
20386         shdr_status = bf_get(lpfc_mbox_hdr_status,
20387                              &redisc_fcf->header.cfg_shdr.response);
20388         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20389                              &redisc_fcf->header.cfg_shdr.response);
20390         if (shdr_status || shdr_add_status) {
20391                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20392                                 "2746 Requesting for FCF rediscovery failed "
20393                                 "status x%x add_status x%x\n",
20394                                 shdr_status, shdr_add_status);
20395                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
20396                         spin_lock_irq(&phba->hbalock);
20397                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
20398                         spin_unlock_irq(&phba->hbalock);
20399                         /*
20400                          * CVL event triggered FCF rediscover request failed,
20401                          * last resort to re-try current registered FCF entry.
20402                          */
20403                         lpfc_retry_pport_discovery(phba);
20404                 } else {
20405                         spin_lock_irq(&phba->hbalock);
20406                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
20407                         spin_unlock_irq(&phba->hbalock);
20408                         /*
20409                          * DEAD FCF event triggered FCF rediscover request
20410                          * failed, last resort to fail over as a link down
20411                          * to FCF registration.
20412                          */
20413                         lpfc_sli4_fcf_dead_failthrough(phba);
20414                 }
20415         } else {
20416                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20417                                 "2775 Start FCF rediscover quiescent timer\n");
20418                 /*
20419                  * Start FCF rediscovery wait timer for pending FCF
20420                  * before rescan FCF record table.
20421                  */
20422                 lpfc_fcf_redisc_wait_start_timer(phba);
20423         }
20424
20425         mempool_free(mbox, phba->mbox_mem_pool);
20426 }
20427
20428 /**
20429  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
20430  * @phba: pointer to lpfc hba data structure.
20431  *
20432  * This routine is invoked to request for rediscovery of the entire FCF table
20433  * by the port.
20434  **/
20435 int
20436 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
20437 {
20438         LPFC_MBOXQ_t *mbox;
20439         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20440         int rc, length;
20441
20442         /* Cancel retry delay timers to all vports before FCF rediscover */
20443         lpfc_cancel_all_vport_retry_delay_timer(phba);
20444
20445         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20446         if (!mbox) {
20447                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20448                                 "2745 Failed to allocate mbox for "
20449                                 "requesting FCF rediscover.\n");
20450                 return -ENOMEM;
20451         }
20452
20453         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
20454                   sizeof(struct lpfc_sli4_cfg_mhdr));
20455         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
20456                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
20457                          length, LPFC_SLI4_MBX_EMBED);
20458
20459         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20460         /* Set count to 0 for invalidating the entire FCF database */
20461         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
20462
20463         /* Issue the mailbox command asynchronously */
20464         mbox->vport = phba->pport;
20465         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
20466         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
20467
20468         if (rc == MBX_NOT_FINISHED) {
20469                 mempool_free(mbox, phba->mbox_mem_pool);
20470                 return -EIO;
20471         }
20472         return 0;
20473 }
20474
20475 /**
20476  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
20477  * @phba: pointer to lpfc hba data structure.
20478  *
20479  * This function is the failover routine as a last resort to the FCF DEAD
20480  * event when driver failed to perform fast FCF failover.
20481  **/
20482 void
20483 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
20484 {
20485         uint32_t link_state;
20486
20487         /*
20488          * Last resort as FCF DEAD event failover will treat this as
20489          * a link down, but save the link state because we don't want
20490          * it to be changed to Link Down unless it is already down.
20491          */
20492         link_state = phba->link_state;
20493         lpfc_linkdown(phba);
20494         phba->link_state = link_state;
20495
20496         /* Unregister FCF if no devices connected to it */
20497         lpfc_unregister_unused_fcf(phba);
20498 }
20499
20500 /**
20501  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
20502  * @phba: pointer to lpfc hba data structure.
20503  * @rgn23_data: pointer to configure region 23 data.
20504  *
20505  * This function gets SLI3 port configure region 23 data through memory dump
20506  * mailbox command. When it successfully retrieves data, the size of the data
20507  * will be returned, otherwise, 0 will be returned.
20508  **/
20509 static uint32_t
20510 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20511 {
20512         LPFC_MBOXQ_t *pmb = NULL;
20513         MAILBOX_t *mb;
20514         uint32_t offset = 0;
20515         int rc;
20516
20517         if (!rgn23_data)
20518                 return 0;
20519
20520         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20521         if (!pmb) {
20522                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20523                                 "2600 failed to allocate mailbox memory\n");
20524                 return 0;
20525         }
20526         mb = &pmb->u.mb;
20527
20528         do {
20529                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
20530                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
20531
20532                 if (rc != MBX_SUCCESS) {
20533                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20534                                         "2601 failed to read config "
20535                                         "region 23, rc 0x%x Status 0x%x\n",
20536                                         rc, mb->mbxStatus);
20537                         mb->un.varDmp.word_cnt = 0;
20538                 }
20539                 /*
20540                  * dump mem may return a zero when finished or we got a
20541                  * mailbox error, either way we are done.
20542                  */
20543                 if (mb->un.varDmp.word_cnt == 0)
20544                         break;
20545
20546                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
20547                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
20548
20549                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
20550                                        rgn23_data + offset,
20551                                        mb->un.varDmp.word_cnt);
20552                 offset += mb->un.varDmp.word_cnt;
20553         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
20554
20555         mempool_free(pmb, phba->mbox_mem_pool);
20556         return offset;
20557 }
20558
20559 /**
20560  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
20561  * @phba: pointer to lpfc hba data structure.
20562  * @rgn23_data: pointer to configure region 23 data.
20563  *
20564  * This function gets SLI4 port configure region 23 data through memory dump
20565  * mailbox command. When it successfully retrieves data, the size of the data
20566  * will be returned, otherwise, 0 will be returned.
20567  **/
20568 static uint32_t
20569 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20570 {
20571         LPFC_MBOXQ_t *mboxq = NULL;
20572         struct lpfc_dmabuf *mp = NULL;
20573         struct lpfc_mqe *mqe;
20574         uint32_t data_length = 0;
20575         int rc;
20576
20577         if (!rgn23_data)
20578                 return 0;
20579
20580         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20581         if (!mboxq) {
20582                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20583                                 "3105 failed to allocate mailbox memory\n");
20584                 return 0;
20585         }
20586
20587         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
20588                 goto out;
20589         mqe = &mboxq->u.mqe;
20590         mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
20591         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
20592         if (rc)
20593                 goto out;
20594         data_length = mqe->un.mb_words[5];
20595         if (data_length == 0)
20596                 goto out;
20597         if (data_length > DMP_RGN23_SIZE) {
20598                 data_length = 0;
20599                 goto out;
20600         }
20601         lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
20602 out:
20603         mempool_free(mboxq, phba->mbox_mem_pool);
20604         if (mp) {
20605                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
20606                 kfree(mp);
20607         }
20608         return data_length;
20609 }
20610
20611 /**
20612  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
20613  * @phba: pointer to lpfc hba data structure.
20614  *
20615  * This function read region 23 and parse TLV for port status to
20616  * decide if the user disaled the port. If the TLV indicates the
20617  * port is disabled, the hba_flag is set accordingly.
20618  **/
20619 void
20620 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
20621 {
20622         uint8_t *rgn23_data = NULL;
20623         uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
20624         uint32_t offset = 0;
20625
20626         /* Get adapter Region 23 data */
20627         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
20628         if (!rgn23_data)
20629                 goto out;
20630
20631         if (phba->sli_rev < LPFC_SLI_REV4)
20632                 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
20633         else {
20634                 if_type = bf_get(lpfc_sli_intf_if_type,
20635                                  &phba->sli4_hba.sli_intf);
20636                 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
20637                         goto out;
20638                 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
20639         }
20640
20641         if (!data_size)
20642                 goto out;
20643
20644         /* Check the region signature first */
20645         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
20646                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20647                         "2619 Config region 23 has bad signature\n");
20648                         goto out;
20649         }
20650         offset += 4;
20651
20652         /* Check the data structure version */
20653         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
20654                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20655                         "2620 Config region 23 has bad version\n");
20656                 goto out;
20657         }
20658         offset += 4;
20659
20660         /* Parse TLV entries in the region */
20661         while (offset < data_size) {
20662                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
20663                         break;
20664                 /*
20665                  * If the TLV is not driver specific TLV or driver id is
20666                  * not linux driver id, skip the record.
20667                  */
20668                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
20669                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
20670                     (rgn23_data[offset + 3] != 0)) {
20671                         offset += rgn23_data[offset + 1] * 4 + 4;
20672                         continue;
20673                 }
20674
20675                 /* Driver found a driver specific TLV in the config region */
20676                 sub_tlv_len = rgn23_data[offset + 1] * 4;
20677                 offset += 4;
20678                 tlv_offset = 0;
20679
20680                 /*
20681                  * Search for configured port state sub-TLV.
20682                  */
20683                 while ((offset < data_size) &&
20684                         (tlv_offset < sub_tlv_len)) {
20685                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
20686                                 offset += 4;
20687                                 tlv_offset += 4;
20688                                 break;
20689                         }
20690                         if (rgn23_data[offset] != PORT_STE_TYPE) {
20691                                 offset += rgn23_data[offset + 1] * 4 + 4;
20692                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
20693                                 continue;
20694                         }
20695
20696                         /* This HBA contains PORT_STE configured */
20697                         if (!rgn23_data[offset + 2])
20698                                 phba->hba_flag |= LINK_DISABLED;
20699
20700                         goto out;
20701                 }
20702         }
20703
20704 out:
20705         kfree(rgn23_data);
20706         return;
20707 }
20708
20709 /**
20710  * lpfc_log_fw_write_cmpl - logs firmware write completion status
20711  * @phba: pointer to lpfc hba data structure
20712  * @shdr_status: wr_object rsp's status field
20713  * @shdr_add_status: wr_object rsp's add_status field
20714  * @shdr_add_status_2: wr_object rsp's add_status_2 field
20715  * @shdr_change_status: wr_object rsp's change_status field
20716  * @shdr_csf: wr_object rsp's csf bit
20717  *
20718  * This routine is intended to be called after a firmware write completes.
20719  * It will log next action items to be performed by the user to instantiate
20720  * the newly downloaded firmware or reason for incompatibility.
20721  **/
20722 static void
20723 lpfc_log_fw_write_cmpl(struct lpfc_hba *phba, u32 shdr_status,
20724                        u32 shdr_add_status, u32 shdr_add_status_2,
20725                        u32 shdr_change_status, u32 shdr_csf)
20726 {
20727         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20728                         "4198 %s: flash_id x%02x, asic_rev x%02x, "
20729                         "status x%02x, add_status x%02x, add_status_2 x%02x, "
20730                         "change_status x%02x, csf %01x\n", __func__,
20731                         phba->sli4_hba.flash_id, phba->sli4_hba.asic_rev,
20732                         shdr_status, shdr_add_status, shdr_add_status_2,
20733                         shdr_change_status, shdr_csf);
20734
20735         if (shdr_add_status == LPFC_ADD_STATUS_INCOMPAT_OBJ) {
20736                 switch (shdr_add_status_2) {
20737                 case LPFC_ADD_STATUS_2_INCOMPAT_FLASH:
20738                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20739                                         "4199 Firmware write failed: "
20740                                         "image incompatible with flash x%02x\n",
20741                                         phba->sli4_hba.flash_id);
20742                         break;
20743                 case LPFC_ADD_STATUS_2_INCORRECT_ASIC:
20744                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20745                                         "4200 Firmware write failed: "
20746                                         "image incompatible with ASIC "
20747                                         "architecture x%02x\n",
20748                                         phba->sli4_hba.asic_rev);
20749                         break;
20750                 default:
20751                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20752                                         "4210 Firmware write failed: "
20753                                         "add_status_2 x%02x\n",
20754                                         shdr_add_status_2);
20755                         break;
20756                 }
20757         } else if (!shdr_status && !shdr_add_status) {
20758                 if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
20759                     shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
20760                         if (shdr_csf)
20761                                 shdr_change_status =
20762                                                    LPFC_CHANGE_STATUS_PCI_RESET;
20763                 }
20764
20765                 switch (shdr_change_status) {
20766                 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
20767                         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20768                                         "3198 Firmware write complete: System "
20769                                         "reboot required to instantiate\n");
20770                         break;
20771                 case (LPFC_CHANGE_STATUS_FW_RESET):
20772                         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20773                                         "3199 Firmware write complete: "
20774                                         "Firmware reset required to "
20775                                         "instantiate\n");
20776                         break;
20777                 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
20778                         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20779                                         "3200 Firmware write complete: Port "
20780                                         "Migration or PCI Reset required to "
20781                                         "instantiate\n");
20782                         break;
20783                 case (LPFC_CHANGE_STATUS_PCI_RESET):
20784                         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20785                                         "3201 Firmware write complete: PCI "
20786                                         "Reset required to instantiate\n");
20787                         break;
20788                 default:
20789                         break;
20790                 }
20791         }
20792 }
20793
20794 /**
20795  * lpfc_wr_object - write an object to the firmware
20796  * @phba: HBA structure that indicates port to create a queue on.
20797  * @dmabuf_list: list of dmabufs to write to the port.
20798  * @size: the total byte value of the objects to write to the port.
20799  * @offset: the current offset to be used to start the transfer.
20800  *
20801  * This routine will create a wr_object mailbox command to send to the port.
20802  * the mailbox command will be constructed using the dma buffers described in
20803  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
20804  * BDEs that the imbedded mailbox can support. The @offset variable will be
20805  * used to indicate the starting offset of the transfer and will also return
20806  * the offset after the write object mailbox has completed. @size is used to
20807  * determine the end of the object and whether the eof bit should be set.
20808  *
20809  * Return 0 is successful and offset will contain the the new offset to use
20810  * for the next write.
20811  * Return negative value for error cases.
20812  **/
20813 int
20814 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
20815                uint32_t size, uint32_t *offset)
20816 {
20817         struct lpfc_mbx_wr_object *wr_object;
20818         LPFC_MBOXQ_t *mbox;
20819         int rc = 0, i = 0;
20820         uint32_t shdr_status, shdr_add_status, shdr_add_status_2;
20821         uint32_t shdr_change_status = 0, shdr_csf = 0;
20822         uint32_t mbox_tmo;
20823         struct lpfc_dmabuf *dmabuf;
20824         uint32_t written = 0;
20825         bool check_change_status = false;
20826
20827         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20828         if (!mbox)
20829                 return -ENOMEM;
20830
20831         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
20832                         LPFC_MBOX_OPCODE_WRITE_OBJECT,
20833                         sizeof(struct lpfc_mbx_wr_object) -
20834                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
20835
20836         wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
20837         wr_object->u.request.write_offset = *offset;
20838         sprintf((uint8_t *)wr_object->u.request.object_name, "/");
20839         wr_object->u.request.object_name[0] =
20840                 cpu_to_le32(wr_object->u.request.object_name[0]);
20841         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
20842         list_for_each_entry(dmabuf, dmabuf_list, list) {
20843                 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
20844                         break;
20845                 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
20846                 wr_object->u.request.bde[i].addrHigh =
20847                         putPaddrHigh(dmabuf->phys);
20848                 if (written + SLI4_PAGE_SIZE >= size) {
20849                         wr_object->u.request.bde[i].tus.f.bdeSize =
20850                                 (size - written);
20851                         written += (size - written);
20852                         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
20853                         bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
20854                         check_change_status = true;
20855                 } else {
20856                         wr_object->u.request.bde[i].tus.f.bdeSize =
20857                                 SLI4_PAGE_SIZE;
20858                         written += SLI4_PAGE_SIZE;
20859                 }
20860                 i++;
20861         }
20862         wr_object->u.request.bde_count = i;
20863         bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
20864         if (!phba->sli4_hba.intr_enable)
20865                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
20866         else {
20867                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
20868                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
20869         }
20870         /* The IOCTL status is embedded in the mailbox subheader. */
20871         shdr_status = bf_get(lpfc_mbox_hdr_status,
20872                              &wr_object->header.cfg_shdr.response);
20873         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20874                                  &wr_object->header.cfg_shdr.response);
20875         shdr_add_status_2 = bf_get(lpfc_mbox_hdr_add_status_2,
20876                                    &wr_object->header.cfg_shdr.response);
20877         if (check_change_status) {
20878                 shdr_change_status = bf_get(lpfc_wr_object_change_status,
20879                                             &wr_object->u.response);
20880                 shdr_csf = bf_get(lpfc_wr_object_csf,
20881                                   &wr_object->u.response);
20882         }
20883
20884         if (!phba->sli4_hba.intr_enable)
20885                 mempool_free(mbox, phba->mbox_mem_pool);
20886         else if (rc != MBX_TIMEOUT)
20887                 mempool_free(mbox, phba->mbox_mem_pool);
20888         if (shdr_status || shdr_add_status || shdr_add_status_2 || rc) {
20889                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20890                                 "3025 Write Object mailbox failed with "
20891                                 "status x%x add_status x%x, add_status_2 x%x, "
20892                                 "mbx status x%x\n",
20893                                 shdr_status, shdr_add_status, shdr_add_status_2,
20894                                 rc);
20895                 rc = -ENXIO;
20896                 *offset = shdr_add_status;
20897         } else {
20898                 *offset += wr_object->u.response.actual_write_length;
20899         }
20900
20901         if (rc || check_change_status)
20902                 lpfc_log_fw_write_cmpl(phba, shdr_status, shdr_add_status,
20903                                        shdr_add_status_2, shdr_change_status,
20904                                        shdr_csf);
20905         return rc;
20906 }
20907
20908 /**
20909  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
20910  * @vport: pointer to vport data structure.
20911  *
20912  * This function iterate through the mailboxq and clean up all REG_LOGIN
20913  * and REG_VPI mailbox commands associated with the vport. This function
20914  * is called when driver want to restart discovery of the vport due to
20915  * a Clear Virtual Link event.
20916  **/
20917 void
20918 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
20919 {
20920         struct lpfc_hba *phba = vport->phba;
20921         LPFC_MBOXQ_t *mb, *nextmb;
20922         struct lpfc_dmabuf *mp;
20923         struct lpfc_nodelist *ndlp;
20924         struct lpfc_nodelist *act_mbx_ndlp = NULL;
20925         LIST_HEAD(mbox_cmd_list);
20926         uint8_t restart_loop;
20927
20928         /* Clean up internally queued mailbox commands with the vport */
20929         spin_lock_irq(&phba->hbalock);
20930         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
20931                 if (mb->vport != vport)
20932                         continue;
20933
20934                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
20935                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
20936                         continue;
20937
20938                 list_move_tail(&mb->list, &mbox_cmd_list);
20939         }
20940         /* Clean up active mailbox command with the vport */
20941         mb = phba->sli.mbox_active;
20942         if (mb && (mb->vport == vport)) {
20943                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
20944                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
20945                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
20946                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
20947                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
20948                         /* Put reference count for delayed processing */
20949                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
20950                         /* Unregister the RPI when mailbox complete */
20951                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
20952                 }
20953         }
20954         /* Cleanup any mailbox completions which are not yet processed */
20955         do {
20956                 restart_loop = 0;
20957                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
20958                         /*
20959                          * If this mailox is already processed or it is
20960                          * for another vport ignore it.
20961                          */
20962                         if ((mb->vport != vport) ||
20963                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
20964                                 continue;
20965
20966                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
20967                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
20968                                 continue;
20969
20970                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
20971                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
20972                                 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
20973                                 /* Unregister the RPI when mailbox complete */
20974                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
20975                                 restart_loop = 1;
20976                                 spin_unlock_irq(&phba->hbalock);
20977                                 spin_lock(&ndlp->lock);
20978                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
20979                                 spin_unlock(&ndlp->lock);
20980                                 spin_lock_irq(&phba->hbalock);
20981                                 break;
20982                         }
20983                 }
20984         } while (restart_loop);
20985
20986         spin_unlock_irq(&phba->hbalock);
20987
20988         /* Release the cleaned-up mailbox commands */
20989         while (!list_empty(&mbox_cmd_list)) {
20990                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
20991                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
20992                         mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
20993                         if (mp) {
20994                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
20995                                 kfree(mp);
20996                         }
20997                         mb->ctx_buf = NULL;
20998                         ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
20999                         mb->ctx_ndlp = NULL;
21000                         if (ndlp) {
21001                                 spin_lock(&ndlp->lock);
21002                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
21003                                 spin_unlock(&ndlp->lock);
21004                                 lpfc_nlp_put(ndlp);
21005                         }
21006                 }
21007                 mempool_free(mb, phba->mbox_mem_pool);
21008         }
21009
21010         /* Release the ndlp with the cleaned-up active mailbox command */
21011         if (act_mbx_ndlp) {
21012                 spin_lock(&act_mbx_ndlp->lock);
21013                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
21014                 spin_unlock(&act_mbx_ndlp->lock);
21015                 lpfc_nlp_put(act_mbx_ndlp);
21016         }
21017 }
21018
21019 /**
21020  * lpfc_drain_txq - Drain the txq
21021  * @phba: Pointer to HBA context object.
21022  *
21023  * This function attempt to submit IOCBs on the txq
21024  * to the adapter.  For SLI4 adapters, the txq contains
21025  * ELS IOCBs that have been deferred because the there
21026  * are no SGLs.  This congestion can occur with large
21027  * vport counts during node discovery.
21028  **/
21029
21030 uint32_t
21031 lpfc_drain_txq(struct lpfc_hba *phba)
21032 {
21033         LIST_HEAD(completions);
21034         struct lpfc_sli_ring *pring;
21035         struct lpfc_iocbq *piocbq = NULL;
21036         unsigned long iflags = 0;
21037         char *fail_msg = NULL;
21038         struct lpfc_sglq *sglq;
21039         union lpfc_wqe128 wqe;
21040         uint32_t txq_cnt = 0;
21041         struct lpfc_queue *wq;
21042
21043         if (phba->link_flag & LS_MDS_LOOPBACK) {
21044                 /* MDS WQE are posted only to first WQ*/
21045                 wq = phba->sli4_hba.hdwq[0].io_wq;
21046                 if (unlikely(!wq))
21047                         return 0;
21048                 pring = wq->pring;
21049         } else {
21050                 wq = phba->sli4_hba.els_wq;
21051                 if (unlikely(!wq))
21052                         return 0;
21053                 pring = lpfc_phba_elsring(phba);
21054         }
21055
21056         if (unlikely(!pring) || list_empty(&pring->txq))
21057                 return 0;
21058
21059         spin_lock_irqsave(&pring->ring_lock, iflags);
21060         list_for_each_entry(piocbq, &pring->txq, list) {
21061                 txq_cnt++;
21062         }
21063
21064         if (txq_cnt > pring->txq_max)
21065                 pring->txq_max = txq_cnt;
21066
21067         spin_unlock_irqrestore(&pring->ring_lock, iflags);
21068
21069         while (!list_empty(&pring->txq)) {
21070                 spin_lock_irqsave(&pring->ring_lock, iflags);
21071
21072                 piocbq = lpfc_sli_ringtx_get(phba, pring);
21073                 if (!piocbq) {
21074                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
21075                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21076                                 "2823 txq empty and txq_cnt is %d\n ",
21077                                 txq_cnt);
21078                         break;
21079                 }
21080                 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
21081                 if (!sglq) {
21082                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
21083                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
21084                         break;
21085                 }
21086                 txq_cnt--;
21087
21088                 /* The xri and iocb resources secured,
21089                  * attempt to issue request
21090                  */
21091                 piocbq->sli4_lxritag = sglq->sli4_lxritag;
21092                 piocbq->sli4_xritag = sglq->sli4_xritag;
21093                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
21094                         fail_msg = "to convert bpl to sgl";
21095                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
21096                         fail_msg = "to convert iocb to wqe";
21097                 else if (lpfc_sli4_wq_put(wq, &wqe))
21098                         fail_msg = " - Wq is full";
21099                 else
21100                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
21101
21102                 if (fail_msg) {
21103                         /* Failed means we can't issue and need to cancel */
21104                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21105                                         "2822 IOCB failed %s iotag 0x%x "
21106                                         "xri 0x%x\n",
21107                                         fail_msg,
21108                                         piocbq->iotag, piocbq->sli4_xritag);
21109                         list_add_tail(&piocbq->list, &completions);
21110                 }
21111                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
21112         }
21113
21114         /* Cancel all the IOCBs that cannot be issued */
21115         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
21116                                 IOERR_SLI_ABORTED);
21117
21118         return txq_cnt;
21119 }
21120
21121 /**
21122  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
21123  * @phba: Pointer to HBA context object.
21124  * @pwqeq: Pointer to command WQE.
21125  * @sglq: Pointer to the scatter gather queue object.
21126  *
21127  * This routine converts the bpl or bde that is in the WQE
21128  * to a sgl list for the sli4 hardware. The physical address
21129  * of the bpl/bde is converted back to a virtual address.
21130  * If the WQE contains a BPL then the list of BDE's is
21131  * converted to sli4_sge's. If the WQE contains a single
21132  * BDE then it is converted to a single sli_sge.
21133  * The WQE is still in cpu endianness so the contents of
21134  * the bpl can be used without byte swapping.
21135  *
21136  * Returns valid XRI = Success, NO_XRI = Failure.
21137  */
21138 static uint16_t
21139 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
21140                  struct lpfc_sglq *sglq)
21141 {
21142         uint16_t xritag = NO_XRI;
21143         struct ulp_bde64 *bpl = NULL;
21144         struct ulp_bde64 bde;
21145         struct sli4_sge *sgl  = NULL;
21146         struct lpfc_dmabuf *dmabuf;
21147         union lpfc_wqe128 *wqe;
21148         int numBdes = 0;
21149         int i = 0;
21150         uint32_t offset = 0; /* accumulated offset in the sg request list */
21151         int inbound = 0; /* number of sg reply entries inbound from firmware */
21152         uint32_t cmd;
21153
21154         if (!pwqeq || !sglq)
21155                 return xritag;
21156
21157         sgl  = (struct sli4_sge *)sglq->sgl;
21158         wqe = &pwqeq->wqe;
21159         pwqeq->iocb.ulpIoTag = pwqeq->iotag;
21160
21161         cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
21162         if (cmd == CMD_XMIT_BLS_RSP64_WQE)
21163                 return sglq->sli4_xritag;
21164         numBdes = pwqeq->rsvd2;
21165         if (numBdes) {
21166                 /* The addrHigh and addrLow fields within the WQE
21167                  * have not been byteswapped yet so there is no
21168                  * need to swap them back.
21169                  */
21170                 if (pwqeq->context3)
21171                         dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
21172                 else
21173                         return xritag;
21174
21175                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
21176                 if (!bpl)
21177                         return xritag;
21178
21179                 for (i = 0; i < numBdes; i++) {
21180                         /* Should already be byte swapped. */
21181                         sgl->addr_hi = bpl->addrHigh;
21182                         sgl->addr_lo = bpl->addrLow;
21183
21184                         sgl->word2 = le32_to_cpu(sgl->word2);
21185                         if ((i+1) == numBdes)
21186                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
21187                         else
21188                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
21189                         /* swap the size field back to the cpu so we
21190                          * can assign it to the sgl.
21191                          */
21192                         bde.tus.w = le32_to_cpu(bpl->tus.w);
21193                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
21194                         /* The offsets in the sgl need to be accumulated
21195                          * separately for the request and reply lists.
21196                          * The request is always first, the reply follows.
21197                          */
21198                         switch (cmd) {
21199                         case CMD_GEN_REQUEST64_WQE:
21200                                 /* add up the reply sg entries */
21201                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
21202                                         inbound++;
21203                                 /* first inbound? reset the offset */
21204                                 if (inbound == 1)
21205                                         offset = 0;
21206                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
21207                                 bf_set(lpfc_sli4_sge_type, sgl,
21208                                         LPFC_SGE_TYPE_DATA);
21209                                 offset += bde.tus.f.bdeSize;
21210                                 break;
21211                         case CMD_FCP_TRSP64_WQE:
21212                                 bf_set(lpfc_sli4_sge_offset, sgl, 0);
21213                                 bf_set(lpfc_sli4_sge_type, sgl,
21214                                         LPFC_SGE_TYPE_DATA);
21215                                 break;
21216                         case CMD_FCP_TSEND64_WQE:
21217                         case CMD_FCP_TRECEIVE64_WQE:
21218                                 bf_set(lpfc_sli4_sge_type, sgl,
21219                                         bpl->tus.f.bdeFlags);
21220                                 if (i < 3)
21221                                         offset = 0;
21222                                 else
21223                                         offset += bde.tus.f.bdeSize;
21224                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
21225                                 break;
21226                         }
21227                         sgl->word2 = cpu_to_le32(sgl->word2);
21228                         bpl++;
21229                         sgl++;
21230                 }
21231         } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
21232                 /* The addrHigh and addrLow fields of the BDE have not
21233                  * been byteswapped yet so they need to be swapped
21234                  * before putting them in the sgl.
21235                  */
21236                 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
21237                 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
21238                 sgl->word2 = le32_to_cpu(sgl->word2);
21239                 bf_set(lpfc_sli4_sge_last, sgl, 1);
21240                 sgl->word2 = cpu_to_le32(sgl->word2);
21241                 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
21242         }
21243         return sglq->sli4_xritag;
21244 }
21245
21246 /**
21247  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
21248  * @phba: Pointer to HBA context object.
21249  * @qp: Pointer to HDW queue.
21250  * @pwqe: Pointer to command WQE.
21251  **/
21252 int
21253 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21254                     struct lpfc_iocbq *pwqe)
21255 {
21256         union lpfc_wqe128 *wqe = &pwqe->wqe;
21257         struct lpfc_async_xchg_ctx *ctxp;
21258         struct lpfc_queue *wq;
21259         struct lpfc_sglq *sglq;
21260         struct lpfc_sli_ring *pring;
21261         unsigned long iflags;
21262         uint32_t ret = 0;
21263
21264         /* NVME_LS and NVME_LS ABTS requests. */
21265         if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
21266                 pring =  phba->sli4_hba.nvmels_wq->pring;
21267                 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21268                                           qp, wq_access);
21269                 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
21270                 if (!sglq) {
21271                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
21272                         return WQE_BUSY;
21273                 }
21274                 pwqe->sli4_lxritag = sglq->sli4_lxritag;
21275                 pwqe->sli4_xritag = sglq->sli4_xritag;
21276                 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
21277                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
21278                         return WQE_ERROR;
21279                 }
21280                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21281                        pwqe->sli4_xritag);
21282                 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
21283                 if (ret) {
21284                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
21285                         return ret;
21286                 }
21287
21288                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21289                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
21290
21291                 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
21292                 return 0;
21293         }
21294
21295         /* NVME_FCREQ and NVME_ABTS requests */
21296         if (pwqe->iocb_flag & (LPFC_IO_NVME | LPFC_IO_FCP | LPFC_IO_CMF)) {
21297                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
21298                 wq = qp->io_wq;
21299                 pring = wq->pring;
21300
21301                 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21302
21303                 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21304                                           qp, wq_access);
21305                 ret = lpfc_sli4_wq_put(wq, wqe);
21306                 if (ret) {
21307                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
21308                         return ret;
21309                 }
21310                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21311                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
21312
21313                 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
21314                 return 0;
21315         }
21316
21317         /* NVMET requests */
21318         if (pwqe->iocb_flag & LPFC_IO_NVMET) {
21319                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
21320                 wq = qp->io_wq;
21321                 pring = wq->pring;
21322
21323                 ctxp = pwqe->context2;
21324                 sglq = ctxp->ctxbuf->sglq;
21325                 if (pwqe->sli4_xritag ==  NO_XRI) {
21326                         pwqe->sli4_lxritag = sglq->sli4_lxritag;
21327                         pwqe->sli4_xritag = sglq->sli4_xritag;
21328                 }
21329                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21330                        pwqe->sli4_xritag);
21331                 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21332
21333                 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21334                                           qp, wq_access);
21335                 ret = lpfc_sli4_wq_put(wq, wqe);
21336                 if (ret) {
21337                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
21338                         return ret;
21339                 }
21340                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21341                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
21342
21343                 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
21344                 return 0;
21345         }
21346         return WQE_ERROR;
21347 }
21348
21349 /**
21350  * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort
21351  * @phba: Pointer to HBA context object.
21352  * @cmdiocb: Pointer to driver command iocb object.
21353  * @cmpl: completion function.
21354  *
21355  * Fill the appropriate fields for the abort WQE and call
21356  * internal routine lpfc_sli4_issue_wqe to send the WQE
21357  * This function is called with hbalock held and no ring_lock held.
21358  *
21359  * RETURNS 0 - SUCCESS
21360  **/
21361
21362 int
21363 lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
21364                             void *cmpl)
21365 {
21366         struct lpfc_vport *vport = cmdiocb->vport;
21367         struct lpfc_iocbq *abtsiocb = NULL;
21368         union lpfc_wqe128 *abtswqe;
21369         struct lpfc_io_buf *lpfc_cmd;
21370         int retval = IOCB_ERROR;
21371         u16 xritag = cmdiocb->sli4_xritag;
21372
21373         /*
21374          * The scsi command can not be in txq and it is in flight because the
21375          * pCmd is still pointing at the SCSI command we have to abort. There
21376          * is no need to search the txcmplq. Just send an abort to the FW.
21377          */
21378
21379         abtsiocb = __lpfc_sli_get_iocbq(phba);
21380         if (!abtsiocb)
21381                 return WQE_NORESOURCE;
21382
21383         /* Indicate the IO is being aborted by the driver. */
21384         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
21385
21386         abtswqe = &abtsiocb->wqe;
21387         memset(abtswqe, 0, sizeof(*abtswqe));
21388
21389         if (!lpfc_is_link_up(phba))
21390                 bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);
21391         bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);
21392         abtswqe->abort_cmd.rsrvd5 = 0;
21393         abtswqe->abort_cmd.wqe_com.abort_tag = xritag;
21394         bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);
21395         bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
21396         bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);
21397         bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);
21398         bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
21399         bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);
21400
21401         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
21402         abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;
21403         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
21404         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
21405                 abtsiocb->iocb_flag |= LPFC_IO_FCP;
21406         if (cmdiocb->iocb_flag & LPFC_IO_NVME)
21407                 abtsiocb->iocb_flag |= LPFC_IO_NVME;
21408         if (cmdiocb->iocb_flag & LPFC_IO_FOF)
21409                 abtsiocb->iocb_flag |= LPFC_IO_FOF;
21410         abtsiocb->vport = vport;
21411         abtsiocb->wqe_cmpl = cmpl;
21412
21413         lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);
21414         retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);
21415
21416         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
21417                          "0359 Abort xri x%x, original iotag x%x, "
21418                          "abort cmd iotag x%x retval x%x\n",
21419                          xritag, cmdiocb->iotag, abtsiocb->iotag, retval);
21420
21421         if (retval) {
21422                 cmdiocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
21423                 __lpfc_sli_release_iocbq(phba, abtsiocb);
21424         }
21425
21426         return retval;
21427 }
21428
21429 #ifdef LPFC_MXP_STAT
21430 /**
21431  * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
21432  * @phba: pointer to lpfc hba data structure.
21433  * @hwqid: belong to which HWQ.
21434  *
21435  * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
21436  * 15 seconds after a test case is running.
21437  *
21438  * The user should call lpfc_debugfs_multixripools_write before running a test
21439  * case to clear stat_snapshot_taken. Then the user starts a test case. During
21440  * test case is running, stat_snapshot_taken is incremented by 1 every time when
21441  * this routine is called from heartbeat timer. When stat_snapshot_taken is
21442  * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
21443  **/
21444 void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
21445 {
21446         struct lpfc_sli4_hdw_queue *qp;
21447         struct lpfc_multixri_pool *multixri_pool;
21448         struct lpfc_pvt_pool *pvt_pool;
21449         struct lpfc_pbl_pool *pbl_pool;
21450         u32 txcmplq_cnt;
21451
21452         qp = &phba->sli4_hba.hdwq[hwqid];
21453         multixri_pool = qp->p_multixri_pool;
21454         if (!multixri_pool)
21455                 return;
21456
21457         if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
21458                 pvt_pool = &qp->p_multixri_pool->pvt_pool;
21459                 pbl_pool = &qp->p_multixri_pool->pbl_pool;
21460                 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21461
21462                 multixri_pool->stat_pbl_count = pbl_pool->count;
21463                 multixri_pool->stat_pvt_count = pvt_pool->count;
21464                 multixri_pool->stat_busy_count = txcmplq_cnt;
21465         }
21466
21467         multixri_pool->stat_snapshot_taken++;
21468 }
21469 #endif
21470
21471 /**
21472  * lpfc_adjust_pvt_pool_count - Adjust private pool count
21473  * @phba: pointer to lpfc hba data structure.
21474  * @hwqid: belong to which HWQ.
21475  *
21476  * This routine moves some XRIs from private to public pool when private pool
21477  * is not busy.
21478  **/
21479 void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
21480 {
21481         struct lpfc_multixri_pool *multixri_pool;
21482         u32 io_req_count;
21483         u32 prev_io_req_count;
21484
21485         multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21486         if (!multixri_pool)
21487                 return;
21488         io_req_count = multixri_pool->io_req_count;
21489         prev_io_req_count = multixri_pool->prev_io_req_count;
21490
21491         if (prev_io_req_count != io_req_count) {
21492                 /* Private pool is busy */
21493                 multixri_pool->prev_io_req_count = io_req_count;
21494         } else {
21495                 /* Private pool is not busy.
21496                  * Move XRIs from private to public pool.
21497                  */
21498                 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
21499         }
21500 }
21501
21502 /**
21503  * lpfc_adjust_high_watermark - Adjust high watermark
21504  * @phba: pointer to lpfc hba data structure.
21505  * @hwqid: belong to which HWQ.
21506  *
21507  * This routine sets high watermark as number of outstanding XRIs,
21508  * but make sure the new value is between xri_limit/2 and xri_limit.
21509  **/
21510 void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
21511 {
21512         u32 new_watermark;
21513         u32 watermark_max;
21514         u32 watermark_min;
21515         u32 xri_limit;
21516         u32 txcmplq_cnt;
21517         u32 abts_io_bufs;
21518         struct lpfc_multixri_pool *multixri_pool;
21519         struct lpfc_sli4_hdw_queue *qp;
21520
21521         qp = &phba->sli4_hba.hdwq[hwqid];
21522         multixri_pool = qp->p_multixri_pool;
21523         if (!multixri_pool)
21524                 return;
21525         xri_limit = multixri_pool->xri_limit;
21526
21527         watermark_max = xri_limit;
21528         watermark_min = xri_limit / 2;
21529
21530         txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21531         abts_io_bufs = qp->abts_scsi_io_bufs;
21532         abts_io_bufs += qp->abts_nvme_io_bufs;
21533
21534         new_watermark = txcmplq_cnt + abts_io_bufs;
21535         new_watermark = min(watermark_max, new_watermark);
21536         new_watermark = max(watermark_min, new_watermark);
21537         multixri_pool->pvt_pool.high_watermark = new_watermark;
21538
21539 #ifdef LPFC_MXP_STAT
21540         multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
21541                                           new_watermark);
21542 #endif
21543 }
21544
21545 /**
21546  * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
21547  * @phba: pointer to lpfc hba data structure.
21548  * @hwqid: belong to which HWQ.
21549  *
21550  * This routine is called from hearbeat timer when pvt_pool is idle.
21551  * All free XRIs are moved from private to public pool on hwqid with 2 steps.
21552  * The first step moves (all - low_watermark) amount of XRIs.
21553  * The second step moves the rest of XRIs.
21554  **/
21555 void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
21556 {
21557         struct lpfc_pbl_pool *pbl_pool;
21558         struct lpfc_pvt_pool *pvt_pool;
21559         struct lpfc_sli4_hdw_queue *qp;
21560         struct lpfc_io_buf *lpfc_ncmd;
21561         struct lpfc_io_buf *lpfc_ncmd_next;
21562         unsigned long iflag;
21563         struct list_head tmp_list;
21564         u32 tmp_count;
21565
21566         qp = &phba->sli4_hba.hdwq[hwqid];
21567         pbl_pool = &qp->p_multixri_pool->pbl_pool;
21568         pvt_pool = &qp->p_multixri_pool->pvt_pool;
21569         tmp_count = 0;
21570
21571         lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
21572         lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
21573
21574         if (pvt_pool->count > pvt_pool->low_watermark) {
21575                 /* Step 1: move (all - low_watermark) from pvt_pool
21576                  * to pbl_pool
21577                  */
21578
21579                 /* Move low watermark of bufs from pvt_pool to tmp_list */
21580                 INIT_LIST_HEAD(&tmp_list);
21581                 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21582                                          &pvt_pool->list, list) {
21583                         list_move_tail(&lpfc_ncmd->list, &tmp_list);
21584                         tmp_count++;
21585                         if (tmp_count >= pvt_pool->low_watermark)
21586                                 break;
21587                 }
21588
21589                 /* Move all bufs from pvt_pool to pbl_pool */
21590                 list_splice_init(&pvt_pool->list, &pbl_pool->list);
21591
21592                 /* Move all bufs from tmp_list to pvt_pool */
21593                 list_splice(&tmp_list, &pvt_pool->list);
21594
21595                 pbl_pool->count += (pvt_pool->count - tmp_count);
21596                 pvt_pool->count = tmp_count;
21597         } else {
21598                 /* Step 2: move the rest from pvt_pool to pbl_pool */
21599                 list_splice_init(&pvt_pool->list, &pbl_pool->list);
21600                 pbl_pool->count += pvt_pool->count;
21601                 pvt_pool->count = 0;
21602         }
21603
21604         spin_unlock(&pvt_pool->lock);
21605         spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21606 }
21607
21608 /**
21609  * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21610  * @phba: pointer to lpfc hba data structure
21611  * @qp: pointer to HDW queue
21612  * @pbl_pool: specified public free XRI pool
21613  * @pvt_pool: specified private free XRI pool
21614  * @count: number of XRIs to move
21615  *
21616  * This routine tries to move some free common bufs from the specified pbl_pool
21617  * to the specified pvt_pool. It might move less than count XRIs if there's not
21618  * enough in public pool.
21619  *
21620  * Return:
21621  *   true - if XRIs are successfully moved from the specified pbl_pool to the
21622  *          specified pvt_pool
21623  *   false - if the specified pbl_pool is empty or locked by someone else
21624  **/
21625 static bool
21626 _lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21627                           struct lpfc_pbl_pool *pbl_pool,
21628                           struct lpfc_pvt_pool *pvt_pool, u32 count)
21629 {
21630         struct lpfc_io_buf *lpfc_ncmd;
21631         struct lpfc_io_buf *lpfc_ncmd_next;
21632         unsigned long iflag;
21633         int ret;
21634
21635         ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
21636         if (ret) {
21637                 if (pbl_pool->count) {
21638                         /* Move a batch of XRIs from public to private pool */
21639                         lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
21640                         list_for_each_entry_safe(lpfc_ncmd,
21641                                                  lpfc_ncmd_next,
21642                                                  &pbl_pool->list,
21643                                                  list) {
21644                                 list_move_tail(&lpfc_ncmd->list,
21645                                                &pvt_pool->list);
21646                                 pvt_pool->count++;
21647                                 pbl_pool->count--;
21648                                 count--;
21649                                 if (count == 0)
21650                                         break;
21651                         }
21652
21653                         spin_unlock(&pvt_pool->lock);
21654                         spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21655                         return true;
21656                 }
21657                 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21658         }
21659
21660         return false;
21661 }
21662
21663 /**
21664  * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21665  * @phba: pointer to lpfc hba data structure.
21666  * @hwqid: belong to which HWQ.
21667  * @count: number of XRIs to move
21668  *
21669  * This routine tries to find some free common bufs in one of public pools with
21670  * Round Robin method. The search always starts from local hwqid, then the next
21671  * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
21672  * a batch of free common bufs are moved to private pool on hwqid.
21673  * It might move less than count XRIs if there's not enough in public pool.
21674  **/
21675 void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
21676 {
21677         struct lpfc_multixri_pool *multixri_pool;
21678         struct lpfc_multixri_pool *next_multixri_pool;
21679         struct lpfc_pvt_pool *pvt_pool;
21680         struct lpfc_pbl_pool *pbl_pool;
21681         struct lpfc_sli4_hdw_queue *qp;
21682         u32 next_hwqid;
21683         u32 hwq_count;
21684         int ret;
21685
21686         qp = &phba->sli4_hba.hdwq[hwqid];
21687         multixri_pool = qp->p_multixri_pool;
21688         pvt_pool = &multixri_pool->pvt_pool;
21689         pbl_pool = &multixri_pool->pbl_pool;
21690
21691         /* Check if local pbl_pool is available */
21692         ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
21693         if (ret) {
21694 #ifdef LPFC_MXP_STAT
21695                 multixri_pool->local_pbl_hit_count++;
21696 #endif
21697                 return;
21698         }
21699
21700         hwq_count = phba->cfg_hdw_queue;
21701
21702         /* Get the next hwqid which was found last time */
21703         next_hwqid = multixri_pool->rrb_next_hwqid;
21704
21705         do {
21706                 /* Go to next hwq */
21707                 next_hwqid = (next_hwqid + 1) % hwq_count;
21708
21709                 next_multixri_pool =
21710                         phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
21711                 pbl_pool = &next_multixri_pool->pbl_pool;
21712
21713                 /* Check if the public free xri pool is available */
21714                 ret = _lpfc_move_xri_pbl_to_pvt(
21715                         phba, qp, pbl_pool, pvt_pool, count);
21716
21717                 /* Exit while-loop if success or all hwqid are checked */
21718         } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
21719
21720         /* Starting point for the next time */
21721         multixri_pool->rrb_next_hwqid = next_hwqid;
21722
21723         if (!ret) {
21724                 /* stats: all public pools are empty*/
21725                 multixri_pool->pbl_empty_count++;
21726         }
21727
21728 #ifdef LPFC_MXP_STAT
21729         if (ret) {
21730                 if (next_hwqid == hwqid)
21731                         multixri_pool->local_pbl_hit_count++;
21732                 else
21733                         multixri_pool->other_pbl_hit_count++;
21734         }
21735 #endif
21736 }
21737
21738 /**
21739  * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
21740  * @phba: pointer to lpfc hba data structure.
21741  * @hwqid: belong to which HWQ.
21742  *
21743  * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
21744  * low watermark.
21745  **/
21746 void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
21747 {
21748         struct lpfc_multixri_pool *multixri_pool;
21749         struct lpfc_pvt_pool *pvt_pool;
21750
21751         multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21752         pvt_pool = &multixri_pool->pvt_pool;
21753
21754         if (pvt_pool->count < pvt_pool->low_watermark)
21755                 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21756 }
21757
21758 /**
21759  * lpfc_release_io_buf - Return one IO buf back to free pool
21760  * @phba: pointer to lpfc hba data structure.
21761  * @lpfc_ncmd: IO buf to be returned.
21762  * @qp: belong to which HWQ.
21763  *
21764  * This routine returns one IO buf back to free pool. If this is an urgent IO,
21765  * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
21766  * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
21767  * xri_limit.  If cfg_xri_rebalancing==0, the IO buf is returned to
21768  * lpfc_io_buf_list_put.
21769  **/
21770 void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
21771                          struct lpfc_sli4_hdw_queue *qp)
21772 {
21773         unsigned long iflag;
21774         struct lpfc_pbl_pool *pbl_pool;
21775         struct lpfc_pvt_pool *pvt_pool;
21776         struct lpfc_epd_pool *epd_pool;
21777         u32 txcmplq_cnt;
21778         u32 xri_owned;
21779         u32 xri_limit;
21780         u32 abts_io_bufs;
21781
21782         /* MUST zero fields if buffer is reused by another protocol */
21783         lpfc_ncmd->nvmeCmd = NULL;
21784         lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
21785         lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
21786
21787         if (phba->cfg_xpsgl && !phba->nvmet_support &&
21788             !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
21789                 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
21790
21791         if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
21792                 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
21793
21794         if (phba->cfg_xri_rebalancing) {
21795                 if (lpfc_ncmd->expedite) {
21796                         /* Return to expedite pool */
21797                         epd_pool = &phba->epd_pool;
21798                         spin_lock_irqsave(&epd_pool->lock, iflag);
21799                         list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
21800                         epd_pool->count++;
21801                         spin_unlock_irqrestore(&epd_pool->lock, iflag);
21802                         return;
21803                 }
21804
21805                 /* Avoid invalid access if an IO sneaks in and is being rejected
21806                  * just _after_ xri pools are destroyed in lpfc_offline.
21807                  * Nothing much can be done at this point.
21808                  */
21809                 if (!qp->p_multixri_pool)
21810                         return;
21811
21812                 pbl_pool = &qp->p_multixri_pool->pbl_pool;
21813                 pvt_pool = &qp->p_multixri_pool->pvt_pool;
21814
21815                 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21816                 abts_io_bufs = qp->abts_scsi_io_bufs;
21817                 abts_io_bufs += qp->abts_nvme_io_bufs;
21818
21819                 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
21820                 xri_limit = qp->p_multixri_pool->xri_limit;
21821
21822 #ifdef LPFC_MXP_STAT
21823                 if (xri_owned <= xri_limit)
21824                         qp->p_multixri_pool->below_limit_count++;
21825                 else
21826                         qp->p_multixri_pool->above_limit_count++;
21827 #endif
21828
21829                 /* XRI goes to either public or private free xri pool
21830                  *     based on watermark and xri_limit
21831                  */
21832                 if ((pvt_pool->count < pvt_pool->low_watermark) ||
21833                     (xri_owned < xri_limit &&
21834                      pvt_pool->count < pvt_pool->high_watermark)) {
21835                         lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
21836                                                   qp, free_pvt_pool);
21837                         list_add_tail(&lpfc_ncmd->list,
21838                                       &pvt_pool->list);
21839                         pvt_pool->count++;
21840                         spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21841                 } else {
21842                         lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
21843                                                   qp, free_pub_pool);
21844                         list_add_tail(&lpfc_ncmd->list,
21845                                       &pbl_pool->list);
21846                         pbl_pool->count++;
21847                         spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21848                 }
21849         } else {
21850                 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
21851                                           qp, free_xri);
21852                 list_add_tail(&lpfc_ncmd->list,
21853                               &qp->lpfc_io_buf_list_put);
21854                 qp->put_io_bufs++;
21855                 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
21856                                        iflag);
21857         }
21858 }
21859
21860 /**
21861  * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
21862  * @phba: pointer to lpfc hba data structure.
21863  * @qp: pointer to HDW queue
21864  * @pvt_pool: pointer to private pool data structure.
21865  * @ndlp: pointer to lpfc nodelist data structure.
21866  *
21867  * This routine tries to get one free IO buf from private pool.
21868  *
21869  * Return:
21870  *   pointer to one free IO buf - if private pool is not empty
21871  *   NULL - if private pool is empty
21872  **/
21873 static struct lpfc_io_buf *
21874 lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
21875                                   struct lpfc_sli4_hdw_queue *qp,
21876                                   struct lpfc_pvt_pool *pvt_pool,
21877                                   struct lpfc_nodelist *ndlp)
21878 {
21879         struct lpfc_io_buf *lpfc_ncmd;
21880         struct lpfc_io_buf *lpfc_ncmd_next;
21881         unsigned long iflag;
21882
21883         lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
21884         list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21885                                  &pvt_pool->list, list) {
21886                 if (lpfc_test_rrq_active(
21887                         phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
21888                         continue;
21889                 list_del(&lpfc_ncmd->list);
21890                 pvt_pool->count--;
21891                 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21892                 return lpfc_ncmd;
21893         }
21894         spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21895
21896         return NULL;
21897 }
21898
21899 /**
21900  * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
21901  * @phba: pointer to lpfc hba data structure.
21902  *
21903  * This routine tries to get one free IO buf from expedite pool.
21904  *
21905  * Return:
21906  *   pointer to one free IO buf - if expedite pool is not empty
21907  *   NULL - if expedite pool is empty
21908  **/
21909 static struct lpfc_io_buf *
21910 lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
21911 {
21912         struct lpfc_io_buf *lpfc_ncmd;
21913         struct lpfc_io_buf *lpfc_ncmd_next;
21914         unsigned long iflag;
21915         struct lpfc_epd_pool *epd_pool;
21916
21917         epd_pool = &phba->epd_pool;
21918         lpfc_ncmd = NULL;
21919
21920         spin_lock_irqsave(&epd_pool->lock, iflag);
21921         if (epd_pool->count > 0) {
21922                 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21923                                          &epd_pool->list, list) {
21924                         list_del(&lpfc_ncmd->list);
21925                         epd_pool->count--;
21926                         break;
21927                 }
21928         }
21929         spin_unlock_irqrestore(&epd_pool->lock, iflag);
21930
21931         return lpfc_ncmd;
21932 }
21933
21934 /**
21935  * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
21936  * @phba: pointer to lpfc hba data structure.
21937  * @ndlp: pointer to lpfc nodelist data structure.
21938  * @hwqid: belong to which HWQ
21939  * @expedite: 1 means this request is urgent.
21940  *
21941  * This routine will do the following actions and then return a pointer to
21942  * one free IO buf.
21943  *
21944  * 1. If private free xri count is empty, move some XRIs from public to
21945  *    private pool.
21946  * 2. Get one XRI from private free xri pool.
21947  * 3. If we fail to get one from pvt_pool and this is an expedite request,
21948  *    get one free xri from expedite pool.
21949  *
21950  * Note: ndlp is only used on SCSI side for RRQ testing.
21951  *       The caller should pass NULL for ndlp on NVME side.
21952  *
21953  * Return:
21954  *   pointer to one free IO buf - if private pool is not empty
21955  *   NULL - if private pool is empty
21956  **/
21957 static struct lpfc_io_buf *
21958 lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
21959                                     struct lpfc_nodelist *ndlp,
21960                                     int hwqid, int expedite)
21961 {
21962         struct lpfc_sli4_hdw_queue *qp;
21963         struct lpfc_multixri_pool *multixri_pool;
21964         struct lpfc_pvt_pool *pvt_pool;
21965         struct lpfc_io_buf *lpfc_ncmd;
21966
21967         qp = &phba->sli4_hba.hdwq[hwqid];
21968         lpfc_ncmd = NULL;
21969         multixri_pool = qp->p_multixri_pool;
21970         pvt_pool = &multixri_pool->pvt_pool;
21971         multixri_pool->io_req_count++;
21972
21973         /* If pvt_pool is empty, move some XRIs from public to private pool */
21974         if (pvt_pool->count == 0)
21975                 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21976
21977         /* Get one XRI from private free xri pool */
21978         lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
21979
21980         if (lpfc_ncmd) {
21981                 lpfc_ncmd->hdwq = qp;
21982                 lpfc_ncmd->hdwq_no = hwqid;
21983         } else if (expedite) {
21984                 /* If we fail to get one from pvt_pool and this is an expedite
21985                  * request, get one free xri from expedite pool.
21986                  */
21987                 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
21988         }
21989
21990         return lpfc_ncmd;
21991 }
21992
21993 static inline struct lpfc_io_buf *
21994 lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
21995 {
21996         struct lpfc_sli4_hdw_queue *qp;
21997         struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
21998
21999         qp = &phba->sli4_hba.hdwq[idx];
22000         list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
22001                                  &qp->lpfc_io_buf_list_get, list) {
22002                 if (lpfc_test_rrq_active(phba, ndlp,
22003                                          lpfc_cmd->cur_iocbq.sli4_lxritag))
22004                         continue;
22005
22006                 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
22007                         continue;
22008
22009                 list_del_init(&lpfc_cmd->list);
22010                 qp->get_io_bufs--;
22011                 lpfc_cmd->hdwq = qp;
22012                 lpfc_cmd->hdwq_no = idx;
22013                 return lpfc_cmd;
22014         }
22015         return NULL;
22016 }
22017
22018 /**
22019  * lpfc_get_io_buf - Get one IO buffer from free pool
22020  * @phba: The HBA for which this call is being executed.
22021  * @ndlp: pointer to lpfc nodelist data structure.
22022  * @hwqid: belong to which HWQ
22023  * @expedite: 1 means this request is urgent.
22024  *
22025  * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
22026  * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
22027  * a IO buffer from head of @hdwq io_buf_list and returns to caller.
22028  *
22029  * Note: ndlp is only used on SCSI side for RRQ testing.
22030  *       The caller should pass NULL for ndlp on NVME side.
22031  *
22032  * Return codes:
22033  *   NULL - Error
22034  *   Pointer to lpfc_io_buf - Success
22035  **/
22036 struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
22037                                     struct lpfc_nodelist *ndlp,
22038                                     u32 hwqid, int expedite)
22039 {
22040         struct lpfc_sli4_hdw_queue *qp;
22041         unsigned long iflag;
22042         struct lpfc_io_buf *lpfc_cmd;
22043
22044         qp = &phba->sli4_hba.hdwq[hwqid];
22045         lpfc_cmd = NULL;
22046
22047         if (phba->cfg_xri_rebalancing)
22048                 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
22049                         phba, ndlp, hwqid, expedite);
22050         else {
22051                 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
22052                                           qp, alloc_xri_get);
22053                 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
22054                         lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22055                 if (!lpfc_cmd) {
22056                         lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
22057                                           qp, alloc_xri_put);
22058                         list_splice(&qp->lpfc_io_buf_list_put,
22059                                     &qp->lpfc_io_buf_list_get);
22060                         qp->get_io_bufs += qp->put_io_bufs;
22061                         INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
22062                         qp->put_io_bufs = 0;
22063                         spin_unlock(&qp->io_buf_list_put_lock);
22064                         if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
22065                             expedite)
22066                                 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22067                 }
22068                 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
22069         }
22070
22071         return lpfc_cmd;
22072 }
22073
22074 /**
22075  * lpfc_read_object - Retrieve object data from HBA
22076  * @phba: The HBA for which this call is being executed.
22077  * @rdobject: Pathname of object data we want to read.
22078  * @datap: Pointer to where data will be copied to.
22079  * @datasz: size of data area
22080  *
22081  * This routine is limited to object sizes of LPFC_BPL_SIZE (1024) or less.
22082  * The data will be truncated if datasz is not large enough.
22083  * Version 1 is not supported with Embedded mbox cmd, so we must use version 0.
22084  * Returns the actual bytes read from the object.
22085  */
22086 int
22087 lpfc_read_object(struct lpfc_hba *phba, char *rdobject, uint32_t *datap,
22088                  uint32_t datasz)
22089 {
22090         struct lpfc_mbx_read_object *read_object;
22091         LPFC_MBOXQ_t *mbox;
22092         int rc, length, eof, j, byte_cnt = 0;
22093         uint32_t shdr_status, shdr_add_status;
22094         union lpfc_sli4_cfg_shdr *shdr;
22095         struct lpfc_dmabuf *pcmd;
22096         u32 rd_object_name[LPFC_MBX_OBJECT_NAME_LEN_DW] = {0};
22097
22098         /* sanity check on queue memory */
22099         if (!datap)
22100                 return -ENODEV;
22101
22102         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
22103         if (!mbox)
22104                 return -ENOMEM;
22105         length = (sizeof(struct lpfc_mbx_read_object) -
22106                   sizeof(struct lpfc_sli4_cfg_mhdr));
22107         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
22108                          LPFC_MBOX_OPCODE_READ_OBJECT,
22109                          length, LPFC_SLI4_MBX_EMBED);
22110         read_object = &mbox->u.mqe.un.read_object;
22111         shdr = (union lpfc_sli4_cfg_shdr *)&read_object->header.cfg_shdr;
22112
22113         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_0);
22114         bf_set(lpfc_mbx_rd_object_rlen, &read_object->u.request, datasz);
22115         read_object->u.request.rd_object_offset = 0;
22116         read_object->u.request.rd_object_cnt = 1;
22117
22118         memset((void *)read_object->u.request.rd_object_name, 0,
22119                LPFC_OBJ_NAME_SZ);
22120         scnprintf((char *)rd_object_name, sizeof(rd_object_name), rdobject);
22121         for (j = 0; j < strlen(rdobject); j++)
22122                 read_object->u.request.rd_object_name[j] =
22123                         cpu_to_le32(rd_object_name[j]);
22124
22125         pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);
22126         if (pcmd)
22127                 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
22128         if (!pcmd || !pcmd->virt) {
22129                 kfree(pcmd);
22130                 mempool_free(mbox, phba->mbox_mem_pool);
22131                 return -ENOMEM;
22132         }
22133         memset((void *)pcmd->virt, 0, LPFC_BPL_SIZE);
22134         read_object->u.request.rd_object_hbuf[0].pa_lo =
22135                 putPaddrLow(pcmd->phys);
22136         read_object->u.request.rd_object_hbuf[0].pa_hi =
22137                 putPaddrHigh(pcmd->phys);
22138         read_object->u.request.rd_object_hbuf[0].length = LPFC_BPL_SIZE;
22139
22140         mbox->vport = phba->pport;
22141         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
22142         mbox->ctx_buf = NULL;
22143         mbox->ctx_ndlp = NULL;
22144
22145         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
22146         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
22147         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
22148
22149         if (shdr_status == STATUS_FAILED &&
22150             shdr_add_status == ADD_STATUS_INVALID_OBJECT_NAME) {
22151                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22152                                 "4674 No port cfg file in FW.\n");
22153                 byte_cnt = -ENOENT;
22154         } else if (shdr_status || shdr_add_status || rc) {
22155                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22156                                 "2625 READ_OBJECT mailbox failed with "
22157                                 "status x%x add_status x%x, mbx status x%x\n",
22158                                 shdr_status, shdr_add_status, rc);
22159                 byte_cnt = -ENXIO;
22160         } else {
22161                 /* Success */
22162                 length = read_object->u.response.rd_object_actual_rlen;
22163                 eof = bf_get(lpfc_mbx_rd_object_eof, &read_object->u.response);
22164                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_CGN_MGMT,
22165                                 "2626 READ_OBJECT Success len %d:%d, EOF %d\n",
22166                                 length, datasz, eof);
22167
22168                 /* Detect the port config file exists but is empty */
22169                 if (!length && eof) {
22170                         byte_cnt = 0;
22171                         goto exit;
22172                 }
22173
22174                 byte_cnt = length;
22175                 lpfc_sli_pcimem_bcopy(pcmd->virt, datap, byte_cnt);
22176         }
22177
22178  exit:
22179         lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
22180         kfree(pcmd);
22181         mempool_free(mbox, phba->mbox_mem_pool);
22182         return byte_cnt;
22183 }
22184
22185 /**
22186  * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
22187  * @phba: The HBA for which this call is being executed.
22188  * @lpfc_buf: IO buf structure to append the SGL chunk
22189  *
22190  * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
22191  * and will allocate an SGL chunk if the pool is empty.
22192  *
22193  * Return codes:
22194  *   NULL - Error
22195  *   Pointer to sli4_hybrid_sgl - Success
22196  **/
22197 struct sli4_hybrid_sgl *
22198 lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22199 {
22200         struct sli4_hybrid_sgl *list_entry = NULL;
22201         struct sli4_hybrid_sgl *tmp = NULL;
22202         struct sli4_hybrid_sgl *allocated_sgl = NULL;
22203         struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22204         struct list_head *buf_list = &hdwq->sgl_list;
22205         unsigned long iflags;
22206
22207         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22208
22209         if (likely(!list_empty(buf_list))) {
22210                 /* break off 1 chunk from the sgl_list */
22211                 list_for_each_entry_safe(list_entry, tmp,
22212                                          buf_list, list_node) {
22213                         list_move_tail(&list_entry->list_node,
22214                                        &lpfc_buf->dma_sgl_xtra_list);
22215                         break;
22216                 }
22217         } else {
22218                 /* allocate more */
22219                 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22220                 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22221                                    cpu_to_node(hdwq->io_wq->chann));
22222                 if (!tmp) {
22223                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22224                                         "8353 error kmalloc memory for HDWQ "
22225                                         "%d %s\n",
22226                                         lpfc_buf->hdwq_no, __func__);
22227                         return NULL;
22228                 }
22229
22230                 tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
22231                                               GFP_ATOMIC, &tmp->dma_phys_sgl);
22232                 if (!tmp->dma_sgl) {
22233                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22234                                         "8354 error pool_alloc memory for HDWQ "
22235                                         "%d %s\n",
22236                                         lpfc_buf->hdwq_no, __func__);
22237                         kfree(tmp);
22238                         return NULL;
22239                 }
22240
22241                 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22242                 list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
22243         }
22244
22245         allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
22246                                         struct sli4_hybrid_sgl,
22247                                         list_node);
22248
22249         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22250
22251         return allocated_sgl;
22252 }
22253
22254 /**
22255  * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
22256  * @phba: The HBA for which this call is being executed.
22257  * @lpfc_buf: IO buf structure with the SGL chunk
22258  *
22259  * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
22260  *
22261  * Return codes:
22262  *   0 - Success
22263  *   -EINVAL - Error
22264  **/
22265 int
22266 lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22267 {
22268         int rc = 0;
22269         struct sli4_hybrid_sgl *list_entry = NULL;
22270         struct sli4_hybrid_sgl *tmp = NULL;
22271         struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22272         struct list_head *buf_list = &hdwq->sgl_list;
22273         unsigned long iflags;
22274
22275         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22276
22277         if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
22278                 list_for_each_entry_safe(list_entry, tmp,
22279                                          &lpfc_buf->dma_sgl_xtra_list,
22280                                          list_node) {
22281                         list_move_tail(&list_entry->list_node,
22282                                        buf_list);
22283                 }
22284         } else {
22285                 rc = -EINVAL;
22286         }
22287
22288         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22289         return rc;
22290 }
22291
22292 /**
22293  * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
22294  * @phba: phba object
22295  * @hdwq: hdwq to cleanup sgl buff resources on
22296  *
22297  * This routine frees all SGL chunks of hdwq SGL chunk pool.
22298  *
22299  * Return codes:
22300  *   None
22301  **/
22302 void
22303 lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
22304                        struct lpfc_sli4_hdw_queue *hdwq)
22305 {
22306         struct list_head *buf_list = &hdwq->sgl_list;
22307         struct sli4_hybrid_sgl *list_entry = NULL;
22308         struct sli4_hybrid_sgl *tmp = NULL;
22309         unsigned long iflags;
22310
22311         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22312
22313         /* Free sgl pool */
22314         list_for_each_entry_safe(list_entry, tmp,
22315                                  buf_list, list_node) {
22316                 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
22317                               list_entry->dma_sgl,
22318                               list_entry->dma_phys_sgl);
22319                 list_del(&list_entry->list_node);
22320                 kfree(list_entry);
22321         }
22322
22323         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22324 }
22325
22326 /**
22327  * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
22328  * @phba: The HBA for which this call is being executed.
22329  * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
22330  *
22331  * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
22332  * and will allocate an CMD/RSP buffer if the pool is empty.
22333  *
22334  * Return codes:
22335  *   NULL - Error
22336  *   Pointer to fcp_cmd_rsp_buf - Success
22337  **/
22338 struct fcp_cmd_rsp_buf *
22339 lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22340                               struct lpfc_io_buf *lpfc_buf)
22341 {
22342         struct fcp_cmd_rsp_buf *list_entry = NULL;
22343         struct fcp_cmd_rsp_buf *tmp = NULL;
22344         struct fcp_cmd_rsp_buf *allocated_buf = NULL;
22345         struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22346         struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22347         unsigned long iflags;
22348
22349         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22350
22351         if (likely(!list_empty(buf_list))) {
22352                 /* break off 1 chunk from the list */
22353                 list_for_each_entry_safe(list_entry, tmp,
22354                                          buf_list,
22355                                          list_node) {
22356                         list_move_tail(&list_entry->list_node,
22357                                        &lpfc_buf->dma_cmd_rsp_list);
22358                         break;
22359                 }
22360         } else {
22361                 /* allocate more */
22362                 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22363                 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22364                                    cpu_to_node(hdwq->io_wq->chann));
22365                 if (!tmp) {
22366                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22367                                         "8355 error kmalloc memory for HDWQ "
22368                                         "%d %s\n",
22369                                         lpfc_buf->hdwq_no, __func__);
22370                         return NULL;
22371                 }
22372
22373                 tmp->fcp_cmnd = dma_pool_alloc(phba->lpfc_cmd_rsp_buf_pool,
22374                                                 GFP_ATOMIC,
22375                                                 &tmp->fcp_cmd_rsp_dma_handle);
22376
22377                 if (!tmp->fcp_cmnd) {
22378                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22379                                         "8356 error pool_alloc memory for HDWQ "
22380                                         "%d %s\n",
22381                                         lpfc_buf->hdwq_no, __func__);
22382                         kfree(tmp);
22383                         return NULL;
22384                 }
22385
22386                 tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
22387                                 sizeof(struct fcp_cmnd));
22388
22389                 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22390                 list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
22391         }
22392
22393         allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
22394                                         struct fcp_cmd_rsp_buf,
22395                                         list_node);
22396
22397         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22398
22399         return allocated_buf;
22400 }
22401
22402 /**
22403  * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
22404  * @phba: The HBA for which this call is being executed.
22405  * @lpfc_buf: IO buf structure with the CMD/RSP buf
22406  *
22407  * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
22408  *
22409  * Return codes:
22410  *   0 - Success
22411  *   -EINVAL - Error
22412  **/
22413 int
22414 lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22415                               struct lpfc_io_buf *lpfc_buf)
22416 {
22417         int rc = 0;
22418         struct fcp_cmd_rsp_buf *list_entry = NULL;
22419         struct fcp_cmd_rsp_buf *tmp = NULL;
22420         struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22421         struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22422         unsigned long iflags;
22423
22424         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22425
22426         if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
22427                 list_for_each_entry_safe(list_entry, tmp,
22428                                          &lpfc_buf->dma_cmd_rsp_list,
22429                                          list_node) {
22430                         list_move_tail(&list_entry->list_node,
22431                                        buf_list);
22432                 }
22433         } else {
22434                 rc = -EINVAL;
22435         }
22436
22437         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22438         return rc;
22439 }
22440
22441 /**
22442  * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
22443  * @phba: phba object
22444  * @hdwq: hdwq to cleanup cmd rsp buff resources on
22445  *
22446  * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
22447  *
22448  * Return codes:
22449  *   None
22450  **/
22451 void
22452 lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22453                                struct lpfc_sli4_hdw_queue *hdwq)
22454 {
22455         struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22456         struct fcp_cmd_rsp_buf *list_entry = NULL;
22457         struct fcp_cmd_rsp_buf *tmp = NULL;
22458         unsigned long iflags;
22459
22460         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22461
22462         /* Free cmd_rsp buf pool */
22463         list_for_each_entry_safe(list_entry, tmp,
22464                                  buf_list,
22465                                  list_node) {
22466                 dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
22467                               list_entry->fcp_cmnd,
22468                               list_entry->fcp_cmd_rsp_dma_handle);
22469                 list_del(&list_entry->list_node);
22470                 kfree(list_entry);
22471         }
22472
22473         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22474 }