Merge tag 'v5.11' into next
[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-2020 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23
24 #include <linux/blkdev.h>
25 #include <linux/pci.h>
26 #include <linux/interrupt.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/lockdep.h>
30
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_transport_fc.h>
36 #include <scsi/fc/fc_fs.h>
37 #include <linux/aer.h>
38 #include <linux/crash_dump.h>
39 #ifdef CONFIG_X86
40 #include <asm/set_memory.h>
41 #endif
42
43 #include "lpfc_hw4.h"
44 #include "lpfc_hw.h"
45 #include "lpfc_sli.h"
46 #include "lpfc_sli4.h"
47 #include "lpfc_nl.h"
48 #include "lpfc_disc.h"
49 #include "lpfc.h"
50 #include "lpfc_scsi.h"
51 #include "lpfc_nvme.h"
52 #include "lpfc_crtn.h"
53 #include "lpfc_logmsg.h"
54 #include "lpfc_compat.h"
55 #include "lpfc_debugfs.h"
56 #include "lpfc_vport.h"
57 #include "lpfc_version.h"
58
59 /* There are only four IOCB completion types. */
60 typedef enum _lpfc_iocb_type {
61         LPFC_UNKNOWN_IOCB,
62         LPFC_UNSOL_IOCB,
63         LPFC_SOL_IOCB,
64         LPFC_ABORT_IOCB
65 } lpfc_iocb_type;
66
67
68 /* Provide function prototypes local to this module. */
69 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
70                                   uint32_t);
71 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
72                               uint8_t *, uint32_t *);
73 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
74                                                          struct lpfc_iocbq *);
75 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
76                                       struct hbq_dmabuf *);
77 static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
78                                           struct hbq_dmabuf *dmabuf);
79 static bool lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba,
80                                    struct lpfc_queue *cq, struct lpfc_cqe *cqe);
81 static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
82                                        int);
83 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
84                                      struct lpfc_queue *eq,
85                                      struct lpfc_eqe *eqe);
86 static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
87 static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
88 static struct lpfc_cqe *lpfc_sli4_cq_get(struct lpfc_queue *q);
89 static void __lpfc_sli4_consume_cqe(struct lpfc_hba *phba,
90                                     struct lpfc_queue *cq,
91                                     struct lpfc_cqe *cqe);
92
93 union lpfc_wqe128 lpfc_iread_cmd_template;
94 union lpfc_wqe128 lpfc_iwrite_cmd_template;
95 union lpfc_wqe128 lpfc_icmnd_cmd_template;
96
97 static IOCB_t *
98 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
99 {
100         return &iocbq->iocb;
101 }
102
103 /* Setup WQE templates for IOs */
104 void lpfc_wqe_cmd_template(void)
105 {
106         union lpfc_wqe128 *wqe;
107
108         /* IREAD template */
109         wqe = &lpfc_iread_cmd_template;
110         memset(wqe, 0, sizeof(union lpfc_wqe128));
111
112         /* Word 0, 1, 2 - BDE is variable */
113
114         /* Word 3 - cmd_buff_len, payload_offset_len is zero */
115
116         /* Word 4 - total_xfer_len is variable */
117
118         /* Word 5 - is zero */
119
120         /* Word 6 - ctxt_tag, xri_tag is variable */
121
122         /* Word 7 */
123         bf_set(wqe_cmnd, &wqe->fcp_iread.wqe_com, CMD_FCP_IREAD64_WQE);
124         bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, PARM_READ_CHECK);
125         bf_set(wqe_class, &wqe->fcp_iread.wqe_com, CLASS3);
126         bf_set(wqe_ct, &wqe->fcp_iread.wqe_com, SLI4_CT_RPI);
127
128         /* Word 8 - abort_tag is variable */
129
130         /* Word 9  - reqtag is variable */
131
132         /* Word 10 - dbde, wqes is variable */
133         bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
134         bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
135         bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com, LPFC_WQE_LENLOC_WORD4);
136         bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
137         bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
138
139         /* Word 11 - pbde is variable */
140         bf_set(wqe_cmd_type, &wqe->fcp_iread.wqe_com, COMMAND_DATA_IN);
141         bf_set(wqe_cqid, &wqe->fcp_iread.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
142         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
143
144         /* Word 12 - is zero */
145
146         /* Word 13, 14, 15 - PBDE is variable */
147
148         /* IWRITE template */
149         wqe = &lpfc_iwrite_cmd_template;
150         memset(wqe, 0, sizeof(union lpfc_wqe128));
151
152         /* Word 0, 1, 2 - BDE is variable */
153
154         /* Word 3 - cmd_buff_len, payload_offset_len is zero */
155
156         /* Word 4 - total_xfer_len is variable */
157
158         /* Word 5 - initial_xfer_len is variable */
159
160         /* Word 6 - ctxt_tag, xri_tag is variable */
161
162         /* Word 7 */
163         bf_set(wqe_cmnd, &wqe->fcp_iwrite.wqe_com, CMD_FCP_IWRITE64_WQE);
164         bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, PARM_READ_CHECK);
165         bf_set(wqe_class, &wqe->fcp_iwrite.wqe_com, CLASS3);
166         bf_set(wqe_ct, &wqe->fcp_iwrite.wqe_com, SLI4_CT_RPI);
167
168         /* Word 8 - abort_tag is variable */
169
170         /* Word 9  - reqtag is variable */
171
172         /* Word 10 - dbde, wqes is variable */
173         bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
174         bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
175         bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_LENLOC_WORD4);
176         bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
177         bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
178
179         /* Word 11 - pbde is variable */
180         bf_set(wqe_cmd_type, &wqe->fcp_iwrite.wqe_com, COMMAND_DATA_OUT);
181         bf_set(wqe_cqid, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
182         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
183
184         /* Word 12 - is zero */
185
186         /* Word 13, 14, 15 - PBDE is variable */
187
188         /* ICMND template */
189         wqe = &lpfc_icmnd_cmd_template;
190         memset(wqe, 0, sizeof(union lpfc_wqe128));
191
192         /* Word 0, 1, 2 - BDE is variable */
193
194         /* Word 3 - payload_offset_len is variable */
195
196         /* Word 4, 5 - is zero */
197
198         /* Word 6 - ctxt_tag, xri_tag is variable */
199
200         /* Word 7 */
201         bf_set(wqe_cmnd, &wqe->fcp_icmd.wqe_com, CMD_FCP_ICMND64_WQE);
202         bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
203         bf_set(wqe_class, &wqe->fcp_icmd.wqe_com, CLASS3);
204         bf_set(wqe_ct, &wqe->fcp_icmd.wqe_com, SLI4_CT_RPI);
205
206         /* Word 8 - abort_tag is variable */
207
208         /* Word 9  - reqtag is variable */
209
210         /* Word 10 - dbde, wqes is variable */
211         bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
212         bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_NONE);
213         bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com, LPFC_WQE_LENLOC_NONE);
214         bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
215         bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
216
217         /* Word 11 */
218         bf_set(wqe_cmd_type, &wqe->fcp_icmd.wqe_com, COMMAND_DATA_IN);
219         bf_set(wqe_cqid, &wqe->fcp_icmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
220         bf_set(wqe_pbde, &wqe->fcp_icmd.wqe_com, 0);
221
222         /* Word 12, 13, 14, 15 - is zero */
223 }
224
225 #if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
226 /**
227  * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
228  * @srcp: Source memory pointer.
229  * @destp: Destination memory pointer.
230  * @cnt: Number of words required to be copied.
231  *       Must be a multiple of sizeof(uint64_t)
232  *
233  * This function is used for copying data between driver memory
234  * and the SLI WQ. This function also changes the endianness
235  * of each word if native endianness is different from SLI
236  * endianness. This function can be called with or without
237  * lock.
238  **/
239 static void
240 lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
241 {
242         uint64_t *src = srcp;
243         uint64_t *dest = destp;
244         int i;
245
246         for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
247                 *dest++ = *src++;
248 }
249 #else
250 #define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
251 #endif
252
253 /**
254  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
255  * @q: The Work Queue to operate on.
256  * @wqe: The work Queue Entry to put on the Work queue.
257  *
258  * This routine will copy the contents of @wqe to the next available entry on
259  * the @q. This function will then ring the Work Queue Doorbell to signal the
260  * HBA to start processing the Work Queue Entry. This function returns 0 if
261  * successful. If no entries are available on @q then this function will return
262  * -ENOMEM.
263  * The caller is expected to hold the hbalock when calling this routine.
264  **/
265 static int
266 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
267 {
268         union lpfc_wqe *temp_wqe;
269         struct lpfc_register doorbell;
270         uint32_t host_index;
271         uint32_t idx;
272         uint32_t i = 0;
273         uint8_t *tmp;
274         u32 if_type;
275
276         /* sanity check on queue memory */
277         if (unlikely(!q))
278                 return -ENOMEM;
279
280         temp_wqe = lpfc_sli4_qe(q, q->host_index);
281
282         /* If the host has not yet processed the next entry then we are done */
283         idx = ((q->host_index + 1) % q->entry_count);
284         if (idx == q->hba_index) {
285                 q->WQ_overflow++;
286                 return -EBUSY;
287         }
288         q->WQ_posted++;
289         /* set consumption flag every once in a while */
290         if (!((q->host_index + 1) % q->notify_interval))
291                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
292         else
293                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
294         if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
295                 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
296         lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
297         if (q->dpp_enable && q->phba->cfg_enable_dpp) {
298                 /* write to DPP aperture taking advatage of Combined Writes */
299                 tmp = (uint8_t *)temp_wqe;
300 #ifdef __raw_writeq
301                 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
302                         __raw_writeq(*((uint64_t *)(tmp + i)),
303                                         q->dpp_regaddr + i);
304 #else
305                 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
306                         __raw_writel(*((uint32_t *)(tmp + i)),
307                                         q->dpp_regaddr + i);
308 #endif
309         }
310         /* ensure WQE bcopy and DPP flushed before doorbell write */
311         wmb();
312
313         /* Update the host index before invoking device */
314         host_index = q->host_index;
315
316         q->host_index = idx;
317
318         /* Ring Doorbell */
319         doorbell.word0 = 0;
320         if (q->db_format == LPFC_DB_LIST_FORMAT) {
321                 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
322                         bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
323                         bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
324                         bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
325                             q->dpp_id);
326                         bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
327                             q->queue_id);
328                 } else {
329                         bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
330                         bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
331
332                         /* Leave bits <23:16> clear for if_type 6 dpp */
333                         if_type = bf_get(lpfc_sli_intf_if_type,
334                                          &q->phba->sli4_hba.sli_intf);
335                         if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
336                                 bf_set(lpfc_wq_db_list_fm_index, &doorbell,
337                                        host_index);
338                 }
339         } else if (q->db_format == LPFC_DB_RING_FORMAT) {
340                 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
341                 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
342         } else {
343                 return -EINVAL;
344         }
345         writel(doorbell.word0, q->db_regaddr);
346
347         return 0;
348 }
349
350 /**
351  * lpfc_sli4_wq_release - Updates internal hba index for WQ
352  * @q: The Work Queue to operate on.
353  * @index: The index to advance the hba index to.
354  *
355  * This routine will update the HBA index of a queue to reflect consumption of
356  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
357  * an entry the host calls this function to update the queue's internal
358  * pointers.
359  **/
360 static void
361 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
362 {
363         /* sanity check on queue memory */
364         if (unlikely(!q))
365                 return;
366
367         q->hba_index = index;
368 }
369
370 /**
371  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
372  * @q: The Mailbox Queue to operate on.
373  * @mqe: The Mailbox Queue Entry to put on the Work queue.
374  *
375  * This routine will copy the contents of @mqe to the next available entry on
376  * the @q. This function will then ring the Work Queue Doorbell to signal the
377  * HBA to start processing the Work Queue Entry. This function returns 0 if
378  * successful. If no entries are available on @q then this function will return
379  * -ENOMEM.
380  * The caller is expected to hold the hbalock when calling this routine.
381  **/
382 static uint32_t
383 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
384 {
385         struct lpfc_mqe *temp_mqe;
386         struct lpfc_register doorbell;
387
388         /* sanity check on queue memory */
389         if (unlikely(!q))
390                 return -ENOMEM;
391         temp_mqe = lpfc_sli4_qe(q, q->host_index);
392
393         /* If the host has not yet processed the next entry then we are done */
394         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
395                 return -ENOMEM;
396         lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
397         /* Save off the mailbox pointer for completion */
398         q->phba->mbox = (MAILBOX_t *)temp_mqe;
399
400         /* Update the host index before invoking device */
401         q->host_index = ((q->host_index + 1) % q->entry_count);
402
403         /* Ring Doorbell */
404         doorbell.word0 = 0;
405         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
406         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
407         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
408         return 0;
409 }
410
411 /**
412  * lpfc_sli4_mq_release - Updates internal hba index for MQ
413  * @q: The Mailbox Queue to operate on.
414  *
415  * This routine will update the HBA index of a queue to reflect consumption of
416  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
417  * an entry the host calls this function to update the queue's internal
418  * pointers. This routine returns the number of entries that were consumed by
419  * the HBA.
420  **/
421 static uint32_t
422 lpfc_sli4_mq_release(struct lpfc_queue *q)
423 {
424         /* sanity check on queue memory */
425         if (unlikely(!q))
426                 return 0;
427
428         /* Clear the mailbox pointer for completion */
429         q->phba->mbox = NULL;
430         q->hba_index = ((q->hba_index + 1) % q->entry_count);
431         return 1;
432 }
433
434 /**
435  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
436  * @q: The Event Queue to get the first valid EQE from
437  *
438  * This routine will get the first valid Event Queue Entry from @q, update
439  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
440  * the Queue (no more work to do), or the Queue is full of EQEs that have been
441  * processed, but not popped back to the HBA then this routine will return NULL.
442  **/
443 static struct lpfc_eqe *
444 lpfc_sli4_eq_get(struct lpfc_queue *q)
445 {
446         struct lpfc_eqe *eqe;
447
448         /* sanity check on queue memory */
449         if (unlikely(!q))
450                 return NULL;
451         eqe = lpfc_sli4_qe(q, q->host_index);
452
453         /* If the next EQE is not valid then we are done */
454         if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
455                 return NULL;
456
457         /*
458          * insert barrier for instruction interlock : data from the hardware
459          * must have the valid bit checked before it can be copied and acted
460          * upon. Speculative instructions were allowing a bcopy at the start
461          * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
462          * after our return, to copy data before the valid bit check above
463          * was done. As such, some of the copied data was stale. The barrier
464          * ensures the check is before any data is copied.
465          */
466         mb();
467         return eqe;
468 }
469
470 /**
471  * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
472  * @q: The Event Queue to disable interrupts
473  *
474  **/
475 void
476 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
477 {
478         struct lpfc_register doorbell;
479
480         doorbell.word0 = 0;
481         bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
482         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
483         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
484                 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
485         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
486         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
487 }
488
489 /**
490  * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
491  * @q: The Event Queue to disable interrupts
492  *
493  **/
494 void
495 lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
496 {
497         struct lpfc_register doorbell;
498
499         doorbell.word0 = 0;
500         bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
501         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
502 }
503
504 /**
505  * lpfc_sli4_write_eq_db - write EQ DB for eqe's consumed or arm state
506  * @phba: adapter with EQ
507  * @q: The Event Queue that the host has completed processing for.
508  * @count: Number of elements that have been consumed
509  * @arm: Indicates whether the host wants to arms this CQ.
510  *
511  * This routine will notify the HBA, by ringing the doorbell, that count
512  * number of EQEs have been processed. The @arm parameter indicates whether
513  * the queue should be rearmed when ringing the doorbell.
514  **/
515 void
516 lpfc_sli4_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
517                      uint32_t count, bool arm)
518 {
519         struct lpfc_register doorbell;
520
521         /* sanity check on queue memory */
522         if (unlikely(!q || (count == 0 && !arm)))
523                 return;
524
525         /* ring doorbell for number popped */
526         doorbell.word0 = 0;
527         if (arm) {
528                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
529                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
530         }
531         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
532         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
533         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
534                         (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
535         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
536         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
537         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
538         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
539                 readl(q->phba->sli4_hba.EQDBregaddr);
540 }
541
542 /**
543  * lpfc_sli4_if6_write_eq_db - write EQ DB for eqe's consumed or arm state
544  * @phba: adapter with EQ
545  * @q: The Event Queue that the host has completed processing for.
546  * @count: Number of elements that have been consumed
547  * @arm: Indicates whether the host wants to arms this CQ.
548  *
549  * This routine will notify the HBA, by ringing the doorbell, that count
550  * number of EQEs have been processed. The @arm parameter indicates whether
551  * the queue should be rearmed when ringing the doorbell.
552  **/
553 void
554 lpfc_sli4_if6_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
555                           uint32_t count, bool arm)
556 {
557         struct lpfc_register doorbell;
558
559         /* sanity check on queue memory */
560         if (unlikely(!q || (count == 0 && !arm)))
561                 return;
562
563         /* ring doorbell for number popped */
564         doorbell.word0 = 0;
565         if (arm)
566                 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
567         bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, count);
568         bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
569         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
570         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
571         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
572                 readl(q->phba->sli4_hba.EQDBregaddr);
573 }
574
575 static void
576 __lpfc_sli4_consume_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
577                         struct lpfc_eqe *eqe)
578 {
579         if (!phba->sli4_hba.pc_sli4_params.eqav)
580                 bf_set_le32(lpfc_eqe_valid, eqe, 0);
581
582         eq->host_index = ((eq->host_index + 1) % eq->entry_count);
583
584         /* if the index wrapped around, toggle the valid bit */
585         if (phba->sli4_hba.pc_sli4_params.eqav && !eq->host_index)
586                 eq->qe_valid = (eq->qe_valid) ? 0 : 1;
587 }
588
589 static void
590 lpfc_sli4_eqcq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
591 {
592         struct lpfc_eqe *eqe = NULL;
593         u32 eq_count = 0, cq_count = 0;
594         struct lpfc_cqe *cqe = NULL;
595         struct lpfc_queue *cq = NULL, *childq = NULL;
596         int cqid = 0;
597
598         /* walk all the EQ entries and drop on the floor */
599         eqe = lpfc_sli4_eq_get(eq);
600         while (eqe) {
601                 /* Get the reference to the corresponding CQ */
602                 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
603                 cq = NULL;
604
605                 list_for_each_entry(childq, &eq->child_list, list) {
606                         if (childq->queue_id == cqid) {
607                                 cq = childq;
608                                 break;
609                         }
610                 }
611                 /* If CQ is valid, iterate through it and drop all the CQEs */
612                 if (cq) {
613                         cqe = lpfc_sli4_cq_get(cq);
614                         while (cqe) {
615                                 __lpfc_sli4_consume_cqe(phba, cq, cqe);
616                                 cq_count++;
617                                 cqe = lpfc_sli4_cq_get(cq);
618                         }
619                         /* Clear and re-arm the CQ */
620                         phba->sli4_hba.sli4_write_cq_db(phba, cq, cq_count,
621                             LPFC_QUEUE_REARM);
622                         cq_count = 0;
623                 }
624                 __lpfc_sli4_consume_eqe(phba, eq, eqe);
625                 eq_count++;
626                 eqe = lpfc_sli4_eq_get(eq);
627         }
628
629         /* Clear and re-arm the EQ */
630         phba->sli4_hba.sli4_write_eq_db(phba, eq, eq_count, LPFC_QUEUE_REARM);
631 }
632
633 static int
634 lpfc_sli4_process_eq(struct lpfc_hba *phba, struct lpfc_queue *eq,
635                      uint8_t rearm)
636 {
637         struct lpfc_eqe *eqe;
638         int count = 0, consumed = 0;
639
640         if (cmpxchg(&eq->queue_claimed, 0, 1) != 0)
641                 goto rearm_and_exit;
642
643         eqe = lpfc_sli4_eq_get(eq);
644         while (eqe) {
645                 lpfc_sli4_hba_handle_eqe(phba, eq, eqe);
646                 __lpfc_sli4_consume_eqe(phba, eq, eqe);
647
648                 consumed++;
649                 if (!(++count % eq->max_proc_limit))
650                         break;
651
652                 if (!(count % eq->notify_interval)) {
653                         phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed,
654                                                         LPFC_QUEUE_NOARM);
655                         consumed = 0;
656                 }
657
658                 eqe = lpfc_sli4_eq_get(eq);
659         }
660         eq->EQ_processed += count;
661
662         /* Track the max number of EQEs processed in 1 intr */
663         if (count > eq->EQ_max_eqe)
664                 eq->EQ_max_eqe = count;
665
666         xchg(&eq->queue_claimed, 0);
667
668 rearm_and_exit:
669         /* Always clear the EQ. */
670         phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed, rearm);
671
672         return count;
673 }
674
675 /**
676  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
677  * @q: The Completion Queue to get the first valid CQE from
678  *
679  * This routine will get the first valid Completion Queue Entry from @q, update
680  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
681  * the Queue (no more work to do), or the Queue is full of CQEs that have been
682  * processed, but not popped back to the HBA then this routine will return NULL.
683  **/
684 static struct lpfc_cqe *
685 lpfc_sli4_cq_get(struct lpfc_queue *q)
686 {
687         struct lpfc_cqe *cqe;
688
689         /* sanity check on queue memory */
690         if (unlikely(!q))
691                 return NULL;
692         cqe = lpfc_sli4_qe(q, q->host_index);
693
694         /* If the next CQE is not valid then we are done */
695         if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
696                 return NULL;
697
698         /*
699          * insert barrier for instruction interlock : data from the hardware
700          * must have the valid bit checked before it can be copied and acted
701          * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
702          * instructions allowing action on content before valid bit checked,
703          * add barrier here as well. May not be needed as "content" is a
704          * single 32-bit entity here (vs multi word structure for cq's).
705          */
706         mb();
707         return cqe;
708 }
709
710 static void
711 __lpfc_sli4_consume_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
712                         struct lpfc_cqe *cqe)
713 {
714         if (!phba->sli4_hba.pc_sli4_params.cqav)
715                 bf_set_le32(lpfc_cqe_valid, cqe, 0);
716
717         cq->host_index = ((cq->host_index + 1) % cq->entry_count);
718
719         /* if the index wrapped around, toggle the valid bit */
720         if (phba->sli4_hba.pc_sli4_params.cqav && !cq->host_index)
721                 cq->qe_valid = (cq->qe_valid) ? 0 : 1;
722 }
723
724 /**
725  * lpfc_sli4_write_cq_db - write cq DB for entries consumed or arm state.
726  * @phba: the adapter with the CQ
727  * @q: The Completion Queue that the host has completed processing for.
728  * @count: the number of elements that were consumed
729  * @arm: Indicates whether the host wants to arms this CQ.
730  *
731  * This routine will notify the HBA, by ringing the doorbell, that the
732  * CQEs have been processed. The @arm parameter specifies whether the
733  * queue should be rearmed when ringing the doorbell.
734  **/
735 void
736 lpfc_sli4_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
737                      uint32_t count, bool arm)
738 {
739         struct lpfc_register doorbell;
740
741         /* sanity check on queue memory */
742         if (unlikely(!q || (count == 0 && !arm)))
743                 return;
744
745         /* ring doorbell for number popped */
746         doorbell.word0 = 0;
747         if (arm)
748                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
749         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
750         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
751         bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
752                         (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
753         bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
754         writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
755 }
756
757 /**
758  * lpfc_sli4_if6_write_cq_db - write cq DB for entries consumed or arm state.
759  * @phba: the adapter with the CQ
760  * @q: The Completion Queue that the host has completed processing for.
761  * @count: the number of elements that were consumed
762  * @arm: Indicates whether the host wants to arms this CQ.
763  *
764  * This routine will notify the HBA, by ringing the doorbell, that the
765  * CQEs have been processed. The @arm parameter specifies whether the
766  * queue should be rearmed when ringing the doorbell.
767  **/
768 void
769 lpfc_sli4_if6_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
770                          uint32_t count, bool arm)
771 {
772         struct lpfc_register doorbell;
773
774         /* sanity check on queue memory */
775         if (unlikely(!q || (count == 0 && !arm)))
776                 return;
777
778         /* ring doorbell for number popped */
779         doorbell.word0 = 0;
780         if (arm)
781                 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
782         bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, count);
783         bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
784         writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
785 }
786
787 /*
788  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
789  *
790  * This routine will copy the contents of @wqe to the next available entry on
791  * the @q. This function will then ring the Receive Queue Doorbell to signal the
792  * HBA to start processing the Receive Queue Entry. This function returns the
793  * index that the rqe was copied to if successful. If no entries are available
794  * on @q then this function will return -ENOMEM.
795  * The caller is expected to hold the hbalock when calling this routine.
796  **/
797 int
798 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
799                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
800 {
801         struct lpfc_rqe *temp_hrqe;
802         struct lpfc_rqe *temp_drqe;
803         struct lpfc_register doorbell;
804         int hq_put_index;
805         int dq_put_index;
806
807         /* sanity check on queue memory */
808         if (unlikely(!hq) || unlikely(!dq))
809                 return -ENOMEM;
810         hq_put_index = hq->host_index;
811         dq_put_index = dq->host_index;
812         temp_hrqe = lpfc_sli4_qe(hq, hq_put_index);
813         temp_drqe = lpfc_sli4_qe(dq, dq_put_index);
814
815         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
816                 return -EINVAL;
817         if (hq_put_index != dq_put_index)
818                 return -EINVAL;
819         /* If the host has not yet processed the next entry then we are done */
820         if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
821                 return -EBUSY;
822         lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
823         lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
824
825         /* Update the host index to point to the next slot */
826         hq->host_index = ((hq_put_index + 1) % hq->entry_count);
827         dq->host_index = ((dq_put_index + 1) % dq->entry_count);
828         hq->RQ_buf_posted++;
829
830         /* Ring The Header Receive Queue Doorbell */
831         if (!(hq->host_index % hq->notify_interval)) {
832                 doorbell.word0 = 0;
833                 if (hq->db_format == LPFC_DB_RING_FORMAT) {
834                         bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
835                                hq->notify_interval);
836                         bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
837                 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
838                         bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
839                                hq->notify_interval);
840                         bf_set(lpfc_rq_db_list_fm_index, &doorbell,
841                                hq->host_index);
842                         bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
843                 } else {
844                         return -EINVAL;
845                 }
846                 writel(doorbell.word0, hq->db_regaddr);
847         }
848         return hq_put_index;
849 }
850
851 /*
852  * lpfc_sli4_rq_release - Updates internal hba index for RQ
853  *
854  * This routine will update the HBA index of a queue to reflect consumption of
855  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
856  * consumed an entry the host calls this function to update the queue's
857  * internal pointers. This routine returns the number of entries that were
858  * consumed by the HBA.
859  **/
860 static uint32_t
861 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
862 {
863         /* sanity check on queue memory */
864         if (unlikely(!hq) || unlikely(!dq))
865                 return 0;
866
867         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
868                 return 0;
869         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
870         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
871         return 1;
872 }
873
874 /**
875  * lpfc_cmd_iocb - Get next command iocb entry in the ring
876  * @phba: Pointer to HBA context object.
877  * @pring: Pointer to driver SLI ring object.
878  *
879  * This function returns pointer to next command iocb entry
880  * in the command ring. The caller must hold hbalock to prevent
881  * other threads consume the next command iocb.
882  * SLI-2/SLI-3 provide different sized iocbs.
883  **/
884 static inline IOCB_t *
885 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
886 {
887         return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
888                            pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
889 }
890
891 /**
892  * lpfc_resp_iocb - Get next response iocb entry in the ring
893  * @phba: Pointer to HBA context object.
894  * @pring: Pointer to driver SLI ring object.
895  *
896  * This function returns pointer to next response iocb entry
897  * in the response ring. The caller must hold hbalock to make sure
898  * that no other thread consume the next response iocb.
899  * SLI-2/SLI-3 provide different sized iocbs.
900  **/
901 static inline IOCB_t *
902 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
903 {
904         return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
905                            pring->sli.sli3.rspidx * phba->iocb_rsp_size);
906 }
907
908 /**
909  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
910  * @phba: Pointer to HBA context object.
911  *
912  * This function is called with hbalock held. This function
913  * allocates a new driver iocb object from the iocb pool. If the
914  * allocation is successful, it returns pointer to the newly
915  * allocated iocb object else it returns NULL.
916  **/
917 struct lpfc_iocbq *
918 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
919 {
920         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
921         struct lpfc_iocbq * iocbq = NULL;
922
923         lockdep_assert_held(&phba->hbalock);
924
925         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
926         if (iocbq)
927                 phba->iocb_cnt++;
928         if (phba->iocb_cnt > phba->iocb_max)
929                 phba->iocb_max = phba->iocb_cnt;
930         return iocbq;
931 }
932
933 /**
934  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
935  * @phba: Pointer to HBA context object.
936  * @xritag: XRI value.
937  *
938  * This function clears the sglq pointer from the array of acive
939  * sglq's. The xritag that is passed in is used to index into the
940  * array. Before the xritag can be used it needs to be adjusted
941  * by subtracting the xribase.
942  *
943  * Returns sglq ponter = success, NULL = Failure.
944  **/
945 struct lpfc_sglq *
946 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
947 {
948         struct lpfc_sglq *sglq;
949
950         sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
951         phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
952         return sglq;
953 }
954
955 /**
956  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
957  * @phba: Pointer to HBA context object.
958  * @xritag: XRI value.
959  *
960  * This function returns the sglq pointer from the array of acive
961  * sglq's. The xritag that is passed in is used to index into the
962  * array. Before the xritag can be used it needs to be adjusted
963  * by subtracting the xribase.
964  *
965  * Returns sglq ponter = success, NULL = Failure.
966  **/
967 struct lpfc_sglq *
968 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
969 {
970         struct lpfc_sglq *sglq;
971
972         sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];
973         return sglq;
974 }
975
976 /**
977  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
978  * @phba: Pointer to HBA context object.
979  * @xritag: xri used in this exchange.
980  * @rrq: The RRQ to be cleared.
981  *
982  **/
983 void
984 lpfc_clr_rrq_active(struct lpfc_hba *phba,
985                     uint16_t xritag,
986                     struct lpfc_node_rrq *rrq)
987 {
988         struct lpfc_nodelist *ndlp = NULL;
989
990         if (rrq->vport)
991                 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
992
993         /* The target DID could have been swapped (cable swap)
994          * we should use the ndlp from the findnode if it is
995          * available.
996          */
997         if ((!ndlp) && rrq->ndlp)
998                 ndlp = rrq->ndlp;
999
1000         if (!ndlp)
1001                 goto out;
1002
1003         if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
1004                 rrq->send_rrq = 0;
1005                 rrq->xritag = 0;
1006                 rrq->rrq_stop_time = 0;
1007         }
1008 out:
1009         mempool_free(rrq, phba->rrq_pool);
1010 }
1011
1012 /**
1013  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
1014  * @phba: Pointer to HBA context object.
1015  *
1016  * This function is called with hbalock held. This function
1017  * Checks if stop_time (ratov from setting rrq active) has
1018  * been reached, if it has and the send_rrq flag is set then
1019  * it will call lpfc_send_rrq. If the send_rrq flag is not set
1020  * then it will just call the routine to clear the rrq and
1021  * free the rrq resource.
1022  * The timer is set to the next rrq that is going to expire before
1023  * leaving the routine.
1024  *
1025  **/
1026 void
1027 lpfc_handle_rrq_active(struct lpfc_hba *phba)
1028 {
1029         struct lpfc_node_rrq *rrq;
1030         struct lpfc_node_rrq *nextrrq;
1031         unsigned long next_time;
1032         unsigned long iflags;
1033         LIST_HEAD(send_rrq);
1034
1035         spin_lock_irqsave(&phba->hbalock, iflags);
1036         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1037         next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
1038         list_for_each_entry_safe(rrq, nextrrq,
1039                                  &phba->active_rrq_list, list) {
1040                 if (time_after(jiffies, rrq->rrq_stop_time))
1041                         list_move(&rrq->list, &send_rrq);
1042                 else if (time_before(rrq->rrq_stop_time, next_time))
1043                         next_time = rrq->rrq_stop_time;
1044         }
1045         spin_unlock_irqrestore(&phba->hbalock, iflags);
1046         if ((!list_empty(&phba->active_rrq_list)) &&
1047             (!(phba->pport->load_flag & FC_UNLOADING)))
1048                 mod_timer(&phba->rrq_tmr, next_time);
1049         list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
1050                 list_del(&rrq->list);
1051                 if (!rrq->send_rrq) {
1052                         /* this call will free the rrq */
1053                         lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1054                 } else if (lpfc_send_rrq(phba, rrq)) {
1055                         /* if we send the rrq then the completion handler
1056                         *  will clear the bit in the xribitmap.
1057                         */
1058                         lpfc_clr_rrq_active(phba, rrq->xritag,
1059                                             rrq);
1060                 }
1061         }
1062 }
1063
1064 /**
1065  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
1066  * @vport: Pointer to vport context object.
1067  * @xri: The xri used in the exchange.
1068  * @did: The targets DID for this exchange.
1069  *
1070  * returns NULL = rrq not found in the phba->active_rrq_list.
1071  *         rrq = rrq for this xri and target.
1072  **/
1073 struct lpfc_node_rrq *
1074 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
1075 {
1076         struct lpfc_hba *phba = vport->phba;
1077         struct lpfc_node_rrq *rrq;
1078         struct lpfc_node_rrq *nextrrq;
1079         unsigned long iflags;
1080
1081         if (phba->sli_rev != LPFC_SLI_REV4)
1082                 return NULL;
1083         spin_lock_irqsave(&phba->hbalock, iflags);
1084         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
1085                 if (rrq->vport == vport && rrq->xritag == xri &&
1086                                 rrq->nlp_DID == did){
1087                         list_del(&rrq->list);
1088                         spin_unlock_irqrestore(&phba->hbalock, iflags);
1089                         return rrq;
1090                 }
1091         }
1092         spin_unlock_irqrestore(&phba->hbalock, iflags);
1093         return NULL;
1094 }
1095
1096 /**
1097  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
1098  * @vport: Pointer to vport context object.
1099  * @ndlp: Pointer to the lpfc_node_list structure.
1100  * If ndlp is NULL Remove all active RRQs for this vport from the
1101  * phba->active_rrq_list and clear the rrq.
1102  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
1103  **/
1104 void
1105 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1106
1107 {
1108         struct lpfc_hba *phba = vport->phba;
1109         struct lpfc_node_rrq *rrq;
1110         struct lpfc_node_rrq *nextrrq;
1111         unsigned long iflags;
1112         LIST_HEAD(rrq_list);
1113
1114         if (phba->sli_rev != LPFC_SLI_REV4)
1115                 return;
1116         if (!ndlp) {
1117                 lpfc_sli4_vport_delete_els_xri_aborted(vport);
1118                 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
1119         }
1120         spin_lock_irqsave(&phba->hbalock, iflags);
1121         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
1122                 if ((rrq->vport == vport) && (!ndlp  || rrq->ndlp == ndlp))
1123                         list_move(&rrq->list, &rrq_list);
1124         spin_unlock_irqrestore(&phba->hbalock, iflags);
1125
1126         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1127                 list_del(&rrq->list);
1128                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1129         }
1130 }
1131
1132 /**
1133  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
1134  * @phba: Pointer to HBA context object.
1135  * @ndlp: Targets nodelist pointer for this exchange.
1136  * @xritag: the xri in the bitmap to test.
1137  *
1138  * This function returns:
1139  * 0 = rrq not active for this xri
1140  * 1 = rrq is valid for this xri.
1141  **/
1142 int
1143 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1144                         uint16_t  xritag)
1145 {
1146         if (!ndlp)
1147                 return 0;
1148         if (!ndlp->active_rrqs_xri_bitmap)
1149                 return 0;
1150         if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1151                 return 1;
1152         else
1153                 return 0;
1154 }
1155
1156 /**
1157  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1158  * @phba: Pointer to HBA context object.
1159  * @ndlp: nodelist pointer for this target.
1160  * @xritag: xri used in this exchange.
1161  * @rxid: Remote Exchange ID.
1162  * @send_rrq: Flag used to determine if we should send rrq els cmd.
1163  *
1164  * This function takes the hbalock.
1165  * The active bit is always set in the active rrq xri_bitmap even
1166  * if there is no slot avaiable for the other rrq information.
1167  *
1168  * returns 0 rrq actived for this xri
1169  *         < 0 No memory or invalid ndlp.
1170  **/
1171 int
1172 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1173                     uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
1174 {
1175         unsigned long iflags;
1176         struct lpfc_node_rrq *rrq;
1177         int empty;
1178
1179         if (!ndlp)
1180                 return -EINVAL;
1181
1182         if (!phba->cfg_enable_rrq)
1183                 return -EINVAL;
1184
1185         spin_lock_irqsave(&phba->hbalock, iflags);
1186         if (phba->pport->load_flag & FC_UNLOADING) {
1187                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1188                 goto out;
1189         }
1190
1191         if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1192                 goto out;
1193
1194         if (!ndlp->active_rrqs_xri_bitmap)
1195                 goto out;
1196
1197         if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1198                 goto out;
1199
1200         spin_unlock_irqrestore(&phba->hbalock, iflags);
1201         rrq = mempool_alloc(phba->rrq_pool, GFP_ATOMIC);
1202         if (!rrq) {
1203                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1204                                 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1205                                 " DID:0x%x Send:%d\n",
1206                                 xritag, rxid, ndlp->nlp_DID, send_rrq);
1207                 return -EINVAL;
1208         }
1209         if (phba->cfg_enable_rrq == 1)
1210                 rrq->send_rrq = send_rrq;
1211         else
1212                 rrq->send_rrq = 0;
1213         rrq->xritag = xritag;
1214         rrq->rrq_stop_time = jiffies +
1215                                 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
1216         rrq->ndlp = ndlp;
1217         rrq->nlp_DID = ndlp->nlp_DID;
1218         rrq->vport = ndlp->vport;
1219         rrq->rxid = rxid;
1220         spin_lock_irqsave(&phba->hbalock, iflags);
1221         empty = list_empty(&phba->active_rrq_list);
1222         list_add_tail(&rrq->list, &phba->active_rrq_list);
1223         phba->hba_flag |= HBA_RRQ_ACTIVE;
1224         if (empty)
1225                 lpfc_worker_wake_up(phba);
1226         spin_unlock_irqrestore(&phba->hbalock, iflags);
1227         return 0;
1228 out:
1229         spin_unlock_irqrestore(&phba->hbalock, iflags);
1230         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1231                         "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1232                         " DID:0x%x Send:%d\n",
1233                         xritag, rxid, ndlp->nlp_DID, send_rrq);
1234         return -EINVAL;
1235 }
1236
1237 /**
1238  * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
1239  * @phba: Pointer to HBA context object.
1240  * @piocbq: Pointer to the iocbq.
1241  *
1242  * The driver calls this function with either the nvme ls ring lock
1243  * or the fc els ring lock held depending on the iocb usage.  This function
1244  * gets a new driver sglq object from the sglq list. If the list is not empty
1245  * then it is successful, it returns pointer to the newly allocated sglq
1246  * object else it returns NULL.
1247  **/
1248 static struct lpfc_sglq *
1249 __lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1250 {
1251         struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
1252         struct lpfc_sglq *sglq = NULL;
1253         struct lpfc_sglq *start_sglq = NULL;
1254         struct lpfc_io_buf *lpfc_cmd;
1255         struct lpfc_nodelist *ndlp;
1256         struct lpfc_sli_ring *pring = NULL;
1257         int found = 0;
1258
1259         if (piocbq->iocb_flag & LPFC_IO_NVME_LS)
1260                 pring =  phba->sli4_hba.nvmels_wq->pring;
1261         else
1262                 pring = lpfc_phba_elsring(phba);
1263
1264         lockdep_assert_held(&pring->ring_lock);
1265
1266         if (piocbq->iocb_flag &  LPFC_IO_FCP) {
1267                 lpfc_cmd = (struct lpfc_io_buf *) piocbq->context1;
1268                 ndlp = lpfc_cmd->rdata->pnode;
1269         } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
1270                         !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
1271                 ndlp = piocbq->context_un.ndlp;
1272         } else  if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1273                 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1274                         ndlp = NULL;
1275                 else
1276                         ndlp = piocbq->context_un.ndlp;
1277         } else {
1278                 ndlp = piocbq->context1;
1279         }
1280
1281         spin_lock(&phba->sli4_hba.sgl_list_lock);
1282         list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
1283         start_sglq = sglq;
1284         while (!found) {
1285                 if (!sglq)
1286                         break;
1287                 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1288                     test_bit(sglq->sli4_lxritag,
1289                     ndlp->active_rrqs_xri_bitmap)) {
1290                         /* This xri has an rrq outstanding for this DID.
1291                          * put it back in the list and get another xri.
1292                          */
1293                         list_add_tail(&sglq->list, lpfc_els_sgl_list);
1294                         sglq = NULL;
1295                         list_remove_head(lpfc_els_sgl_list, sglq,
1296                                                 struct lpfc_sglq, list);
1297                         if (sglq == start_sglq) {
1298                                 list_add_tail(&sglq->list, lpfc_els_sgl_list);
1299                                 sglq = NULL;
1300                                 break;
1301                         } else
1302                                 continue;
1303                 }
1304                 sglq->ndlp = ndlp;
1305                 found = 1;
1306                 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1307                 sglq->state = SGL_ALLOCATED;
1308         }
1309         spin_unlock(&phba->sli4_hba.sgl_list_lock);
1310         return sglq;
1311 }
1312
1313 /**
1314  * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1315  * @phba: Pointer to HBA context object.
1316  * @piocbq: Pointer to the iocbq.
1317  *
1318  * This function is called with the sgl_list lock held. This function
1319  * gets a new driver sglq object from the sglq list. If the
1320  * list is not empty then it is successful, it returns pointer to the newly
1321  * allocated sglq object else it returns NULL.
1322  **/
1323 struct lpfc_sglq *
1324 __lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1325 {
1326         struct list_head *lpfc_nvmet_sgl_list;
1327         struct lpfc_sglq *sglq = NULL;
1328
1329         lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1330
1331         lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1332
1333         list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1334         if (!sglq)
1335                 return NULL;
1336         phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1337         sglq->state = SGL_ALLOCATED;
1338         return sglq;
1339 }
1340
1341 /**
1342  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
1343  * @phba: Pointer to HBA context object.
1344  *
1345  * This function is called with no lock held. This function
1346  * allocates a new driver iocb object from the iocb pool. If the
1347  * allocation is successful, it returns pointer to the newly
1348  * allocated iocb object else it returns NULL.
1349  **/
1350 struct lpfc_iocbq *
1351 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1352 {
1353         struct lpfc_iocbq * iocbq = NULL;
1354         unsigned long iflags;
1355
1356         spin_lock_irqsave(&phba->hbalock, iflags);
1357         iocbq = __lpfc_sli_get_iocbq(phba);
1358         spin_unlock_irqrestore(&phba->hbalock, iflags);
1359         return iocbq;
1360 }
1361
1362 /**
1363  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1364  * @phba: Pointer to HBA context object.
1365  * @iocbq: Pointer to driver iocb object.
1366  *
1367  * This function is called to release the driver iocb object
1368  * to the iocb pool. The iotag in the iocb object
1369  * does not change for each use of the iocb object. This function
1370  * clears all other fields of the iocb object when it is freed.
1371  * The sqlq structure that holds the xritag and phys and virtual
1372  * mappings for the scatter gather list is retrieved from the
1373  * active array of sglq. The get of the sglq pointer also clears
1374  * the entry in the array. If the status of the IO indiactes that
1375  * this IO was aborted then the sglq entry it put on the
1376  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1377  * IO has good status or fails for any other reason then the sglq
1378  * entry is added to the free list (lpfc_els_sgl_list). The hbalock is
1379  *  asserted held in the code path calling this routine.
1380  **/
1381 static void
1382 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1383 {
1384         struct lpfc_sglq *sglq;
1385         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1386         unsigned long iflag = 0;
1387         struct lpfc_sli_ring *pring;
1388
1389         if (iocbq->sli4_xritag == NO_XRI)
1390                 sglq = NULL;
1391         else
1392                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1393
1394
1395         if (sglq)  {
1396                 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1397                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1398                                           iflag);
1399                         sglq->state = SGL_FREED;
1400                         sglq->ndlp = NULL;
1401                         list_add_tail(&sglq->list,
1402                                       &phba->sli4_hba.lpfc_nvmet_sgl_list);
1403                         spin_unlock_irqrestore(
1404                                 &phba->sli4_hba.sgl_list_lock, iflag);
1405                         goto out;
1406                 }
1407
1408                 pring = phba->sli4_hba.els_wq->pring;
1409                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1410                         (sglq->state != SGL_XRI_ABORTED)) {
1411                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1412                                           iflag);
1413
1414                         /* Check if we can get a reference on ndlp */
1415                         if (sglq->ndlp && !lpfc_nlp_get(sglq->ndlp))
1416                                 sglq->ndlp = NULL;
1417
1418                         list_add(&sglq->list,
1419                                  &phba->sli4_hba.lpfc_abts_els_sgl_list);
1420                         spin_unlock_irqrestore(
1421                                 &phba->sli4_hba.sgl_list_lock, iflag);
1422                 } else {
1423                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1424                                           iflag);
1425                         sglq->state = SGL_FREED;
1426                         sglq->ndlp = NULL;
1427                         list_add_tail(&sglq->list,
1428                                       &phba->sli4_hba.lpfc_els_sgl_list);
1429                         spin_unlock_irqrestore(
1430                                 &phba->sli4_hba.sgl_list_lock, iflag);
1431
1432                         /* Check if TXQ queue needs to be serviced */
1433                         if (!list_empty(&pring->txq))
1434                                 lpfc_worker_wake_up(phba);
1435                 }
1436         }
1437
1438 out:
1439         /*
1440          * Clean all volatile data fields, preserve iotag and node struct.
1441          */
1442         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1443         iocbq->sli4_lxritag = NO_XRI;
1444         iocbq->sli4_xritag = NO_XRI;
1445         iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1446                               LPFC_IO_NVME_LS);
1447         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1448 }
1449
1450
1451 /**
1452  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1453  * @phba: Pointer to HBA context object.
1454  * @iocbq: Pointer to driver iocb object.
1455  *
1456  * This function is called to release the driver iocb object to the
1457  * iocb pool. The iotag in the iocb object does not change for each
1458  * use of the iocb object. This function clears all other fields of
1459  * the iocb object when it is freed. The hbalock is asserted held in
1460  * the code path calling this routine.
1461  **/
1462 static void
1463 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1464 {
1465         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1466
1467         /*
1468          * Clean all volatile data fields, preserve iotag and node struct.
1469          */
1470         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1471         iocbq->sli4_xritag = NO_XRI;
1472         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1473 }
1474
1475 /**
1476  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1477  * @phba: Pointer to HBA context object.
1478  * @iocbq: Pointer to driver iocb object.
1479  *
1480  * This function is called with hbalock held to release driver
1481  * iocb object to the iocb pool. The iotag in the iocb object
1482  * does not change for each use of the iocb object. This function
1483  * clears all other fields of the iocb object when it is freed.
1484  **/
1485 static void
1486 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1487 {
1488         lockdep_assert_held(&phba->hbalock);
1489
1490         phba->__lpfc_sli_release_iocbq(phba, iocbq);
1491         phba->iocb_cnt--;
1492 }
1493
1494 /**
1495  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1496  * @phba: Pointer to HBA context object.
1497  * @iocbq: Pointer to driver iocb object.
1498  *
1499  * This function is called with no lock held to release the iocb to
1500  * iocb pool.
1501  **/
1502 void
1503 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1504 {
1505         unsigned long iflags;
1506
1507         /*
1508          * Clean all volatile data fields, preserve iotag and node struct.
1509          */
1510         spin_lock_irqsave(&phba->hbalock, iflags);
1511         __lpfc_sli_release_iocbq(phba, iocbq);
1512         spin_unlock_irqrestore(&phba->hbalock, iflags);
1513 }
1514
1515 /**
1516  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1517  * @phba: Pointer to HBA context object.
1518  * @iocblist: List of IOCBs.
1519  * @ulpstatus: ULP status in IOCB command field.
1520  * @ulpWord4: ULP word-4 in IOCB command field.
1521  *
1522  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1523  * on the list by invoking the complete callback function associated with the
1524  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1525  * fields.
1526  **/
1527 void
1528 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1529                       uint32_t ulpstatus, uint32_t ulpWord4)
1530 {
1531         struct lpfc_iocbq *piocb;
1532
1533         while (!list_empty(iocblist)) {
1534                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1535                 if (!piocb->iocb_cmpl) {
1536                         if (piocb->iocb_flag & LPFC_IO_NVME)
1537                                 lpfc_nvme_cancel_iocb(phba, piocb);
1538                         else
1539                                 lpfc_sli_release_iocbq(phba, piocb);
1540                 } else {
1541                         piocb->iocb.ulpStatus = ulpstatus;
1542                         piocb->iocb.un.ulpWord[4] = ulpWord4;
1543                         (piocb->iocb_cmpl) (phba, piocb, piocb);
1544                 }
1545         }
1546         return;
1547 }
1548
1549 /**
1550  * lpfc_sli_iocb_cmd_type - Get the iocb type
1551  * @iocb_cmnd: iocb command code.
1552  *
1553  * This function is called by ring event handler function to get the iocb type.
1554  * This function translates the iocb command to an iocb command type used to
1555  * decide the final disposition of each completed IOCB.
1556  * The function returns
1557  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1558  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1559  * LPFC_ABORT_IOCB   if it is an abort iocb
1560  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1561  *
1562  * The caller is not required to hold any lock.
1563  **/
1564 static lpfc_iocb_type
1565 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1566 {
1567         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1568
1569         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1570                 return 0;
1571
1572         switch (iocb_cmnd) {
1573         case CMD_XMIT_SEQUENCE_CR:
1574         case CMD_XMIT_SEQUENCE_CX:
1575         case CMD_XMIT_BCAST_CN:
1576         case CMD_XMIT_BCAST_CX:
1577         case CMD_ELS_REQUEST_CR:
1578         case CMD_ELS_REQUEST_CX:
1579         case CMD_CREATE_XRI_CR:
1580         case CMD_CREATE_XRI_CX:
1581         case CMD_GET_RPI_CN:
1582         case CMD_XMIT_ELS_RSP_CX:
1583         case CMD_GET_RPI_CR:
1584         case CMD_FCP_IWRITE_CR:
1585         case CMD_FCP_IWRITE_CX:
1586         case CMD_FCP_IREAD_CR:
1587         case CMD_FCP_IREAD_CX:
1588         case CMD_FCP_ICMND_CR:
1589         case CMD_FCP_ICMND_CX:
1590         case CMD_FCP_TSEND_CX:
1591         case CMD_FCP_TRSP_CX:
1592         case CMD_FCP_TRECEIVE_CX:
1593         case CMD_FCP_AUTO_TRSP_CX:
1594         case CMD_ADAPTER_MSG:
1595         case CMD_ADAPTER_DUMP:
1596         case CMD_XMIT_SEQUENCE64_CR:
1597         case CMD_XMIT_SEQUENCE64_CX:
1598         case CMD_XMIT_BCAST64_CN:
1599         case CMD_XMIT_BCAST64_CX:
1600         case CMD_ELS_REQUEST64_CR:
1601         case CMD_ELS_REQUEST64_CX:
1602         case CMD_FCP_IWRITE64_CR:
1603         case CMD_FCP_IWRITE64_CX:
1604         case CMD_FCP_IREAD64_CR:
1605         case CMD_FCP_IREAD64_CX:
1606         case CMD_FCP_ICMND64_CR:
1607         case CMD_FCP_ICMND64_CX:
1608         case CMD_FCP_TSEND64_CX:
1609         case CMD_FCP_TRSP64_CX:
1610         case CMD_FCP_TRECEIVE64_CX:
1611         case CMD_GEN_REQUEST64_CR:
1612         case CMD_GEN_REQUEST64_CX:
1613         case CMD_XMIT_ELS_RSP64_CX:
1614         case DSSCMD_IWRITE64_CR:
1615         case DSSCMD_IWRITE64_CX:
1616         case DSSCMD_IREAD64_CR:
1617         case DSSCMD_IREAD64_CX:
1618         case CMD_SEND_FRAME:
1619                 type = LPFC_SOL_IOCB;
1620                 break;
1621         case CMD_ABORT_XRI_CN:
1622         case CMD_ABORT_XRI_CX:
1623         case CMD_CLOSE_XRI_CN:
1624         case CMD_CLOSE_XRI_CX:
1625         case CMD_XRI_ABORTED_CX:
1626         case CMD_ABORT_MXRI64_CN:
1627         case CMD_XMIT_BLS_RSP64_CX:
1628                 type = LPFC_ABORT_IOCB;
1629                 break;
1630         case CMD_RCV_SEQUENCE_CX:
1631         case CMD_RCV_ELS_REQ_CX:
1632         case CMD_RCV_SEQUENCE64_CX:
1633         case CMD_RCV_ELS_REQ64_CX:
1634         case CMD_ASYNC_STATUS:
1635         case CMD_IOCB_RCV_SEQ64_CX:
1636         case CMD_IOCB_RCV_ELS64_CX:
1637         case CMD_IOCB_RCV_CONT64_CX:
1638         case CMD_IOCB_RET_XRI64_CX:
1639                 type = LPFC_UNSOL_IOCB;
1640                 break;
1641         case CMD_IOCB_XMIT_MSEQ64_CR:
1642         case CMD_IOCB_XMIT_MSEQ64_CX:
1643         case CMD_IOCB_RCV_SEQ_LIST64_CX:
1644         case CMD_IOCB_RCV_ELS_LIST64_CX:
1645         case CMD_IOCB_CLOSE_EXTENDED_CN:
1646         case CMD_IOCB_ABORT_EXTENDED_CN:
1647         case CMD_IOCB_RET_HBQE64_CN:
1648         case CMD_IOCB_FCP_IBIDIR64_CR:
1649         case CMD_IOCB_FCP_IBIDIR64_CX:
1650         case CMD_IOCB_FCP_ITASKMGT64_CX:
1651         case CMD_IOCB_LOGENTRY_CN:
1652         case CMD_IOCB_LOGENTRY_ASYNC_CN:
1653                 printk("%s - Unhandled SLI-3 Command x%x\n",
1654                                 __func__, iocb_cmnd);
1655                 type = LPFC_UNKNOWN_IOCB;
1656                 break;
1657         default:
1658                 type = LPFC_UNKNOWN_IOCB;
1659                 break;
1660         }
1661
1662         return type;
1663 }
1664
1665 /**
1666  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1667  * @phba: Pointer to HBA context object.
1668  *
1669  * This function is called from SLI initialization code
1670  * to configure every ring of the HBA's SLI interface. The
1671  * caller is not required to hold any lock. This function issues
1672  * a config_ring mailbox command for each ring.
1673  * This function returns zero if successful else returns a negative
1674  * error code.
1675  **/
1676 static int
1677 lpfc_sli_ring_map(struct lpfc_hba *phba)
1678 {
1679         struct lpfc_sli *psli = &phba->sli;
1680         LPFC_MBOXQ_t *pmb;
1681         MAILBOX_t *pmbox;
1682         int i, rc, ret = 0;
1683
1684         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1685         if (!pmb)
1686                 return -ENOMEM;
1687         pmbox = &pmb->u.mb;
1688         phba->link_state = LPFC_INIT_MBX_CMDS;
1689         for (i = 0; i < psli->num_rings; i++) {
1690                 lpfc_config_ring(phba, i, pmb);
1691                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1692                 if (rc != MBX_SUCCESS) {
1693                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1694                                         "0446 Adapter failed to init (%d), "
1695                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1696                                         "ring %d\n",
1697                                         rc, pmbox->mbxCommand,
1698                                         pmbox->mbxStatus, i);
1699                         phba->link_state = LPFC_HBA_ERROR;
1700                         ret = -ENXIO;
1701                         break;
1702                 }
1703         }
1704         mempool_free(pmb, phba->mbox_mem_pool);
1705         return ret;
1706 }
1707
1708 /**
1709  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1710  * @phba: Pointer to HBA context object.
1711  * @pring: Pointer to driver SLI ring object.
1712  * @piocb: Pointer to the driver iocb object.
1713  *
1714  * The driver calls this function with the hbalock held for SLI3 ports or
1715  * the ring lock held for SLI4 ports. The function adds the
1716  * new iocb to txcmplq of the given ring. This function always returns
1717  * 0. If this function is called for ELS ring, this function checks if
1718  * there is a vport associated with the ELS command. This function also
1719  * starts els_tmofunc timer if this is an ELS command.
1720  **/
1721 static int
1722 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1723                         struct lpfc_iocbq *piocb)
1724 {
1725         if (phba->sli_rev == LPFC_SLI_REV4)
1726                 lockdep_assert_held(&pring->ring_lock);
1727         else
1728                 lockdep_assert_held(&phba->hbalock);
1729
1730         BUG_ON(!piocb);
1731
1732         list_add_tail(&piocb->list, &pring->txcmplq);
1733         piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1734         pring->txcmplq_cnt++;
1735
1736         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1737            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1738            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1739                 BUG_ON(!piocb->vport);
1740                 if (!(piocb->vport->load_flag & FC_UNLOADING))
1741                         mod_timer(&piocb->vport->els_tmofunc,
1742                                   jiffies +
1743                                   msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1744         }
1745
1746         return 0;
1747 }
1748
1749 /**
1750  * lpfc_sli_ringtx_get - Get first element of the txq
1751  * @phba: Pointer to HBA context object.
1752  * @pring: Pointer to driver SLI ring object.
1753  *
1754  * This function is called with hbalock held to get next
1755  * iocb in txq of the given ring. If there is any iocb in
1756  * the txq, the function returns first iocb in the list after
1757  * removing the iocb from the list, else it returns NULL.
1758  **/
1759 struct lpfc_iocbq *
1760 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1761 {
1762         struct lpfc_iocbq *cmd_iocb;
1763
1764         lockdep_assert_held(&phba->hbalock);
1765
1766         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1767         return cmd_iocb;
1768 }
1769
1770 /**
1771  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1772  * @phba: Pointer to HBA context object.
1773  * @pring: Pointer to driver SLI ring object.
1774  *
1775  * This function is called with hbalock held and the caller must post the
1776  * iocb without releasing the lock. If the caller releases the lock,
1777  * iocb slot returned by the function is not guaranteed to be available.
1778  * The function returns pointer to the next available iocb slot if there
1779  * is available slot in the ring, else it returns NULL.
1780  * If the get index of the ring is ahead of the put index, the function
1781  * will post an error attention event to the worker thread to take the
1782  * HBA to offline state.
1783  **/
1784 static IOCB_t *
1785 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1786 {
1787         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1788         uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
1789
1790         lockdep_assert_held(&phba->hbalock);
1791
1792         if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1793            (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1794                 pring->sli.sli3.next_cmdidx = 0;
1795
1796         if (unlikely(pring->sli.sli3.local_getidx ==
1797                 pring->sli.sli3.next_cmdidx)) {
1798
1799                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
1800
1801                 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
1802                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1803                                         "0315 Ring %d issue: portCmdGet %d "
1804                                         "is bigger than cmd ring %d\n",
1805                                         pring->ringno,
1806                                         pring->sli.sli3.local_getidx,
1807                                         max_cmd_idx);
1808
1809                         phba->link_state = LPFC_HBA_ERROR;
1810                         /*
1811                          * All error attention handlers are posted to
1812                          * worker thread
1813                          */
1814                         phba->work_ha |= HA_ERATT;
1815                         phba->work_hs = HS_FFER3;
1816
1817                         lpfc_worker_wake_up(phba);
1818
1819                         return NULL;
1820                 }
1821
1822                 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
1823                         return NULL;
1824         }
1825
1826         return lpfc_cmd_iocb(phba, pring);
1827 }
1828
1829 /**
1830  * lpfc_sli_next_iotag - Get an iotag for the iocb
1831  * @phba: Pointer to HBA context object.
1832  * @iocbq: Pointer to driver iocb object.
1833  *
1834  * This function gets an iotag for the iocb. If there is no unused iotag and
1835  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1836  * array and assigns a new iotag.
1837  * The function returns the allocated iotag if successful, else returns zero.
1838  * Zero is not a valid iotag.
1839  * The caller is not required to hold any lock.
1840  **/
1841 uint16_t
1842 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1843 {
1844         struct lpfc_iocbq **new_arr;
1845         struct lpfc_iocbq **old_arr;
1846         size_t new_len;
1847         struct lpfc_sli *psli = &phba->sli;
1848         uint16_t iotag;
1849
1850         spin_lock_irq(&phba->hbalock);
1851         iotag = psli->last_iotag;
1852         if(++iotag < psli->iocbq_lookup_len) {
1853                 psli->last_iotag = iotag;
1854                 psli->iocbq_lookup[iotag] = iocbq;
1855                 spin_unlock_irq(&phba->hbalock);
1856                 iocbq->iotag = iotag;
1857                 return iotag;
1858         } else if (psli->iocbq_lookup_len < (0xffff
1859                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1860                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1861                 spin_unlock_irq(&phba->hbalock);
1862                 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
1863                                   GFP_KERNEL);
1864                 if (new_arr) {
1865                         spin_lock_irq(&phba->hbalock);
1866                         old_arr = psli->iocbq_lookup;
1867                         if (new_len <= psli->iocbq_lookup_len) {
1868                                 /* highly unprobable case */
1869                                 kfree(new_arr);
1870                                 iotag = psli->last_iotag;
1871                                 if(++iotag < psli->iocbq_lookup_len) {
1872                                         psli->last_iotag = iotag;
1873                                         psli->iocbq_lookup[iotag] = iocbq;
1874                                         spin_unlock_irq(&phba->hbalock);
1875                                         iocbq->iotag = iotag;
1876                                         return iotag;
1877                                 }
1878                                 spin_unlock_irq(&phba->hbalock);
1879                                 return 0;
1880                         }
1881                         if (psli->iocbq_lookup)
1882                                 memcpy(new_arr, old_arr,
1883                                        ((psli->last_iotag  + 1) *
1884                                         sizeof (struct lpfc_iocbq *)));
1885                         psli->iocbq_lookup = new_arr;
1886                         psli->iocbq_lookup_len = new_len;
1887                         psli->last_iotag = iotag;
1888                         psli->iocbq_lookup[iotag] = iocbq;
1889                         spin_unlock_irq(&phba->hbalock);
1890                         iocbq->iotag = iotag;
1891                         kfree(old_arr);
1892                         return iotag;
1893                 }
1894         } else
1895                 spin_unlock_irq(&phba->hbalock);
1896
1897         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1898                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1899                         psli->last_iotag);
1900
1901         return 0;
1902 }
1903
1904 /**
1905  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1906  * @phba: Pointer to HBA context object.
1907  * @pring: Pointer to driver SLI ring object.
1908  * @iocb: Pointer to iocb slot in the ring.
1909  * @nextiocb: Pointer to driver iocb object which need to be
1910  *            posted to firmware.
1911  *
1912  * This function is called to post a new iocb to the firmware. This
1913  * function copies the new iocb to ring iocb slot and updates the
1914  * ring pointers. It adds the new iocb to txcmplq if there is
1915  * a completion call back for this iocb else the function will free the
1916  * iocb object.  The hbalock is asserted held in the code path calling
1917  * this routine.
1918  **/
1919 static void
1920 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1921                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1922 {
1923         /*
1924          * Set up an iotag
1925          */
1926         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1927
1928
1929         if (pring->ringno == LPFC_ELS_RING) {
1930                 lpfc_debugfs_slow_ring_trc(phba,
1931                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1932                         *(((uint32_t *) &nextiocb->iocb) + 4),
1933                         *(((uint32_t *) &nextiocb->iocb) + 6),
1934                         *(((uint32_t *) &nextiocb->iocb) + 7));
1935         }
1936
1937         /*
1938          * Issue iocb command to adapter
1939          */
1940         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1941         wmb();
1942         pring->stats.iocb_cmd++;
1943
1944         /*
1945          * If there is no completion routine to call, we can release the
1946          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1947          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1948          */
1949         if (nextiocb->iocb_cmpl)
1950                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1951         else
1952                 __lpfc_sli_release_iocbq(phba, nextiocb);
1953
1954         /*
1955          * Let the HBA know what IOCB slot will be the next one the
1956          * driver will put a command into.
1957          */
1958         pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1959         writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1960 }
1961
1962 /**
1963  * lpfc_sli_update_full_ring - Update the chip attention register
1964  * @phba: Pointer to HBA context object.
1965  * @pring: Pointer to driver SLI ring object.
1966  *
1967  * The caller is not required to hold any lock for calling this function.
1968  * This function updates the chip attention bits for the ring to inform firmware
1969  * that there are pending work to be done for this ring and requests an
1970  * interrupt when there is space available in the ring. This function is
1971  * called when the driver is unable to post more iocbs to the ring due
1972  * to unavailability of space in the ring.
1973  **/
1974 static void
1975 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1976 {
1977         int ringno = pring->ringno;
1978
1979         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1980
1981         wmb();
1982
1983         /*
1984          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1985          * The HBA will tell us when an IOCB entry is available.
1986          */
1987         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1988         readl(phba->CAregaddr); /* flush */
1989
1990         pring->stats.iocb_cmd_full++;
1991 }
1992
1993 /**
1994  * lpfc_sli_update_ring - Update chip attention register
1995  * @phba: Pointer to HBA context object.
1996  * @pring: Pointer to driver SLI ring object.
1997  *
1998  * This function updates the chip attention register bit for the
1999  * given ring to inform HBA that there is more work to be done
2000  * in this ring. The caller is not required to hold any lock.
2001  **/
2002 static void
2003 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2004 {
2005         int ringno = pring->ringno;
2006
2007         /*
2008          * Tell the HBA that there is work to do in this ring.
2009          */
2010         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
2011                 wmb();
2012                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
2013                 readl(phba->CAregaddr); /* flush */
2014         }
2015 }
2016
2017 /**
2018  * lpfc_sli_resume_iocb - Process iocbs in the txq
2019  * @phba: Pointer to HBA context object.
2020  * @pring: Pointer to driver SLI ring object.
2021  *
2022  * This function is called with hbalock held to post pending iocbs
2023  * in the txq to the firmware. This function is called when driver
2024  * detects space available in the ring.
2025  **/
2026 static void
2027 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2028 {
2029         IOCB_t *iocb;
2030         struct lpfc_iocbq *nextiocb;
2031
2032         lockdep_assert_held(&phba->hbalock);
2033
2034         /*
2035          * Check to see if:
2036          *  (a) there is anything on the txq to send
2037          *  (b) link is up
2038          *  (c) link attention events can be processed (fcp ring only)
2039          *  (d) IOCB processing is not blocked by the outstanding mbox command.
2040          */
2041
2042         if (lpfc_is_link_up(phba) &&
2043             (!list_empty(&pring->txq)) &&
2044             (pring->ringno != LPFC_FCP_RING ||
2045              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
2046
2047                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2048                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
2049                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2050
2051                 if (iocb)
2052                         lpfc_sli_update_ring(phba, pring);
2053                 else
2054                         lpfc_sli_update_full_ring(phba, pring);
2055         }
2056
2057         return;
2058 }
2059
2060 /**
2061  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
2062  * @phba: Pointer to HBA context object.
2063  * @hbqno: HBQ number.
2064  *
2065  * This function is called with hbalock held to get the next
2066  * available slot for the given HBQ. If there is free slot
2067  * available for the HBQ it will return pointer to the next available
2068  * HBQ entry else it will return NULL.
2069  **/
2070 static struct lpfc_hbq_entry *
2071 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
2072 {
2073         struct hbq_s *hbqp = &phba->hbqs[hbqno];
2074
2075         lockdep_assert_held(&phba->hbalock);
2076
2077         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
2078             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
2079                 hbqp->next_hbqPutIdx = 0;
2080
2081         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
2082                 uint32_t raw_index = phba->hbq_get[hbqno];
2083                 uint32_t getidx = le32_to_cpu(raw_index);
2084
2085                 hbqp->local_hbqGetIdx = getidx;
2086
2087                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
2088                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2089                                         "1802 HBQ %d: local_hbqGetIdx "
2090                                         "%u is > than hbqp->entry_count %u\n",
2091                                         hbqno, hbqp->local_hbqGetIdx,
2092                                         hbqp->entry_count);
2093
2094                         phba->link_state = LPFC_HBA_ERROR;
2095                         return NULL;
2096                 }
2097
2098                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
2099                         return NULL;
2100         }
2101
2102         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
2103                         hbqp->hbqPutIdx;
2104 }
2105
2106 /**
2107  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
2108  * @phba: Pointer to HBA context object.
2109  *
2110  * This function is called with no lock held to free all the
2111  * hbq buffers while uninitializing the SLI interface. It also
2112  * frees the HBQ buffers returned by the firmware but not yet
2113  * processed by the upper layers.
2114  **/
2115 void
2116 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
2117 {
2118         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2119         struct hbq_dmabuf *hbq_buf;
2120         unsigned long flags;
2121         int i, hbq_count;
2122
2123         hbq_count = lpfc_sli_hbq_count();
2124         /* Return all memory used by all HBQs */
2125         spin_lock_irqsave(&phba->hbalock, flags);
2126         for (i = 0; i < hbq_count; ++i) {
2127                 list_for_each_entry_safe(dmabuf, next_dmabuf,
2128                                 &phba->hbqs[i].hbq_buffer_list, list) {
2129                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
2130                         list_del(&hbq_buf->dbuf.list);
2131                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2132                 }
2133                 phba->hbqs[i].buffer_count = 0;
2134         }
2135
2136         /* Mark the HBQs not in use */
2137         phba->hbq_in_use = 0;
2138         spin_unlock_irqrestore(&phba->hbalock, flags);
2139 }
2140
2141 /**
2142  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
2143  * @phba: Pointer to HBA context object.
2144  * @hbqno: HBQ number.
2145  * @hbq_buf: Pointer to HBQ buffer.
2146  *
2147  * This function is called with the hbalock held to post a
2148  * hbq buffer to the firmware. If the function finds an empty
2149  * slot in the HBQ, it will post the buffer. The function will return
2150  * pointer to the hbq entry if it successfully post the buffer
2151  * else it will return NULL.
2152  **/
2153 static int
2154 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
2155                          struct hbq_dmabuf *hbq_buf)
2156 {
2157         lockdep_assert_held(&phba->hbalock);
2158         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2159 }
2160
2161 /**
2162  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2163  * @phba: Pointer to HBA context object.
2164  * @hbqno: HBQ number.
2165  * @hbq_buf: Pointer to HBQ buffer.
2166  *
2167  * This function is called with the hbalock held to post a hbq buffer to the
2168  * firmware. If the function finds an empty slot in the HBQ, it will post the
2169  * buffer and place it on the hbq_buffer_list. The function will return zero if
2170  * it successfully post the buffer else it will return an error.
2171  **/
2172 static int
2173 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2174                             struct hbq_dmabuf *hbq_buf)
2175 {
2176         struct lpfc_hbq_entry *hbqe;
2177         dma_addr_t physaddr = hbq_buf->dbuf.phys;
2178
2179         lockdep_assert_held(&phba->hbalock);
2180         /* Get next HBQ entry slot to use */
2181         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2182         if (hbqe) {
2183                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2184
2185                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2186                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
2187                 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
2188                 hbqe->bde.tus.f.bdeFlags = 0;
2189                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2190                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2191                                 /* Sync SLIM */
2192                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2193                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
2194                                 /* flush */
2195                 readl(phba->hbq_put + hbqno);
2196                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
2197                 return 0;
2198         } else
2199                 return -ENOMEM;
2200 }
2201
2202 /**
2203  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2204  * @phba: Pointer to HBA context object.
2205  * @hbqno: HBQ number.
2206  * @hbq_buf: Pointer to HBQ buffer.
2207  *
2208  * This function is called with the hbalock held to post an RQE to the SLI4
2209  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2210  * the hbq_buffer_list and return zero, otherwise it will return an error.
2211  **/
2212 static int
2213 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2214                             struct hbq_dmabuf *hbq_buf)
2215 {
2216         int rc;
2217         struct lpfc_rqe hrqe;
2218         struct lpfc_rqe drqe;
2219         struct lpfc_queue *hrq;
2220         struct lpfc_queue *drq;
2221
2222         if (hbqno != LPFC_ELS_HBQ)
2223                 return 1;
2224         hrq = phba->sli4_hba.hdr_rq;
2225         drq = phba->sli4_hba.dat_rq;
2226
2227         lockdep_assert_held(&phba->hbalock);
2228         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2229         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2230         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2231         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
2232         rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
2233         if (rc < 0)
2234                 return rc;
2235         hbq_buf->tag = (rc | (hbqno << 16));
2236         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2237         return 0;
2238 }
2239
2240 /* HBQ for ELS and CT traffic. */
2241 static struct lpfc_hbq_init lpfc_els_hbq = {
2242         .rn = 1,
2243         .entry_count = 256,
2244         .mask_count = 0,
2245         .profile = 0,
2246         .ring_mask = (1 << LPFC_ELS_RING),
2247         .buffer_count = 0,
2248         .init_count = 40,
2249         .add_count = 40,
2250 };
2251
2252 /* Array of HBQs */
2253 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
2254         &lpfc_els_hbq,
2255 };
2256
2257 /**
2258  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
2259  * @phba: Pointer to HBA context object.
2260  * @hbqno: HBQ number.
2261  * @count: Number of HBQ buffers to be posted.
2262  *
2263  * This function is called with no lock held to post more hbq buffers to the
2264  * given HBQ. The function returns the number of HBQ buffers successfully
2265  * posted.
2266  **/
2267 static int
2268 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2269 {
2270         uint32_t i, posted = 0;
2271         unsigned long flags;
2272         struct hbq_dmabuf *hbq_buffer;
2273         LIST_HEAD(hbq_buf_list);
2274         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
2275                 return 0;
2276
2277         if ((phba->hbqs[hbqno].buffer_count + count) >
2278             lpfc_hbq_defs[hbqno]->entry_count)
2279                 count = lpfc_hbq_defs[hbqno]->entry_count -
2280                                         phba->hbqs[hbqno].buffer_count;
2281         if (!count)
2282                 return 0;
2283         /* Allocate HBQ entries */
2284         for (i = 0; i < count; i++) {
2285                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2286                 if (!hbq_buffer)
2287                         break;
2288                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2289         }
2290         /* Check whether HBQ is still in use */
2291         spin_lock_irqsave(&phba->hbalock, flags);
2292         if (!phba->hbq_in_use)
2293                 goto err;
2294         while (!list_empty(&hbq_buf_list)) {
2295                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2296                                  dbuf.list);
2297                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2298                                       (hbqno << 16));
2299                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
2300                         phba->hbqs[hbqno].buffer_count++;
2301                         posted++;
2302                 } else
2303                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2304         }
2305         spin_unlock_irqrestore(&phba->hbalock, flags);
2306         return posted;
2307 err:
2308         spin_unlock_irqrestore(&phba->hbalock, flags);
2309         while (!list_empty(&hbq_buf_list)) {
2310                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2311                                  dbuf.list);
2312                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2313         }
2314         return 0;
2315 }
2316
2317 /**
2318  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
2319  * @phba: Pointer to HBA context object.
2320  * @qno: HBQ number.
2321  *
2322  * This function posts more buffers to the HBQ. This function
2323  * is called with no lock held. The function returns the number of HBQ entries
2324  * successfully allocated.
2325  **/
2326 int
2327 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
2328 {
2329         if (phba->sli_rev == LPFC_SLI_REV4)
2330                 return 0;
2331         else
2332                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2333                                          lpfc_hbq_defs[qno]->add_count);
2334 }
2335
2336 /**
2337  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
2338  * @phba: Pointer to HBA context object.
2339  * @qno:  HBQ queue number.
2340  *
2341  * This function is called from SLI initialization code path with
2342  * no lock held to post initial HBQ buffers to firmware. The
2343  * function returns the number of HBQ entries successfully allocated.
2344  **/
2345 static int
2346 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2347 {
2348         if (phba->sli_rev == LPFC_SLI_REV4)
2349                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2350                                         lpfc_hbq_defs[qno]->entry_count);
2351         else
2352                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2353                                          lpfc_hbq_defs[qno]->init_count);
2354 }
2355
2356 /*
2357  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2358  *
2359  * This function removes the first hbq buffer on an hbq list and returns a
2360  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2361  **/
2362 static struct hbq_dmabuf *
2363 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2364 {
2365         struct lpfc_dmabuf *d_buf;
2366
2367         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2368         if (!d_buf)
2369                 return NULL;
2370         return container_of(d_buf, struct hbq_dmabuf, dbuf);
2371 }
2372
2373 /**
2374  * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2375  * @phba: Pointer to HBA context object.
2376  * @hrq: HBQ number.
2377  *
2378  * This function removes the first RQ buffer on an RQ buffer list and returns a
2379  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2380  **/
2381 static struct rqb_dmabuf *
2382 lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2383 {
2384         struct lpfc_dmabuf *h_buf;
2385         struct lpfc_rqb *rqbp;
2386
2387         rqbp = hrq->rqbp;
2388         list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2389                          struct lpfc_dmabuf, list);
2390         if (!h_buf)
2391                 return NULL;
2392         rqbp->buffer_count--;
2393         return container_of(h_buf, struct rqb_dmabuf, hbuf);
2394 }
2395
2396 /**
2397  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
2398  * @phba: Pointer to HBA context object.
2399  * @tag: Tag of the hbq buffer.
2400  *
2401  * This function searches for the hbq buffer associated with the given tag in
2402  * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2403  * otherwise it returns NULL.
2404  **/
2405 static struct hbq_dmabuf *
2406 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2407 {
2408         struct lpfc_dmabuf *d_buf;
2409         struct hbq_dmabuf *hbq_buf;
2410         uint32_t hbqno;
2411
2412         hbqno = tag >> 16;
2413         if (hbqno >= LPFC_MAX_HBQS)
2414                 return NULL;
2415
2416         spin_lock_irq(&phba->hbalock);
2417         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2418                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2419                 if (hbq_buf->tag == tag) {
2420                         spin_unlock_irq(&phba->hbalock);
2421                         return hbq_buf;
2422                 }
2423         }
2424         spin_unlock_irq(&phba->hbalock);
2425         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2426                         "1803 Bad hbq tag. Data: x%x x%x\n",
2427                         tag, phba->hbqs[tag >> 16].buffer_count);
2428         return NULL;
2429 }
2430
2431 /**
2432  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2433  * @phba: Pointer to HBA context object.
2434  * @hbq_buffer: Pointer to HBQ buffer.
2435  *
2436  * This function is called with hbalock. This function gives back
2437  * the hbq buffer to firmware. If the HBQ does not have space to
2438  * post the buffer, it will free the buffer.
2439  **/
2440 void
2441 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2442 {
2443         uint32_t hbqno;
2444
2445         if (hbq_buffer) {
2446                 hbqno = hbq_buffer->tag >> 16;
2447                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2448                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2449         }
2450 }
2451
2452 /**
2453  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2454  * @mbxCommand: mailbox command code.
2455  *
2456  * This function is called by the mailbox event handler function to verify
2457  * that the completed mailbox command is a legitimate mailbox command. If the
2458  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2459  * and the mailbox event handler will take the HBA offline.
2460  **/
2461 static int
2462 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2463 {
2464         uint8_t ret;
2465
2466         switch (mbxCommand) {
2467         case MBX_LOAD_SM:
2468         case MBX_READ_NV:
2469         case MBX_WRITE_NV:
2470         case MBX_WRITE_VPARMS:
2471         case MBX_RUN_BIU_DIAG:
2472         case MBX_INIT_LINK:
2473         case MBX_DOWN_LINK:
2474         case MBX_CONFIG_LINK:
2475         case MBX_CONFIG_RING:
2476         case MBX_RESET_RING:
2477         case MBX_READ_CONFIG:
2478         case MBX_READ_RCONFIG:
2479         case MBX_READ_SPARM:
2480         case MBX_READ_STATUS:
2481         case MBX_READ_RPI:
2482         case MBX_READ_XRI:
2483         case MBX_READ_REV:
2484         case MBX_READ_LNK_STAT:
2485         case MBX_REG_LOGIN:
2486         case MBX_UNREG_LOGIN:
2487         case MBX_CLEAR_LA:
2488         case MBX_DUMP_MEMORY:
2489         case MBX_DUMP_CONTEXT:
2490         case MBX_RUN_DIAGS:
2491         case MBX_RESTART:
2492         case MBX_UPDATE_CFG:
2493         case MBX_DOWN_LOAD:
2494         case MBX_DEL_LD_ENTRY:
2495         case MBX_RUN_PROGRAM:
2496         case MBX_SET_MASK:
2497         case MBX_SET_VARIABLE:
2498         case MBX_UNREG_D_ID:
2499         case MBX_KILL_BOARD:
2500         case MBX_CONFIG_FARP:
2501         case MBX_BEACON:
2502         case MBX_LOAD_AREA:
2503         case MBX_RUN_BIU_DIAG64:
2504         case MBX_CONFIG_PORT:
2505         case MBX_READ_SPARM64:
2506         case MBX_READ_RPI64:
2507         case MBX_REG_LOGIN64:
2508         case MBX_READ_TOPOLOGY:
2509         case MBX_WRITE_WWN:
2510         case MBX_SET_DEBUG:
2511         case MBX_LOAD_EXP_ROM:
2512         case MBX_ASYNCEVT_ENABLE:
2513         case MBX_REG_VPI:
2514         case MBX_UNREG_VPI:
2515         case MBX_HEARTBEAT:
2516         case MBX_PORT_CAPABILITIES:
2517         case MBX_PORT_IOV_CONTROL:
2518         case MBX_SLI4_CONFIG:
2519         case MBX_SLI4_REQ_FTRS:
2520         case MBX_REG_FCFI:
2521         case MBX_UNREG_FCFI:
2522         case MBX_REG_VFI:
2523         case MBX_UNREG_VFI:
2524         case MBX_INIT_VPI:
2525         case MBX_INIT_VFI:
2526         case MBX_RESUME_RPI:
2527         case MBX_READ_EVENT_LOG_STATUS:
2528         case MBX_READ_EVENT_LOG:
2529         case MBX_SECURITY_MGMT:
2530         case MBX_AUTH_PORT:
2531         case MBX_ACCESS_VDATA:
2532                 ret = mbxCommand;
2533                 break;
2534         default:
2535                 ret = MBX_SHUTDOWN;
2536                 break;
2537         }
2538         return ret;
2539 }
2540
2541 /**
2542  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2543  * @phba: Pointer to HBA context object.
2544  * @pmboxq: Pointer to mailbox command.
2545  *
2546  * This is completion handler function for mailbox commands issued from
2547  * lpfc_sli_issue_mbox_wait function. This function is called by the
2548  * mailbox event handler function with no lock held. This function
2549  * will wake up thread waiting on the wait queue pointed by context1
2550  * of the mailbox.
2551  **/
2552 void
2553 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2554 {
2555         unsigned long drvr_flag;
2556         struct completion *pmbox_done;
2557
2558         /*
2559          * If pmbox_done is empty, the driver thread gave up waiting and
2560          * continued running.
2561          */
2562         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2563         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2564         pmbox_done = (struct completion *)pmboxq->context3;
2565         if (pmbox_done)
2566                 complete(pmbox_done);
2567         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2568         return;
2569 }
2570
2571 static void
2572 __lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2573 {
2574         unsigned long iflags;
2575
2576         if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
2577                 lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
2578                 spin_lock_irqsave(&ndlp->lock, iflags);
2579                 ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
2580                 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
2581                 spin_unlock_irqrestore(&ndlp->lock, iflags);
2582         }
2583         ndlp->nlp_flag &= ~NLP_UNREG_INP;
2584 }
2585
2586 /**
2587  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2588  * @phba: Pointer to HBA context object.
2589  * @pmb: Pointer to mailbox object.
2590  *
2591  * This function is the default mailbox completion handler. It
2592  * frees the memory resources associated with the completed mailbox
2593  * command. If the completed command is a REG_LOGIN mailbox command,
2594  * this function will issue a UREG_LOGIN to re-claim the RPI.
2595  **/
2596 void
2597 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2598 {
2599         struct lpfc_vport  *vport = pmb->vport;
2600         struct lpfc_dmabuf *mp;
2601         struct lpfc_nodelist *ndlp;
2602         struct Scsi_Host *shost;
2603         uint16_t rpi, vpi;
2604         int rc;
2605
2606         mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
2607
2608         if (mp) {
2609                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2610                 kfree(mp);
2611         }
2612
2613         /*
2614          * If a REG_LOGIN succeeded  after node is destroyed or node
2615          * is in re-discovery driver need to cleanup the RPI.
2616          */
2617         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2618             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2619             !pmb->u.mb.mbxStatus) {
2620                 rpi = pmb->u.mb.un.varWords[0];
2621                 vpi = pmb->u.mb.un.varRegLogin.vpi;
2622                 if (phba->sli_rev == LPFC_SLI_REV4)
2623                         vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
2624                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2625                 pmb->vport = vport;
2626                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2627                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2628                 if (rc != MBX_NOT_FINISHED)
2629                         return;
2630         }
2631
2632         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2633                 !(phba->pport->load_flag & FC_UNLOADING) &&
2634                 !pmb->u.mb.mbxStatus) {
2635                 shost = lpfc_shost_from_vport(vport);
2636                 spin_lock_irq(shost->host_lock);
2637                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2638                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2639                 spin_unlock_irq(shost->host_lock);
2640         }
2641
2642         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2643                 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2644                 lpfc_nlp_put(ndlp);
2645                 pmb->ctx_buf = NULL;
2646                 pmb->ctx_ndlp = NULL;
2647         }
2648
2649         if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2650                 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2651
2652                 /* Check to see if there are any deferred events to process */
2653                 if (ndlp) {
2654                         lpfc_printf_vlog(
2655                                 vport,
2656                                 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2657                                 "1438 UNREG cmpl deferred mbox x%x "
2658                                 "on NPort x%x Data: x%x x%x %px x%x x%x\n",
2659                                 ndlp->nlp_rpi, ndlp->nlp_DID,
2660                                 ndlp->nlp_flag, ndlp->nlp_defer_did,
2661                                 ndlp, vport->load_flag, kref_read(&ndlp->kref));
2662
2663                         if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2664                             (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
2665                                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
2666                                 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2667                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2668                         } else {
2669                                 __lpfc_sli_rpi_release(vport, ndlp);
2670                         }
2671
2672                         /* The unreg_login mailbox is complete and had a
2673                          * reference that has to be released.  The PLOGI
2674                          * got its own ref.
2675                          */
2676                         lpfc_nlp_put(ndlp);
2677                         pmb->ctx_ndlp = NULL;
2678                 }
2679         }
2680
2681         /* Check security permission status on INIT_LINK mailbox command */
2682         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2683             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2684                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2685                                 "2860 SLI authentication is required "
2686                                 "for INIT_LINK but has not done yet\n");
2687
2688         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2689                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2690         else
2691                 mempool_free(pmb, phba->mbox_mem_pool);
2692 }
2693  /**
2694  * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2695  * @phba: Pointer to HBA context object.
2696  * @pmb: Pointer to mailbox object.
2697  *
2698  * This function is the unreg rpi mailbox completion handler. It
2699  * frees the memory resources associated with the completed mailbox
2700  * command. An additional reference is put on the ndlp to prevent
2701  * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2702  * the unreg mailbox command completes, this routine puts the
2703  * reference back.
2704  *
2705  **/
2706 void
2707 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2708 {
2709         struct lpfc_vport  *vport = pmb->vport;
2710         struct lpfc_nodelist *ndlp;
2711
2712         ndlp = pmb->ctx_ndlp;
2713         if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2714                 if (phba->sli_rev == LPFC_SLI_REV4 &&
2715                     (bf_get(lpfc_sli_intf_if_type,
2716                      &phba->sli4_hba.sli_intf) >=
2717                      LPFC_SLI_INTF_IF_TYPE_2)) {
2718                         if (ndlp) {
2719                                 lpfc_printf_vlog(
2720                                          vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2721                                          "0010 UNREG_LOGIN vpi:%x "
2722                                          "rpi:%x DID:%x defer x%x flg x%x "
2723                                          "%px\n",
2724                                          vport->vpi, ndlp->nlp_rpi,
2725                                          ndlp->nlp_DID, ndlp->nlp_defer_did,
2726                                          ndlp->nlp_flag,
2727                                          ndlp);
2728                                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2729
2730                                 /* Check to see if there are any deferred
2731                                  * events to process
2732                                  */
2733                                 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2734                                     (ndlp->nlp_defer_did !=
2735                                     NLP_EVT_NOTHING_PENDING)) {
2736                                         lpfc_printf_vlog(
2737                                                 vport, KERN_INFO, LOG_DISCOVERY,
2738                                                 "4111 UNREG cmpl deferred "
2739                                                 "clr x%x on "
2740                                                 "NPort x%x Data: x%x x%px\n",
2741                                                 ndlp->nlp_rpi, ndlp->nlp_DID,
2742                                                 ndlp->nlp_defer_did, ndlp);
2743                                         ndlp->nlp_flag &= ~NLP_UNREG_INP;
2744                                         ndlp->nlp_defer_did =
2745                                                 NLP_EVT_NOTHING_PENDING;
2746                                         lpfc_issue_els_plogi(
2747                                                 vport, ndlp->nlp_DID, 0);
2748                                 } else {
2749                                         __lpfc_sli_rpi_release(vport, ndlp);
2750                                 }
2751
2752                                 lpfc_nlp_put(ndlp);
2753                         }
2754                 }
2755         }
2756
2757         mempool_free(pmb, phba->mbox_mem_pool);
2758 }
2759
2760 /**
2761  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2762  * @phba: Pointer to HBA context object.
2763  *
2764  * This function is called with no lock held. This function processes all
2765  * the completed mailbox commands and gives it to upper layers. The interrupt
2766  * service routine processes mailbox completion interrupt and adds completed
2767  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2768  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2769  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2770  * function returns the mailbox commands to the upper layer by calling the
2771  * completion handler function of each mailbox.
2772  **/
2773 int
2774 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2775 {
2776         MAILBOX_t *pmbox;
2777         LPFC_MBOXQ_t *pmb;
2778         int rc;
2779         LIST_HEAD(cmplq);
2780
2781         phba->sli.slistat.mbox_event++;
2782
2783         /* Get all completed mailboxe buffers into the cmplq */
2784         spin_lock_irq(&phba->hbalock);
2785         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2786         spin_unlock_irq(&phba->hbalock);
2787
2788         /* Get a Mailbox buffer to setup mailbox commands for callback */
2789         do {
2790                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2791                 if (pmb == NULL)
2792                         break;
2793
2794                 pmbox = &pmb->u.mb;
2795
2796                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2797                         if (pmb->vport) {
2798                                 lpfc_debugfs_disc_trc(pmb->vport,
2799                                         LPFC_DISC_TRC_MBOX_VPORT,
2800                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2801                                         (uint32_t)pmbox->mbxCommand,
2802                                         pmbox->un.varWords[0],
2803                                         pmbox->un.varWords[1]);
2804                         }
2805                         else {
2806                                 lpfc_debugfs_disc_trc(phba->pport,
2807                                         LPFC_DISC_TRC_MBOX,
2808                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2809                                         (uint32_t)pmbox->mbxCommand,
2810                                         pmbox->un.varWords[0],
2811                                         pmbox->un.varWords[1]);
2812                         }
2813                 }
2814
2815                 /*
2816                  * It is a fatal error if unknown mbox command completion.
2817                  */
2818                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2819                     MBX_SHUTDOWN) {
2820                         /* Unknown mailbox command compl */
2821                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2822                                         "(%d):0323 Unknown Mailbox command "
2823                                         "x%x (x%x/x%x) Cmpl\n",
2824                                         pmb->vport ? pmb->vport->vpi :
2825                                         LPFC_VPORT_UNKNOWN,
2826                                         pmbox->mbxCommand,
2827                                         lpfc_sli_config_mbox_subsys_get(phba,
2828                                                                         pmb),
2829                                         lpfc_sli_config_mbox_opcode_get(phba,
2830                                                                         pmb));
2831                         phba->link_state = LPFC_HBA_ERROR;
2832                         phba->work_hs = HS_FFER3;
2833                         lpfc_handle_eratt(phba);
2834                         continue;
2835                 }
2836
2837                 if (pmbox->mbxStatus) {
2838                         phba->sli.slistat.mbox_stat_err++;
2839                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2840                                 /* Mbox cmd cmpl error - RETRYing */
2841                                 lpfc_printf_log(phba, KERN_INFO,
2842                                         LOG_MBOX | LOG_SLI,
2843                                         "(%d):0305 Mbox cmd cmpl "
2844                                         "error - RETRYing Data: x%x "
2845                                         "(x%x/x%x) x%x x%x x%x\n",
2846                                         pmb->vport ? pmb->vport->vpi :
2847                                         LPFC_VPORT_UNKNOWN,
2848                                         pmbox->mbxCommand,
2849                                         lpfc_sli_config_mbox_subsys_get(phba,
2850                                                                         pmb),
2851                                         lpfc_sli_config_mbox_opcode_get(phba,
2852                                                                         pmb),
2853                                         pmbox->mbxStatus,
2854                                         pmbox->un.varWords[0],
2855                                         pmb->vport ? pmb->vport->port_state :
2856                                         LPFC_VPORT_UNKNOWN);
2857                                 pmbox->mbxStatus = 0;
2858                                 pmbox->mbxOwner = OWN_HOST;
2859                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2860                                 if (rc != MBX_NOT_FINISHED)
2861                                         continue;
2862                         }
2863                 }
2864
2865                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2866                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2867                                 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
2868                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2869                                 "x%x x%x x%x\n",
2870                                 pmb->vport ? pmb->vport->vpi : 0,
2871                                 pmbox->mbxCommand,
2872                                 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2873                                 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2874                                 pmb->mbox_cmpl,
2875                                 *((uint32_t *) pmbox),
2876                                 pmbox->un.varWords[0],
2877                                 pmbox->un.varWords[1],
2878                                 pmbox->un.varWords[2],
2879                                 pmbox->un.varWords[3],
2880                                 pmbox->un.varWords[4],
2881                                 pmbox->un.varWords[5],
2882                                 pmbox->un.varWords[6],
2883                                 pmbox->un.varWords[7],
2884                                 pmbox->un.varWords[8],
2885                                 pmbox->un.varWords[9],
2886                                 pmbox->un.varWords[10]);
2887
2888                 if (pmb->mbox_cmpl)
2889                         pmb->mbox_cmpl(phba,pmb);
2890         } while (1);
2891         return 0;
2892 }
2893
2894 /**
2895  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2896  * @phba: Pointer to HBA context object.
2897  * @pring: Pointer to driver SLI ring object.
2898  * @tag: buffer tag.
2899  *
2900  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2901  * is set in the tag the buffer is posted for a particular exchange,
2902  * the function will return the buffer without replacing the buffer.
2903  * If the buffer is for unsolicited ELS or CT traffic, this function
2904  * returns the buffer and also posts another buffer to the firmware.
2905  **/
2906 static struct lpfc_dmabuf *
2907 lpfc_sli_get_buff(struct lpfc_hba *phba,
2908                   struct lpfc_sli_ring *pring,
2909                   uint32_t tag)
2910 {
2911         struct hbq_dmabuf *hbq_entry;
2912
2913         if (tag & QUE_BUFTAG_BIT)
2914                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2915         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2916         if (!hbq_entry)
2917                 return NULL;
2918         return &hbq_entry->dbuf;
2919 }
2920
2921 /**
2922  * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
2923  *                              containing a NVME LS request.
2924  * @phba: pointer to lpfc hba data structure.
2925  * @piocb: pointer to the iocbq struct representing the sequence starting
2926  *        frame.
2927  *
2928  * This routine initially validates the NVME LS, validates there is a login
2929  * with the port that sent the LS, and then calls the appropriate nvme host
2930  * or target LS request handler.
2931  **/
2932 static void
2933 lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
2934 {
2935         struct lpfc_nodelist *ndlp;
2936         struct lpfc_dmabuf *d_buf;
2937         struct hbq_dmabuf *nvmebuf;
2938         struct fc_frame_header *fc_hdr;
2939         struct lpfc_async_xchg_ctx *axchg = NULL;
2940         char *failwhy = NULL;
2941         uint32_t oxid, sid, did, fctl, size;
2942         int ret = 1;
2943
2944         d_buf = piocb->context2;
2945
2946         nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2947         fc_hdr = nvmebuf->hbuf.virt;
2948         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2949         sid = sli4_sid_from_fc_hdr(fc_hdr);
2950         did = sli4_did_from_fc_hdr(fc_hdr);
2951         fctl = (fc_hdr->fh_f_ctl[0] << 16 |
2952                 fc_hdr->fh_f_ctl[1] << 8 |
2953                 fc_hdr->fh_f_ctl[2]);
2954         size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
2955
2956         lpfc_nvmeio_data(phba, "NVME LS    RCV: xri x%x sz %d from %06x\n",
2957                          oxid, size, sid);
2958
2959         if (phba->pport->load_flag & FC_UNLOADING) {
2960                 failwhy = "Driver Unloading";
2961         } else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
2962                 failwhy = "NVME FC4 Disabled";
2963         } else if (!phba->nvmet_support && !phba->pport->localport) {
2964                 failwhy = "No Localport";
2965         } else if (phba->nvmet_support && !phba->targetport) {
2966                 failwhy = "No Targetport";
2967         } else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
2968                 failwhy = "Bad NVME LS R_CTL";
2969         } else if (unlikely((fctl & 0x00FF0000) !=
2970                         (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
2971                 failwhy = "Bad NVME LS F_CTL";
2972         } else {
2973                 axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);
2974                 if (!axchg)
2975                         failwhy = "No CTX memory";
2976         }
2977
2978         if (unlikely(failwhy)) {
2979                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2980                                 "6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
2981                                 sid, oxid, failwhy);
2982                 goto out_fail;
2983         }
2984
2985         /* validate the source of the LS is logged in */
2986         ndlp = lpfc_findnode_did(phba->pport, sid);
2987         if (!ndlp ||
2988             ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2989              (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2990                 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2991                                 "6216 NVME Unsol rcv: No ndlp: "
2992                                 "NPort_ID x%x oxid x%x\n",
2993                                 sid, oxid);
2994                 goto out_fail;
2995         }
2996
2997         axchg->phba = phba;
2998         axchg->ndlp = ndlp;
2999         axchg->size = size;
3000         axchg->oxid = oxid;
3001         axchg->sid = sid;
3002         axchg->wqeq = NULL;
3003         axchg->state = LPFC_NVME_STE_LS_RCV;
3004         axchg->entry_cnt = 1;
3005         axchg->rqb_buffer = (void *)nvmebuf;
3006         axchg->hdwq = &phba->sli4_hba.hdwq[0];
3007         axchg->payload = nvmebuf->dbuf.virt;
3008         INIT_LIST_HEAD(&axchg->list);
3009
3010         if (phba->nvmet_support)
3011                 ret = lpfc_nvmet_handle_lsreq(phba, axchg);
3012         else
3013                 ret = lpfc_nvme_handle_lsreq(phba, axchg);
3014
3015         /* if zero, LS was successfully handled. If non-zero, LS not handled */
3016         if (!ret)
3017                 return;
3018
3019         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3020                         "6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
3021                         "NVMe%s handler failed %d\n",
3022                         did, sid, oxid,
3023                         (phba->nvmet_support) ? "T" : "I", ret);
3024
3025 out_fail:
3026
3027         /* recycle receive buffer */
3028         lpfc_in_buf_free(phba, &nvmebuf->dbuf);
3029
3030         /* If start of new exchange, abort it */
3031         if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
3032                 ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
3033
3034         if (ret)
3035                 kfree(axchg);
3036 }
3037
3038 /**
3039  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
3040  * @phba: Pointer to HBA context object.
3041  * @pring: Pointer to driver SLI ring object.
3042  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
3043  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
3044  * @fch_type: the type for the first frame of the sequence.
3045  *
3046  * This function is called with no lock held. This function uses the r_ctl and
3047  * type of the received sequence to find the correct callback function to call
3048  * to process the sequence.
3049  **/
3050 static int
3051 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3052                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
3053                          uint32_t fch_type)
3054 {
3055         int i;
3056
3057         switch (fch_type) {
3058         case FC_TYPE_NVME:
3059                 lpfc_nvme_unsol_ls_handler(phba, saveq);
3060                 return 1;
3061         default:
3062                 break;
3063         }
3064
3065         /* unSolicited Responses */
3066         if (pring->prt[0].profile) {
3067                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
3068                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
3069                                                                         saveq);
3070                 return 1;
3071         }
3072         /* We must search, based on rctl / type
3073            for the right routine */
3074         for (i = 0; i < pring->num_mask; i++) {
3075                 if ((pring->prt[i].rctl == fch_r_ctl) &&
3076                     (pring->prt[i].type == fch_type)) {
3077                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
3078                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
3079                                                 (phba, pring, saveq);
3080                         return 1;
3081                 }
3082         }
3083         return 0;
3084 }
3085
3086 /**
3087  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
3088  * @phba: Pointer to HBA context object.
3089  * @pring: Pointer to driver SLI ring object.
3090  * @saveq: Pointer to the unsolicited iocb.
3091  *
3092  * This function is called with no lock held by the ring event handler
3093  * when there is an unsolicited iocb posted to the response ring by the
3094  * firmware. This function gets the buffer associated with the iocbs
3095  * and calls the event handler for the ring. This function handles both
3096  * qring buffers and hbq buffers.
3097  * When the function returns 1 the caller can free the iocb object otherwise
3098  * upper layer functions will free the iocb objects.
3099  **/
3100 static int
3101 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3102                             struct lpfc_iocbq *saveq)
3103 {
3104         IOCB_t           * irsp;
3105         WORD5            * w5p;
3106         uint32_t           Rctl, Type;
3107         struct lpfc_iocbq *iocbq;
3108         struct lpfc_dmabuf *dmzbuf;
3109
3110         irsp = &(saveq->iocb);
3111
3112         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
3113                 if (pring->lpfc_sli_rcv_async_status)
3114                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
3115                 else
3116                         lpfc_printf_log(phba,
3117                                         KERN_WARNING,
3118                                         LOG_SLI,
3119                                         "0316 Ring %d handler: unexpected "
3120                                         "ASYNC_STATUS iocb received evt_code "
3121                                         "0x%x\n",
3122                                         pring->ringno,
3123                                         irsp->un.asyncstat.evt_code);
3124                 return 1;
3125         }
3126
3127         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
3128                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
3129                 if (irsp->ulpBdeCount > 0) {
3130                         dmzbuf = lpfc_sli_get_buff(phba, pring,
3131                                         irsp->un.ulpWord[3]);
3132                         lpfc_in_buf_free(phba, dmzbuf);
3133                 }
3134
3135                 if (irsp->ulpBdeCount > 1) {
3136                         dmzbuf = lpfc_sli_get_buff(phba, pring,
3137                                         irsp->unsli3.sli3Words[3]);
3138                         lpfc_in_buf_free(phba, dmzbuf);
3139                 }
3140
3141                 if (irsp->ulpBdeCount > 2) {
3142                         dmzbuf = lpfc_sli_get_buff(phba, pring,
3143                                 irsp->unsli3.sli3Words[7]);
3144                         lpfc_in_buf_free(phba, dmzbuf);
3145                 }
3146
3147                 return 1;
3148         }
3149
3150         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3151                 if (irsp->ulpBdeCount != 0) {
3152                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
3153                                                 irsp->un.ulpWord[3]);
3154                         if (!saveq->context2)
3155                                 lpfc_printf_log(phba,
3156                                         KERN_ERR,
3157                                         LOG_SLI,
3158                                         "0341 Ring %d Cannot find buffer for "
3159                                         "an unsolicited iocb. tag 0x%x\n",
3160                                         pring->ringno,
3161                                         irsp->un.ulpWord[3]);
3162                 }
3163                 if (irsp->ulpBdeCount == 2) {
3164                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
3165                                                 irsp->unsli3.sli3Words[7]);
3166                         if (!saveq->context3)
3167                                 lpfc_printf_log(phba,
3168                                         KERN_ERR,
3169                                         LOG_SLI,
3170                                         "0342 Ring %d Cannot find buffer for an"
3171                                         " unsolicited iocb. tag 0x%x\n",
3172                                         pring->ringno,
3173                                         irsp->unsli3.sli3Words[7]);
3174                 }
3175                 list_for_each_entry(iocbq, &saveq->list, list) {
3176                         irsp = &(iocbq->iocb);
3177                         if (irsp->ulpBdeCount != 0) {
3178                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
3179                                                         irsp->un.ulpWord[3]);
3180                                 if (!iocbq->context2)
3181                                         lpfc_printf_log(phba,
3182                                                 KERN_ERR,
3183                                                 LOG_SLI,
3184                                                 "0343 Ring %d Cannot find "
3185                                                 "buffer for an unsolicited iocb"
3186                                                 ". tag 0x%x\n", pring->ringno,
3187                                                 irsp->un.ulpWord[3]);
3188                         }
3189                         if (irsp->ulpBdeCount == 2) {
3190                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
3191                                                 irsp->unsli3.sli3Words[7]);
3192                                 if (!iocbq->context3)
3193                                         lpfc_printf_log(phba,
3194                                                 KERN_ERR,
3195                                                 LOG_SLI,
3196                                                 "0344 Ring %d Cannot find "
3197                                                 "buffer for an unsolicited "
3198                                                 "iocb. tag 0x%x\n",
3199                                                 pring->ringno,
3200                                                 irsp->unsli3.sli3Words[7]);
3201                         }
3202                 }
3203         }
3204         if (irsp->ulpBdeCount != 0 &&
3205             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
3206              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
3207                 int found = 0;
3208
3209                 /* search continue save q for same XRI */
3210                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
3211                         if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
3212                                 saveq->iocb.unsli3.rcvsli3.ox_id) {
3213                                 list_add_tail(&saveq->list, &iocbq->list);
3214                                 found = 1;
3215                                 break;
3216                         }
3217                 }
3218                 if (!found)
3219                         list_add_tail(&saveq->clist,
3220                                       &pring->iocb_continue_saveq);
3221                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
3222                         list_del_init(&iocbq->clist);
3223                         saveq = iocbq;
3224                         irsp = &(saveq->iocb);
3225                 } else
3226                         return 0;
3227         }
3228         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
3229             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
3230             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
3231                 Rctl = FC_RCTL_ELS_REQ;
3232                 Type = FC_TYPE_ELS;
3233         } else {
3234                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
3235                 Rctl = w5p->hcsw.Rctl;
3236                 Type = w5p->hcsw.Type;
3237
3238                 /* Firmware Workaround */
3239                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
3240                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
3241                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
3242                         Rctl = FC_RCTL_ELS_REQ;
3243                         Type = FC_TYPE_ELS;
3244                         w5p->hcsw.Rctl = Rctl;
3245                         w5p->hcsw.Type = Type;
3246                 }
3247         }
3248
3249         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
3250                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3251                                 "0313 Ring %d handler: unexpected Rctl x%x "
3252                                 "Type x%x received\n",
3253                                 pring->ringno, Rctl, Type);
3254
3255         return 1;
3256 }
3257
3258 /**
3259  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
3260  * @phba: Pointer to HBA context object.
3261  * @pring: Pointer to driver SLI ring object.
3262  * @prspiocb: Pointer to response iocb object.
3263  *
3264  * This function looks up the iocb_lookup table to get the command iocb
3265  * corresponding to the given response iocb using the iotag of the
3266  * response iocb. The driver calls this function with the hbalock held
3267  * for SLI3 ports or the ring lock held for SLI4 ports.
3268  * This function returns the command iocb object if it finds the command
3269  * iocb else returns NULL.
3270  **/
3271 static struct lpfc_iocbq *
3272 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3273                       struct lpfc_sli_ring *pring,
3274                       struct lpfc_iocbq *prspiocb)
3275 {
3276         struct lpfc_iocbq *cmd_iocb = NULL;
3277         uint16_t iotag;
3278         spinlock_t *temp_lock = NULL;
3279         unsigned long iflag = 0;
3280
3281         if (phba->sli_rev == LPFC_SLI_REV4)
3282                 temp_lock = &pring->ring_lock;
3283         else
3284                 temp_lock = &phba->hbalock;
3285
3286         spin_lock_irqsave(temp_lock, iflag);
3287         iotag = prspiocb->iocb.ulpIoTag;
3288
3289         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3290                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
3291                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3292                         /* remove from txcmpl queue list */
3293                         list_del_init(&cmd_iocb->list);
3294                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3295                         pring->txcmplq_cnt--;
3296                         spin_unlock_irqrestore(temp_lock, iflag);
3297                         return cmd_iocb;
3298                 }
3299         }
3300
3301         spin_unlock_irqrestore(temp_lock, iflag);
3302         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3303                         "0317 iotag x%x is out of "
3304                         "range: max iotag x%x wd0 x%x\n",
3305                         iotag, phba->sli.last_iotag,
3306                         *(((uint32_t *) &prspiocb->iocb) + 7));
3307         return NULL;
3308 }
3309
3310 /**
3311  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3312  * @phba: Pointer to HBA context object.
3313  * @pring: Pointer to driver SLI ring object.
3314  * @iotag: IOCB tag.
3315  *
3316  * This function looks up the iocb_lookup table to get the command iocb
3317  * corresponding to the given iotag. The driver calls this function with
3318  * the ring lock held because this function is an SLI4 port only helper.
3319  * This function returns the command iocb object if it finds the command
3320  * iocb else returns NULL.
3321  **/
3322 static struct lpfc_iocbq *
3323 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3324                              struct lpfc_sli_ring *pring, uint16_t iotag)
3325 {
3326         struct lpfc_iocbq *cmd_iocb = NULL;
3327         spinlock_t *temp_lock = NULL;
3328         unsigned long iflag = 0;
3329
3330         if (phba->sli_rev == LPFC_SLI_REV4)
3331                 temp_lock = &pring->ring_lock;
3332         else
3333                 temp_lock = &phba->hbalock;
3334
3335         spin_lock_irqsave(temp_lock, iflag);
3336         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3337                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
3338                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3339                         /* remove from txcmpl queue list */
3340                         list_del_init(&cmd_iocb->list);
3341                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3342                         pring->txcmplq_cnt--;
3343                         spin_unlock_irqrestore(temp_lock, iflag);
3344                         return cmd_iocb;
3345                 }
3346         }
3347
3348         spin_unlock_irqrestore(temp_lock, iflag);
3349         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3350                         "0372 iotag x%x lookup error: max iotag (x%x) "
3351                         "iocb_flag x%x\n",
3352                         iotag, phba->sli.last_iotag,
3353                         cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3354         return NULL;
3355 }
3356
3357 /**
3358  * lpfc_sli_process_sol_iocb - process solicited iocb completion
3359  * @phba: Pointer to HBA context object.
3360  * @pring: Pointer to driver SLI ring object.
3361  * @saveq: Pointer to the response iocb to be processed.
3362  *
3363  * This function is called by the ring event handler for non-fcp
3364  * rings when there is a new response iocb in the response ring.
3365  * The caller is not required to hold any locks. This function
3366  * gets the command iocb associated with the response iocb and
3367  * calls the completion handler for the command iocb. If there
3368  * is no completion handler, the function will free the resources
3369  * associated with command iocb. If the response iocb is for
3370  * an already aborted command iocb, the status of the completion
3371  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3372  * This function always returns 1.
3373  **/
3374 static int
3375 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3376                           struct lpfc_iocbq *saveq)
3377 {
3378         struct lpfc_iocbq *cmdiocbp;
3379         int rc = 1;
3380         unsigned long iflag;
3381
3382         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
3383         if (cmdiocbp) {
3384                 if (cmdiocbp->iocb_cmpl) {
3385                         /*
3386                          * If an ELS command failed send an event to mgmt
3387                          * application.
3388                          */
3389                         if (saveq->iocb.ulpStatus &&
3390                              (pring->ringno == LPFC_ELS_RING) &&
3391                              (cmdiocbp->iocb.ulpCommand ==
3392                                 CMD_ELS_REQUEST64_CR))
3393                                 lpfc_send_els_failure_event(phba,
3394                                         cmdiocbp, saveq);
3395
3396                         /*
3397                          * Post all ELS completions to the worker thread.
3398                          * All other are passed to the completion callback.
3399                          */
3400                         if (pring->ringno == LPFC_ELS_RING) {
3401                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3402                                     (cmdiocbp->iocb_flag &
3403                                                         LPFC_DRIVER_ABORTED)) {
3404                                         spin_lock_irqsave(&phba->hbalock,
3405                                                           iflag);
3406                                         cmdiocbp->iocb_flag &=
3407                                                 ~LPFC_DRIVER_ABORTED;
3408                                         spin_unlock_irqrestore(&phba->hbalock,
3409                                                                iflag);
3410                                         saveq->iocb.ulpStatus =
3411                                                 IOSTAT_LOCAL_REJECT;
3412                                         saveq->iocb.un.ulpWord[4] =
3413                                                 IOERR_SLI_ABORTED;
3414
3415                                         /* Firmware could still be in progress
3416                                          * of DMAing payload, so don't free data
3417                                          * buffer till after a hbeat.
3418                                          */
3419                                         spin_lock_irqsave(&phba->hbalock,
3420                                                           iflag);
3421                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
3422                                         spin_unlock_irqrestore(&phba->hbalock,
3423                                                                iflag);
3424                                 }
3425                                 if (phba->sli_rev == LPFC_SLI_REV4) {
3426                                         if (saveq->iocb_flag &
3427                                             LPFC_EXCHANGE_BUSY) {
3428                                                 /* Set cmdiocb flag for the
3429                                                  * exchange busy so sgl (xri)
3430                                                  * will not be released until
3431                                                  * the abort xri is received
3432                                                  * from hba.
3433                                                  */
3434                                                 spin_lock_irqsave(
3435                                                         &phba->hbalock, iflag);
3436                                                 cmdiocbp->iocb_flag |=
3437                                                         LPFC_EXCHANGE_BUSY;
3438                                                 spin_unlock_irqrestore(
3439                                                         &phba->hbalock, iflag);
3440                                         }
3441                                         if (cmdiocbp->iocb_flag &
3442                                             LPFC_DRIVER_ABORTED) {
3443                                                 /*
3444                                                  * Clear LPFC_DRIVER_ABORTED
3445                                                  * bit in case it was driver
3446                                                  * initiated abort.
3447                                                  */
3448                                                 spin_lock_irqsave(
3449                                                         &phba->hbalock, iflag);
3450                                                 cmdiocbp->iocb_flag &=
3451                                                         ~LPFC_DRIVER_ABORTED;
3452                                                 spin_unlock_irqrestore(
3453                                                         &phba->hbalock, iflag);
3454                                                 cmdiocbp->iocb.ulpStatus =
3455                                                         IOSTAT_LOCAL_REJECT;
3456                                                 cmdiocbp->iocb.un.ulpWord[4] =
3457                                                         IOERR_ABORT_REQUESTED;
3458                                                 /*
3459                                                  * For SLI4, irsiocb contains
3460                                                  * NO_XRI in sli_xritag, it
3461                                                  * shall not affect releasing
3462                                                  * sgl (xri) process.
3463                                                  */
3464                                                 saveq->iocb.ulpStatus =
3465                                                         IOSTAT_LOCAL_REJECT;
3466                                                 saveq->iocb.un.ulpWord[4] =
3467                                                         IOERR_SLI_ABORTED;
3468                                                 spin_lock_irqsave(
3469                                                         &phba->hbalock, iflag);
3470                                                 saveq->iocb_flag |=
3471                                                         LPFC_DELAY_MEM_FREE;
3472                                                 spin_unlock_irqrestore(
3473                                                         &phba->hbalock, iflag);
3474                                         }
3475                                 }
3476                         }
3477                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
3478                 } else
3479                         lpfc_sli_release_iocbq(phba, cmdiocbp);
3480         } else {
3481                 /*
3482                  * Unknown initiating command based on the response iotag.
3483                  * This could be the case on the ELS ring because of
3484                  * lpfc_els_abort().
3485                  */
3486                 if (pring->ringno != LPFC_ELS_RING) {
3487                         /*
3488                          * Ring <ringno> handler: unexpected completion IoTag
3489                          * <IoTag>
3490                          */
3491                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3492                                          "0322 Ring %d handler: "
3493                                          "unexpected completion IoTag x%x "
3494                                          "Data: x%x x%x x%x x%x\n",
3495                                          pring->ringno,
3496                                          saveq->iocb.ulpIoTag,
3497                                          saveq->iocb.ulpStatus,
3498                                          saveq->iocb.un.ulpWord[4],
3499                                          saveq->iocb.ulpCommand,
3500                                          saveq->iocb.ulpContext);
3501                 }
3502         }
3503
3504         return rc;
3505 }
3506
3507 /**
3508  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
3509  * @phba: Pointer to HBA context object.
3510  * @pring: Pointer to driver SLI ring object.
3511  *
3512  * This function is called from the iocb ring event handlers when
3513  * put pointer is ahead of the get pointer for a ring. This function signal
3514  * an error attention condition to the worker thread and the worker
3515  * thread will transition the HBA to offline state.
3516  **/
3517 static void
3518 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3519 {
3520         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3521         /*
3522          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3523          * rsp ring <portRspMax>
3524          */
3525         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3526                         "0312 Ring %d handler: portRspPut %d "
3527                         "is bigger than rsp ring %d\n",
3528                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
3529                         pring->sli.sli3.numRiocb);
3530
3531         phba->link_state = LPFC_HBA_ERROR;
3532
3533         /*
3534          * All error attention handlers are posted to
3535          * worker thread
3536          */
3537         phba->work_ha |= HA_ERATT;
3538         phba->work_hs = HS_FFER3;
3539
3540         lpfc_worker_wake_up(phba);
3541
3542         return;
3543 }
3544
3545 /**
3546  * lpfc_poll_eratt - Error attention polling timer timeout handler
3547  * @t: Context to fetch pointer to address of HBA context object from.
3548  *
3549  * This function is invoked by the Error Attention polling timer when the
3550  * timer times out. It will check the SLI Error Attention register for
3551  * possible attention events. If so, it will post an Error Attention event
3552  * and wake up worker thread to process it. Otherwise, it will set up the
3553  * Error Attention polling timer for the next poll.
3554  **/
3555 void lpfc_poll_eratt(struct timer_list *t)
3556 {
3557         struct lpfc_hba *phba;
3558         uint32_t eratt = 0;
3559         uint64_t sli_intr, cnt;
3560
3561         phba = from_timer(phba, t, eratt_poll);
3562
3563         /* Here we will also keep track of interrupts per sec of the hba */
3564         sli_intr = phba->sli.slistat.sli_intr;
3565
3566         if (phba->sli.slistat.sli_prev_intr > sli_intr)
3567                 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3568                         sli_intr);
3569         else
3570                 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3571
3572         /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3573         do_div(cnt, phba->eratt_poll_interval);
3574         phba->sli.slistat.sli_ips = cnt;
3575
3576         phba->sli.slistat.sli_prev_intr = sli_intr;
3577
3578         /* Check chip HA register for error event */
3579         eratt = lpfc_sli_check_eratt(phba);
3580
3581         if (eratt)
3582                 /* Tell the worker thread there is work to do */
3583                 lpfc_worker_wake_up(phba);
3584         else
3585                 /* Restart the timer for next eratt poll */
3586                 mod_timer(&phba->eratt_poll,
3587                           jiffies +
3588                           msecs_to_jiffies(1000 * phba->eratt_poll_interval));
3589         return;
3590 }
3591
3592
3593 /**
3594  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3595  * @phba: Pointer to HBA context object.
3596  * @pring: Pointer to driver SLI ring object.
3597  * @mask: Host attention register mask for this ring.
3598  *
3599  * This function is called from the interrupt context when there is a ring
3600  * event for the fcp ring. The caller does not hold any lock.
3601  * The function processes each response iocb in the response ring until it
3602  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3603  * LE bit set. The function will call the completion handler of the command iocb
3604  * if the response iocb indicates a completion for a command iocb or it is
3605  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3606  * function if this is an unsolicited iocb.
3607  * This routine presumes LPFC_FCP_RING handling and doesn't bother
3608  * to check it explicitly.
3609  */
3610 int
3611 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3612                                 struct lpfc_sli_ring *pring, uint32_t mask)
3613 {
3614         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3615         IOCB_t *irsp = NULL;
3616         IOCB_t *entry = NULL;
3617         struct lpfc_iocbq *cmdiocbq = NULL;
3618         struct lpfc_iocbq rspiocbq;
3619         uint32_t status;
3620         uint32_t portRspPut, portRspMax;
3621         int rc = 1;
3622         lpfc_iocb_type type;
3623         unsigned long iflag;
3624         uint32_t rsp_cmpl = 0;
3625
3626         spin_lock_irqsave(&phba->hbalock, iflag);
3627         pring->stats.iocb_event++;
3628
3629         /*
3630          * The next available response entry should never exceed the maximum
3631          * entries.  If it does, treat it as an adapter hardware error.
3632          */
3633         portRspMax = pring->sli.sli3.numRiocb;
3634         portRspPut = le32_to_cpu(pgp->rspPutInx);
3635         if (unlikely(portRspPut >= portRspMax)) {
3636                 lpfc_sli_rsp_pointers_error(phba, pring);
3637                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3638                 return 1;
3639         }
3640         if (phba->fcp_ring_in_use) {
3641                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3642                 return 1;
3643         } else
3644                 phba->fcp_ring_in_use = 1;
3645
3646         rmb();
3647         while (pring->sli.sli3.rspidx != portRspPut) {
3648                 /*
3649                  * Fetch an entry off the ring and copy it into a local data
3650                  * structure.  The copy involves a byte-swap since the
3651                  * network byte order and pci byte orders are different.
3652                  */
3653                 entry = lpfc_resp_iocb(phba, pring);
3654                 phba->last_completion_time = jiffies;
3655
3656                 if (++pring->sli.sli3.rspidx >= portRspMax)
3657                         pring->sli.sli3.rspidx = 0;
3658
3659                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3660                                       (uint32_t *) &rspiocbq.iocb,
3661                                       phba->iocb_rsp_size);
3662                 INIT_LIST_HEAD(&(rspiocbq.list));
3663                 irsp = &rspiocbq.iocb;
3664
3665                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3666                 pring->stats.iocb_rsp++;
3667                 rsp_cmpl++;
3668
3669                 if (unlikely(irsp->ulpStatus)) {
3670                         /*
3671                          * If resource errors reported from HBA, reduce
3672                          * queuedepths of the SCSI device.
3673                          */
3674                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3675                             ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3676                              IOERR_NO_RESOURCES)) {
3677                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3678                                 phba->lpfc_rampdown_queue_depth(phba);
3679                                 spin_lock_irqsave(&phba->hbalock, iflag);
3680                         }
3681
3682                         /* Rsp ring <ringno> error: IOCB */
3683                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3684                                         "0336 Rsp Ring %d error: IOCB Data: "
3685                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3686                                         pring->ringno,
3687                                         irsp->un.ulpWord[0],
3688                                         irsp->un.ulpWord[1],
3689                                         irsp->un.ulpWord[2],
3690                                         irsp->un.ulpWord[3],
3691                                         irsp->un.ulpWord[4],
3692                                         irsp->un.ulpWord[5],
3693                                         *(uint32_t *)&irsp->un1,
3694                                         *((uint32_t *)&irsp->un1 + 1));
3695                 }
3696
3697                 switch (type) {
3698                 case LPFC_ABORT_IOCB:
3699                 case LPFC_SOL_IOCB:
3700                         /*
3701                          * Idle exchange closed via ABTS from port.  No iocb
3702                          * resources need to be recovered.
3703                          */
3704                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3705                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3706                                                 "0333 IOCB cmd 0x%x"
3707                                                 " processed. Skipping"
3708                                                 " completion\n",
3709                                                 irsp->ulpCommand);
3710                                 break;
3711                         }
3712
3713                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3714                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3715                                                          &rspiocbq);
3716                         spin_lock_irqsave(&phba->hbalock, iflag);
3717                         if (unlikely(!cmdiocbq))
3718                                 break;
3719                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3720                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3721                         if (cmdiocbq->iocb_cmpl) {
3722                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3723                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3724                                                       &rspiocbq);
3725                                 spin_lock_irqsave(&phba->hbalock, iflag);
3726                         }
3727                         break;
3728                 case LPFC_UNSOL_IOCB:
3729                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3730                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3731                         spin_lock_irqsave(&phba->hbalock, iflag);
3732                         break;
3733                 default:
3734                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3735                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3736                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3737                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3738                                        MAX_MSG_DATA);
3739                                 dev_warn(&((phba->pcidev)->dev),
3740                                          "lpfc%d: %s\n",
3741                                          phba->brd_no, adaptermsg);
3742                         } else {
3743                                 /* Unknown IOCB command */
3744                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3745                                                 "0334 Unknown IOCB command "
3746                                                 "Data: x%x, x%x x%x x%x x%x\n",
3747                                                 type, irsp->ulpCommand,
3748                                                 irsp->ulpStatus,
3749                                                 irsp->ulpIoTag,
3750                                                 irsp->ulpContext);
3751                         }
3752                         break;
3753                 }
3754
3755                 /*
3756                  * The response IOCB has been processed.  Update the ring
3757                  * pointer in SLIM.  If the port response put pointer has not
3758                  * been updated, sync the pgp->rspPutInx and fetch the new port
3759                  * response put pointer.
3760                  */
3761                 writel(pring->sli.sli3.rspidx,
3762                         &phba->host_gp[pring->ringno].rspGetInx);
3763
3764                 if (pring->sli.sli3.rspidx == portRspPut)
3765                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3766         }
3767
3768         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3769                 pring->stats.iocb_rsp_full++;
3770                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3771                 writel(status, phba->CAregaddr);
3772                 readl(phba->CAregaddr);
3773         }
3774         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3775                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3776                 pring->stats.iocb_cmd_empty++;
3777
3778                 /* Force update of the local copy of cmdGetInx */
3779                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3780                 lpfc_sli_resume_iocb(phba, pring);
3781
3782                 if ((pring->lpfc_sli_cmd_available))
3783                         (pring->lpfc_sli_cmd_available) (phba, pring);
3784
3785         }
3786
3787         phba->fcp_ring_in_use = 0;
3788         spin_unlock_irqrestore(&phba->hbalock, iflag);
3789         return rc;
3790 }
3791
3792 /**
3793  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3794  * @phba: Pointer to HBA context object.
3795  * @pring: Pointer to driver SLI ring object.
3796  * @rspiocbp: Pointer to driver response IOCB object.
3797  *
3798  * This function is called from the worker thread when there is a slow-path
3799  * response IOCB to process. This function chains all the response iocbs until
3800  * seeing the iocb with the LE bit set. The function will call
3801  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3802  * completion of a command iocb. The function will call the
3803  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3804  * The function frees the resources or calls the completion handler if this
3805  * iocb is an abort completion. The function returns NULL when the response
3806  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3807  * this function shall chain the iocb on to the iocb_continueq and return the
3808  * response iocb passed in.
3809  **/
3810 static struct lpfc_iocbq *
3811 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3812                         struct lpfc_iocbq *rspiocbp)
3813 {
3814         struct lpfc_iocbq *saveq;
3815         struct lpfc_iocbq *cmdiocbp;
3816         struct lpfc_iocbq *next_iocb;
3817         IOCB_t *irsp = NULL;
3818         uint32_t free_saveq;
3819         uint8_t iocb_cmd_type;
3820         lpfc_iocb_type type;
3821         unsigned long iflag;
3822         int rc;
3823
3824         spin_lock_irqsave(&phba->hbalock, iflag);
3825         /* First add the response iocb to the countinueq list */
3826         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3827         pring->iocb_continueq_cnt++;
3828
3829         /* Now, determine whether the list is completed for processing */
3830         irsp = &rspiocbp->iocb;
3831         if (irsp->ulpLe) {
3832                 /*
3833                  * By default, the driver expects to free all resources
3834                  * associated with this iocb completion.
3835                  */
3836                 free_saveq = 1;
3837                 saveq = list_get_first(&pring->iocb_continueq,
3838                                        struct lpfc_iocbq, list);
3839                 irsp = &(saveq->iocb);
3840                 list_del_init(&pring->iocb_continueq);
3841                 pring->iocb_continueq_cnt = 0;
3842
3843                 pring->stats.iocb_rsp++;
3844
3845                 /*
3846                  * If resource errors reported from HBA, reduce
3847                  * queuedepths of the SCSI device.
3848                  */
3849                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3850                     ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3851                      IOERR_NO_RESOURCES)) {
3852                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3853                         phba->lpfc_rampdown_queue_depth(phba);
3854                         spin_lock_irqsave(&phba->hbalock, iflag);
3855                 }
3856
3857                 if (irsp->ulpStatus) {
3858                         /* Rsp ring <ringno> error: IOCB */
3859                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3860                                         "0328 Rsp Ring %d error: "
3861                                         "IOCB Data: "
3862                                         "x%x x%x x%x x%x "
3863                                         "x%x x%x x%x x%x "
3864                                         "x%x x%x x%x x%x "
3865                                         "x%x x%x x%x x%x\n",
3866                                         pring->ringno,
3867                                         irsp->un.ulpWord[0],
3868                                         irsp->un.ulpWord[1],
3869                                         irsp->un.ulpWord[2],
3870                                         irsp->un.ulpWord[3],
3871                                         irsp->un.ulpWord[4],
3872                                         irsp->un.ulpWord[5],
3873                                         *(((uint32_t *) irsp) + 6),
3874                                         *(((uint32_t *) irsp) + 7),
3875                                         *(((uint32_t *) irsp) + 8),
3876                                         *(((uint32_t *) irsp) + 9),
3877                                         *(((uint32_t *) irsp) + 10),
3878                                         *(((uint32_t *) irsp) + 11),
3879                                         *(((uint32_t *) irsp) + 12),
3880                                         *(((uint32_t *) irsp) + 13),
3881                                         *(((uint32_t *) irsp) + 14),
3882                                         *(((uint32_t *) irsp) + 15));
3883                 }
3884
3885                 /*
3886                  * Fetch the IOCB command type and call the correct completion
3887                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3888                  * get freed back to the lpfc_iocb_list by the discovery
3889                  * kernel thread.
3890                  */
3891                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3892                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3893                 switch (type) {
3894                 case LPFC_SOL_IOCB:
3895                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3896                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3897                         spin_lock_irqsave(&phba->hbalock, iflag);
3898                         break;
3899
3900                 case LPFC_UNSOL_IOCB:
3901                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3902                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3903                         spin_lock_irqsave(&phba->hbalock, iflag);
3904                         if (!rc)
3905                                 free_saveq = 0;
3906                         break;
3907
3908                 case LPFC_ABORT_IOCB:
3909                         cmdiocbp = NULL;
3910                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX) {
3911                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3912                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3913                                                                  saveq);
3914                                 spin_lock_irqsave(&phba->hbalock, iflag);
3915                         }
3916                         if (cmdiocbp) {
3917                                 /* Call the specified completion routine */
3918                                 if (cmdiocbp->iocb_cmpl) {
3919                                         spin_unlock_irqrestore(&phba->hbalock,
3920                                                                iflag);
3921                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3922                                                               saveq);
3923                                         spin_lock_irqsave(&phba->hbalock,
3924                                                           iflag);
3925                                 } else
3926                                         __lpfc_sli_release_iocbq(phba,
3927                                                                  cmdiocbp);
3928                         }
3929                         break;
3930
3931                 case LPFC_UNKNOWN_IOCB:
3932                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3933                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3934                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3935                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3936                                        MAX_MSG_DATA);
3937                                 dev_warn(&((phba->pcidev)->dev),
3938                                          "lpfc%d: %s\n",
3939                                          phba->brd_no, adaptermsg);
3940                         } else {
3941                                 /* Unknown IOCB command */
3942                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3943                                                 "0335 Unknown IOCB "
3944                                                 "command Data: x%x "
3945                                                 "x%x x%x x%x\n",
3946                                                 irsp->ulpCommand,
3947                                                 irsp->ulpStatus,
3948                                                 irsp->ulpIoTag,
3949                                                 irsp->ulpContext);
3950                         }
3951                         break;
3952                 }
3953
3954                 if (free_saveq) {
3955                         list_for_each_entry_safe(rspiocbp, next_iocb,
3956                                                  &saveq->list, list) {
3957                                 list_del_init(&rspiocbp->list);
3958                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3959                         }
3960                         __lpfc_sli_release_iocbq(phba, saveq);
3961                 }
3962                 rspiocbp = NULL;
3963         }
3964         spin_unlock_irqrestore(&phba->hbalock, iflag);
3965         return rspiocbp;
3966 }
3967
3968 /**
3969  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3970  * @phba: Pointer to HBA context object.
3971  * @pring: Pointer to driver SLI ring object.
3972  * @mask: Host attention register mask for this ring.
3973  *
3974  * This routine wraps the actual slow_ring event process routine from the
3975  * API jump table function pointer from the lpfc_hba struct.
3976  **/
3977 void
3978 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3979                                 struct lpfc_sli_ring *pring, uint32_t mask)
3980 {
3981         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3982 }
3983
3984 /**
3985  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3986  * @phba: Pointer to HBA context object.
3987  * @pring: Pointer to driver SLI ring object.
3988  * @mask: Host attention register mask for this ring.
3989  *
3990  * This function is called from the worker thread when there is a ring event
3991  * for non-fcp rings. The caller does not hold any lock. The function will
3992  * remove each response iocb in the response ring and calls the handle
3993  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3994  **/
3995 static void
3996 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3997                                    struct lpfc_sli_ring *pring, uint32_t mask)
3998 {
3999         struct lpfc_pgp *pgp;
4000         IOCB_t *entry;
4001         IOCB_t *irsp = NULL;
4002         struct lpfc_iocbq *rspiocbp = NULL;
4003         uint32_t portRspPut, portRspMax;
4004         unsigned long iflag;
4005         uint32_t status;
4006
4007         pgp = &phba->port_gp[pring->ringno];
4008         spin_lock_irqsave(&phba->hbalock, iflag);
4009         pring->stats.iocb_event++;
4010
4011         /*
4012          * The next available response entry should never exceed the maximum
4013          * entries.  If it does, treat it as an adapter hardware error.
4014          */
4015         portRspMax = pring->sli.sli3.numRiocb;
4016         portRspPut = le32_to_cpu(pgp->rspPutInx);
4017         if (portRspPut >= portRspMax) {
4018                 /*
4019                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
4020                  * rsp ring <portRspMax>
4021                  */
4022                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4023                                 "0303 Ring %d handler: portRspPut %d "
4024                                 "is bigger than rsp ring %d\n",
4025                                 pring->ringno, portRspPut, portRspMax);
4026
4027                 phba->link_state = LPFC_HBA_ERROR;
4028                 spin_unlock_irqrestore(&phba->hbalock, iflag);
4029
4030                 phba->work_hs = HS_FFER3;
4031                 lpfc_handle_eratt(phba);
4032
4033                 return;
4034         }
4035
4036         rmb();
4037         while (pring->sli.sli3.rspidx != portRspPut) {
4038                 /*
4039                  * Build a completion list and call the appropriate handler.
4040                  * The process is to get the next available response iocb, get
4041                  * a free iocb from the list, copy the response data into the
4042                  * free iocb, insert to the continuation list, and update the
4043                  * next response index to slim.  This process makes response
4044                  * iocb's in the ring available to DMA as fast as possible but
4045                  * pays a penalty for a copy operation.  Since the iocb is
4046                  * only 32 bytes, this penalty is considered small relative to
4047                  * the PCI reads for register values and a slim write.  When
4048                  * the ulpLe field is set, the entire Command has been
4049                  * received.
4050                  */
4051                 entry = lpfc_resp_iocb(phba, pring);
4052
4053                 phba->last_completion_time = jiffies;
4054                 rspiocbp = __lpfc_sli_get_iocbq(phba);
4055                 if (rspiocbp == NULL) {
4056                         printk(KERN_ERR "%s: out of buffers! Failing "
4057                                "completion.\n", __func__);
4058                         break;
4059                 }
4060
4061                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
4062                                       phba->iocb_rsp_size);
4063                 irsp = &rspiocbp->iocb;
4064
4065                 if (++pring->sli.sli3.rspidx >= portRspMax)
4066                         pring->sli.sli3.rspidx = 0;
4067
4068                 if (pring->ringno == LPFC_ELS_RING) {
4069                         lpfc_debugfs_slow_ring_trc(phba,
4070                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
4071                                 *(((uint32_t *) irsp) + 4),
4072                                 *(((uint32_t *) irsp) + 6),
4073                                 *(((uint32_t *) irsp) + 7));
4074                 }
4075
4076                 writel(pring->sli.sli3.rspidx,
4077                         &phba->host_gp[pring->ringno].rspGetInx);
4078
4079                 spin_unlock_irqrestore(&phba->hbalock, iflag);
4080                 /* Handle the response IOCB */
4081                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
4082                 spin_lock_irqsave(&phba->hbalock, iflag);
4083
4084                 /*
4085                  * If the port response put pointer has not been updated, sync
4086                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
4087                  * response put pointer.
4088                  */
4089                 if (pring->sli.sli3.rspidx == portRspPut) {
4090                         portRspPut = le32_to_cpu(pgp->rspPutInx);
4091                 }
4092         } /* while (pring->sli.sli3.rspidx != portRspPut) */
4093
4094         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
4095                 /* At least one response entry has been freed */
4096                 pring->stats.iocb_rsp_full++;
4097                 /* SET RxRE_RSP in Chip Att register */
4098                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4099                 writel(status, phba->CAregaddr);
4100                 readl(phba->CAregaddr); /* flush */
4101         }
4102         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4103                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4104                 pring->stats.iocb_cmd_empty++;
4105
4106                 /* Force update of the local copy of cmdGetInx */
4107                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
4108                 lpfc_sli_resume_iocb(phba, pring);
4109
4110                 if ((pring->lpfc_sli_cmd_available))
4111                         (pring->lpfc_sli_cmd_available) (phba, pring);
4112
4113         }
4114
4115         spin_unlock_irqrestore(&phba->hbalock, iflag);
4116         return;
4117 }
4118
4119 /**
4120  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
4121  * @phba: Pointer to HBA context object.
4122  * @pring: Pointer to driver SLI ring object.
4123  * @mask: Host attention register mask for this ring.
4124  *
4125  * This function is called from the worker thread when there is a pending
4126  * ELS response iocb on the driver internal slow-path response iocb worker
4127  * queue. The caller does not hold any lock. The function will remove each
4128  * response iocb from the response worker queue and calls the handle
4129  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4130  **/
4131 static void
4132 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4133                                    struct lpfc_sli_ring *pring, uint32_t mask)
4134 {
4135         struct lpfc_iocbq *irspiocbq;
4136         struct hbq_dmabuf *dmabuf;
4137         struct lpfc_cq_event *cq_event;
4138         unsigned long iflag;
4139         int count = 0;
4140
4141         spin_lock_irqsave(&phba->hbalock, iflag);
4142         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
4143         spin_unlock_irqrestore(&phba->hbalock, iflag);
4144         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4145                 /* Get the response iocb from the head of work queue */
4146                 spin_lock_irqsave(&phba->hbalock, iflag);
4147                 list_remove_head(&phba->sli4_hba.sp_queue_event,
4148                                  cq_event, struct lpfc_cq_event, list);
4149                 spin_unlock_irqrestore(&phba->hbalock, iflag);
4150
4151                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4152                 case CQE_CODE_COMPL_WQE:
4153                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4154                                                  cq_event);
4155                         /* Translate ELS WCQE to response IOCBQ */
4156                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
4157                                                                    irspiocbq);
4158                         if (irspiocbq)
4159                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
4160                                                            irspiocbq);
4161                         count++;
4162                         break;
4163                 case CQE_CODE_RECEIVE:
4164                 case CQE_CODE_RECEIVE_V1:
4165                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
4166                                               cq_event);
4167                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
4168                         count++;
4169                         break;
4170                 default:
4171                         break;
4172                 }
4173
4174                 /* Limit the number of events to 64 to avoid soft lockups */
4175                 if (count == 64)
4176                         break;
4177         }
4178 }
4179
4180 /**
4181  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
4182  * @phba: Pointer to HBA context object.
4183  * @pring: Pointer to driver SLI ring object.
4184  *
4185  * This function aborts all iocbs in the given ring and frees all the iocb
4186  * objects in txq. This function issues an abort iocb for all the iocb commands
4187  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4188  * the return of this function. The caller is not required to hold any locks.
4189  **/
4190 void
4191 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4192 {
4193         LIST_HEAD(completions);
4194         struct lpfc_iocbq *iocb, *next_iocb;
4195
4196         if (pring->ringno == LPFC_ELS_RING) {
4197                 lpfc_fabric_abort_hba(phba);
4198         }
4199
4200         /* Error everything on txq and txcmplq
4201          * First do the txq.
4202          */
4203         if (phba->sli_rev >= LPFC_SLI_REV4) {
4204                 spin_lock_irq(&pring->ring_lock);
4205                 list_splice_init(&pring->txq, &completions);
4206                 pring->txq_cnt = 0;
4207                 spin_unlock_irq(&pring->ring_lock);
4208
4209                 spin_lock_irq(&phba->hbalock);
4210                 /* Next issue ABTS for everything on the txcmplq */
4211                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
4212                         lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
4213                 spin_unlock_irq(&phba->hbalock);
4214         } else {
4215                 spin_lock_irq(&phba->hbalock);
4216                 list_splice_init(&pring->txq, &completions);
4217                 pring->txq_cnt = 0;
4218
4219                 /* Next issue ABTS for everything on the txcmplq */
4220                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
4221                         lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
4222                 spin_unlock_irq(&phba->hbalock);
4223         }
4224
4225         /* Cancel all the IOCBs from the completions list */
4226         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4227                               IOERR_SLI_ABORTED);
4228 }
4229
4230 /**
4231  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4232  * @phba: Pointer to HBA context object.
4233  *
4234  * This function aborts all iocbs in FCP rings and frees all the iocb
4235  * objects in txq. This function issues an abort iocb for all the iocb commands
4236  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4237  * the return of this function. The caller is not required to hold any locks.
4238  **/
4239 void
4240 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4241 {
4242         struct lpfc_sli *psli = &phba->sli;
4243         struct lpfc_sli_ring  *pring;
4244         uint32_t i;
4245
4246         /* Look on all the FCP Rings for the iotag */
4247         if (phba->sli_rev >= LPFC_SLI_REV4) {
4248                 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4249                         pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4250                         lpfc_sli_abort_iocb_ring(phba, pring);
4251                 }
4252         } else {
4253                 pring = &psli->sli3_ring[LPFC_FCP_RING];
4254                 lpfc_sli_abort_iocb_ring(phba, pring);
4255         }
4256 }
4257
4258 /**
4259  * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
4260  * @phba: Pointer to HBA context object.
4261  *
4262  * This function flushes all iocbs in the IO ring and frees all the iocb
4263  * objects in txq and txcmplq. This function will not issue abort iocbs
4264  * for all the iocb commands in txcmplq, they will just be returned with
4265  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4266  * slot has been permanently disabled.
4267  **/
4268 void
4269 lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
4270 {
4271         LIST_HEAD(txq);
4272         LIST_HEAD(txcmplq);
4273         struct lpfc_sli *psli = &phba->sli;
4274         struct lpfc_sli_ring  *pring;
4275         uint32_t i;
4276         struct lpfc_iocbq *piocb, *next_iocb;
4277
4278         spin_lock_irq(&phba->hbalock);
4279         if (phba->hba_flag & HBA_IOQ_FLUSH ||
4280             !phba->sli4_hba.hdwq) {
4281                 spin_unlock_irq(&phba->hbalock);
4282                 return;
4283         }
4284         /* Indicate the I/O queues are flushed */
4285         phba->hba_flag |= HBA_IOQ_FLUSH;
4286         spin_unlock_irq(&phba->hbalock);
4287
4288         /* Look on all the FCP Rings for the iotag */
4289         if (phba->sli_rev >= LPFC_SLI_REV4) {
4290                 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4291                         pring = phba->sli4_hba.hdwq[i].io_wq->pring;
4292
4293                         spin_lock_irq(&pring->ring_lock);
4294                         /* Retrieve everything on txq */
4295                         list_splice_init(&pring->txq, &txq);
4296                         list_for_each_entry_safe(piocb, next_iocb,
4297                                                  &pring->txcmplq, list)
4298                                 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4299                         /* Retrieve everything on the txcmplq */
4300                         list_splice_init(&pring->txcmplq, &txcmplq);
4301                         pring->txq_cnt = 0;
4302                         pring->txcmplq_cnt = 0;
4303                         spin_unlock_irq(&pring->ring_lock);
4304
4305                         /* Flush the txq */
4306                         lpfc_sli_cancel_iocbs(phba, &txq,
4307                                               IOSTAT_LOCAL_REJECT,
4308                                               IOERR_SLI_DOWN);
4309                         /* Flush the txcmpq */
4310                         lpfc_sli_cancel_iocbs(phba, &txcmplq,
4311                                               IOSTAT_LOCAL_REJECT,
4312                                               IOERR_SLI_DOWN);
4313                 }
4314         } else {
4315                 pring = &psli->sli3_ring[LPFC_FCP_RING];
4316
4317                 spin_lock_irq(&phba->hbalock);
4318                 /* Retrieve everything on txq */
4319                 list_splice_init(&pring->txq, &txq);
4320                 list_for_each_entry_safe(piocb, next_iocb,
4321                                          &pring->txcmplq, list)
4322                         piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4323                 /* Retrieve everything on the txcmplq */
4324                 list_splice_init(&pring->txcmplq, &txcmplq);
4325                 pring->txq_cnt = 0;
4326                 pring->txcmplq_cnt = 0;
4327                 spin_unlock_irq(&phba->hbalock);
4328
4329                 /* Flush the txq */
4330                 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4331                                       IOERR_SLI_DOWN);
4332                 /* Flush the txcmpq */
4333                 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4334                                       IOERR_SLI_DOWN);
4335         }
4336 }
4337
4338 /**
4339  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
4340  * @phba: Pointer to HBA context object.
4341  * @mask: Bit mask to be checked.
4342  *
4343  * This function reads the host status register and compares
4344  * with the provided bit mask to check if HBA completed
4345  * the restart. This function will wait in a loop for the
4346  * HBA to complete restart. If the HBA does not restart within
4347  * 15 iterations, the function will reset the HBA again. The
4348  * function returns 1 when HBA fail to restart otherwise returns
4349  * zero.
4350  **/
4351 static int
4352 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
4353 {
4354         uint32_t status;
4355         int i = 0;
4356         int retval = 0;
4357
4358         /* Read the HBA Host Status Register */
4359         if (lpfc_readl(phba->HSregaddr, &status))
4360                 return 1;
4361
4362         /*
4363          * Check status register every 100ms for 5 retries, then every
4364          * 500ms for 5, then every 2.5 sec for 5, then reset board and
4365          * every 2.5 sec for 4.
4366          * Break our of the loop if errors occurred during init.
4367          */
4368         while (((status & mask) != mask) &&
4369                !(status & HS_FFERM) &&
4370                i++ < 20) {
4371
4372                 if (i <= 5)
4373                         msleep(10);
4374                 else if (i <= 10)
4375                         msleep(500);
4376                 else
4377                         msleep(2500);
4378
4379                 if (i == 15) {
4380                                 /* Do post */
4381                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4382                         lpfc_sli_brdrestart(phba);
4383                 }
4384                 /* Read the HBA Host Status Register */
4385                 if (lpfc_readl(phba->HSregaddr, &status)) {
4386                         retval = 1;
4387                         break;
4388                 }
4389         }
4390
4391         /* Check to see if any errors occurred during init */
4392         if ((status & HS_FFERM) || (i >= 20)) {
4393                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4394                                 "2751 Adapter failed to restart, "
4395                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4396                                 status,
4397                                 readl(phba->MBslimaddr + 0xa8),
4398                                 readl(phba->MBslimaddr + 0xac));
4399                 phba->link_state = LPFC_HBA_ERROR;
4400                 retval = 1;
4401         }
4402
4403         return retval;
4404 }
4405
4406 /**
4407  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4408  * @phba: Pointer to HBA context object.
4409  * @mask: Bit mask to be checked.
4410  *
4411  * This function checks the host status register to check if HBA is
4412  * ready. This function will wait in a loop for the HBA to be ready
4413  * If the HBA is not ready , the function will will reset the HBA PCI
4414  * function again. The function returns 1 when HBA fail to be ready
4415  * otherwise returns zero.
4416  **/
4417 static int
4418 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4419 {
4420         uint32_t status;
4421         int retval = 0;
4422
4423         /* Read the HBA Host Status Register */
4424         status = lpfc_sli4_post_status_check(phba);
4425
4426         if (status) {
4427                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4428                 lpfc_sli_brdrestart(phba);
4429                 status = lpfc_sli4_post_status_check(phba);
4430         }
4431
4432         /* Check to see if any errors occurred during init */
4433         if (status) {
4434                 phba->link_state = LPFC_HBA_ERROR;
4435                 retval = 1;
4436         } else
4437                 phba->sli4_hba.intr_enable = 0;
4438
4439         return retval;
4440 }
4441
4442 /**
4443  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4444  * @phba: Pointer to HBA context object.
4445  * @mask: Bit mask to be checked.
4446  *
4447  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4448  * from the API jump table function pointer from the lpfc_hba struct.
4449  **/
4450 int
4451 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4452 {
4453         return phba->lpfc_sli_brdready(phba, mask);
4454 }
4455
4456 #define BARRIER_TEST_PATTERN (0xdeadbeef)
4457
4458 /**
4459  * lpfc_reset_barrier - Make HBA ready for HBA reset
4460  * @phba: Pointer to HBA context object.
4461  *
4462  * This function is called before resetting an HBA. This function is called
4463  * with hbalock held and requests HBA to quiesce DMAs before a reset.
4464  **/
4465 void lpfc_reset_barrier(struct lpfc_hba *phba)
4466 {
4467         uint32_t __iomem *resp_buf;
4468         uint32_t __iomem *mbox_buf;
4469         volatile uint32_t mbox;
4470         uint32_t hc_copy, ha_copy, resp_data;
4471         int  i;
4472         uint8_t hdrtype;
4473
4474         lockdep_assert_held(&phba->hbalock);
4475
4476         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4477         if (hdrtype != 0x80 ||
4478             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4479              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4480                 return;
4481
4482         /*
4483          * Tell the other part of the chip to suspend temporarily all
4484          * its DMA activity.
4485          */
4486         resp_buf = phba->MBslimaddr;
4487
4488         /* Disable the error attention */
4489         if (lpfc_readl(phba->HCregaddr, &hc_copy))
4490                 return;
4491         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4492         readl(phba->HCregaddr); /* flush */
4493         phba->link_flag |= LS_IGNORE_ERATT;
4494
4495         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4496                 return;
4497         if (ha_copy & HA_ERATT) {
4498                 /* Clear Chip error bit */
4499                 writel(HA_ERATT, phba->HAregaddr);
4500                 phba->pport->stopped = 1;
4501         }
4502
4503         mbox = 0;
4504         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4505         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4506
4507         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4508         mbox_buf = phba->MBslimaddr;
4509         writel(mbox, mbox_buf);
4510
4511         for (i = 0; i < 50; i++) {
4512                 if (lpfc_readl((resp_buf + 1), &resp_data))
4513                         return;
4514                 if (resp_data != ~(BARRIER_TEST_PATTERN))
4515                         mdelay(1);
4516                 else
4517                         break;
4518         }
4519         resp_data = 0;
4520         if (lpfc_readl((resp_buf + 1), &resp_data))
4521                 return;
4522         if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4523                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4524                     phba->pport->stopped)
4525                         goto restore_hc;
4526                 else
4527                         goto clear_errat;
4528         }
4529
4530         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
4531         resp_data = 0;
4532         for (i = 0; i < 500; i++) {
4533                 if (lpfc_readl(resp_buf, &resp_data))
4534                         return;
4535                 if (resp_data != mbox)
4536                         mdelay(1);
4537                 else
4538                         break;
4539         }
4540
4541 clear_errat:
4542
4543         while (++i < 500) {
4544                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4545                         return;
4546                 if (!(ha_copy & HA_ERATT))
4547                         mdelay(1);
4548                 else
4549                         break;
4550         }
4551
4552         if (readl(phba->HAregaddr) & HA_ERATT) {
4553                 writel(HA_ERATT, phba->HAregaddr);
4554                 phba->pport->stopped = 1;
4555         }
4556
4557 restore_hc:
4558         phba->link_flag &= ~LS_IGNORE_ERATT;
4559         writel(hc_copy, phba->HCregaddr);
4560         readl(phba->HCregaddr); /* flush */
4561 }
4562
4563 /**
4564  * lpfc_sli_brdkill - Issue a kill_board mailbox command
4565  * @phba: Pointer to HBA context object.
4566  *
4567  * This function issues a kill_board mailbox command and waits for
4568  * the error attention interrupt. This function is called for stopping
4569  * the firmware processing. The caller is not required to hold any
4570  * locks. This function calls lpfc_hba_down_post function to free
4571  * any pending commands after the kill. The function will return 1 when it
4572  * fails to kill the board else will return 0.
4573  **/
4574 int
4575 lpfc_sli_brdkill(struct lpfc_hba *phba)
4576 {
4577         struct lpfc_sli *psli;
4578         LPFC_MBOXQ_t *pmb;
4579         uint32_t status;
4580         uint32_t ha_copy;
4581         int retval;
4582         int i = 0;
4583
4584         psli = &phba->sli;
4585
4586         /* Kill HBA */
4587         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4588                         "0329 Kill HBA Data: x%x x%x\n",
4589                         phba->pport->port_state, psli->sli_flag);
4590
4591         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4592         if (!pmb)
4593                 return 1;
4594
4595         /* Disable the error attention */
4596         spin_lock_irq(&phba->hbalock);
4597         if (lpfc_readl(phba->HCregaddr, &status)) {
4598                 spin_unlock_irq(&phba->hbalock);
4599                 mempool_free(pmb, phba->mbox_mem_pool);
4600                 return 1;
4601         }
4602         status &= ~HC_ERINT_ENA;
4603         writel(status, phba->HCregaddr);
4604         readl(phba->HCregaddr); /* flush */
4605         phba->link_flag |= LS_IGNORE_ERATT;
4606         spin_unlock_irq(&phba->hbalock);
4607
4608         lpfc_kill_board(phba, pmb);
4609         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4610         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4611
4612         if (retval != MBX_SUCCESS) {
4613                 if (retval != MBX_BUSY)
4614                         mempool_free(pmb, phba->mbox_mem_pool);
4615                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4616                                 "2752 KILL_BOARD command failed retval %d\n",
4617                                 retval);
4618                 spin_lock_irq(&phba->hbalock);
4619                 phba->link_flag &= ~LS_IGNORE_ERATT;
4620                 spin_unlock_irq(&phba->hbalock);
4621                 return 1;
4622         }
4623
4624         spin_lock_irq(&phba->hbalock);
4625         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4626         spin_unlock_irq(&phba->hbalock);
4627
4628         mempool_free(pmb, phba->mbox_mem_pool);
4629
4630         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4631          * attention every 100ms for 3 seconds. If we don't get ERATT after
4632          * 3 seconds we still set HBA_ERROR state because the status of the
4633          * board is now undefined.
4634          */
4635         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4636                 return 1;
4637         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4638                 mdelay(100);
4639                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4640                         return 1;
4641         }
4642
4643         del_timer_sync(&psli->mbox_tmo);
4644         if (ha_copy & HA_ERATT) {
4645                 writel(HA_ERATT, phba->HAregaddr);
4646                 phba->pport->stopped = 1;
4647         }
4648         spin_lock_irq(&phba->hbalock);
4649         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4650         psli->mbox_active = NULL;
4651         phba->link_flag &= ~LS_IGNORE_ERATT;
4652         spin_unlock_irq(&phba->hbalock);
4653
4654         lpfc_hba_down_post(phba);
4655         phba->link_state = LPFC_HBA_ERROR;
4656
4657         return ha_copy & HA_ERATT ? 0 : 1;
4658 }
4659
4660 /**
4661  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
4662  * @phba: Pointer to HBA context object.
4663  *
4664  * This function resets the HBA by writing HC_INITFF to the control
4665  * register. After the HBA resets, this function resets all the iocb ring
4666  * indices. This function disables PCI layer parity checking during
4667  * the reset.
4668  * This function returns 0 always.
4669  * The caller is not required to hold any locks.
4670  **/
4671 int
4672 lpfc_sli_brdreset(struct lpfc_hba *phba)
4673 {
4674         struct lpfc_sli *psli;
4675         struct lpfc_sli_ring *pring;
4676         uint16_t cfg_value;
4677         int i;
4678
4679         psli = &phba->sli;
4680
4681         /* Reset HBA */
4682         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4683                         "0325 Reset HBA Data: x%x x%x\n",
4684                         (phba->pport) ? phba->pport->port_state : 0,
4685                         psli->sli_flag);
4686
4687         /* perform board reset */
4688         phba->fc_eventTag = 0;
4689         phba->link_events = 0;
4690         if (phba->pport) {
4691                 phba->pport->fc_myDID = 0;
4692                 phba->pport->fc_prevDID = 0;
4693         }
4694
4695         /* Turn off parity checking and serr during the physical reset */
4696         if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
4697                 return -EIO;
4698
4699         pci_write_config_word(phba->pcidev, PCI_COMMAND,
4700                               (cfg_value &
4701                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4702
4703         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4704
4705         /* Now toggle INITFF bit in the Host Control Register */
4706         writel(HC_INITFF, phba->HCregaddr);
4707         mdelay(1);
4708         readl(phba->HCregaddr); /* flush */
4709         writel(0, phba->HCregaddr);
4710         readl(phba->HCregaddr); /* flush */
4711
4712         /* Restore PCI cmd register */
4713         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4714
4715         /* Initialize relevant SLI info */
4716         for (i = 0; i < psli->num_rings; i++) {
4717                 pring = &psli->sli3_ring[i];
4718                 pring->flag = 0;
4719                 pring->sli.sli3.rspidx = 0;
4720                 pring->sli.sli3.next_cmdidx  = 0;
4721                 pring->sli.sli3.local_getidx = 0;
4722                 pring->sli.sli3.cmdidx = 0;
4723                 pring->missbufcnt = 0;
4724         }
4725
4726         phba->link_state = LPFC_WARM_START;
4727         return 0;
4728 }
4729
4730 /**
4731  * lpfc_sli4_brdreset - Reset a sli-4 HBA
4732  * @phba: Pointer to HBA context object.
4733  *
4734  * This function resets a SLI4 HBA. This function disables PCI layer parity
4735  * checking during resets the device. The caller is not required to hold
4736  * any locks.
4737  *
4738  * This function returns 0 on success else returns negative error code.
4739  **/
4740 int
4741 lpfc_sli4_brdreset(struct lpfc_hba *phba)
4742 {
4743         struct lpfc_sli *psli = &phba->sli;
4744         uint16_t cfg_value;
4745         int rc = 0;
4746
4747         /* Reset HBA */
4748         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4749                         "0295 Reset HBA Data: x%x x%x x%x\n",
4750                         phba->pport->port_state, psli->sli_flag,
4751                         phba->hba_flag);
4752
4753         /* perform board reset */
4754         phba->fc_eventTag = 0;
4755         phba->link_events = 0;
4756         phba->pport->fc_myDID = 0;
4757         phba->pport->fc_prevDID = 0;
4758
4759         spin_lock_irq(&phba->hbalock);
4760         psli->sli_flag &= ~(LPFC_PROCESS_LA);
4761         phba->fcf.fcf_flag = 0;
4762         spin_unlock_irq(&phba->hbalock);
4763
4764         /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4765         if (phba->hba_flag & HBA_FW_DUMP_OP) {
4766                 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4767                 return rc;
4768         }
4769
4770         /* Now physically reset the device */
4771         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4772                         "0389 Performing PCI function reset!\n");
4773
4774         /* Turn off parity checking and serr during the physical reset */
4775         if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
4776                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4777                                 "3205 PCI read Config failed\n");
4778                 return -EIO;
4779         }
4780
4781         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4782                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4783
4784         /* Perform FCoE PCI function reset before freeing queue memory */
4785         rc = lpfc_pci_function_reset(phba);
4786
4787         /* Restore PCI cmd register */
4788         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4789
4790         return rc;
4791 }
4792
4793 /**
4794  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4795  * @phba: Pointer to HBA context object.
4796  *
4797  * This function is called in the SLI initialization code path to
4798  * restart the HBA. The caller is not required to hold any lock.
4799  * This function writes MBX_RESTART mailbox command to the SLIM and
4800  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4801  * function to free any pending commands. The function enables
4802  * POST only during the first initialization. The function returns zero.
4803  * The function does not guarantee completion of MBX_RESTART mailbox
4804  * command before the return of this function.
4805  **/
4806 static int
4807 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4808 {
4809         MAILBOX_t *mb;
4810         struct lpfc_sli *psli;
4811         volatile uint32_t word0;
4812         void __iomem *to_slim;
4813         uint32_t hba_aer_enabled;
4814
4815         spin_lock_irq(&phba->hbalock);
4816
4817         /* Take PCIe device Advanced Error Reporting (AER) state */
4818         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4819
4820         psli = &phba->sli;
4821
4822         /* Restart HBA */
4823         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4824                         "0337 Restart HBA Data: x%x x%x\n",
4825                         (phba->pport) ? phba->pport->port_state : 0,
4826                         psli->sli_flag);
4827
4828         word0 = 0;
4829         mb = (MAILBOX_t *) &word0;
4830         mb->mbxCommand = MBX_RESTART;
4831         mb->mbxHc = 1;
4832
4833         lpfc_reset_barrier(phba);
4834
4835         to_slim = phba->MBslimaddr;
4836         writel(*(uint32_t *) mb, to_slim);
4837         readl(to_slim); /* flush */
4838
4839         /* Only skip post after fc_ffinit is completed */
4840         if (phba->pport && phba->pport->port_state)
4841                 word0 = 1;      /* This is really setting up word1 */
4842         else
4843                 word0 = 0;      /* This is really setting up word1 */
4844         to_slim = phba->MBslimaddr + sizeof (uint32_t);
4845         writel(*(uint32_t *) mb, to_slim);
4846         readl(to_slim); /* flush */
4847
4848         lpfc_sli_brdreset(phba);
4849         if (phba->pport)
4850                 phba->pport->stopped = 0;
4851         phba->link_state = LPFC_INIT_START;
4852         phba->hba_flag = 0;
4853         spin_unlock_irq(&phba->hbalock);
4854
4855         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4856         psli->stats_start = ktime_get_seconds();
4857
4858         /* Give the INITFF and Post time to settle. */
4859         mdelay(100);
4860
4861         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4862         if (hba_aer_enabled)
4863                 pci_disable_pcie_error_reporting(phba->pcidev);
4864
4865         lpfc_hba_down_post(phba);
4866
4867         return 0;
4868 }
4869
4870 /**
4871  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4872  * @phba: Pointer to HBA context object.
4873  *
4874  * This function is called in the SLI initialization code path to restart
4875  * a SLI4 HBA. The caller is not required to hold any lock.
4876  * At the end of the function, it calls lpfc_hba_down_post function to
4877  * free any pending commands.
4878  **/
4879 static int
4880 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4881 {
4882         struct lpfc_sli *psli = &phba->sli;
4883         uint32_t hba_aer_enabled;
4884         int rc;
4885
4886         /* Restart HBA */
4887         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4888                         "0296 Restart HBA Data: x%x x%x\n",
4889                         phba->pport->port_state, psli->sli_flag);
4890
4891         /* Take PCIe device Advanced Error Reporting (AER) state */
4892         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4893
4894         rc = lpfc_sli4_brdreset(phba);
4895         if (rc) {
4896                 phba->link_state = LPFC_HBA_ERROR;
4897                 goto hba_down_queue;
4898         }
4899
4900         spin_lock_irq(&phba->hbalock);
4901         phba->pport->stopped = 0;
4902         phba->link_state = LPFC_INIT_START;
4903         phba->hba_flag = 0;
4904         spin_unlock_irq(&phba->hbalock);
4905
4906         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4907         psli->stats_start = ktime_get_seconds();
4908
4909         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4910         if (hba_aer_enabled)
4911                 pci_disable_pcie_error_reporting(phba->pcidev);
4912
4913 hba_down_queue:
4914         lpfc_hba_down_post(phba);
4915         lpfc_sli4_queue_destroy(phba);
4916
4917         return rc;
4918 }
4919
4920 /**
4921  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4922  * @phba: Pointer to HBA context object.
4923  *
4924  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4925  * API jump table function pointer from the lpfc_hba struct.
4926 **/
4927 int
4928 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4929 {
4930         return phba->lpfc_sli_brdrestart(phba);
4931 }
4932
4933 /**
4934  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4935  * @phba: Pointer to HBA context object.
4936  *
4937  * This function is called after a HBA restart to wait for successful
4938  * restart of the HBA. Successful restart of the HBA is indicated by
4939  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4940  * iteration, the function will restart the HBA again. The function returns
4941  * zero if HBA successfully restarted else returns negative error code.
4942  **/
4943 int
4944 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4945 {
4946         uint32_t status, i = 0;
4947
4948         /* Read the HBA Host Status Register */
4949         if (lpfc_readl(phba->HSregaddr, &status))
4950                 return -EIO;
4951
4952         /* Check status register to see what current state is */
4953         i = 0;
4954         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4955
4956                 /* Check every 10ms for 10 retries, then every 100ms for 90
4957                  * retries, then every 1 sec for 50 retires for a total of
4958                  * ~60 seconds before reset the board again and check every
4959                  * 1 sec for 50 retries. The up to 60 seconds before the
4960                  * board ready is required by the Falcon FIPS zeroization
4961                  * complete, and any reset the board in between shall cause
4962                  * restart of zeroization, further delay the board ready.
4963                  */
4964                 if (i++ >= 200) {
4965                         /* Adapter failed to init, timeout, status reg
4966                            <status> */
4967                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4968                                         "0436 Adapter failed to init, "
4969                                         "timeout, status reg x%x, "
4970                                         "FW Data: A8 x%x AC x%x\n", status,
4971                                         readl(phba->MBslimaddr + 0xa8),
4972                                         readl(phba->MBslimaddr + 0xac));
4973                         phba->link_state = LPFC_HBA_ERROR;
4974                         return -ETIMEDOUT;
4975                 }
4976
4977                 /* Check to see if any errors occurred during init */
4978                 if (status & HS_FFERM) {
4979                         /* ERROR: During chipset initialization */
4980                         /* Adapter failed to init, chipset, status reg
4981                            <status> */
4982                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4983                                         "0437 Adapter failed to init, "
4984                                         "chipset, status reg x%x, "
4985                                         "FW Data: A8 x%x AC x%x\n", status,
4986                                         readl(phba->MBslimaddr + 0xa8),
4987                                         readl(phba->MBslimaddr + 0xac));
4988                         phba->link_state = LPFC_HBA_ERROR;
4989                         return -EIO;
4990                 }
4991
4992                 if (i <= 10)
4993                         msleep(10);
4994                 else if (i <= 100)
4995                         msleep(100);
4996                 else
4997                         msleep(1000);
4998
4999                 if (i == 150) {
5000                         /* Do post */
5001                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5002                         lpfc_sli_brdrestart(phba);
5003                 }
5004                 /* Read the HBA Host Status Register */
5005                 if (lpfc_readl(phba->HSregaddr, &status))
5006                         return -EIO;
5007         }
5008
5009         /* Check to see if any errors occurred during init */
5010         if (status & HS_FFERM) {
5011                 /* ERROR: During chipset initialization */
5012                 /* Adapter failed to init, chipset, status reg <status> */
5013                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5014                                 "0438 Adapter failed to init, chipset, "
5015                                 "status reg x%x, "
5016                                 "FW Data: A8 x%x AC x%x\n", status,
5017                                 readl(phba->MBslimaddr + 0xa8),
5018                                 readl(phba->MBslimaddr + 0xac));
5019                 phba->link_state = LPFC_HBA_ERROR;
5020                 return -EIO;
5021         }
5022
5023         /* Clear all interrupt enable conditions */
5024         writel(0, phba->HCregaddr);
5025         readl(phba->HCregaddr); /* flush */
5026
5027         /* setup host attn register */
5028         writel(0xffffffff, phba->HAregaddr);
5029         readl(phba->HAregaddr); /* flush */
5030         return 0;
5031 }
5032
5033 /**
5034  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
5035  *
5036  * This function calculates and returns the number of HBQs required to be
5037  * configured.
5038  **/
5039 int
5040 lpfc_sli_hbq_count(void)
5041 {
5042         return ARRAY_SIZE(lpfc_hbq_defs);
5043 }
5044
5045 /**
5046  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
5047  *
5048  * This function adds the number of hbq entries in every HBQ to get
5049  * the total number of hbq entries required for the HBA and returns
5050  * the total count.
5051  **/
5052 static int
5053 lpfc_sli_hbq_entry_count(void)
5054 {
5055         int  hbq_count = lpfc_sli_hbq_count();
5056         int  count = 0;
5057         int  i;
5058
5059         for (i = 0; i < hbq_count; ++i)
5060                 count += lpfc_hbq_defs[i]->entry_count;
5061         return count;
5062 }
5063
5064 /**
5065  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
5066  *
5067  * This function calculates amount of memory required for all hbq entries
5068  * to be configured and returns the total memory required.
5069  **/
5070 int
5071 lpfc_sli_hbq_size(void)
5072 {
5073         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
5074 }
5075
5076 /**
5077  * lpfc_sli_hbq_setup - configure and initialize HBQs
5078  * @phba: Pointer to HBA context object.
5079  *
5080  * This function is called during the SLI initialization to configure
5081  * all the HBQs and post buffers to the HBQ. The caller is not
5082  * required to hold any locks. This function will return zero if successful
5083  * else it will return negative error code.
5084  **/
5085 static int
5086 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
5087 {
5088         int  hbq_count = lpfc_sli_hbq_count();
5089         LPFC_MBOXQ_t *pmb;
5090         MAILBOX_t *pmbox;
5091         uint32_t hbqno;
5092         uint32_t hbq_entry_index;
5093
5094                                 /* Get a Mailbox buffer to setup mailbox
5095                                  * commands for HBA initialization
5096                                  */
5097         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5098
5099         if (!pmb)
5100                 return -ENOMEM;
5101
5102         pmbox = &pmb->u.mb;
5103
5104         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
5105         phba->link_state = LPFC_INIT_MBX_CMDS;
5106         phba->hbq_in_use = 1;
5107
5108         hbq_entry_index = 0;
5109         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
5110                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
5111                 phba->hbqs[hbqno].hbqPutIdx      = 0;
5112                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
5113                 phba->hbqs[hbqno].entry_count =
5114                         lpfc_hbq_defs[hbqno]->entry_count;
5115                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
5116                         hbq_entry_index, pmb);
5117                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
5118
5119                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
5120                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
5121                            mbxStatus <status>, ring <num> */
5122
5123                         lpfc_printf_log(phba, KERN_ERR,
5124                                         LOG_SLI | LOG_VPORT,
5125                                         "1805 Adapter failed to init. "
5126                                         "Data: x%x x%x x%x\n",
5127                                         pmbox->mbxCommand,
5128                                         pmbox->mbxStatus, hbqno);
5129
5130                         phba->link_state = LPFC_HBA_ERROR;
5131                         mempool_free(pmb, phba->mbox_mem_pool);
5132                         return -ENXIO;
5133                 }
5134         }
5135         phba->hbq_count = hbq_count;
5136
5137         mempool_free(pmb, phba->mbox_mem_pool);
5138
5139         /* Initially populate or replenish the HBQs */
5140         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5141                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
5142         return 0;
5143 }
5144
5145 /**
5146  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5147  * @phba: Pointer to HBA context object.
5148  *
5149  * This function is called during the SLI initialization to configure
5150  * all the HBQs and post buffers to the HBQ. The caller is not
5151  * required to hold any locks. This function will return zero if successful
5152  * else it will return negative error code.
5153  **/
5154 static int
5155 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5156 {
5157         phba->hbq_in_use = 1;
5158         /**
5159          * Specific case when the MDS diagnostics is enabled and supported.
5160          * The receive buffer count is truncated to manage the incoming
5161          * traffic.
5162          **/
5163         if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5164                 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5165                         lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5166         else
5167                 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5168                         lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
5169         phba->hbq_count = 1;
5170         lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
5171         /* Initially populate or replenish the HBQs */
5172         return 0;
5173 }
5174
5175 /**
5176  * lpfc_sli_config_port - Issue config port mailbox command
5177  * @phba: Pointer to HBA context object.
5178  * @sli_mode: sli mode - 2/3
5179  *
5180  * This function is called by the sli initialization code path
5181  * to issue config_port mailbox command. This function restarts the
5182  * HBA firmware and issues a config_port mailbox command to configure
5183  * the SLI interface in the sli mode specified by sli_mode
5184  * variable. The caller is not required to hold any locks.
5185  * The function returns 0 if successful, else returns negative error
5186  * code.
5187  **/
5188 int
5189 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
5190 {
5191         LPFC_MBOXQ_t *pmb;
5192         uint32_t resetcount = 0, rc = 0, done = 0;
5193
5194         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5195         if (!pmb) {
5196                 phba->link_state = LPFC_HBA_ERROR;
5197                 return -ENOMEM;
5198         }
5199
5200         phba->sli_rev = sli_mode;
5201         while (resetcount < 2 && !done) {
5202                 spin_lock_irq(&phba->hbalock);
5203                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5204                 spin_unlock_irq(&phba->hbalock);
5205                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
5206                 lpfc_sli_brdrestart(phba);
5207                 rc = lpfc_sli_chipset_init(phba);
5208                 if (rc)
5209                         break;
5210
5211                 spin_lock_irq(&phba->hbalock);
5212                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5213                 spin_unlock_irq(&phba->hbalock);
5214                 resetcount++;
5215
5216                 /* Call pre CONFIG_PORT mailbox command initialization.  A
5217                  * value of 0 means the call was successful.  Any other
5218                  * nonzero value is a failure, but if ERESTART is returned,
5219                  * the driver may reset the HBA and try again.
5220                  */
5221                 rc = lpfc_config_port_prep(phba);
5222                 if (rc == -ERESTART) {
5223                         phba->link_state = LPFC_LINK_UNKNOWN;
5224                         continue;
5225                 } else if (rc)
5226                         break;
5227
5228                 phba->link_state = LPFC_INIT_MBX_CMDS;
5229                 lpfc_config_port(phba, pmb);
5230                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5231                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5232                                         LPFC_SLI3_HBQ_ENABLED |
5233                                         LPFC_SLI3_CRP_ENABLED |
5234                                         LPFC_SLI3_DSS_ENABLED);
5235                 if (rc != MBX_SUCCESS) {
5236                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5237                                 "0442 Adapter failed to init, mbxCmd x%x "
5238                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
5239                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
5240                         spin_lock_irq(&phba->hbalock);
5241                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
5242                         spin_unlock_irq(&phba->hbalock);
5243                         rc = -ENXIO;
5244                 } else {
5245                         /* Allow asynchronous mailbox command to go through */
5246                         spin_lock_irq(&phba->hbalock);
5247                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5248                         spin_unlock_irq(&phba->hbalock);
5249                         done = 1;
5250
5251                         if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5252                             (pmb->u.mb.un.varCfgPort.gasabt == 0))
5253                                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5254                                         "3110 Port did not grant ASABT\n");
5255                 }
5256         }
5257         if (!done) {
5258                 rc = -EINVAL;
5259                 goto do_prep_failed;
5260         }
5261         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5262                 if (!pmb->u.mb.un.varCfgPort.cMA) {
5263                         rc = -ENXIO;
5264                         goto do_prep_failed;
5265                 }
5266                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
5267                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
5268                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5269                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5270                                 phba->max_vpi : phba->max_vports;
5271
5272                 } else
5273                         phba->max_vpi = 0;
5274                 if (pmb->u.mb.un.varCfgPort.gerbm)
5275                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
5276                 if (pmb->u.mb.un.varCfgPort.gcrp)
5277                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
5278
5279                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5280                 phba->port_gp = phba->mbox->us.s3_pgp.port;
5281
5282                 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5283                         if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5284                                 phba->cfg_enable_bg = 0;
5285                                 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5286                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5287                                                 "0443 Adapter did not grant "
5288                                                 "BlockGuard\n");
5289                         }
5290                 }
5291         } else {
5292                 phba->hbq_get = NULL;
5293                 phba->port_gp = phba->mbox->us.s2.port;
5294                 phba->max_vpi = 0;
5295         }
5296 do_prep_failed:
5297         mempool_free(pmb, phba->mbox_mem_pool);
5298         return rc;
5299 }
5300
5301
5302 /**
5303  * lpfc_sli_hba_setup - SLI initialization function
5304  * @phba: Pointer to HBA context object.
5305  *
5306  * This function is the main SLI initialization function. This function
5307  * is called by the HBA initialization code, HBA reset code and HBA
5308  * error attention handler code. Caller is not required to hold any
5309  * locks. This function issues config_port mailbox command to configure
5310  * the SLI, setup iocb rings and HBQ rings. In the end the function
5311  * calls the config_port_post function to issue init_link mailbox
5312  * command and to start the discovery. The function will return zero
5313  * if successful, else it will return negative error code.
5314  **/
5315 int
5316 lpfc_sli_hba_setup(struct lpfc_hba *phba)
5317 {
5318         uint32_t rc;
5319         int  mode = 3, i;
5320         int longs;
5321
5322         switch (phba->cfg_sli_mode) {
5323         case 2:
5324                 if (phba->cfg_enable_npiv) {
5325                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5326                                 "1824 NPIV enabled: Override sli_mode "
5327                                 "parameter (%d) to auto (0).\n",
5328                                 phba->cfg_sli_mode);
5329                         break;
5330                 }
5331                 mode = 2;
5332                 break;
5333         case 0:
5334         case 3:
5335                 break;
5336         default:
5337                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5338                                 "1819 Unrecognized sli_mode parameter: %d.\n",
5339                                 phba->cfg_sli_mode);
5340
5341                 break;
5342         }
5343         phba->fcp_embed_io = 0; /* SLI4 FC support only */
5344
5345         rc = lpfc_sli_config_port(phba, mode);
5346
5347         if (rc && phba->cfg_sli_mode == 3)
5348                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5349                                 "1820 Unable to select SLI-3.  "
5350                                 "Not supported by adapter.\n");
5351         if (rc && mode != 2)
5352                 rc = lpfc_sli_config_port(phba, 2);
5353         else if (rc && mode == 2)
5354                 rc = lpfc_sli_config_port(phba, 3);
5355         if (rc)
5356                 goto lpfc_sli_hba_setup_error;
5357
5358         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5359         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5360                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5361                 if (!rc) {
5362                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5363                                         "2709 This device supports "
5364                                         "Advanced Error Reporting (AER)\n");
5365                         spin_lock_irq(&phba->hbalock);
5366                         phba->hba_flag |= HBA_AER_ENABLED;
5367                         spin_unlock_irq(&phba->hbalock);
5368                 } else {
5369                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5370                                         "2708 This device does not support "
5371                                         "Advanced Error Reporting (AER): %d\n",
5372                                         rc);
5373                         phba->cfg_aer_support = 0;
5374                 }
5375         }
5376
5377         if (phba->sli_rev == 3) {
5378                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5379                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
5380         } else {
5381                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5382                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
5383                 phba->sli3_options = 0;
5384         }
5385
5386         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5387                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5388                         phba->sli_rev, phba->max_vpi);
5389         rc = lpfc_sli_ring_map(phba);
5390
5391         if (rc)
5392                 goto lpfc_sli_hba_setup_error;
5393
5394         /* Initialize VPIs. */
5395         if (phba->sli_rev == LPFC_SLI_REV3) {
5396                 /*
5397                  * The VPI bitmask and physical ID array are allocated
5398                  * and initialized once only - at driver load.  A port
5399                  * reset doesn't need to reinitialize this memory.
5400                  */
5401                 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5402                         longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5403                         phba->vpi_bmask = kcalloc(longs,
5404                                                   sizeof(unsigned long),
5405                                                   GFP_KERNEL);
5406                         if (!phba->vpi_bmask) {
5407                                 rc = -ENOMEM;
5408                                 goto lpfc_sli_hba_setup_error;
5409                         }
5410
5411                         phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5412                                                 sizeof(uint16_t),
5413                                                 GFP_KERNEL);
5414                         if (!phba->vpi_ids) {
5415                                 kfree(phba->vpi_bmask);
5416                                 rc = -ENOMEM;
5417                                 goto lpfc_sli_hba_setup_error;
5418                         }
5419                         for (i = 0; i < phba->max_vpi; i++)
5420                                 phba->vpi_ids[i] = i;
5421                 }
5422         }
5423
5424         /* Init HBQs */
5425         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5426                 rc = lpfc_sli_hbq_setup(phba);
5427                 if (rc)
5428                         goto lpfc_sli_hba_setup_error;
5429         }
5430         spin_lock_irq(&phba->hbalock);
5431         phba->sli.sli_flag |= LPFC_PROCESS_LA;
5432         spin_unlock_irq(&phba->hbalock);
5433
5434         rc = lpfc_config_port_post(phba);
5435         if (rc)
5436                 goto lpfc_sli_hba_setup_error;
5437
5438         return rc;
5439
5440 lpfc_sli_hba_setup_error:
5441         phba->link_state = LPFC_HBA_ERROR;
5442         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5443                         "0445 Firmware initialization failed\n");
5444         return rc;
5445 }
5446
5447 /**
5448  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5449  * @phba: Pointer to HBA context object.
5450  *
5451  * This function issue a dump mailbox command to read config region
5452  * 23 and parse the records in the region and populate driver
5453  * data structure.
5454  **/
5455 static int
5456 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
5457 {
5458         LPFC_MBOXQ_t *mboxq;
5459         struct lpfc_dmabuf *mp;
5460         struct lpfc_mqe *mqe;
5461         uint32_t data_length;
5462         int rc;
5463
5464         /* Program the default value of vlan_id and fc_map */
5465         phba->valid_vlan = 0;
5466         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5467         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5468         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5469
5470         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5471         if (!mboxq)
5472                 return -ENOMEM;
5473
5474         mqe = &mboxq->u.mqe;
5475         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5476                 rc = -ENOMEM;
5477                 goto out_free_mboxq;
5478         }
5479
5480         mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
5481         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5482
5483         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5484                         "(%d):2571 Mailbox cmd x%x Status x%x "
5485                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5486                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5487                         "CQ: x%x x%x x%x x%x\n",
5488                         mboxq->vport ? mboxq->vport->vpi : 0,
5489                         bf_get(lpfc_mqe_command, mqe),
5490                         bf_get(lpfc_mqe_status, mqe),
5491                         mqe->un.mb_words[0], mqe->un.mb_words[1],
5492                         mqe->un.mb_words[2], mqe->un.mb_words[3],
5493                         mqe->un.mb_words[4], mqe->un.mb_words[5],
5494                         mqe->un.mb_words[6], mqe->un.mb_words[7],
5495                         mqe->un.mb_words[8], mqe->un.mb_words[9],
5496                         mqe->un.mb_words[10], mqe->un.mb_words[11],
5497                         mqe->un.mb_words[12], mqe->un.mb_words[13],
5498                         mqe->un.mb_words[14], mqe->un.mb_words[15],
5499                         mqe->un.mb_words[16], mqe->un.mb_words[50],
5500                         mboxq->mcqe.word0,
5501                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5502                         mboxq->mcqe.trailer);
5503
5504         if (rc) {
5505                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5506                 kfree(mp);
5507                 rc = -EIO;
5508                 goto out_free_mboxq;
5509         }
5510         data_length = mqe->un.mb_words[5];
5511         if (data_length > DMP_RGN23_SIZE) {
5512                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5513                 kfree(mp);
5514                 rc = -EIO;
5515                 goto out_free_mboxq;
5516         }
5517
5518         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5519         lpfc_mbuf_free(phba, mp->virt, mp->phys);
5520         kfree(mp);
5521         rc = 0;
5522
5523 out_free_mboxq:
5524         mempool_free(mboxq, phba->mbox_mem_pool);
5525         return rc;
5526 }
5527
5528 /**
5529  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5530  * @phba: pointer to lpfc hba data structure.
5531  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5532  * @vpd: pointer to the memory to hold resulting port vpd data.
5533  * @vpd_size: On input, the number of bytes allocated to @vpd.
5534  *            On output, the number of data bytes in @vpd.
5535  *
5536  * This routine executes a READ_REV SLI4 mailbox command.  In
5537  * addition, this routine gets the port vpd data.
5538  *
5539  * Return codes
5540  *      0 - successful
5541  *      -ENOMEM - could not allocated memory.
5542  **/
5543 static int
5544 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5545                     uint8_t *vpd, uint32_t *vpd_size)
5546 {
5547         int rc = 0;
5548         uint32_t dma_size;
5549         struct lpfc_dmabuf *dmabuf;
5550         struct lpfc_mqe *mqe;
5551
5552         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5553         if (!dmabuf)
5554                 return -ENOMEM;
5555
5556         /*
5557          * Get a DMA buffer for the vpd data resulting from the READ_REV
5558          * mailbox command.
5559          */
5560         dma_size = *vpd_size;
5561         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5562                                           &dmabuf->phys, GFP_KERNEL);
5563         if (!dmabuf->virt) {
5564                 kfree(dmabuf);
5565                 return -ENOMEM;
5566         }
5567
5568         /*
5569          * The SLI4 implementation of READ_REV conflicts at word1,
5570          * bits 31:16 and SLI4 adds vpd functionality not present
5571          * in SLI3.  This code corrects the conflicts.
5572          */
5573         lpfc_read_rev(phba, mboxq);
5574         mqe = &mboxq->u.mqe;
5575         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5576         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5577         mqe->un.read_rev.word1 &= 0x0000FFFF;
5578         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5579         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5580
5581         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5582         if (rc) {
5583                 dma_free_coherent(&phba->pcidev->dev, dma_size,
5584                                   dmabuf->virt, dmabuf->phys);
5585                 kfree(dmabuf);
5586                 return -EIO;
5587         }
5588
5589         /*
5590          * The available vpd length cannot be bigger than the
5591          * DMA buffer passed to the port.  Catch the less than
5592          * case and update the caller's size.
5593          */
5594         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5595                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5596
5597         memcpy(vpd, dmabuf->virt, *vpd_size);
5598
5599         dma_free_coherent(&phba->pcidev->dev, dma_size,
5600                           dmabuf->virt, dmabuf->phys);
5601         kfree(dmabuf);
5602         return 0;
5603 }
5604
5605 /**
5606  * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
5607  * @phba: pointer to lpfc hba data structure.
5608  *
5609  * This routine retrieves SLI4 device physical port name this PCI function
5610  * is attached to.
5611  *
5612  * Return codes
5613  *      0 - successful
5614  *      otherwise - failed to retrieve controller attributes
5615  **/
5616 static int
5617 lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
5618 {
5619         LPFC_MBOXQ_t *mboxq;
5620         struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5621         struct lpfc_controller_attribute *cntl_attr;
5622         void *virtaddr = NULL;
5623         uint32_t alloclen, reqlen;
5624         uint32_t shdr_status, shdr_add_status;
5625         union lpfc_sli4_cfg_shdr *shdr;
5626         int rc;
5627
5628         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5629         if (!mboxq)
5630                 return -ENOMEM;
5631
5632         /* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
5633         reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5634         alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5635                         LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5636                         LPFC_SLI4_MBX_NEMBED);
5637
5638         if (alloclen < reqlen) {
5639                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5640                                 "3084 Allocated DMA memory size (%d) is "
5641                                 "less than the requested DMA memory size "
5642                                 "(%d)\n", alloclen, reqlen);
5643                 rc = -ENOMEM;
5644                 goto out_free_mboxq;
5645         }
5646         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5647         virtaddr = mboxq->sge_array->addr[0];
5648         mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5649         shdr = &mbx_cntl_attr->cfg_shdr;
5650         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5651         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5652         if (shdr_status || shdr_add_status || rc) {
5653                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5654                                 "3085 Mailbox x%x (x%x/x%x) failed, "
5655                                 "rc:x%x, status:x%x, add_status:x%x\n",
5656                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5657                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5658                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5659                                 rc, shdr_status, shdr_add_status);
5660                 rc = -ENXIO;
5661                 goto out_free_mboxq;
5662         }
5663
5664         cntl_attr = &mbx_cntl_attr->cntl_attr;
5665         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5666         phba->sli4_hba.lnk_info.lnk_tp =
5667                 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5668         phba->sli4_hba.lnk_info.lnk_no =
5669                 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5670
5671         memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
5672         strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
5673                 sizeof(phba->BIOSVersion));
5674
5675         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5676                         "3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s\n",
5677                         phba->sli4_hba.lnk_info.lnk_tp,
5678                         phba->sli4_hba.lnk_info.lnk_no,
5679                         phba->BIOSVersion);
5680 out_free_mboxq:
5681         if (rc != MBX_TIMEOUT) {
5682                 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5683                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
5684                 else
5685                         mempool_free(mboxq, phba->mbox_mem_pool);
5686         }
5687         return rc;
5688 }
5689
5690 /**
5691  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5692  * @phba: pointer to lpfc hba data structure.
5693  *
5694  * This routine retrieves SLI4 device physical port name this PCI function
5695  * is attached to.
5696  *
5697  * Return codes
5698  *      0 - successful
5699  *      otherwise - failed to retrieve physical port name
5700  **/
5701 static int
5702 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5703 {
5704         LPFC_MBOXQ_t *mboxq;
5705         struct lpfc_mbx_get_port_name *get_port_name;
5706         uint32_t shdr_status, shdr_add_status;
5707         union lpfc_sli4_cfg_shdr *shdr;
5708         char cport_name = 0;
5709         int rc;
5710
5711         /* We assume nothing at this point */
5712         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5713         phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5714
5715         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5716         if (!mboxq)
5717                 return -ENOMEM;
5718         /* obtain link type and link number via READ_CONFIG */
5719         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5720         lpfc_sli4_read_config(phba);
5721         if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5722                 goto retrieve_ppname;
5723
5724         /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5725         rc = lpfc_sli4_get_ctl_attr(phba);
5726         if (rc)
5727                 goto out_free_mboxq;
5728
5729 retrieve_ppname:
5730         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5731                 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5732                 sizeof(struct lpfc_mbx_get_port_name) -
5733                 sizeof(struct lpfc_sli4_cfg_mhdr),
5734                 LPFC_SLI4_MBX_EMBED);
5735         get_port_name = &mboxq->u.mqe.un.get_port_name;
5736         shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5737         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5738         bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5739                 phba->sli4_hba.lnk_info.lnk_tp);
5740         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5741         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5742         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5743         if (shdr_status || shdr_add_status || rc) {
5744                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5745                                 "3087 Mailbox x%x (x%x/x%x) failed: "
5746                                 "rc:x%x, status:x%x, add_status:x%x\n",
5747                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5748                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5749                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5750                                 rc, shdr_status, shdr_add_status);
5751                 rc = -ENXIO;
5752                 goto out_free_mboxq;
5753         }
5754         switch (phba->sli4_hba.lnk_info.lnk_no) {
5755         case LPFC_LINK_NUMBER_0:
5756                 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5757                                 &get_port_name->u.response);
5758                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5759                 break;
5760         case LPFC_LINK_NUMBER_1:
5761                 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5762                                 &get_port_name->u.response);
5763                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5764                 break;
5765         case LPFC_LINK_NUMBER_2:
5766                 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5767                                 &get_port_name->u.response);
5768                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5769                 break;
5770         case LPFC_LINK_NUMBER_3:
5771                 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5772                                 &get_port_name->u.response);
5773                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5774                 break;
5775         default:
5776                 break;
5777         }
5778
5779         if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5780                 phba->Port[0] = cport_name;
5781                 phba->Port[1] = '\0';
5782                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5783                                 "3091 SLI get port name: %s\n", phba->Port);
5784         }
5785
5786 out_free_mboxq:
5787         if (rc != MBX_TIMEOUT) {
5788                 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5789                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
5790                 else
5791                         mempool_free(mboxq, phba->mbox_mem_pool);
5792         }
5793         return rc;
5794 }
5795
5796 /**
5797  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5798  * @phba: pointer to lpfc hba data structure.
5799  *
5800  * This routine is called to explicitly arm the SLI4 device's completion and
5801  * event queues
5802  **/
5803 static void
5804 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5805 {
5806         int qidx;
5807         struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
5808         struct lpfc_sli4_hdw_queue *qp;
5809         struct lpfc_queue *eq;
5810
5811         sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
5812         sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
5813         if (sli4_hba->nvmels_cq)
5814                 sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
5815                                            LPFC_QUEUE_REARM);
5816
5817         if (sli4_hba->hdwq) {
5818                 /* Loop thru all Hardware Queues */
5819                 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
5820                         qp = &sli4_hba->hdwq[qidx];
5821                         /* ARM the corresponding CQ */
5822                         sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
5823                                                 LPFC_QUEUE_REARM);
5824                 }
5825
5826                 /* Loop thru all IRQ vectors */
5827                 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
5828                         eq = sli4_hba->hba_eq_hdl[qidx].eq;
5829                         /* ARM the corresponding EQ */
5830                         sli4_hba->sli4_write_eq_db(phba, eq,
5831                                                    0, LPFC_QUEUE_REARM);
5832                 }
5833         }
5834
5835         if (phba->nvmet_support) {
5836                 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5837                         sli4_hba->sli4_write_cq_db(phba,
5838                                 sli4_hba->nvmet_cqset[qidx], 0,
5839                                 LPFC_QUEUE_REARM);
5840                 }
5841         }
5842 }
5843
5844 /**
5845  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5846  * @phba: Pointer to HBA context object.
5847  * @type: The resource extent type.
5848  * @extnt_count: buffer to hold port available extent count.
5849  * @extnt_size: buffer to hold element count per extent.
5850  *
5851  * This function calls the port and retrievs the number of available
5852  * extents and their size for a particular extent type.
5853  *
5854  * Returns: 0 if successful.  Nonzero otherwise.
5855  **/
5856 int
5857 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5858                                uint16_t *extnt_count, uint16_t *extnt_size)
5859 {
5860         int rc = 0;
5861         uint32_t length;
5862         uint32_t mbox_tmo;
5863         struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5864         LPFC_MBOXQ_t *mbox;
5865
5866         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5867         if (!mbox)
5868                 return -ENOMEM;
5869
5870         /* Find out how many extents are available for this resource type */
5871         length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5872                   sizeof(struct lpfc_sli4_cfg_mhdr));
5873         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5874                          LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5875                          length, LPFC_SLI4_MBX_EMBED);
5876
5877         /* Send an extents count of 0 - the GET doesn't use it. */
5878         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5879                                         LPFC_SLI4_MBX_EMBED);
5880         if (unlikely(rc)) {
5881                 rc = -EIO;
5882                 goto err_exit;
5883         }
5884
5885         if (!phba->sli4_hba.intr_enable)
5886                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5887         else {
5888                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5889                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5890         }
5891         if (unlikely(rc)) {
5892                 rc = -EIO;
5893                 goto err_exit;
5894         }
5895
5896         rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5897         if (bf_get(lpfc_mbox_hdr_status,
5898                    &rsrc_info->header.cfg_shdr.response)) {
5899                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5900                                 "2930 Failed to get resource extents "
5901                                 "Status 0x%x Add'l Status 0x%x\n",
5902                                 bf_get(lpfc_mbox_hdr_status,
5903                                        &rsrc_info->header.cfg_shdr.response),
5904                                 bf_get(lpfc_mbox_hdr_add_status,
5905                                        &rsrc_info->header.cfg_shdr.response));
5906                 rc = -EIO;
5907                 goto err_exit;
5908         }
5909
5910         *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5911                               &rsrc_info->u.rsp);
5912         *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5913                              &rsrc_info->u.rsp);
5914
5915         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5916                         "3162 Retrieved extents type-%d from port: count:%d, "
5917                         "size:%d\n", type, *extnt_count, *extnt_size);
5918
5919 err_exit:
5920         mempool_free(mbox, phba->mbox_mem_pool);
5921         return rc;
5922 }
5923
5924 /**
5925  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5926  * @phba: Pointer to HBA context object.
5927  * @type: The extent type to check.
5928  *
5929  * This function reads the current available extents from the port and checks
5930  * if the extent count or extent size has changed since the last access.
5931  * Callers use this routine post port reset to understand if there is a
5932  * extent reprovisioning requirement.
5933  *
5934  * Returns:
5935  *   -Error: error indicates problem.
5936  *   1: Extent count or size has changed.
5937  *   0: No changes.
5938  **/
5939 static int
5940 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5941 {
5942         uint16_t curr_ext_cnt, rsrc_ext_cnt;
5943         uint16_t size_diff, rsrc_ext_size;
5944         int rc = 0;
5945         struct lpfc_rsrc_blks *rsrc_entry;
5946         struct list_head *rsrc_blk_list = NULL;
5947
5948         size_diff = 0;
5949         curr_ext_cnt = 0;
5950         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5951                                             &rsrc_ext_cnt,
5952                                             &rsrc_ext_size);
5953         if (unlikely(rc))
5954                 return -EIO;
5955
5956         switch (type) {
5957         case LPFC_RSC_TYPE_FCOE_RPI:
5958                 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5959                 break;
5960         case LPFC_RSC_TYPE_FCOE_VPI:
5961                 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5962                 break;
5963         case LPFC_RSC_TYPE_FCOE_XRI:
5964                 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5965                 break;
5966         case LPFC_RSC_TYPE_FCOE_VFI:
5967                 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5968                 break;
5969         default:
5970                 break;
5971         }
5972
5973         list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5974                 curr_ext_cnt++;
5975                 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5976                         size_diff++;
5977         }
5978
5979         if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5980                 rc = 1;
5981
5982         return rc;
5983 }
5984
5985 /**
5986  * lpfc_sli4_cfg_post_extnts -
5987  * @phba: Pointer to HBA context object.
5988  * @extnt_cnt: number of available extents.
5989  * @type: the extent type (rpi, xri, vfi, vpi).
5990  * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5991  * @mbox: pointer to the caller's allocated mailbox structure.
5992  *
5993  * This function executes the extents allocation request.  It also
5994  * takes care of the amount of memory needed to allocate or get the
5995  * allocated extents. It is the caller's responsibility to evaluate
5996  * the response.
5997  *
5998  * Returns:
5999  *   -Error:  Error value describes the condition found.
6000  *   0: if successful
6001  **/
6002 static int
6003 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6004                           uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
6005 {
6006         int rc = 0;
6007         uint32_t req_len;
6008         uint32_t emb_len;
6009         uint32_t alloc_len, mbox_tmo;
6010
6011         /* Calculate the total requested length of the dma memory */
6012         req_len = extnt_cnt * sizeof(uint16_t);
6013
6014         /*
6015          * Calculate the size of an embedded mailbox.  The uint32_t
6016          * accounts for extents-specific word.
6017          */
6018         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6019                 sizeof(uint32_t);
6020
6021         /*
6022          * Presume the allocation and response will fit into an embedded
6023          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6024          */
6025         *emb = LPFC_SLI4_MBX_EMBED;
6026         if (req_len > emb_len) {
6027                 req_len = extnt_cnt * sizeof(uint16_t) +
6028                         sizeof(union lpfc_sli4_cfg_shdr) +
6029                         sizeof(uint32_t);
6030                 *emb = LPFC_SLI4_MBX_NEMBED;
6031         }
6032
6033         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6034                                      LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
6035                                      req_len, *emb);
6036         if (alloc_len < req_len) {
6037                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6038                         "2982 Allocated DMA memory size (x%x) is "
6039                         "less than the requested DMA memory "
6040                         "size (x%x)\n", alloc_len, req_len);
6041                 return -ENOMEM;
6042         }
6043         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6044         if (unlikely(rc))
6045                 return -EIO;
6046
6047         if (!phba->sli4_hba.intr_enable)
6048                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6049         else {
6050                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6051                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6052         }
6053
6054         if (unlikely(rc))
6055                 rc = -EIO;
6056         return rc;
6057 }
6058
6059 /**
6060  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
6061  * @phba: Pointer to HBA context object.
6062  * @type:  The resource extent type to allocate.
6063  *
6064  * This function allocates the number of elements for the specified
6065  * resource type.
6066  **/
6067 static int
6068 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
6069 {
6070         bool emb = false;
6071         uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
6072         uint16_t rsrc_id, rsrc_start, j, k;
6073         uint16_t *ids;
6074         int i, rc;
6075         unsigned long longs;
6076         unsigned long *bmask;
6077         struct lpfc_rsrc_blks *rsrc_blks;
6078         LPFC_MBOXQ_t *mbox;
6079         uint32_t length;
6080         struct lpfc_id_range *id_array = NULL;
6081         void *virtaddr = NULL;
6082         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6083         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6084         struct list_head *ext_blk_list;
6085
6086         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6087                                             &rsrc_cnt,
6088                                             &rsrc_size);
6089         if (unlikely(rc))
6090                 return -EIO;
6091
6092         if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
6093                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6094                         "3009 No available Resource Extents "
6095                         "for resource type 0x%x: Count: 0x%x, "
6096                         "Size 0x%x\n", type, rsrc_cnt,
6097                         rsrc_size);
6098                 return -ENOMEM;
6099         }
6100
6101         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
6102                         "2903 Post resource extents type-0x%x: "
6103                         "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6104
6105         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6106         if (!mbox)
6107                 return -ENOMEM;
6108
6109         rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6110         if (unlikely(rc)) {
6111                 rc = -EIO;
6112                 goto err_exit;
6113         }
6114
6115         /*
6116          * Figure out where the response is located.  Then get local pointers
6117          * to the response data.  The port does not guarantee to respond to
6118          * all extents counts request so update the local variable with the
6119          * allocated count from the port.
6120          */
6121         if (emb == LPFC_SLI4_MBX_EMBED) {
6122                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6123                 id_array = &rsrc_ext->u.rsp.id[0];
6124                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6125         } else {
6126                 virtaddr = mbox->sge_array->addr[0];
6127                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6128                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6129                 id_array = &n_rsrc->id;
6130         }
6131
6132         longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6133         rsrc_id_cnt = rsrc_cnt * rsrc_size;
6134
6135         /*
6136          * Based on the resource size and count, correct the base and max
6137          * resource values.
6138          */
6139         length = sizeof(struct lpfc_rsrc_blks);
6140         switch (type) {
6141         case LPFC_RSC_TYPE_FCOE_RPI:
6142                 phba->sli4_hba.rpi_bmask = kcalloc(longs,
6143                                                    sizeof(unsigned long),
6144                                                    GFP_KERNEL);
6145                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6146                         rc = -ENOMEM;
6147                         goto err_exit;
6148                 }
6149                 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
6150                                                  sizeof(uint16_t),
6151                                                  GFP_KERNEL);
6152                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6153                         kfree(phba->sli4_hba.rpi_bmask);
6154                         rc = -ENOMEM;
6155                         goto err_exit;
6156                 }
6157
6158                 /*
6159                  * The next_rpi was initialized with the maximum available
6160                  * count but the port may allocate a smaller number.  Catch
6161                  * that case and update the next_rpi.
6162                  */
6163                 phba->sli4_hba.next_rpi = rsrc_id_cnt;
6164
6165                 /* Initialize local ptrs for common extent processing later. */
6166                 bmask = phba->sli4_hba.rpi_bmask;
6167                 ids = phba->sli4_hba.rpi_ids;
6168                 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6169                 break;
6170         case LPFC_RSC_TYPE_FCOE_VPI:
6171                 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6172                                           GFP_KERNEL);
6173                 if (unlikely(!phba->vpi_bmask)) {
6174                         rc = -ENOMEM;
6175                         goto err_exit;
6176                 }
6177                 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
6178                                          GFP_KERNEL);
6179                 if (unlikely(!phba->vpi_ids)) {
6180                         kfree(phba->vpi_bmask);
6181                         rc = -ENOMEM;
6182                         goto err_exit;
6183                 }
6184
6185                 /* Initialize local ptrs for common extent processing later. */
6186                 bmask = phba->vpi_bmask;
6187                 ids = phba->vpi_ids;
6188                 ext_blk_list = &phba->lpfc_vpi_blk_list;
6189                 break;
6190         case LPFC_RSC_TYPE_FCOE_XRI:
6191                 phba->sli4_hba.xri_bmask = kcalloc(longs,
6192                                                    sizeof(unsigned long),
6193                                                    GFP_KERNEL);
6194                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6195                         rc = -ENOMEM;
6196                         goto err_exit;
6197                 }
6198                 phba->sli4_hba.max_cfg_param.xri_used = 0;
6199                 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
6200                                                  sizeof(uint16_t),
6201                                                  GFP_KERNEL);
6202                 if (unlikely(!phba->sli4_hba.xri_ids)) {
6203                         kfree(phba->sli4_hba.xri_bmask);
6204                         rc = -ENOMEM;
6205                         goto err_exit;
6206                 }
6207
6208                 /* Initialize local ptrs for common extent processing later. */
6209                 bmask = phba->sli4_hba.xri_bmask;
6210                 ids = phba->sli4_hba.xri_ids;
6211                 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6212                 break;
6213         case LPFC_RSC_TYPE_FCOE_VFI:
6214                 phba->sli4_hba.vfi_bmask = kcalloc(longs,
6215                                                    sizeof(unsigned long),
6216                                                    GFP_KERNEL);
6217                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6218                         rc = -ENOMEM;
6219                         goto err_exit;
6220                 }
6221                 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
6222                                                  sizeof(uint16_t),
6223                                                  GFP_KERNEL);
6224                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6225                         kfree(phba->sli4_hba.vfi_bmask);
6226                         rc = -ENOMEM;
6227                         goto err_exit;
6228                 }
6229
6230                 /* Initialize local ptrs for common extent processing later. */
6231                 bmask = phba->sli4_hba.vfi_bmask;
6232                 ids = phba->sli4_hba.vfi_ids;
6233                 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6234                 break;
6235         default:
6236                 /* Unsupported Opcode.  Fail call. */
6237                 id_array = NULL;
6238                 bmask = NULL;
6239                 ids = NULL;
6240                 ext_blk_list = NULL;
6241                 goto err_exit;
6242         }
6243
6244         /*
6245          * Complete initializing the extent configuration with the
6246          * allocated ids assigned to this function.  The bitmask serves
6247          * as an index into the array and manages the available ids.  The
6248          * array just stores the ids communicated to the port via the wqes.
6249          */
6250         for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6251                 if ((i % 2) == 0)
6252                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6253                                          &id_array[k]);
6254                 else
6255                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6256                                          &id_array[k]);
6257
6258                 rsrc_blks = kzalloc(length, GFP_KERNEL);
6259                 if (unlikely(!rsrc_blks)) {
6260                         rc = -ENOMEM;
6261                         kfree(bmask);
6262                         kfree(ids);
6263                         goto err_exit;
6264                 }
6265                 rsrc_blks->rsrc_start = rsrc_id;
6266                 rsrc_blks->rsrc_size = rsrc_size;
6267                 list_add_tail(&rsrc_blks->list, ext_blk_list);
6268                 rsrc_start = rsrc_id;
6269                 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6270                         phba->sli4_hba.io_xri_start = rsrc_start +
6271                                 lpfc_sli4_get_iocb_cnt(phba);
6272                 }
6273
6274                 while (rsrc_id < (rsrc_start + rsrc_size)) {
6275                         ids[j] = rsrc_id;
6276                         rsrc_id++;
6277                         j++;
6278                 }
6279                 /* Entire word processed.  Get next word.*/
6280                 if ((i % 2) == 1)
6281                         k++;
6282         }
6283  err_exit:
6284         lpfc_sli4_mbox_cmd_free(phba, mbox);
6285         return rc;
6286 }
6287
6288
6289
6290 /**
6291  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6292  * @phba: Pointer to HBA context object.
6293  * @type: the extent's type.
6294  *
6295  * This function deallocates all extents of a particular resource type.
6296  * SLI4 does not allow for deallocating a particular extent range.  It
6297  * is the caller's responsibility to release all kernel memory resources.
6298  **/
6299 static int
6300 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6301 {
6302         int rc;
6303         uint32_t length, mbox_tmo = 0;
6304         LPFC_MBOXQ_t *mbox;
6305         struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6306         struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6307
6308         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6309         if (!mbox)
6310                 return -ENOMEM;
6311
6312         /*
6313          * This function sends an embedded mailbox because it only sends the
6314          * the resource type.  All extents of this type are released by the
6315          * port.
6316          */
6317         length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6318                   sizeof(struct lpfc_sli4_cfg_mhdr));
6319         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6320                          LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6321                          length, LPFC_SLI4_MBX_EMBED);
6322
6323         /* Send an extents count of 0 - the dealloc doesn't use it. */
6324         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6325                                         LPFC_SLI4_MBX_EMBED);
6326         if (unlikely(rc)) {
6327                 rc = -EIO;
6328                 goto out_free_mbox;
6329         }
6330         if (!phba->sli4_hba.intr_enable)
6331                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6332         else {
6333                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6334                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6335         }
6336         if (unlikely(rc)) {
6337                 rc = -EIO;
6338                 goto out_free_mbox;
6339         }
6340
6341         dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6342         if (bf_get(lpfc_mbox_hdr_status,
6343                    &dealloc_rsrc->header.cfg_shdr.response)) {
6344                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6345                                 "2919 Failed to release resource extents "
6346                                 "for type %d - Status 0x%x Add'l Status 0x%x. "
6347                                 "Resource memory not released.\n",
6348                                 type,
6349                                 bf_get(lpfc_mbox_hdr_status,
6350                                     &dealloc_rsrc->header.cfg_shdr.response),
6351                                 bf_get(lpfc_mbox_hdr_add_status,
6352                                     &dealloc_rsrc->header.cfg_shdr.response));
6353                 rc = -EIO;
6354                 goto out_free_mbox;
6355         }
6356
6357         /* Release kernel memory resources for the specific type. */
6358         switch (type) {
6359         case LPFC_RSC_TYPE_FCOE_VPI:
6360                 kfree(phba->vpi_bmask);
6361                 kfree(phba->vpi_ids);
6362                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6363                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6364                                     &phba->lpfc_vpi_blk_list, list) {
6365                         list_del_init(&rsrc_blk->list);
6366                         kfree(rsrc_blk);
6367                 }
6368                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6369                 break;
6370         case LPFC_RSC_TYPE_FCOE_XRI:
6371                 kfree(phba->sli4_hba.xri_bmask);
6372                 kfree(phba->sli4_hba.xri_ids);
6373                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6374                                     &phba->sli4_hba.lpfc_xri_blk_list, list) {
6375                         list_del_init(&rsrc_blk->list);
6376                         kfree(rsrc_blk);
6377                 }
6378                 break;
6379         case LPFC_RSC_TYPE_FCOE_VFI:
6380                 kfree(phba->sli4_hba.vfi_bmask);
6381                 kfree(phba->sli4_hba.vfi_ids);
6382                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6383                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6384                                     &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6385                         list_del_init(&rsrc_blk->list);
6386                         kfree(rsrc_blk);
6387                 }
6388                 break;
6389         case LPFC_RSC_TYPE_FCOE_RPI:
6390                 /* RPI bitmask and physical id array are cleaned up earlier. */
6391                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6392                                     &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6393                         list_del_init(&rsrc_blk->list);
6394                         kfree(rsrc_blk);
6395                 }
6396                 break;
6397         default:
6398                 break;
6399         }
6400
6401         bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6402
6403  out_free_mbox:
6404         mempool_free(mbox, phba->mbox_mem_pool);
6405         return rc;
6406 }
6407
6408 static void
6409 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6410                   uint32_t feature)
6411 {
6412         uint32_t len;
6413
6414         len = sizeof(struct lpfc_mbx_set_feature) -
6415                 sizeof(struct lpfc_sli4_cfg_mhdr);
6416         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6417                          LPFC_MBOX_OPCODE_SET_FEATURES, len,
6418                          LPFC_SLI4_MBX_EMBED);
6419
6420         switch (feature) {
6421         case LPFC_SET_UE_RECOVERY:
6422                 bf_set(lpfc_mbx_set_feature_UER,
6423                        &mbox->u.mqe.un.set_feature, 1);
6424                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6425                 mbox->u.mqe.un.set_feature.param_len = 8;
6426                 break;
6427         case LPFC_SET_MDS_DIAGS:
6428                 bf_set(lpfc_mbx_set_feature_mds,
6429                        &mbox->u.mqe.un.set_feature, 1);
6430                 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
6431                        &mbox->u.mqe.un.set_feature, 1);
6432                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6433                 mbox->u.mqe.un.set_feature.param_len = 8;
6434                 break;
6435         case LPFC_SET_DUAL_DUMP:
6436                 bf_set(lpfc_mbx_set_feature_dd,
6437                        &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6438                 bf_set(lpfc_mbx_set_feature_ddquery,
6439                        &mbox->u.mqe.un.set_feature, 0);
6440                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6441                 mbox->u.mqe.un.set_feature.param_len = 4;
6442                 break;
6443         }
6444
6445         return;
6446 }
6447
6448 /**
6449  * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6450  * @phba: Pointer to HBA context object.
6451  *
6452  * Disable FW logging into host memory on the adapter. To
6453  * be done before reading logs from the host memory.
6454  **/
6455 void
6456 lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6457 {
6458         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6459
6460         spin_lock_irq(&phba->hbalock);
6461         ras_fwlog->state = INACTIVE;
6462         spin_unlock_irq(&phba->hbalock);
6463
6464         /* Disable FW logging to host memory */
6465         writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6466                phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6467
6468         /* Wait 10ms for firmware to stop using DMA buffer */
6469         usleep_range(10 * 1000, 20 * 1000);
6470 }
6471
6472 /**
6473  * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6474  * @phba: Pointer to HBA context object.
6475  *
6476  * This function is called to free memory allocated for RAS FW logging
6477  * support in the driver.
6478  **/
6479 void
6480 lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6481 {
6482         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6483         struct lpfc_dmabuf *dmabuf, *next;
6484
6485         if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6486                 list_for_each_entry_safe(dmabuf, next,
6487                                     &ras_fwlog->fwlog_buff_list,
6488                                     list) {
6489                         list_del(&dmabuf->list);
6490                         dma_free_coherent(&phba->pcidev->dev,
6491                                           LPFC_RAS_MAX_ENTRY_SIZE,
6492                                           dmabuf->virt, dmabuf->phys);
6493                         kfree(dmabuf);
6494                 }
6495         }
6496
6497         if (ras_fwlog->lwpd.virt) {
6498                 dma_free_coherent(&phba->pcidev->dev,
6499                                   sizeof(uint32_t) * 2,
6500                                   ras_fwlog->lwpd.virt,
6501                                   ras_fwlog->lwpd.phys);
6502                 ras_fwlog->lwpd.virt = NULL;
6503         }
6504
6505         spin_lock_irq(&phba->hbalock);
6506         ras_fwlog->state = INACTIVE;
6507         spin_unlock_irq(&phba->hbalock);
6508 }
6509
6510 /**
6511  * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6512  * @phba: Pointer to HBA context object.
6513  * @fwlog_buff_count: Count of buffers to be created.
6514  *
6515  * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6516  * to update FW log is posted to the adapter.
6517  * Buffer count is calculated based on module param ras_fwlog_buffsize
6518  * Size of each buffer posted to FW is 64K.
6519  **/
6520
6521 static int
6522 lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6523                         uint32_t fwlog_buff_count)
6524 {
6525         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6526         struct lpfc_dmabuf *dmabuf;
6527         int rc = 0, i = 0;
6528
6529         /* Initialize List */
6530         INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6531
6532         /* Allocate memory for the LWPD */
6533         ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6534                                             sizeof(uint32_t) * 2,
6535                                             &ras_fwlog->lwpd.phys,
6536                                             GFP_KERNEL);
6537         if (!ras_fwlog->lwpd.virt) {
6538                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6539                                 "6185 LWPD Memory Alloc Failed\n");
6540
6541                 return -ENOMEM;
6542         }
6543
6544         ras_fwlog->fw_buffcount = fwlog_buff_count;
6545         for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6546                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6547                                  GFP_KERNEL);
6548                 if (!dmabuf) {
6549                         rc = -ENOMEM;
6550                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6551                                         "6186 Memory Alloc failed FW logging");
6552                         goto free_mem;
6553                 }
6554
6555                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6556                                                   LPFC_RAS_MAX_ENTRY_SIZE,
6557                                                   &dmabuf->phys, GFP_KERNEL);
6558                 if (!dmabuf->virt) {
6559                         kfree(dmabuf);
6560                         rc = -ENOMEM;
6561                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6562                                         "6187 DMA Alloc Failed FW logging");
6563                         goto free_mem;
6564                 }
6565                 dmabuf->buffer_tag = i;
6566                 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6567         }
6568
6569 free_mem:
6570         if (rc)
6571                 lpfc_sli4_ras_dma_free(phba);
6572
6573         return rc;
6574 }
6575
6576 /**
6577  * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6578  * @phba: pointer to lpfc hba data structure.
6579  * @pmb: pointer to the driver internal queue element for mailbox command.
6580  *
6581  * Completion handler for driver's RAS MBX command to the device.
6582  **/
6583 static void
6584 lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6585 {
6586         MAILBOX_t *mb;
6587         union lpfc_sli4_cfg_shdr *shdr;
6588         uint32_t shdr_status, shdr_add_status;
6589         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6590
6591         mb = &pmb->u.mb;
6592
6593         shdr = (union lpfc_sli4_cfg_shdr *)
6594                 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6595         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6596         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6597
6598         if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
6599                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6600                                 "6188 FW LOG mailbox "
6601                                 "completed with status x%x add_status x%x,"
6602                                 " mbx status x%x\n",
6603                                 shdr_status, shdr_add_status, mb->mbxStatus);
6604
6605                 ras_fwlog->ras_hwsupport = false;
6606                 goto disable_ras;
6607         }
6608
6609         spin_lock_irq(&phba->hbalock);
6610         ras_fwlog->state = ACTIVE;
6611         spin_unlock_irq(&phba->hbalock);
6612         mempool_free(pmb, phba->mbox_mem_pool);
6613
6614         return;
6615
6616 disable_ras:
6617         /* Free RAS DMA memory */
6618         lpfc_sli4_ras_dma_free(phba);
6619         mempool_free(pmb, phba->mbox_mem_pool);
6620 }
6621
6622 /**
6623  * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6624  * @phba: pointer to lpfc hba data structure.
6625  * @fwlog_level: Logging verbosity level.
6626  * @fwlog_enable: Enable/Disable logging.
6627  *
6628  * Initialize memory and post mailbox command to enable FW logging in host
6629  * memory.
6630  **/
6631 int
6632 lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6633                          uint32_t fwlog_level,
6634                          uint32_t fwlog_enable)
6635 {
6636         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6637         struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6638         struct lpfc_dmabuf *dmabuf;
6639         LPFC_MBOXQ_t *mbox;
6640         uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6641         int rc = 0;
6642
6643         spin_lock_irq(&phba->hbalock);
6644         ras_fwlog->state = INACTIVE;
6645         spin_unlock_irq(&phba->hbalock);
6646
6647         fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6648                           phba->cfg_ras_fwlog_buffsize);
6649         fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6650
6651         /*
6652          * If re-enabling FW logging support use earlier allocated
6653          * DMA buffers while posting MBX command.
6654          **/
6655         if (!ras_fwlog->lwpd.virt) {
6656                 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6657                 if (rc) {
6658                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6659                                         "6189 FW Log Memory Allocation Failed");
6660                         return rc;
6661                 }
6662         }
6663
6664         /* Setup Mailbox command */
6665         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6666         if (!mbox) {
6667                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6668                                 "6190 RAS MBX Alloc Failed");
6669                 rc = -ENOMEM;
6670                 goto mem_free;
6671         }
6672
6673         ras_fwlog->fw_loglevel = fwlog_level;
6674         len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6675                 sizeof(struct lpfc_sli4_cfg_mhdr));
6676
6677         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6678                          LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6679                          len, LPFC_SLI4_MBX_EMBED);
6680
6681         mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6682         bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6683                fwlog_enable);
6684         bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6685                ras_fwlog->fw_loglevel);
6686         bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6687                ras_fwlog->fw_buffcount);
6688         bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6689                LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6690
6691         /* Update DMA buffer address */
6692         list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6693                 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6694
6695                 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6696                         putPaddrLow(dmabuf->phys);
6697
6698                 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6699                         putPaddrHigh(dmabuf->phys);
6700         }
6701
6702         /* Update LPWD address */
6703         mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6704         mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6705
6706         spin_lock_irq(&phba->hbalock);
6707         ras_fwlog->state = REG_INPROGRESS;
6708         spin_unlock_irq(&phba->hbalock);
6709         mbox->vport = phba->pport;
6710         mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6711
6712         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6713
6714         if (rc == MBX_NOT_FINISHED) {
6715                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6716                                 "6191 FW-Log Mailbox failed. "
6717                                 "status %d mbxStatus : x%x", rc,
6718                                 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6719                 mempool_free(mbox, phba->mbox_mem_pool);
6720                 rc = -EIO;
6721                 goto mem_free;
6722         } else
6723                 rc = 0;
6724 mem_free:
6725         if (rc)
6726                 lpfc_sli4_ras_dma_free(phba);
6727
6728         return rc;
6729 }
6730
6731 /**
6732  * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6733  * @phba: Pointer to HBA context object.
6734  *
6735  * Check if RAS is supported on the adapter and initialize it.
6736  **/
6737 void
6738 lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6739 {
6740         /* Check RAS FW Log needs to be enabled or not */
6741         if (lpfc_check_fwlog_support(phba))
6742                 return;
6743
6744         lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6745                                  LPFC_RAS_ENABLE_LOGGING);
6746 }
6747
6748 /**
6749  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6750  * @phba: Pointer to HBA context object.
6751  *
6752  * This function allocates all SLI4 resource identifiers.
6753  **/
6754 int
6755 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6756 {
6757         int i, rc, error = 0;
6758         uint16_t count, base;
6759         unsigned long longs;
6760
6761         if (!phba->sli4_hba.rpi_hdrs_in_use)
6762                 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6763         if (phba->sli4_hba.extents_in_use) {
6764                 /*
6765                  * The port supports resource extents. The XRI, VPI, VFI, RPI
6766                  * resource extent count must be read and allocated before
6767                  * provisioning the resource id arrays.
6768                  */
6769                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6770                     LPFC_IDX_RSRC_RDY) {
6771                         /*
6772                          * Extent-based resources are set - the driver could
6773                          * be in a port reset. Figure out if any corrective
6774                          * actions need to be taken.
6775                          */
6776                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6777                                                  LPFC_RSC_TYPE_FCOE_VFI);
6778                         if (rc != 0)
6779                                 error++;
6780                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6781                                                  LPFC_RSC_TYPE_FCOE_VPI);
6782                         if (rc != 0)
6783                                 error++;
6784                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6785                                                  LPFC_RSC_TYPE_FCOE_XRI);
6786                         if (rc != 0)
6787                                 error++;
6788                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6789                                                  LPFC_RSC_TYPE_FCOE_RPI);
6790                         if (rc != 0)
6791                                 error++;
6792
6793                         /*
6794                          * It's possible that the number of resources
6795                          * provided to this port instance changed between
6796                          * resets.  Detect this condition and reallocate
6797                          * resources.  Otherwise, there is no action.
6798                          */
6799                         if (error) {
6800                                 lpfc_printf_log(phba, KERN_INFO,
6801                                                 LOG_MBOX | LOG_INIT,
6802                                                 "2931 Detected extent resource "
6803                                                 "change.  Reallocating all "
6804                                                 "extents.\n");
6805                                 rc = lpfc_sli4_dealloc_extent(phba,
6806                                                  LPFC_RSC_TYPE_FCOE_VFI);
6807                                 rc = lpfc_sli4_dealloc_extent(phba,
6808                                                  LPFC_RSC_TYPE_FCOE_VPI);
6809                                 rc = lpfc_sli4_dealloc_extent(phba,
6810                                                  LPFC_RSC_TYPE_FCOE_XRI);
6811                                 rc = lpfc_sli4_dealloc_extent(phba,
6812                                                  LPFC_RSC_TYPE_FCOE_RPI);
6813                         } else
6814                                 return 0;
6815                 }
6816
6817                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6818                 if (unlikely(rc))
6819                         goto err_exit;
6820
6821                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6822                 if (unlikely(rc))
6823                         goto err_exit;
6824
6825                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6826                 if (unlikely(rc))
6827                         goto err_exit;
6828
6829                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6830                 if (unlikely(rc))
6831                         goto err_exit;
6832                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6833                        LPFC_IDX_RSRC_RDY);
6834                 return rc;
6835         } else {
6836                 /*
6837                  * The port does not support resource extents.  The XRI, VPI,
6838                  * VFI, RPI resource ids were determined from READ_CONFIG.
6839                  * Just allocate the bitmasks and provision the resource id
6840                  * arrays.  If a port reset is active, the resources don't
6841                  * need any action - just exit.
6842                  */
6843                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6844                     LPFC_IDX_RSRC_RDY) {
6845                         lpfc_sli4_dealloc_resource_identifiers(phba);
6846                         lpfc_sli4_remove_rpis(phba);
6847                 }
6848                 /* RPIs. */
6849                 count = phba->sli4_hba.max_cfg_param.max_rpi;
6850                 if (count <= 0) {
6851                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6852                                         "3279 Invalid provisioning of "
6853                                         "rpi:%d\n", count);
6854                         rc = -EINVAL;
6855                         goto err_exit;
6856                 }
6857                 base = phba->sli4_hba.max_cfg_param.rpi_base;
6858                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6859                 phba->sli4_hba.rpi_bmask = kcalloc(longs,
6860                                                    sizeof(unsigned long),
6861                                                    GFP_KERNEL);
6862                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6863                         rc = -ENOMEM;
6864                         goto err_exit;
6865                 }
6866                 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
6867                                                  GFP_KERNEL);
6868                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6869                         rc = -ENOMEM;
6870                         goto free_rpi_bmask;
6871                 }
6872
6873                 for (i = 0; i < count; i++)
6874                         phba->sli4_hba.rpi_ids[i] = base + i;
6875
6876                 /* VPIs. */
6877                 count = phba->sli4_hba.max_cfg_param.max_vpi;
6878                 if (count <= 0) {
6879                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6880                                         "3280 Invalid provisioning of "
6881                                         "vpi:%d\n", count);
6882                         rc = -EINVAL;
6883                         goto free_rpi_ids;
6884                 }
6885                 base = phba->sli4_hba.max_cfg_param.vpi_base;
6886                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6887                 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6888                                           GFP_KERNEL);
6889                 if (unlikely(!phba->vpi_bmask)) {
6890                         rc = -ENOMEM;
6891                         goto free_rpi_ids;
6892                 }
6893                 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
6894                                         GFP_KERNEL);
6895                 if (unlikely(!phba->vpi_ids)) {
6896                         rc = -ENOMEM;
6897                         goto free_vpi_bmask;
6898                 }
6899
6900                 for (i = 0; i < count; i++)
6901                         phba->vpi_ids[i] = base + i;
6902
6903                 /* XRIs. */
6904                 count = phba->sli4_hba.max_cfg_param.max_xri;
6905                 if (count <= 0) {
6906                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6907                                         "3281 Invalid provisioning of "
6908                                         "xri:%d\n", count);
6909                         rc = -EINVAL;
6910                         goto free_vpi_ids;
6911                 }
6912                 base = phba->sli4_hba.max_cfg_param.xri_base;
6913                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6914                 phba->sli4_hba.xri_bmask = kcalloc(longs,
6915                                                    sizeof(unsigned long),
6916                                                    GFP_KERNEL);
6917                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6918                         rc = -ENOMEM;
6919                         goto free_vpi_ids;
6920                 }
6921                 phba->sli4_hba.max_cfg_param.xri_used = 0;
6922                 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
6923                                                  GFP_KERNEL);
6924                 if (unlikely(!phba->sli4_hba.xri_ids)) {
6925                         rc = -ENOMEM;
6926                         goto free_xri_bmask;
6927                 }
6928
6929                 for (i = 0; i < count; i++)
6930                         phba->sli4_hba.xri_ids[i] = base + i;
6931
6932                 /* VFIs. */
6933                 count = phba->sli4_hba.max_cfg_param.max_vfi;
6934                 if (count <= 0) {
6935                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6936                                         "3282 Invalid provisioning of "
6937                                         "vfi:%d\n", count);
6938                         rc = -EINVAL;
6939                         goto free_xri_ids;
6940                 }
6941                 base = phba->sli4_hba.max_cfg_param.vfi_base;
6942                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6943                 phba->sli4_hba.vfi_bmask = kcalloc(longs,
6944                                                    sizeof(unsigned long),
6945                                                    GFP_KERNEL);
6946                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6947                         rc = -ENOMEM;
6948                         goto free_xri_ids;
6949                 }
6950                 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
6951                                                  GFP_KERNEL);
6952                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6953                         rc = -ENOMEM;
6954                         goto free_vfi_bmask;
6955                 }
6956
6957                 for (i = 0; i < count; i++)
6958                         phba->sli4_hba.vfi_ids[i] = base + i;
6959
6960                 /*
6961                  * Mark all resources ready.  An HBA reset doesn't need
6962                  * to reset the initialization.
6963                  */
6964                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6965                        LPFC_IDX_RSRC_RDY);
6966                 return 0;
6967         }
6968
6969  free_vfi_bmask:
6970         kfree(phba->sli4_hba.vfi_bmask);
6971         phba->sli4_hba.vfi_bmask = NULL;
6972  free_xri_ids:
6973         kfree(phba->sli4_hba.xri_ids);
6974         phba->sli4_hba.xri_ids = NULL;
6975  free_xri_bmask:
6976         kfree(phba->sli4_hba.xri_bmask);
6977         phba->sli4_hba.xri_bmask = NULL;
6978  free_vpi_ids:
6979         kfree(phba->vpi_ids);
6980         phba->vpi_ids = NULL;
6981  free_vpi_bmask:
6982         kfree(phba->vpi_bmask);
6983         phba->vpi_bmask = NULL;
6984  free_rpi_ids:
6985         kfree(phba->sli4_hba.rpi_ids);
6986         phba->sli4_hba.rpi_ids = NULL;
6987  free_rpi_bmask:
6988         kfree(phba->sli4_hba.rpi_bmask);
6989         phba->sli4_hba.rpi_bmask = NULL;
6990  err_exit:
6991         return rc;
6992 }
6993
6994 /**
6995  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6996  * @phba: Pointer to HBA context object.
6997  *
6998  * This function allocates the number of elements for the specified
6999  * resource type.
7000  **/
7001 int
7002 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
7003 {
7004         if (phba->sli4_hba.extents_in_use) {
7005                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7006                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7007                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7008                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7009         } else {
7010                 kfree(phba->vpi_bmask);
7011                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
7012                 kfree(phba->vpi_ids);
7013                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7014                 kfree(phba->sli4_hba.xri_bmask);
7015                 kfree(phba->sli4_hba.xri_ids);
7016                 kfree(phba->sli4_hba.vfi_bmask);
7017                 kfree(phba->sli4_hba.vfi_ids);
7018                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7019                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7020         }
7021
7022         return 0;
7023 }
7024
7025 /**
7026  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
7027  * @phba: Pointer to HBA context object.
7028  * @type: The resource extent type.
7029  * @extnt_cnt: buffer to hold port extent count response
7030  * @extnt_size: buffer to hold port extent size response.
7031  *
7032  * This function calls the port to read the host allocated extents
7033  * for a particular type.
7034  **/
7035 int
7036 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
7037                                uint16_t *extnt_cnt, uint16_t *extnt_size)
7038 {
7039         bool emb;
7040         int rc = 0;
7041         uint16_t curr_blks = 0;
7042         uint32_t req_len, emb_len;
7043         uint32_t alloc_len, mbox_tmo;
7044         struct list_head *blk_list_head;
7045         struct lpfc_rsrc_blks *rsrc_blk;
7046         LPFC_MBOXQ_t *mbox;
7047         void *virtaddr = NULL;
7048         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
7049         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
7050         union  lpfc_sli4_cfg_shdr *shdr;
7051
7052         switch (type) {
7053         case LPFC_RSC_TYPE_FCOE_VPI:
7054                 blk_list_head = &phba->lpfc_vpi_blk_list;
7055                 break;
7056         case LPFC_RSC_TYPE_FCOE_XRI:
7057                 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
7058                 break;
7059         case LPFC_RSC_TYPE_FCOE_VFI:
7060                 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
7061                 break;
7062         case LPFC_RSC_TYPE_FCOE_RPI:
7063                 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
7064                 break;
7065         default:
7066                 return -EIO;
7067         }
7068
7069         /* Count the number of extents currently allocatd for this type. */
7070         list_for_each_entry(rsrc_blk, blk_list_head, list) {
7071                 if (curr_blks == 0) {
7072                         /*
7073                          * The GET_ALLOCATED mailbox does not return the size,
7074                          * just the count.  The size should be just the size
7075                          * stored in the current allocated block and all sizes
7076                          * for an extent type are the same so set the return
7077                          * value now.
7078                          */
7079                         *extnt_size = rsrc_blk->rsrc_size;
7080                 }
7081                 curr_blks++;
7082         }
7083
7084         /*
7085          * Calculate the size of an embedded mailbox.  The uint32_t
7086          * accounts for extents-specific word.
7087          */
7088         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
7089                 sizeof(uint32_t);
7090
7091         /*
7092          * Presume the allocation and response will fit into an embedded
7093          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
7094          */
7095         emb = LPFC_SLI4_MBX_EMBED;
7096         req_len = emb_len;
7097         if (req_len > emb_len) {
7098                 req_len = curr_blks * sizeof(uint16_t) +
7099                         sizeof(union lpfc_sli4_cfg_shdr) +
7100                         sizeof(uint32_t);
7101                 emb = LPFC_SLI4_MBX_NEMBED;
7102         }
7103
7104         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7105         if (!mbox)
7106                 return -ENOMEM;
7107         memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
7108
7109         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7110                                      LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
7111                                      req_len, emb);
7112         if (alloc_len < req_len) {
7113                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7114                         "2983 Allocated DMA memory size (x%x) is "
7115                         "less than the requested DMA memory "
7116                         "size (x%x)\n", alloc_len, req_len);
7117                 rc = -ENOMEM;
7118                 goto err_exit;
7119         }
7120         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
7121         if (unlikely(rc)) {
7122                 rc = -EIO;
7123                 goto err_exit;
7124         }
7125
7126         if (!phba->sli4_hba.intr_enable)
7127                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7128         else {
7129                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
7130                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7131         }
7132
7133         if (unlikely(rc)) {
7134                 rc = -EIO;
7135                 goto err_exit;
7136         }
7137
7138         /*
7139          * Figure out where the response is located.  Then get local pointers
7140          * to the response data.  The port does not guarantee to respond to
7141          * all extents counts request so update the local variable with the
7142          * allocated count from the port.
7143          */
7144         if (emb == LPFC_SLI4_MBX_EMBED) {
7145                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7146                 shdr = &rsrc_ext->header.cfg_shdr;
7147                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7148         } else {
7149                 virtaddr = mbox->sge_array->addr[0];
7150                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7151                 shdr = &n_rsrc->cfg_shdr;
7152                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7153         }
7154
7155         if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
7156                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7157                         "2984 Failed to read allocated resources "
7158                         "for type %d - Status 0x%x Add'l Status 0x%x.\n",
7159                         type,
7160                         bf_get(lpfc_mbox_hdr_status, &shdr->response),
7161                         bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7162                 rc = -EIO;
7163                 goto err_exit;
7164         }
7165  err_exit:
7166         lpfc_sli4_mbox_cmd_free(phba, mbox);
7167         return rc;
7168 }
7169
7170 /**
7171  * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
7172  * @phba: pointer to lpfc hba data structure.
7173  * @sgl_list: linked link of sgl buffers to post
7174  * @cnt: number of linked list buffers
7175  *
7176  * This routine walks the list of buffers that have been allocated and
7177  * repost them to the port by using SGL block post. This is needed after a
7178  * pci_function_reset/warm_start or start. It attempts to construct blocks
7179  * of buffer sgls which contains contiguous xris and uses the non-embedded
7180  * SGL block post mailbox commands to post them to the port. For single
7181  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7182  * mailbox command for posting.
7183  *
7184  * Returns: 0 = success, non-zero failure.
7185  **/
7186 static int
7187 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7188                           struct list_head *sgl_list, int cnt)
7189 {
7190         struct lpfc_sglq *sglq_entry = NULL;
7191         struct lpfc_sglq *sglq_entry_next = NULL;
7192         struct lpfc_sglq *sglq_entry_first = NULL;
7193         int status, total_cnt;
7194         int post_cnt = 0, num_posted = 0, block_cnt = 0;
7195         int last_xritag = NO_XRI;
7196         LIST_HEAD(prep_sgl_list);
7197         LIST_HEAD(blck_sgl_list);
7198         LIST_HEAD(allc_sgl_list);
7199         LIST_HEAD(post_sgl_list);
7200         LIST_HEAD(free_sgl_list);
7201
7202         spin_lock_irq(&phba->hbalock);
7203         spin_lock(&phba->sli4_hba.sgl_list_lock);
7204         list_splice_init(sgl_list, &allc_sgl_list);
7205         spin_unlock(&phba->sli4_hba.sgl_list_lock);
7206         spin_unlock_irq(&phba->hbalock);
7207
7208         total_cnt = cnt;
7209         list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7210                                  &allc_sgl_list, list) {
7211                 list_del_init(&sglq_entry->list);
7212                 block_cnt++;
7213                 if ((last_xritag != NO_XRI) &&
7214                     (sglq_entry->sli4_xritag != last_xritag + 1)) {
7215                         /* a hole in xri block, form a sgl posting block */
7216                         list_splice_init(&prep_sgl_list, &blck_sgl_list);
7217                         post_cnt = block_cnt - 1;
7218                         /* prepare list for next posting block */
7219                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
7220                         block_cnt = 1;
7221                 } else {
7222                         /* prepare list for next posting block */
7223                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
7224                         /* enough sgls for non-embed sgl mbox command */
7225                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7226                                 list_splice_init(&prep_sgl_list,
7227                                                  &blck_sgl_list);
7228                                 post_cnt = block_cnt;
7229                                 block_cnt = 0;
7230                         }
7231                 }
7232                 num_posted++;
7233
7234                 /* keep track of last sgl's xritag */
7235                 last_xritag = sglq_entry->sli4_xritag;
7236
7237                 /* end of repost sgl list condition for buffers */
7238                 if (num_posted == total_cnt) {
7239                         if (post_cnt == 0) {
7240                                 list_splice_init(&prep_sgl_list,
7241                                                  &blck_sgl_list);
7242                                 post_cnt = block_cnt;
7243                         } else if (block_cnt == 1) {
7244                                 status = lpfc_sli4_post_sgl(phba,
7245                                                 sglq_entry->phys, 0,
7246                                                 sglq_entry->sli4_xritag);
7247                                 if (!status) {
7248                                         /* successful, put sgl to posted list */
7249                                         list_add_tail(&sglq_entry->list,
7250                                                       &post_sgl_list);
7251                                 } else {
7252                                         /* Failure, put sgl to free list */
7253                                         lpfc_printf_log(phba, KERN_WARNING,
7254                                                 LOG_SLI,
7255                                                 "3159 Failed to post "
7256                                                 "sgl, xritag:x%x\n",
7257                                                 sglq_entry->sli4_xritag);
7258                                         list_add_tail(&sglq_entry->list,
7259                                                       &free_sgl_list);
7260                                         total_cnt--;
7261                                 }
7262                         }
7263                 }
7264
7265                 /* continue until a nembed page worth of sgls */
7266                 if (post_cnt == 0)
7267                         continue;
7268
7269                 /* post the buffer list sgls as a block */
7270                 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7271                                                  post_cnt);
7272
7273                 if (!status) {
7274                         /* success, put sgl list to posted sgl list */
7275                         list_splice_init(&blck_sgl_list, &post_sgl_list);
7276                 } else {
7277                         /* Failure, put sgl list to free sgl list */
7278                         sglq_entry_first = list_first_entry(&blck_sgl_list,
7279                                                             struct lpfc_sglq,
7280                                                             list);
7281                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7282                                         "3160 Failed to post sgl-list, "
7283                                         "xritag:x%x-x%x\n",
7284                                         sglq_entry_first->sli4_xritag,
7285                                         (sglq_entry_first->sli4_xritag +
7286                                          post_cnt - 1));
7287                         list_splice_init(&blck_sgl_list, &free_sgl_list);
7288                         total_cnt -= post_cnt;
7289                 }
7290
7291                 /* don't reset xirtag due to hole in xri block */
7292                 if (block_cnt == 0)
7293                         last_xritag = NO_XRI;
7294
7295                 /* reset sgl post count for next round of posting */
7296                 post_cnt = 0;
7297         }
7298
7299         /* free the sgls failed to post */
7300         lpfc_free_sgl_list(phba, &free_sgl_list);
7301
7302         /* push sgls posted to the available list */
7303         if (!list_empty(&post_sgl_list)) {
7304                 spin_lock_irq(&phba->hbalock);
7305                 spin_lock(&phba->sli4_hba.sgl_list_lock);
7306                 list_splice_init(&post_sgl_list, sgl_list);
7307                 spin_unlock(&phba->sli4_hba.sgl_list_lock);
7308                 spin_unlock_irq(&phba->hbalock);
7309         } else {
7310                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7311                                 "3161 Failure to post sgl to port.\n");
7312                 return -EIO;
7313         }
7314
7315         /* return the number of XRIs actually posted */
7316         return total_cnt;
7317 }
7318
7319 /**
7320  * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
7321  * @phba: pointer to lpfc hba data structure.
7322  *
7323  * This routine walks the list of nvme buffers that have been allocated and
7324  * repost them to the port by using SGL block post. This is needed after a
7325  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7326  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
7327  * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
7328  *
7329  * Returns: 0 = success, non-zero failure.
7330  **/
7331 static int
7332 lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
7333 {
7334         LIST_HEAD(post_nblist);
7335         int num_posted, rc = 0;
7336
7337         /* get all NVME buffers need to repost to a local list */
7338         lpfc_io_buf_flush(phba, &post_nblist);
7339
7340         /* post the list of nvme buffer sgls to port if available */
7341         if (!list_empty(&post_nblist)) {
7342                 num_posted = lpfc_sli4_post_io_sgl_list(
7343                         phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
7344                 /* failed to post any nvme buffer, return error */
7345                 if (num_posted == 0)
7346                         rc = -EIO;
7347         }
7348         return rc;
7349 }
7350
7351 static void
7352 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7353 {
7354         uint32_t len;
7355
7356         len = sizeof(struct lpfc_mbx_set_host_data) -
7357                 sizeof(struct lpfc_sli4_cfg_mhdr);
7358         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7359                          LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7360                          LPFC_SLI4_MBX_EMBED);
7361
7362         mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
7363         mbox->u.mqe.un.set_host_data.param_len =
7364                                         LPFC_HOST_OS_DRIVER_VERSION_SIZE;
7365         snprintf(mbox->u.mqe.un.set_host_data.data,
7366                  LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7367                  "Linux %s v"LPFC_DRIVER_VERSION,
7368                  (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7369 }
7370
7371 int
7372 lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
7373                     struct lpfc_queue *drq, int count, int idx)
7374 {
7375         int rc, i;
7376         struct lpfc_rqe hrqe;
7377         struct lpfc_rqe drqe;
7378         struct lpfc_rqb *rqbp;
7379         unsigned long flags;
7380         struct rqb_dmabuf *rqb_buffer;
7381         LIST_HEAD(rqb_buf_list);
7382
7383         rqbp = hrq->rqbp;
7384         for (i = 0; i < count; i++) {
7385                 spin_lock_irqsave(&phba->hbalock, flags);
7386                 /* IF RQ is already full, don't bother */
7387                 if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
7388                         spin_unlock_irqrestore(&phba->hbalock, flags);
7389                         break;
7390                 }
7391                 spin_unlock_irqrestore(&phba->hbalock, flags);
7392
7393                 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7394                 if (!rqb_buffer)
7395                         break;
7396                 rqb_buffer->hrq = hrq;
7397                 rqb_buffer->drq = drq;
7398                 rqb_buffer->idx = idx;
7399                 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7400         }
7401
7402         spin_lock_irqsave(&phba->hbalock, flags);
7403         while (!list_empty(&rqb_buf_list)) {
7404                 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7405                                  hbuf.list);
7406
7407                 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7408                 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7409                 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7410                 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7411                 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7412                 if (rc < 0) {
7413                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7414                                         "6421 Cannot post to HRQ %d: %x %x %x "
7415                                         "DRQ %x %x\n",
7416                                         hrq->queue_id,
7417                                         hrq->host_index,
7418                                         hrq->hba_index,
7419                                         hrq->entry_count,
7420                                         drq->host_index,
7421                                         drq->hba_index);
7422                         rqbp->rqb_free_buffer(phba, rqb_buffer);
7423                 } else {
7424                         list_add_tail(&rqb_buffer->hbuf.list,
7425                                       &rqbp->rqb_buffer_list);
7426                         rqbp->buffer_count++;
7427                 }
7428         }
7429         spin_unlock_irqrestore(&phba->hbalock, flags);
7430         return 1;
7431 }
7432
7433 /**
7434  * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
7435  * @phba: pointer to lpfc hba data structure.
7436  *
7437  * This routine initializes the per-cq idle_stat to dynamically dictate
7438  * polling decisions.
7439  *
7440  * Return codes:
7441  *   None
7442  **/
7443 static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
7444 {
7445         int i;
7446         struct lpfc_sli4_hdw_queue *hdwq;
7447         struct lpfc_queue *cq;
7448         struct lpfc_idle_stat *idle_stat;
7449         u64 wall;
7450
7451         for_each_present_cpu(i) {
7452                 hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
7453                 cq = hdwq->io_cq;
7454
7455                 /* Skip if we've already handled this cq's primary CPU */
7456                 if (cq->chann != i)
7457                         continue;
7458
7459                 idle_stat = &phba->sli4_hba.idle_stat[i];
7460
7461                 idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
7462                 idle_stat->prev_wall = wall;
7463
7464                 if (phba->nvmet_support)
7465                         cq->poll_mode = LPFC_QUEUE_WORK;
7466                 else
7467                         cq->poll_mode = LPFC_IRQ_POLL;
7468         }
7469
7470         if (!phba->nvmet_support)
7471                 schedule_delayed_work(&phba->idle_stat_delay_work,
7472                                       msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
7473 }
7474
7475 static void lpfc_sli4_dip(struct lpfc_hba *phba)
7476 {
7477         uint32_t if_type;
7478
7479         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7480         if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
7481             if_type == LPFC_SLI_INTF_IF_TYPE_6) {
7482                 struct lpfc_register reg_data;
7483
7484                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7485                                &reg_data.word0))
7486                         return;
7487
7488                 if (bf_get(lpfc_sliport_status_dip, &reg_data))
7489                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7490                                         "2904 Firmware Dump Image Present"
7491                                         " on Adapter");
7492         }
7493 }
7494
7495 /**
7496  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
7497  * @phba: Pointer to HBA context object.
7498  *
7499  * This function is the main SLI4 device initialization PCI function. This
7500  * function is called by the HBA initialization code, HBA reset code and
7501  * HBA error attention handler code. Caller is not required to hold any
7502  * locks.
7503  **/
7504 int
7505 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7506 {
7507         int rc, i, cnt, len, dd;
7508         LPFC_MBOXQ_t *mboxq;
7509         struct lpfc_mqe *mqe;
7510         uint8_t *vpd;
7511         uint32_t vpd_size;
7512         uint32_t ftr_rsp = 0;
7513         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7514         struct lpfc_vport *vport = phba->pport;
7515         struct lpfc_dmabuf *mp;
7516         struct lpfc_rqb *rqbp;
7517
7518         /* Perform a PCI function reset to start from clean */
7519         rc = lpfc_pci_function_reset(phba);
7520         if (unlikely(rc))
7521                 return -ENODEV;
7522
7523         /* Check the HBA Host Status Register for readyness */
7524         rc = lpfc_sli4_post_status_check(phba);
7525         if (unlikely(rc))
7526                 return -ENODEV;
7527         else {
7528                 spin_lock_irq(&phba->hbalock);
7529                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7530                 spin_unlock_irq(&phba->hbalock);
7531         }
7532
7533         lpfc_sli4_dip(phba);
7534
7535         /*
7536          * Allocate a single mailbox container for initializing the
7537          * port.
7538          */
7539         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7540         if (!mboxq)
7541                 return -ENOMEM;
7542
7543         /* Issue READ_REV to collect vpd and FW information. */
7544         vpd_size = SLI4_PAGE_SIZE;
7545         vpd = kzalloc(vpd_size, GFP_KERNEL);
7546         if (!vpd) {
7547                 rc = -ENOMEM;
7548                 goto out_free_mbox;
7549         }
7550
7551         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
7552         if (unlikely(rc)) {
7553                 kfree(vpd);
7554                 goto out_free_mbox;
7555         }
7556
7557         mqe = &mboxq->u.mqe;
7558         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
7559         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
7560                 phba->hba_flag |= HBA_FCOE_MODE;
7561                 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7562         } else {
7563                 phba->hba_flag &= ~HBA_FCOE_MODE;
7564         }
7565
7566         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7567                 LPFC_DCBX_CEE_MODE)
7568                 phba->hba_flag |= HBA_FIP_SUPPORT;
7569         else
7570                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7571
7572         phba->hba_flag &= ~HBA_IOQ_FLUSH;
7573
7574         if (phba->sli_rev != LPFC_SLI_REV4) {
7575                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7576                         "0376 READ_REV Error. SLI Level %d "
7577                         "FCoE enabled %d\n",
7578                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
7579                 rc = -EIO;
7580                 kfree(vpd);
7581                 goto out_free_mbox;
7582         }
7583
7584         /*
7585          * Continue initialization with default values even if driver failed
7586          * to read FCoE param config regions, only read parameters if the
7587          * board is FCoE
7588          */
7589         if (phba->hba_flag & HBA_FCOE_MODE &&
7590             lpfc_sli4_read_fcoe_params(phba))
7591                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7592                         "2570 Failed to read FCoE parameters\n");
7593
7594         /*
7595          * Retrieve sli4 device physical port name, failure of doing it
7596          * is considered as non-fatal.
7597          */
7598         rc = lpfc_sli4_retrieve_pport_name(phba);
7599         if (!rc)
7600                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7601                                 "3080 Successful retrieving SLI4 device "
7602                                 "physical port name: %s.\n", phba->Port);
7603
7604         rc = lpfc_sli4_get_ctl_attr(phba);
7605         if (!rc)
7606                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7607                                 "8351 Successful retrieving SLI4 device "
7608                                 "CTL ATTR\n");
7609
7610         /*
7611          * Evaluate the read rev and vpd data. Populate the driver
7612          * state with the results. If this routine fails, the failure
7613          * is not fatal as the driver will use generic values.
7614          */
7615         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7616         if (unlikely(!rc)) {
7617                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7618                                 "0377 Error %d parsing vpd. "
7619                                 "Using defaults.\n", rc);
7620                 rc = 0;
7621         }
7622         kfree(vpd);
7623
7624         /* Save information as VPD data */
7625         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7626         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
7627
7628         /*
7629          * This is because first G7 ASIC doesn't support the standard
7630          * 0x5a NVME cmd descriptor type/subtype
7631          */
7632         if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7633                         LPFC_SLI_INTF_IF_TYPE_6) &&
7634             (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7635             (phba->vpd.rev.smRev == 0) &&
7636             (phba->cfg_nvme_embed_cmd == 1))
7637                 phba->cfg_nvme_embed_cmd = 0;
7638
7639         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7640         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7641                                          &mqe->un.read_rev);
7642         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7643                                        &mqe->un.read_rev);
7644         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7645                                             &mqe->un.read_rev);
7646         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7647                                            &mqe->un.read_rev);
7648         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7649         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7650         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7651         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7652         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7653         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7654         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7655                         "(%d):0380 READ_REV Status x%x "
7656                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7657                         mboxq->vport ? mboxq->vport->vpi : 0,
7658                         bf_get(lpfc_mqe_status, mqe),
7659                         phba->vpd.rev.opFwName,
7660                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7661                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
7662
7663         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7664             LPFC_SLI_INTF_IF_TYPE_0) {
7665                 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7666                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7667                 if (rc == MBX_SUCCESS) {
7668                         phba->hba_flag |= HBA_RECOVERABLE_UE;
7669                         /* Set 1Sec interval to detect UE */
7670                         phba->eratt_poll_interval = 1;
7671                         phba->sli4_hba.ue_to_sr = bf_get(
7672                                         lpfc_mbx_set_feature_UESR,
7673                                         &mboxq->u.mqe.un.set_feature);
7674                         phba->sli4_hba.ue_to_rp = bf_get(
7675                                         lpfc_mbx_set_feature_UERP,
7676                                         &mboxq->u.mqe.un.set_feature);
7677                 }
7678         }
7679
7680         if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7681                 /* Enable MDS Diagnostics only if the SLI Port supports it */
7682                 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7683                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7684                 if (rc != MBX_SUCCESS)
7685                         phba->mds_diags_support = 0;
7686         }
7687
7688         /*
7689          * Discover the port's supported feature set and match it against the
7690          * hosts requests.
7691          */
7692         lpfc_request_features(phba, mboxq);
7693         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7694         if (unlikely(rc)) {
7695                 rc = -EIO;
7696                 goto out_free_mbox;
7697         }
7698
7699         /*
7700          * The port must support FCP initiator mode as this is the
7701          * only mode running in the host.
7702          */
7703         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7704                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7705                                 "0378 No support for fcpi mode.\n");
7706                 ftr_rsp++;
7707         }
7708
7709         /* Performance Hints are ONLY for FCoE */
7710         if (phba->hba_flag & HBA_FCOE_MODE) {
7711                 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7712                         phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7713                 else
7714                         phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7715         }
7716
7717         /*
7718          * If the port cannot support the host's requested features
7719          * then turn off the global config parameters to disable the
7720          * feature in the driver.  This is not a fatal error.
7721          */
7722         if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7723                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7724                         phba->cfg_enable_bg = 0;
7725                         phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
7726                         ftr_rsp++;
7727                 }
7728         }
7729
7730         if (phba->max_vpi && phba->cfg_enable_npiv &&
7731             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7732                 ftr_rsp++;
7733
7734         if (ftr_rsp) {
7735                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7736                                 "0379 Feature Mismatch Data: x%08x %08x "
7737                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7738                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7739                                 phba->cfg_enable_npiv, phba->max_vpi);
7740                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7741                         phba->cfg_enable_bg = 0;
7742                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7743                         phba->cfg_enable_npiv = 0;
7744         }
7745
7746         /* These SLI3 features are assumed in SLI4 */
7747         spin_lock_irq(&phba->hbalock);
7748         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7749         spin_unlock_irq(&phba->hbalock);
7750
7751         /* Always try to enable dual dump feature if we can */
7752         lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
7753         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7754         dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
7755         if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
7756                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7757                                 "6448 Dual Dump is enabled\n");
7758         else
7759                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
7760                                 "6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
7761                                 "rc:x%x dd:x%x\n",
7762                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7763                                 lpfc_sli_config_mbox_subsys_get(
7764                                         phba, mboxq),
7765                                 lpfc_sli_config_mbox_opcode_get(
7766                                         phba, mboxq),
7767                                 rc, dd);
7768         /*
7769          * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
7770          * calls depends on these resources to complete port setup.
7771          */
7772         rc = lpfc_sli4_alloc_resource_identifiers(phba);
7773         if (rc) {
7774                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7775                                 "2920 Failed to alloc Resource IDs "
7776                                 "rc = x%x\n", rc);
7777                 goto out_free_mbox;
7778         }
7779
7780         lpfc_set_host_data(phba, mboxq);
7781
7782         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7783         if (rc) {
7784                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7785                                 "2134 Failed to set host os driver version %x",
7786                                 rc);
7787         }
7788
7789         /* Read the port's service parameters. */
7790         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7791         if (rc) {
7792                 phba->link_state = LPFC_HBA_ERROR;
7793                 rc = -ENOMEM;
7794                 goto out_free_mbox;
7795         }
7796
7797         mboxq->vport = vport;
7798         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7799         mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
7800         if (rc == MBX_SUCCESS) {
7801                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7802                 rc = 0;
7803         }
7804
7805         /*
7806          * This memory was allocated by the lpfc_read_sparam routine. Release
7807          * it to the mbuf pool.
7808          */
7809         lpfc_mbuf_free(phba, mp->virt, mp->phys);
7810         kfree(mp);
7811         mboxq->ctx_buf = NULL;
7812         if (unlikely(rc)) {
7813                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7814                                 "0382 READ_SPARAM command failed "
7815                                 "status %d, mbxStatus x%x\n",
7816                                 rc, bf_get(lpfc_mqe_status, mqe));
7817                 phba->link_state = LPFC_HBA_ERROR;
7818                 rc = -EIO;
7819                 goto out_free_mbox;
7820         }
7821
7822         lpfc_update_vport_wwn(vport);
7823
7824         /* Update the fc_host data structures with new wwn. */
7825         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7826         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7827
7828         /* Create all the SLI4 queues */
7829         rc = lpfc_sli4_queue_create(phba);
7830         if (rc) {
7831                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7832                                 "3089 Failed to allocate queues\n");
7833                 rc = -ENODEV;
7834                 goto out_free_mbox;
7835         }
7836         /* Set up all the queues to the device */
7837         rc = lpfc_sli4_queue_setup(phba);
7838         if (unlikely(rc)) {
7839                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7840                                 "0381 Error %d during queue setup.\n ", rc);
7841                 goto out_stop_timers;
7842         }
7843         /* Initialize the driver internal SLI layer lists. */
7844         lpfc_sli4_setup(phba);
7845         lpfc_sli4_queue_init(phba);
7846
7847         /* update host els xri-sgl sizes and mappings */
7848         rc = lpfc_sli4_els_sgl_update(phba);
7849         if (unlikely(rc)) {
7850                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7851                                 "1400 Failed to update xri-sgl size and "
7852                                 "mapping: %d\n", rc);
7853                 goto out_destroy_queue;
7854         }
7855
7856         /* register the els sgl pool to the port */
7857         rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7858                                        phba->sli4_hba.els_xri_cnt);
7859         if (unlikely(rc < 0)) {
7860                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7861                                 "0582 Error %d during els sgl post "
7862                                 "operation\n", rc);
7863                 rc = -ENODEV;
7864                 goto out_destroy_queue;
7865         }
7866         phba->sli4_hba.els_xri_cnt = rc;
7867
7868         if (phba->nvmet_support) {
7869                 /* update host nvmet xri-sgl sizes and mappings */
7870                 rc = lpfc_sli4_nvmet_sgl_update(phba);
7871                 if (unlikely(rc)) {
7872                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7873                                         "6308 Failed to update nvmet-sgl size "
7874                                         "and mapping: %d\n", rc);
7875                         goto out_destroy_queue;
7876                 }
7877
7878                 /* register the nvmet sgl pool to the port */
7879                 rc = lpfc_sli4_repost_sgl_list(
7880                         phba,
7881                         &phba->sli4_hba.lpfc_nvmet_sgl_list,
7882                         phba->sli4_hba.nvmet_xri_cnt);
7883                 if (unlikely(rc < 0)) {
7884                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7885                                         "3117 Error %d during nvmet "
7886                                         "sgl post\n", rc);
7887                         rc = -ENODEV;
7888                         goto out_destroy_queue;
7889                 }
7890                 phba->sli4_hba.nvmet_xri_cnt = rc;
7891
7892                 /* We allocate an iocbq for every receive context SGL.
7893                  * The additional allocation is for abort and ls handling.
7894                  */
7895                 cnt = phba->sli4_hba.nvmet_xri_cnt +
7896                         phba->sli4_hba.max_cfg_param.max_xri;
7897         } else {
7898                 /* update host common xri-sgl sizes and mappings */
7899                 rc = lpfc_sli4_io_sgl_update(phba);
7900                 if (unlikely(rc)) {
7901                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7902                                         "6082 Failed to update nvme-sgl size "
7903                                         "and mapping: %d\n", rc);
7904                         goto out_destroy_queue;
7905                 }
7906
7907                 /* register the allocated common sgl pool to the port */
7908                 rc = lpfc_sli4_repost_io_sgl_list(phba);
7909                 if (unlikely(rc)) {
7910                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7911                                         "6116 Error %d during nvme sgl post "
7912                                         "operation\n", rc);
7913                         /* Some NVME buffers were moved to abort nvme list */
7914                         /* A pci function reset will repost them */
7915                         rc = -ENODEV;
7916                         goto out_destroy_queue;
7917                 }
7918                 /* Each lpfc_io_buf job structure has an iocbq element.
7919                  * This cnt provides for abort, els, ct and ls requests.
7920                  */
7921                 cnt = phba->sli4_hba.max_cfg_param.max_xri;
7922         }
7923
7924         if (!phba->sli.iocbq_lookup) {
7925                 /* Initialize and populate the iocb list per host */
7926                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7927                                 "2821 initialize iocb list with %d entries\n",
7928                                 cnt);
7929                 rc = lpfc_init_iocb_list(phba, cnt);
7930                 if (rc) {
7931                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7932                                         "1413 Failed to init iocb list.\n");
7933                         goto out_destroy_queue;
7934                 }
7935         }
7936
7937         if (phba->nvmet_support)
7938                 lpfc_nvmet_create_targetport(phba);
7939
7940         if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
7941                 /* Post initial buffers to all RQs created */
7942                 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7943                         rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7944                         INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7945                         rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7946                         rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
7947                         rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
7948                         rqbp->buffer_count = 0;
7949
7950                         lpfc_post_rq_buffer(
7951                                 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7952                                 phba->sli4_hba.nvmet_mrq_data[i],
7953                                 phba->cfg_nvmet_mrq_post, i);
7954                 }
7955         }
7956
7957         /* Post the rpi header region to the device. */
7958         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7959         if (unlikely(rc)) {
7960                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7961                                 "0393 Error %d during rpi post operation\n",
7962                                 rc);
7963                 rc = -ENODEV;
7964                 goto out_destroy_queue;
7965         }
7966         lpfc_sli4_node_prep(phba);
7967
7968         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
7969                 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
7970                         /*
7971                          * The FC Port needs to register FCFI (index 0)
7972                          */
7973                         lpfc_reg_fcfi(phba, mboxq);
7974                         mboxq->vport = phba->pport;
7975                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7976                         if (rc != MBX_SUCCESS)
7977                                 goto out_unset_queue;
7978                         rc = 0;
7979                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7980                                                 &mboxq->u.mqe.un.reg_fcfi);
7981                 } else {
7982                         /* We are a NVME Target mode with MRQ > 1 */
7983
7984                         /* First register the FCFI */
7985                         lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7986                         mboxq->vport = phba->pport;
7987                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7988                         if (rc != MBX_SUCCESS)
7989                                 goto out_unset_queue;
7990                         rc = 0;
7991                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7992                                                 &mboxq->u.mqe.un.reg_fcfi_mrq);
7993
7994                         /* Next register the MRQs */
7995                         lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7996                         mboxq->vport = phba->pport;
7997                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7998                         if (rc != MBX_SUCCESS)
7999                                 goto out_unset_queue;
8000                         rc = 0;
8001                 }
8002                 /* Check if the port is configured to be disabled */
8003                 lpfc_sli_read_link_ste(phba);
8004         }
8005
8006         /* Don't post more new bufs if repost already recovered
8007          * the nvme sgls.
8008          */
8009         if (phba->nvmet_support == 0) {
8010                 if (phba->sli4_hba.io_xri_cnt == 0) {
8011                         len = lpfc_new_io_buf(
8012                                               phba, phba->sli4_hba.io_xri_max);
8013                         if (len == 0) {
8014                                 rc = -ENOMEM;
8015                                 goto out_unset_queue;
8016                         }
8017
8018                         if (phba->cfg_xri_rebalancing)
8019                                 lpfc_create_multixri_pools(phba);
8020                 }
8021         } else {
8022                 phba->cfg_xri_rebalancing = 0;
8023         }
8024
8025         /* Allow asynchronous mailbox command to go through */
8026         spin_lock_irq(&phba->hbalock);
8027         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8028         spin_unlock_irq(&phba->hbalock);
8029
8030         /* Post receive buffers to the device */
8031         lpfc_sli4_rb_setup(phba);
8032
8033         /* Reset HBA FCF states after HBA reset */
8034         phba->fcf.fcf_flag = 0;
8035         phba->fcf.current_rec.flag = 0;
8036
8037         /* Start the ELS watchdog timer */
8038         mod_timer(&vport->els_tmofunc,
8039                   jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
8040
8041         /* Start heart beat timer */
8042         mod_timer(&phba->hb_tmofunc,
8043                   jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
8044         phba->hb_outstanding = 0;
8045         phba->last_completion_time = jiffies;
8046
8047         /* start eq_delay heartbeat */
8048         if (phba->cfg_auto_imax)
8049                 queue_delayed_work(phba->wq, &phba->eq_delay_work,
8050                                    msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
8051
8052         /* start per phba idle_stat_delay heartbeat */
8053         lpfc_init_idle_stat_hb(phba);
8054
8055         /* Start error attention (ERATT) polling timer */
8056         mod_timer(&phba->eratt_poll,
8057                   jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
8058
8059         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
8060         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
8061                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
8062                 if (!rc) {
8063                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8064                                         "2829 This device supports "
8065                                         "Advanced Error Reporting (AER)\n");
8066                         spin_lock_irq(&phba->hbalock);
8067                         phba->hba_flag |= HBA_AER_ENABLED;
8068                         spin_unlock_irq(&phba->hbalock);
8069                 } else {
8070                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8071                                         "2830 This device does not support "
8072                                         "Advanced Error Reporting (AER)\n");
8073                         phba->cfg_aer_support = 0;
8074                 }
8075                 rc = 0;
8076         }
8077
8078         /*
8079          * The port is ready, set the host's link state to LINK_DOWN
8080          * in preparation for link interrupts.
8081          */
8082         spin_lock_irq(&phba->hbalock);
8083         phba->link_state = LPFC_LINK_DOWN;
8084
8085         /* Check if physical ports are trunked */
8086         if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
8087                 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
8088         if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
8089                 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
8090         if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
8091                 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
8092         if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
8093                 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
8094         spin_unlock_irq(&phba->hbalock);
8095
8096         /* Arm the CQs and then EQs on device */
8097         lpfc_sli4_arm_cqeq_intr(phba);
8098
8099         /* Indicate device interrupt mode */
8100         phba->sli4_hba.intr_enable = 1;
8101
8102         if (!(phba->hba_flag & HBA_FCOE_MODE) &&
8103             (phba->hba_flag & LINK_DISABLED)) {
8104                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8105                                 "3103 Adapter Link is disabled.\n");
8106                 lpfc_down_link(phba, mboxq);
8107                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8108                 if (rc != MBX_SUCCESS) {
8109                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8110                                         "3104 Adapter failed to issue "
8111                                         "DOWN_LINK mbox cmd, rc:x%x\n", rc);
8112                         goto out_io_buff_free;
8113                 }
8114         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
8115                 /* don't perform init_link on SLI4 FC port loopback test */
8116                 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
8117                         rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
8118                         if (rc)
8119                                 goto out_io_buff_free;
8120                 }
8121         }
8122         mempool_free(mboxq, phba->mbox_mem_pool);
8123         return rc;
8124 out_io_buff_free:
8125         /* Free allocated IO Buffers */
8126         lpfc_io_free(phba);
8127 out_unset_queue:
8128         /* Unset all the queues set up in this routine when error out */
8129         lpfc_sli4_queue_unset(phba);
8130 out_destroy_queue:
8131         lpfc_free_iocb_list(phba);
8132         lpfc_sli4_queue_destroy(phba);
8133 out_stop_timers:
8134         lpfc_stop_hba_timers(phba);
8135 out_free_mbox:
8136         mempool_free(mboxq, phba->mbox_mem_pool);
8137         return rc;
8138 }
8139
8140 /**
8141  * lpfc_mbox_timeout - Timeout call back function for mbox timer
8142  * @t: Context to fetch pointer to hba structure from.
8143  *
8144  * This is the callback function for mailbox timer. The mailbox
8145  * timer is armed when a new mailbox command is issued and the timer
8146  * is deleted when the mailbox complete. The function is called by
8147  * the kernel timer code when a mailbox does not complete within
8148  * expected time. This function wakes up the worker thread to
8149  * process the mailbox timeout and returns. All the processing is
8150  * done by the worker thread function lpfc_mbox_timeout_handler.
8151  **/
8152 void
8153 lpfc_mbox_timeout(struct timer_list *t)
8154 {
8155         struct lpfc_hba  *phba = from_timer(phba, t, sli.mbox_tmo);
8156         unsigned long iflag;
8157         uint32_t tmo_posted;
8158
8159         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
8160         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
8161         if (!tmo_posted)
8162                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
8163         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
8164
8165         if (!tmo_posted)
8166                 lpfc_worker_wake_up(phba);
8167         return;
8168 }
8169
8170 /**
8171  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
8172  *                                    are pending
8173  * @phba: Pointer to HBA context object.
8174  *
8175  * This function checks if any mailbox completions are present on the mailbox
8176  * completion queue.
8177  **/
8178 static bool
8179 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
8180 {
8181
8182         uint32_t idx;
8183         struct lpfc_queue *mcq;
8184         struct lpfc_mcqe *mcqe;
8185         bool pending_completions = false;
8186         uint8_t qe_valid;
8187
8188         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8189                 return false;
8190
8191         /* Check for completions on mailbox completion queue */
8192
8193         mcq = phba->sli4_hba.mbx_cq;
8194         idx = mcq->hba_index;
8195         qe_valid = mcq->qe_valid;
8196         while (bf_get_le32(lpfc_cqe_valid,
8197                (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
8198                 mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
8199                 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
8200                     (!bf_get_le32(lpfc_trailer_async, mcqe))) {
8201                         pending_completions = true;
8202                         break;
8203                 }
8204                 idx = (idx + 1) % mcq->entry_count;
8205                 if (mcq->hba_index == idx)
8206                         break;
8207
8208                 /* if the index wrapped around, toggle the valid bit */
8209                 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
8210                         qe_valid = (qe_valid) ? 0 : 1;
8211         }
8212         return pending_completions;
8213
8214 }
8215
8216 /**
8217  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
8218  *                                            that were missed.
8219  * @phba: Pointer to HBA context object.
8220  *
8221  * For sli4, it is possible to miss an interrupt. As such mbox completions
8222  * maybe missed causing erroneous mailbox timeouts to occur. This function
8223  * checks to see if mbox completions are on the mailbox completion queue
8224  * and will process all the completions associated with the eq for the
8225  * mailbox completion queue.
8226  **/
8227 static bool
8228 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
8229 {
8230         struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
8231         uint32_t eqidx;
8232         struct lpfc_queue *fpeq = NULL;
8233         struct lpfc_queue *eq;
8234         bool mbox_pending;
8235
8236         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8237                 return false;
8238
8239         /* Find the EQ associated with the mbox CQ */
8240         if (sli4_hba->hdwq) {
8241                 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
8242                         eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
8243                         if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
8244                                 fpeq = eq;
8245                                 break;
8246                         }
8247                 }
8248         }
8249         if (!fpeq)
8250                 return false;
8251
8252         /* Turn off interrupts from this EQ */
8253
8254         sli4_hba->sli4_eq_clr_intr(fpeq);
8255
8256         /* Check to see if a mbox completion is pending */
8257
8258         mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
8259
8260         /*
8261          * If a mbox completion is pending, process all the events on EQ
8262          * associated with the mbox completion queue (this could include
8263          * mailbox commands, async events, els commands, receive queue data
8264          * and fcp commands)
8265          */
8266
8267         if (mbox_pending)
8268                 /* process and rearm the EQ */
8269                 lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
8270         else
8271                 /* Always clear and re-arm the EQ */
8272                 sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
8273
8274         return mbox_pending;
8275
8276 }
8277
8278 /**
8279  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
8280  * @phba: Pointer to HBA context object.
8281  *
8282  * This function is called from worker thread when a mailbox command times out.
8283  * The caller is not required to hold any locks. This function will reset the
8284  * HBA and recover all the pending commands.
8285  **/
8286 void
8287 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
8288 {
8289         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
8290         MAILBOX_t *mb = NULL;
8291
8292         struct lpfc_sli *psli = &phba->sli;
8293
8294         /* If the mailbox completed, process the completion and return */
8295         if (lpfc_sli4_process_missed_mbox_completions(phba))
8296                 return;
8297
8298         if (pmbox != NULL)
8299                 mb = &pmbox->u.mb;
8300         /* Check the pmbox pointer first.  There is a race condition
8301          * between the mbox timeout handler getting executed in the
8302          * worklist and the mailbox actually completing. When this
8303          * race condition occurs, the mbox_active will be NULL.
8304          */
8305         spin_lock_irq(&phba->hbalock);
8306         if (pmbox == NULL) {
8307                 lpfc_printf_log(phba, KERN_WARNING,
8308                                 LOG_MBOX | LOG_SLI,
8309                                 "0353 Active Mailbox cleared - mailbox timeout "
8310                                 "exiting\n");
8311                 spin_unlock_irq(&phba->hbalock);
8312                 return;
8313         }
8314
8315         /* Mbox cmd <mbxCommand> timeout */
8316         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8317                         "0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
8318                         mb->mbxCommand,
8319                         phba->pport->port_state,
8320                         phba->sli.sli_flag,
8321                         phba->sli.mbox_active);
8322         spin_unlock_irq(&phba->hbalock);
8323
8324         /* Setting state unknown so lpfc_sli_abort_iocb_ring
8325          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
8326          * it to fail all outstanding SCSI IO.
8327          */
8328         spin_lock_irq(&phba->pport->work_port_lock);
8329         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8330         spin_unlock_irq(&phba->pport->work_port_lock);
8331         spin_lock_irq(&phba->hbalock);
8332         phba->link_state = LPFC_LINK_UNKNOWN;
8333         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
8334         spin_unlock_irq(&phba->hbalock);
8335
8336         lpfc_sli_abort_fcp_rings(phba);
8337
8338         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8339                         "0345 Resetting board due to mailbox timeout\n");
8340
8341         /* Reset the HBA device */
8342         lpfc_reset_hba(phba);
8343 }
8344
8345 /**
8346  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
8347  * @phba: Pointer to HBA context object.
8348  * @pmbox: Pointer to mailbox object.
8349  * @flag: Flag indicating how the mailbox need to be processed.
8350  *
8351  * This function is called by discovery code and HBA management code
8352  * to submit a mailbox command to firmware with SLI-3 interface spec. This
8353  * function gets the hbalock to protect the data structures.
8354  * The mailbox command can be submitted in polling mode, in which case
8355  * this function will wait in a polling loop for the completion of the
8356  * mailbox.
8357  * If the mailbox is submitted in no_wait mode (not polling) the
8358  * function will submit the command and returns immediately without waiting
8359  * for the mailbox completion. The no_wait is supported only when HBA
8360  * is in SLI2/SLI3 mode - interrupts are enabled.
8361  * The SLI interface allows only one mailbox pending at a time. If the
8362  * mailbox is issued in polling mode and there is already a mailbox
8363  * pending, then the function will return an error. If the mailbox is issued
8364  * in NO_WAIT mode and there is a mailbox pending already, the function
8365  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
8366  * The sli layer owns the mailbox object until the completion of mailbox
8367  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
8368  * return codes the caller owns the mailbox command after the return of
8369  * the function.
8370  **/
8371 static int
8372 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
8373                        uint32_t flag)
8374 {
8375         MAILBOX_t *mbx;
8376         struct lpfc_sli *psli = &phba->sli;
8377         uint32_t status, evtctr;
8378         uint32_t ha_copy, hc_copy;
8379         int i;
8380         unsigned long timeout;
8381         unsigned long drvr_flag = 0;
8382         uint32_t word0, ldata;
8383         void __iomem *to_slim;
8384         int processing_queue = 0;
8385
8386         spin_lock_irqsave(&phba->hbalock, drvr_flag);
8387         if (!pmbox) {
8388                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8389                 /* processing mbox queue from intr_handler */
8390                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8391                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8392                         return MBX_SUCCESS;
8393                 }
8394                 processing_queue = 1;
8395                 pmbox = lpfc_mbox_get(phba);
8396                 if (!pmbox) {
8397                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8398                         return MBX_SUCCESS;
8399                 }
8400         }
8401
8402         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
8403                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
8404                 if(!pmbox->vport) {
8405                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8406                         lpfc_printf_log(phba, KERN_ERR,
8407                                         LOG_MBOX | LOG_VPORT,
8408                                         "1806 Mbox x%x failed. No vport\n",
8409                                         pmbox->u.mb.mbxCommand);
8410                         dump_stack();
8411                         goto out_not_finished;
8412                 }
8413         }
8414
8415         /* If the PCI channel is in offline state, do not post mbox. */
8416         if (unlikely(pci_channel_offline(phba->pcidev))) {
8417                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8418                 goto out_not_finished;
8419         }
8420
8421         /* If HBA has a deferred error attention, fail the iocb. */
8422         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8423                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8424                 goto out_not_finished;
8425         }
8426
8427         psli = &phba->sli;
8428
8429         mbx = &pmbox->u.mb;
8430         status = MBX_SUCCESS;
8431
8432         if (phba->link_state == LPFC_HBA_ERROR) {
8433                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8434
8435                 /* Mbox command <mbxCommand> cannot issue */
8436                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8437                                 "(%d):0311 Mailbox command x%x cannot "
8438                                 "issue Data: x%x x%x\n",
8439                                 pmbox->vport ? pmbox->vport->vpi : 0,
8440                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
8441                 goto out_not_finished;
8442         }
8443
8444         if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
8445                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8446                         !(hc_copy & HC_MBINT_ENA)) {
8447                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8448                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8449                                 "(%d):2528 Mailbox command x%x cannot "
8450                                 "issue Data: x%x x%x\n",
8451                                 pmbox->vport ? pmbox->vport->vpi : 0,
8452                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
8453                         goto out_not_finished;
8454                 }
8455         }
8456
8457         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8458                 /* Polling for a mbox command when another one is already active
8459                  * is not allowed in SLI. Also, the driver must have established
8460                  * SLI2 mode to queue and process multiple mbox commands.
8461                  */
8462
8463                 if (flag & MBX_POLL) {
8464                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8465
8466                         /* Mbox command <mbxCommand> cannot issue */
8467                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8468                                         "(%d):2529 Mailbox command x%x "
8469                                         "cannot issue Data: x%x x%x\n",
8470                                         pmbox->vport ? pmbox->vport->vpi : 0,
8471                                         pmbox->u.mb.mbxCommand,
8472                                         psli->sli_flag, flag);
8473                         goto out_not_finished;
8474                 }
8475
8476                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
8477                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8478                         /* Mbox command <mbxCommand> cannot issue */
8479                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8480                                         "(%d):2530 Mailbox command x%x "
8481                                         "cannot issue Data: x%x x%x\n",
8482                                         pmbox->vport ? pmbox->vport->vpi : 0,
8483                                         pmbox->u.mb.mbxCommand,
8484                                         psli->sli_flag, flag);
8485                         goto out_not_finished;
8486                 }
8487
8488                 /* Another mailbox command is still being processed, queue this
8489                  * command to be processed later.
8490                  */
8491                 lpfc_mbox_put(phba, pmbox);
8492
8493                 /* Mbox cmd issue - BUSY */
8494                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8495                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
8496                                 "x%x x%x x%x x%x\n",
8497                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
8498                                 mbx->mbxCommand,
8499                                 phba->pport ? phba->pport->port_state : 0xff,
8500                                 psli->sli_flag, flag);
8501
8502                 psli->slistat.mbox_busy++;
8503                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8504
8505                 if (pmbox->vport) {
8506                         lpfc_debugfs_disc_trc(pmbox->vport,
8507                                 LPFC_DISC_TRC_MBOX_VPORT,
8508                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
8509                                 (uint32_t)mbx->mbxCommand,
8510                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8511                 }
8512                 else {
8513                         lpfc_debugfs_disc_trc(phba->pport,
8514                                 LPFC_DISC_TRC_MBOX,
8515                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
8516                                 (uint32_t)mbx->mbxCommand,
8517                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8518                 }
8519
8520                 return MBX_BUSY;
8521         }
8522
8523         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8524
8525         /* If we are not polling, we MUST be in SLI2 mode */
8526         if (flag != MBX_POLL) {
8527                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
8528                     (mbx->mbxCommand != MBX_KILL_BOARD)) {
8529                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8530                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8531                         /* Mbox command <mbxCommand> cannot issue */
8532                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8533                                         "(%d):2531 Mailbox command x%x "
8534                                         "cannot issue Data: x%x x%x\n",
8535                                         pmbox->vport ? pmbox->vport->vpi : 0,
8536                                         pmbox->u.mb.mbxCommand,
8537                                         psli->sli_flag, flag);
8538                         goto out_not_finished;
8539                 }
8540                 /* timeout active mbox command */
8541                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8542                                            1000);
8543                 mod_timer(&psli->mbox_tmo, jiffies + timeout);
8544         }
8545
8546         /* Mailbox cmd <cmd> issue */
8547         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8548                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
8549                         "x%x\n",
8550                         pmbox->vport ? pmbox->vport->vpi : 0,
8551                         mbx->mbxCommand,
8552                         phba->pport ? phba->pport->port_state : 0xff,
8553                         psli->sli_flag, flag);
8554
8555         if (mbx->mbxCommand != MBX_HEARTBEAT) {
8556                 if (pmbox->vport) {
8557                         lpfc_debugfs_disc_trc(pmbox->vport,
8558                                 LPFC_DISC_TRC_MBOX_VPORT,
8559                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8560                                 (uint32_t)mbx->mbxCommand,
8561                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8562                 }
8563                 else {
8564                         lpfc_debugfs_disc_trc(phba->pport,
8565                                 LPFC_DISC_TRC_MBOX,
8566                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
8567                                 (uint32_t)mbx->mbxCommand,
8568                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8569                 }
8570         }
8571
8572         psli->slistat.mbox_cmd++;
8573         evtctr = psli->slistat.mbox_event;
8574
8575         /* next set own bit for the adapter and copy over command word */
8576         mbx->mbxOwner = OWN_CHIP;
8577
8578         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8579                 /* Populate mbox extension offset word. */
8580                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
8581                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
8582                                 = (uint8_t *)phba->mbox_ext
8583                                   - (uint8_t *)phba->mbox;
8584                 }
8585
8586                 /* Copy the mailbox extension data */
8587                 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8588                         lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8589                                               (uint8_t *)phba->mbox_ext,
8590                                               pmbox->in_ext_byte_len);
8591                 }
8592                 /* Copy command data to host SLIM area */
8593                 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
8594         } else {
8595                 /* Populate mbox extension offset word. */
8596                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
8597                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
8598                                 = MAILBOX_HBA_EXT_OFFSET;
8599
8600                 /* Copy the mailbox extension data */
8601                 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
8602                         lpfc_memcpy_to_slim(phba->MBslimaddr +
8603                                 MAILBOX_HBA_EXT_OFFSET,
8604                                 pmbox->ctx_buf, pmbox->in_ext_byte_len);
8605
8606                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
8607                         /* copy command data into host mbox for cmpl */
8608                         lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8609                                               MAILBOX_CMD_SIZE);
8610
8611                 /* First copy mbox command data to HBA SLIM, skip past first
8612                    word */
8613                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
8614                 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
8615                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
8616
8617                 /* Next copy over first word, with mbxOwner set */
8618                 ldata = *((uint32_t *)mbx);
8619                 to_slim = phba->MBslimaddr;
8620                 writel(ldata, to_slim);
8621                 readl(to_slim); /* flush */
8622
8623                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
8624                         /* switch over to host mailbox */
8625                         psli->sli_flag |= LPFC_SLI_ACTIVE;
8626         }
8627
8628         wmb();
8629
8630         switch (flag) {
8631         case MBX_NOWAIT:
8632                 /* Set up reference to mailbox command */
8633                 psli->mbox_active = pmbox;
8634                 /* Interrupt board to do it */
8635                 writel(CA_MBATT, phba->CAregaddr);
8636                 readl(phba->CAregaddr); /* flush */
8637                 /* Don't wait for it to finish, just return */
8638                 break;
8639
8640         case MBX_POLL:
8641                 /* Set up null reference to mailbox command */
8642                 psli->mbox_active = NULL;
8643                 /* Interrupt board to do it */
8644                 writel(CA_MBATT, phba->CAregaddr);
8645                 readl(phba->CAregaddr); /* flush */
8646
8647                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8648                         /* First read mbox status word */
8649                         word0 = *((uint32_t *)phba->mbox);
8650                         word0 = le32_to_cpu(word0);
8651                 } else {
8652                         /* First read mbox status word */
8653                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
8654                                 spin_unlock_irqrestore(&phba->hbalock,
8655                                                        drvr_flag);
8656                                 goto out_not_finished;
8657                         }
8658                 }
8659
8660                 /* Read the HBA Host Attention Register */
8661                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8662                         spin_unlock_irqrestore(&phba->hbalock,
8663                                                        drvr_flag);
8664                         goto out_not_finished;
8665                 }
8666                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8667                                                         1000) + jiffies;
8668                 i = 0;
8669                 /* Wait for command to complete */
8670                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8671                        (!(ha_copy & HA_MBATT) &&
8672                         (phba->link_state > LPFC_WARM_START))) {
8673                         if (time_after(jiffies, timeout)) {
8674                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8675                                 spin_unlock_irqrestore(&phba->hbalock,
8676                                                        drvr_flag);
8677                                 goto out_not_finished;
8678                         }
8679
8680                         /* Check if we took a mbox interrupt while we were
8681                            polling */
8682                         if (((word0 & OWN_CHIP) != OWN_CHIP)
8683                             && (evtctr != psli->slistat.mbox_event))
8684                                 break;
8685
8686                         if (i++ > 10) {
8687                                 spin_unlock_irqrestore(&phba->hbalock,
8688                                                        drvr_flag);
8689                                 msleep(1);
8690                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8691                         }
8692
8693                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8694                                 /* First copy command data */
8695                                 word0 = *((uint32_t *)phba->mbox);
8696                                 word0 = le32_to_cpu(word0);
8697                                 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
8698                                         MAILBOX_t *slimmb;
8699                                         uint32_t slimword0;
8700                                         /* Check real SLIM for any errors */
8701                                         slimword0 = readl(phba->MBslimaddr);
8702                                         slimmb = (MAILBOX_t *) & slimword0;
8703                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8704                                             && slimmb->mbxStatus) {
8705                                                 psli->sli_flag &=
8706                                                     ~LPFC_SLI_ACTIVE;
8707                                                 word0 = slimword0;
8708                                         }
8709                                 }
8710                         } else {
8711                                 /* First copy command data */
8712                                 word0 = readl(phba->MBslimaddr);
8713                         }
8714                         /* Read the HBA Host Attention Register */
8715                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8716                                 spin_unlock_irqrestore(&phba->hbalock,
8717                                                        drvr_flag);
8718                                 goto out_not_finished;
8719                         }
8720                 }
8721
8722                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8723                         /* copy results back to user */
8724                         lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8725                                                 MAILBOX_CMD_SIZE);
8726                         /* Copy the mailbox extension data */
8727                         if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8728                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
8729                                                       pmbox->ctx_buf,
8730                                                       pmbox->out_ext_byte_len);
8731                         }
8732                 } else {
8733                         /* First copy command data */
8734                         lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
8735                                                 MAILBOX_CMD_SIZE);
8736                         /* Copy the mailbox extension data */
8737                         if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8738                                 lpfc_memcpy_from_slim(
8739                                         pmbox->ctx_buf,
8740                                         phba->MBslimaddr +
8741                                         MAILBOX_HBA_EXT_OFFSET,
8742                                         pmbox->out_ext_byte_len);
8743                         }
8744                 }
8745
8746                 writel(HA_MBATT, phba->HAregaddr);
8747                 readl(phba->HAregaddr); /* flush */
8748
8749                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8750                 status = mbx->mbxStatus;
8751         }
8752
8753         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8754         return status;
8755
8756 out_not_finished:
8757         if (processing_queue) {
8758                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
8759                 lpfc_mbox_cmpl_put(phba, pmbox);
8760         }
8761         return MBX_NOT_FINISHED;
8762 }
8763
8764 /**
8765  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8766  * @phba: Pointer to HBA context object.
8767  *
8768  * The function blocks the posting of SLI4 asynchronous mailbox commands from
8769  * the driver internal pending mailbox queue. It will then try to wait out the
8770  * possible outstanding mailbox command before return.
8771  *
8772  * Returns:
8773  *      0 - the outstanding mailbox command completed; otherwise, the wait for
8774  *      the outstanding mailbox command timed out.
8775  **/
8776 static int
8777 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8778 {
8779         struct lpfc_sli *psli = &phba->sli;
8780         int rc = 0;
8781         unsigned long timeout = 0;
8782
8783         /* Mark the asynchronous mailbox command posting as blocked */
8784         spin_lock_irq(&phba->hbalock);
8785         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8786         /* Determine how long we might wait for the active mailbox
8787          * command to be gracefully completed by firmware.
8788          */
8789         if (phba->sli.mbox_active)
8790                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8791                                                 phba->sli.mbox_active) *
8792                                                 1000) + jiffies;
8793         spin_unlock_irq(&phba->hbalock);
8794
8795         /* Make sure the mailbox is really active */
8796         if (timeout)
8797                 lpfc_sli4_process_missed_mbox_completions(phba);
8798
8799         /* Wait for the outstnading mailbox command to complete */
8800         while (phba->sli.mbox_active) {
8801                 /* Check active mailbox complete status every 2ms */
8802                 msleep(2);
8803                 if (time_after(jiffies, timeout)) {
8804                         /* Timeout, marked the outstanding cmd not complete */
8805                         rc = 1;
8806                         break;
8807                 }
8808         }
8809
8810         /* Can not cleanly block async mailbox command, fails it */
8811         if (rc) {
8812                 spin_lock_irq(&phba->hbalock);
8813                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8814                 spin_unlock_irq(&phba->hbalock);
8815         }
8816         return rc;
8817 }
8818
8819 /**
8820  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8821  * @phba: Pointer to HBA context object.
8822  *
8823  * The function unblocks and resume posting of SLI4 asynchronous mailbox
8824  * commands from the driver internal pending mailbox queue. It makes sure
8825  * that there is no outstanding mailbox command before resuming posting
8826  * asynchronous mailbox commands. If, for any reason, there is outstanding
8827  * mailbox command, it will try to wait it out before resuming asynchronous
8828  * mailbox command posting.
8829  **/
8830 static void
8831 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8832 {
8833         struct lpfc_sli *psli = &phba->sli;
8834
8835         spin_lock_irq(&phba->hbalock);
8836         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8837                 /* Asynchronous mailbox posting is not blocked, do nothing */
8838                 spin_unlock_irq(&phba->hbalock);
8839                 return;
8840         }
8841
8842         /* Outstanding synchronous mailbox command is guaranteed to be done,
8843          * successful or timeout, after timing-out the outstanding mailbox
8844          * command shall always be removed, so just unblock posting async
8845          * mailbox command and resume
8846          */
8847         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8848         spin_unlock_irq(&phba->hbalock);
8849
8850         /* wake up worker thread to post asynchronous mailbox command */
8851         lpfc_worker_wake_up(phba);
8852 }
8853
8854 /**
8855  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8856  * @phba: Pointer to HBA context object.
8857  * @mboxq: Pointer to mailbox object.
8858  *
8859  * The function waits for the bootstrap mailbox register ready bit from
8860  * port for twice the regular mailbox command timeout value.
8861  *
8862  *      0 - no timeout on waiting for bootstrap mailbox register ready.
8863  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8864  **/
8865 static int
8866 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8867 {
8868         uint32_t db_ready;
8869         unsigned long timeout;
8870         struct lpfc_register bmbx_reg;
8871
8872         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8873                                    * 1000) + jiffies;
8874
8875         do {
8876                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8877                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8878                 if (!db_ready)
8879                         mdelay(2);
8880
8881                 if (time_after(jiffies, timeout))
8882                         return MBXERR_ERROR;
8883         } while (!db_ready);
8884
8885         return 0;
8886 }
8887
8888 /**
8889  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8890  * @phba: Pointer to HBA context object.
8891  * @mboxq: Pointer to mailbox object.
8892  *
8893  * The function posts a mailbox to the port.  The mailbox is expected
8894  * to be comletely filled in and ready for the port to operate on it.
8895  * This routine executes a synchronous completion operation on the
8896  * mailbox by polling for its completion.
8897  *
8898  * The caller must not be holding any locks when calling this routine.
8899  *
8900  * Returns:
8901  *      MBX_SUCCESS - mailbox posted successfully
8902  *      Any of the MBX error values.
8903  **/
8904 static int
8905 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8906 {
8907         int rc = MBX_SUCCESS;
8908         unsigned long iflag;
8909         uint32_t mcqe_status;
8910         uint32_t mbx_cmnd;
8911         struct lpfc_sli *psli = &phba->sli;
8912         struct lpfc_mqe *mb = &mboxq->u.mqe;
8913         struct lpfc_bmbx_create *mbox_rgn;
8914         struct dma_address *dma_address;
8915
8916         /*
8917          * Only one mailbox can be active to the bootstrap mailbox region
8918          * at a time and there is no queueing provided.
8919          */
8920         spin_lock_irqsave(&phba->hbalock, iflag);
8921         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8922                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8923                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8924                                 "(%d):2532 Mailbox command x%x (x%x/x%x) "
8925                                 "cannot issue Data: x%x x%x\n",
8926                                 mboxq->vport ? mboxq->vport->vpi : 0,
8927                                 mboxq->u.mb.mbxCommand,
8928                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8929                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8930                                 psli->sli_flag, MBX_POLL);
8931                 return MBXERR_ERROR;
8932         }
8933         /* The server grabs the token and owns it until release */
8934         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8935         phba->sli.mbox_active = mboxq;
8936         spin_unlock_irqrestore(&phba->hbalock, iflag);
8937
8938         /* wait for bootstrap mbox register for readyness */
8939         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8940         if (rc)
8941                 goto exit;
8942         /*
8943          * Initialize the bootstrap memory region to avoid stale data areas
8944          * in the mailbox post.  Then copy the caller's mailbox contents to
8945          * the bmbx mailbox region.
8946          */
8947         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8948         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
8949         lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8950                                sizeof(struct lpfc_mqe));
8951
8952         /* Post the high mailbox dma address to the port and wait for ready. */
8953         dma_address = &phba->sli4_hba.bmbx.dma_address;
8954         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8955
8956         /* wait for bootstrap mbox register for hi-address write done */
8957         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8958         if (rc)
8959                 goto exit;
8960
8961         /* Post the low mailbox dma address to the port. */
8962         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
8963
8964         /* wait for bootstrap mbox register for low address write done */
8965         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8966         if (rc)
8967                 goto exit;
8968
8969         /*
8970          * Read the CQ to ensure the mailbox has completed.
8971          * If so, update the mailbox status so that the upper layers
8972          * can complete the request normally.
8973          */
8974         lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8975                                sizeof(struct lpfc_mqe));
8976         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8977         lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8978                                sizeof(struct lpfc_mcqe));
8979         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
8980         /*
8981          * When the CQE status indicates a failure and the mailbox status
8982          * indicates success then copy the CQE status into the mailbox status
8983          * (and prefix it with x4000).
8984          */
8985         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
8986                 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8987                         bf_set(lpfc_mqe_status, mb,
8988                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
8989                 rc = MBXERR_ERROR;
8990         } else
8991                 lpfc_sli4_swap_str(phba, mboxq);
8992
8993         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8994                         "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
8995                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8996                         " x%x x%x CQ: x%x x%x x%x x%x\n",
8997                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8998                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8999                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9000                         bf_get(lpfc_mqe_status, mb),
9001                         mb->un.mb_words[0], mb->un.mb_words[1],
9002                         mb->un.mb_words[2], mb->un.mb_words[3],
9003                         mb->un.mb_words[4], mb->un.mb_words[5],
9004                         mb->un.mb_words[6], mb->un.mb_words[7],
9005                         mb->un.mb_words[8], mb->un.mb_words[9],
9006                         mb->un.mb_words[10], mb->un.mb_words[11],
9007                         mb->un.mb_words[12], mboxq->mcqe.word0,
9008                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
9009                         mboxq->mcqe.trailer);
9010 exit:
9011         /* We are holding the token, no needed for lock when release */
9012         spin_lock_irqsave(&phba->hbalock, iflag);
9013         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9014         phba->sli.mbox_active = NULL;
9015         spin_unlock_irqrestore(&phba->hbalock, iflag);
9016         return rc;
9017 }
9018
9019 /**
9020  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
9021  * @phba: Pointer to HBA context object.
9022  * @mboxq: Pointer to mailbox object.
9023  * @flag: Flag indicating how the mailbox need to be processed.
9024  *
9025  * This function is called by discovery code and HBA management code to submit
9026  * a mailbox command to firmware with SLI-4 interface spec.
9027  *
9028  * Return codes the caller owns the mailbox command after the return of the
9029  * function.
9030  **/
9031 static int
9032 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
9033                        uint32_t flag)
9034 {
9035         struct lpfc_sli *psli = &phba->sli;
9036         unsigned long iflags;
9037         int rc;
9038
9039         /* dump from issue mailbox command if setup */
9040         lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
9041
9042         rc = lpfc_mbox_dev_check(phba);
9043         if (unlikely(rc)) {
9044                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9045                                 "(%d):2544 Mailbox command x%x (x%x/x%x) "
9046                                 "cannot issue Data: x%x x%x\n",
9047                                 mboxq->vport ? mboxq->vport->vpi : 0,
9048                                 mboxq->u.mb.mbxCommand,
9049                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9050                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9051                                 psli->sli_flag, flag);
9052                 goto out_not_finished;
9053         }
9054
9055         /* Detect polling mode and jump to a handler */
9056         if (!phba->sli4_hba.intr_enable) {
9057                 if (flag == MBX_POLL)
9058                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
9059                 else
9060                         rc = -EIO;
9061                 if (rc != MBX_SUCCESS)
9062                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
9063                                         "(%d):2541 Mailbox command x%x "
9064                                         "(x%x/x%x) failure: "
9065                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
9066                                         "Data: x%x x%x\n,",
9067                                         mboxq->vport ? mboxq->vport->vpi : 0,
9068                                         mboxq->u.mb.mbxCommand,
9069                                         lpfc_sli_config_mbox_subsys_get(phba,
9070                                                                         mboxq),
9071                                         lpfc_sli_config_mbox_opcode_get(phba,
9072                                                                         mboxq),
9073                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
9074                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
9075                                         bf_get(lpfc_mcqe_ext_status,
9076                                                &mboxq->mcqe),
9077                                         psli->sli_flag, flag);
9078                 return rc;
9079         } else if (flag == MBX_POLL) {
9080                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
9081                                 "(%d):2542 Try to issue mailbox command "
9082                                 "x%x (x%x/x%x) synchronously ahead of async "
9083                                 "mailbox command queue: x%x x%x\n",
9084                                 mboxq->vport ? mboxq->vport->vpi : 0,
9085                                 mboxq->u.mb.mbxCommand,
9086                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9087                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9088                                 psli->sli_flag, flag);
9089                 /* Try to block the asynchronous mailbox posting */
9090                 rc = lpfc_sli4_async_mbox_block(phba);
9091                 if (!rc) {
9092                         /* Successfully blocked, now issue sync mbox cmd */
9093                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
9094                         if (rc != MBX_SUCCESS)
9095                                 lpfc_printf_log(phba, KERN_WARNING,
9096                                         LOG_MBOX | LOG_SLI,
9097                                         "(%d):2597 Sync Mailbox command "
9098                                         "x%x (x%x/x%x) failure: "
9099                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
9100                                         "Data: x%x x%x\n,",
9101                                         mboxq->vport ? mboxq->vport->vpi : 0,
9102                                         mboxq->u.mb.mbxCommand,
9103                                         lpfc_sli_config_mbox_subsys_get(phba,
9104                                                                         mboxq),
9105                                         lpfc_sli_config_mbox_opcode_get(phba,
9106                                                                         mboxq),
9107                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
9108                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
9109                                         bf_get(lpfc_mcqe_ext_status,
9110                                                &mboxq->mcqe),
9111                                         psli->sli_flag, flag);
9112                         /* Unblock the async mailbox posting afterward */
9113                         lpfc_sli4_async_mbox_unblock(phba);
9114                 }
9115                 return rc;
9116         }
9117
9118         /* Now, interrupt mode asynchronous mailbox command */
9119         rc = lpfc_mbox_cmd_check(phba, mboxq);
9120         if (rc) {
9121                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9122                                 "(%d):2543 Mailbox command x%x (x%x/x%x) "
9123                                 "cannot issue Data: x%x x%x\n",
9124                                 mboxq->vport ? mboxq->vport->vpi : 0,
9125                                 mboxq->u.mb.mbxCommand,
9126                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9127                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9128                                 psli->sli_flag, flag);
9129                 goto out_not_finished;
9130         }
9131
9132         /* Put the mailbox command to the driver internal FIFO */
9133         psli->slistat.mbox_busy++;
9134         spin_lock_irqsave(&phba->hbalock, iflags);
9135         lpfc_mbox_put(phba, mboxq);
9136         spin_unlock_irqrestore(&phba->hbalock, iflags);
9137         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9138                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
9139                         "x%x (x%x/x%x) x%x x%x x%x\n",
9140                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
9141                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
9142                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9143                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9144                         phba->pport->port_state,
9145                         psli->sli_flag, MBX_NOWAIT);
9146         /* Wake up worker thread to transport mailbox command from head */
9147         lpfc_worker_wake_up(phba);
9148
9149         return MBX_BUSY;
9150
9151 out_not_finished:
9152         return MBX_NOT_FINISHED;
9153 }
9154
9155 /**
9156  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
9157  * @phba: Pointer to HBA context object.
9158  *
9159  * This function is called by worker thread to send a mailbox command to
9160  * SLI4 HBA firmware.
9161  *
9162  **/
9163 int
9164 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
9165 {
9166         struct lpfc_sli *psli = &phba->sli;
9167         LPFC_MBOXQ_t *mboxq;
9168         int rc = MBX_SUCCESS;
9169         unsigned long iflags;
9170         struct lpfc_mqe *mqe;
9171         uint32_t mbx_cmnd;
9172
9173         /* Check interrupt mode before post async mailbox command */
9174         if (unlikely(!phba->sli4_hba.intr_enable))
9175                 return MBX_NOT_FINISHED;
9176
9177         /* Check for mailbox command service token */
9178         spin_lock_irqsave(&phba->hbalock, iflags);
9179         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9180                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9181                 return MBX_NOT_FINISHED;
9182         }
9183         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9184                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9185                 return MBX_NOT_FINISHED;
9186         }
9187         if (unlikely(phba->sli.mbox_active)) {
9188                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9189                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9190                                 "0384 There is pending active mailbox cmd\n");
9191                 return MBX_NOT_FINISHED;
9192         }
9193         /* Take the mailbox command service token */
9194         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9195
9196         /* Get the next mailbox command from head of queue */
9197         mboxq = lpfc_mbox_get(phba);
9198
9199         /* If no more mailbox command waiting for post, we're done */
9200         if (!mboxq) {
9201                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9202                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9203                 return MBX_SUCCESS;
9204         }
9205         phba->sli.mbox_active = mboxq;
9206         spin_unlock_irqrestore(&phba->hbalock, iflags);
9207
9208         /* Check device readiness for posting mailbox command */
9209         rc = lpfc_mbox_dev_check(phba);
9210         if (unlikely(rc))
9211                 /* Driver clean routine will clean up pending mailbox */
9212                 goto out_not_finished;
9213
9214         /* Prepare the mbox command to be posted */
9215         mqe = &mboxq->u.mqe;
9216         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
9217
9218         /* Start timer for the mbox_tmo and log some mailbox post messages */
9219         mod_timer(&psli->mbox_tmo, (jiffies +
9220                   msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
9221
9222         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9223                         "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
9224                         "x%x x%x\n",
9225                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
9226                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9227                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9228                         phba->pport->port_state, psli->sli_flag);
9229
9230         if (mbx_cmnd != MBX_HEARTBEAT) {
9231                 if (mboxq->vport) {
9232                         lpfc_debugfs_disc_trc(mboxq->vport,
9233                                 LPFC_DISC_TRC_MBOX_VPORT,
9234                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
9235                                 mbx_cmnd, mqe->un.mb_words[0],
9236                                 mqe->un.mb_words[1]);
9237                 } else {
9238                         lpfc_debugfs_disc_trc(phba->pport,
9239                                 LPFC_DISC_TRC_MBOX,
9240                                 "MBOX Send: cmd:x%x mb:x%x x%x",
9241                                 mbx_cmnd, mqe->un.mb_words[0],
9242                                 mqe->un.mb_words[1]);
9243                 }
9244         }
9245         psli->slistat.mbox_cmd++;
9246
9247         /* Post the mailbox command to the port */
9248         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
9249         if (rc != MBX_SUCCESS) {
9250                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9251                                 "(%d):2533 Mailbox command x%x (x%x/x%x) "
9252                                 "cannot issue Data: x%x x%x\n",
9253                                 mboxq->vport ? mboxq->vport->vpi : 0,
9254                                 mboxq->u.mb.mbxCommand,
9255                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9256                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
9257                                 psli->sli_flag, MBX_NOWAIT);
9258                 goto out_not_finished;
9259         }
9260
9261         return rc;
9262
9263 out_not_finished:
9264         spin_lock_irqsave(&phba->hbalock, iflags);
9265         if (phba->sli.mbox_active) {
9266                 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9267                 __lpfc_mbox_cmpl_put(phba, mboxq);
9268                 /* Release the token */
9269                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9270                 phba->sli.mbox_active = NULL;
9271         }
9272         spin_unlock_irqrestore(&phba->hbalock, iflags);
9273
9274         return MBX_NOT_FINISHED;
9275 }
9276
9277 /**
9278  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
9279  * @phba: Pointer to HBA context object.
9280  * @pmbox: Pointer to mailbox object.
9281  * @flag: Flag indicating how the mailbox need to be processed.
9282  *
9283  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
9284  * the API jump table function pointer from the lpfc_hba struct.
9285  *
9286  * Return codes the caller owns the mailbox command after the return of the
9287  * function.
9288  **/
9289 int
9290 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
9291 {
9292         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
9293 }
9294
9295 /**
9296  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
9297  * @phba: The hba struct for which this call is being executed.
9298  * @dev_grp: The HBA PCI-Device group number.
9299  *
9300  * This routine sets up the mbox interface API function jump table in @phba
9301  * struct.
9302  * Returns: 0 - success, -ENODEV - failure.
9303  **/
9304 int
9305 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9306 {
9307
9308         switch (dev_grp) {
9309         case LPFC_PCI_DEV_LP:
9310                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
9311                 phba->lpfc_sli_handle_slow_ring_event =
9312                                 lpfc_sli_handle_slow_ring_event_s3;
9313                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
9314                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
9315                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
9316                 break;
9317         case LPFC_PCI_DEV_OC:
9318                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
9319                 phba->lpfc_sli_handle_slow_ring_event =
9320                                 lpfc_sli_handle_slow_ring_event_s4;
9321                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
9322                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
9323                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
9324                 break;
9325         default:
9326                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9327                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
9328                                 dev_grp);
9329                 return -ENODEV;
9330         }
9331         return 0;
9332 }
9333
9334 /**
9335  * __lpfc_sli_ringtx_put - Add an iocb to the txq
9336  * @phba: Pointer to HBA context object.
9337  * @pring: Pointer to driver SLI ring object.
9338  * @piocb: Pointer to address of newly added command iocb.
9339  *
9340  * This function is called with hbalock held for SLI3 ports or
9341  * the ring lock held for SLI4 ports to add a command
9342  * iocb to the txq when SLI layer cannot submit the command iocb
9343  * to the ring.
9344  **/
9345 void
9346 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9347                     struct lpfc_iocbq *piocb)
9348 {
9349         if (phba->sli_rev == LPFC_SLI_REV4)
9350                 lockdep_assert_held(&pring->ring_lock);
9351         else
9352                 lockdep_assert_held(&phba->hbalock);
9353         /* Insert the caller's iocb in the txq tail for later processing. */
9354         list_add_tail(&piocb->list, &pring->txq);
9355 }
9356
9357 /**
9358  * lpfc_sli_next_iocb - Get the next iocb in the txq
9359  * @phba: Pointer to HBA context object.
9360  * @pring: Pointer to driver SLI ring object.
9361  * @piocb: Pointer to address of newly added command iocb.
9362  *
9363  * This function is called with hbalock held before a new
9364  * iocb is submitted to the firmware. This function checks
9365  * txq to flush the iocbs in txq to Firmware before
9366  * submitting new iocbs to the Firmware.
9367  * If there are iocbs in the txq which need to be submitted
9368  * to firmware, lpfc_sli_next_iocb returns the first element
9369  * of the txq after dequeuing it from txq.
9370  * If there is no iocb in the txq then the function will return
9371  * *piocb and *piocb is set to NULL. Caller needs to check
9372  * *piocb to find if there are more commands in the txq.
9373  **/
9374 static struct lpfc_iocbq *
9375 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9376                    struct lpfc_iocbq **piocb)
9377 {
9378         struct lpfc_iocbq * nextiocb;
9379
9380         lockdep_assert_held(&phba->hbalock);
9381
9382         nextiocb = lpfc_sli_ringtx_get(phba, pring);
9383         if (!nextiocb) {
9384                 nextiocb = *piocb;
9385                 *piocb = NULL;
9386         }
9387
9388         return nextiocb;
9389 }
9390
9391 /**
9392  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
9393  * @phba: Pointer to HBA context object.
9394  * @ring_number: SLI ring number to issue iocb on.
9395  * @piocb: Pointer to command iocb.
9396  * @flag: Flag indicating if this command can be put into txq.
9397  *
9398  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
9399  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
9400  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
9401  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
9402  * this function allows only iocbs for posting buffers. This function finds
9403  * next available slot in the command ring and posts the command to the
9404  * available slot and writes the port attention register to request HBA start
9405  * processing new iocb. If there is no slot available in the ring and
9406  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9407  * the function returns IOCB_BUSY.
9408  *
9409  * This function is called with hbalock held. The function will return success
9410  * after it successfully submit the iocb to firmware or after adding to the
9411  * txq.
9412  **/
9413 static int
9414 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
9415                     struct lpfc_iocbq *piocb, uint32_t flag)
9416 {
9417         struct lpfc_iocbq *nextiocb;
9418         IOCB_t *iocb;
9419         struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
9420
9421         lockdep_assert_held(&phba->hbalock);
9422
9423         if (piocb->iocb_cmpl && (!piocb->vport) &&
9424            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9425            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
9426                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9427                                 "1807 IOCB x%x failed. No vport\n",
9428                                 piocb->iocb.ulpCommand);
9429                 dump_stack();
9430                 return IOCB_ERROR;
9431         }
9432
9433
9434         /* If the PCI channel is in offline state, do not post iocbs. */
9435         if (unlikely(pci_channel_offline(phba->pcidev)))
9436                 return IOCB_ERROR;
9437
9438         /* If HBA has a deferred error attention, fail the iocb. */
9439         if (unlikely(phba->hba_flag & DEFER_ERATT))
9440                 return IOCB_ERROR;
9441
9442         /*
9443          * We should never get an IOCB if we are in a < LINK_DOWN state
9444          */
9445         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
9446                 return IOCB_ERROR;
9447
9448         /*
9449          * Check to see if we are blocking IOCB processing because of a
9450          * outstanding event.
9451          */
9452         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
9453                 goto iocb_busy;
9454
9455         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
9456                 /*
9457                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
9458                  * can be issued if the link is not up.
9459                  */
9460                 switch (piocb->iocb.ulpCommand) {
9461                 case CMD_GEN_REQUEST64_CR:
9462                 case CMD_GEN_REQUEST64_CX:
9463                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9464                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
9465                                         FC_RCTL_DD_UNSOL_CMD) ||
9466                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9467                                         MENLO_TRANSPORT_TYPE))
9468
9469                                 goto iocb_busy;
9470                         break;
9471                 case CMD_QUE_RING_BUF_CN:
9472                 case CMD_QUE_RING_BUF64_CN:
9473                         /*
9474                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9475                          * completion, iocb_cmpl MUST be 0.
9476                          */
9477                         if (piocb->iocb_cmpl)
9478                                 piocb->iocb_cmpl = NULL;
9479                         fallthrough;
9480                 case CMD_CREATE_XRI_CR:
9481                 case CMD_CLOSE_XRI_CN:
9482                 case CMD_CLOSE_XRI_CX:
9483                         break;
9484                 default:
9485                         goto iocb_busy;
9486                 }
9487
9488         /*
9489          * For FCP commands, we must be in a state where we can process link
9490          * attention events.
9491          */
9492         } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
9493                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
9494                 goto iocb_busy;
9495         }
9496
9497         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9498                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9499                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9500
9501         if (iocb)
9502                 lpfc_sli_update_ring(phba, pring);
9503         else
9504                 lpfc_sli_update_full_ring(phba, pring);
9505
9506         if (!piocb)
9507                 return IOCB_SUCCESS;
9508
9509         goto out_busy;
9510
9511  iocb_busy:
9512         pring->stats.iocb_cmd_delay++;
9513
9514  out_busy:
9515
9516         if (!(flag & SLI_IOCB_RET_IOCB)) {
9517                 __lpfc_sli_ringtx_put(phba, pring, piocb);
9518                 return IOCB_SUCCESS;
9519         }
9520
9521         return IOCB_BUSY;
9522 }
9523
9524 /**
9525  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9526  * @phba: Pointer to HBA context object.
9527  * @piocbq: Pointer to command iocb.
9528  * @sglq: Pointer to the scatter gather queue object.
9529  *
9530  * This routine converts the bpl or bde that is in the IOCB
9531  * to a sgl list for the sli4 hardware. The physical address
9532  * of the bpl/bde is converted back to a virtual address.
9533  * If the IOCB contains a BPL then the list of BDE's is
9534  * converted to sli4_sge's. If the IOCB contains a single
9535  * BDE then it is converted to a single sli_sge.
9536  * The IOCB is still in cpu endianess so the contents of
9537  * the bpl can be used without byte swapping.
9538  *
9539  * Returns valid XRI = Success, NO_XRI = Failure.
9540 **/
9541 static uint16_t
9542 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9543                 struct lpfc_sglq *sglq)
9544 {
9545         uint16_t xritag = NO_XRI;
9546         struct ulp_bde64 *bpl = NULL;
9547         struct ulp_bde64 bde;
9548         struct sli4_sge *sgl  = NULL;
9549         struct lpfc_dmabuf *dmabuf;
9550         IOCB_t *icmd;
9551         int numBdes = 0;
9552         int i = 0;
9553         uint32_t offset = 0; /* accumulated offset in the sg request list */
9554         int inbound = 0; /* number of sg reply entries inbound from firmware */
9555
9556         if (!piocbq || !sglq)
9557                 return xritag;
9558
9559         sgl  = (struct sli4_sge *)sglq->sgl;
9560         icmd = &piocbq->iocb;
9561         if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9562                 return sglq->sli4_xritag;
9563         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9564                 numBdes = icmd->un.genreq64.bdl.bdeSize /
9565                                 sizeof(struct ulp_bde64);
9566                 /* The addrHigh and addrLow fields within the IOCB
9567                  * have not been byteswapped yet so there is no
9568                  * need to swap them back.
9569                  */
9570                 if (piocbq->context3)
9571                         dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9572                 else
9573                         return xritag;
9574
9575                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
9576                 if (!bpl)
9577                         return xritag;
9578
9579                 for (i = 0; i < numBdes; i++) {
9580                         /* Should already be byte swapped. */
9581                         sgl->addr_hi = bpl->addrHigh;
9582                         sgl->addr_lo = bpl->addrLow;
9583
9584                         sgl->word2 = le32_to_cpu(sgl->word2);
9585                         if ((i+1) == numBdes)
9586                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
9587                         else
9588                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
9589                         /* swap the size field back to the cpu so we
9590                          * can assign it to the sgl.
9591                          */
9592                         bde.tus.w = le32_to_cpu(bpl->tus.w);
9593                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
9594                         /* The offsets in the sgl need to be accumulated
9595                          * separately for the request and reply lists.
9596                          * The request is always first, the reply follows.
9597                          */
9598                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9599                                 /* add up the reply sg entries */
9600                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9601                                         inbound++;
9602                                 /* first inbound? reset the offset */
9603                                 if (inbound == 1)
9604                                         offset = 0;
9605                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
9606                                 bf_set(lpfc_sli4_sge_type, sgl,
9607                                         LPFC_SGE_TYPE_DATA);
9608                                 offset += bde.tus.f.bdeSize;
9609                         }
9610                         sgl->word2 = cpu_to_le32(sgl->word2);
9611                         bpl++;
9612                         sgl++;
9613                 }
9614         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9615                         /* The addrHigh and addrLow fields of the BDE have not
9616                          * been byteswapped yet so they need to be swapped
9617                          * before putting them in the sgl.
9618                          */
9619                         sgl->addr_hi =
9620                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9621                         sgl->addr_lo =
9622                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
9623                         sgl->word2 = le32_to_cpu(sgl->word2);
9624                         bf_set(lpfc_sli4_sge_last, sgl, 1);
9625                         sgl->word2 = cpu_to_le32(sgl->word2);
9626                         sgl->sge_len =
9627                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
9628         }
9629         return sglq->sli4_xritag;
9630 }
9631
9632 /**
9633  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9634  * @phba: Pointer to HBA context object.
9635  * @iocbq: Pointer to command iocb.
9636  * @wqe: Pointer to the work queue entry.
9637  *
9638  * This routine converts the iocb command to its Work Queue Entry
9639  * equivalent. The wqe pointer should not have any fields set when
9640  * this routine is called because it will memcpy over them.
9641  * This routine does not set the CQ_ID or the WQEC bits in the
9642  * wqe.
9643  *
9644  * Returns: 0 = Success, IOCB_ERROR = Failure.
9645  **/
9646 static int
9647 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
9648                 union lpfc_wqe128 *wqe)
9649 {
9650         uint32_t xmit_len = 0, total_len = 0;
9651         uint8_t ct = 0;
9652         uint32_t fip;
9653         uint32_t abort_tag;
9654         uint8_t command_type = ELS_COMMAND_NON_FIP;
9655         uint8_t cmnd;
9656         uint16_t xritag;
9657         uint16_t abrt_iotag;
9658         struct lpfc_iocbq *abrtiocbq;
9659         struct ulp_bde64 *bpl = NULL;
9660         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
9661         int numBdes, i;
9662         struct ulp_bde64 bde;
9663         struct lpfc_nodelist *ndlp;
9664         uint32_t *pcmd;
9665         uint32_t if_type;
9666
9667         fip = phba->hba_flag & HBA_FIP_SUPPORT;
9668         /* The fcp commands will set command type */
9669         if (iocbq->iocb_flag &  LPFC_IO_FCP)
9670                 command_type = FCP_COMMAND;
9671         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
9672                 command_type = ELS_COMMAND_FIP;
9673         else
9674                 command_type = ELS_COMMAND_NON_FIP;
9675
9676         if (phba->fcp_embed_io)
9677                 memset(wqe, 0, sizeof(union lpfc_wqe128));
9678         /* Some of the fields are in the right position already */
9679         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
9680         /* The ct field has moved so reset */
9681         wqe->generic.wqe_com.word7 = 0;
9682         wqe->generic.wqe_com.word10 = 0;
9683
9684         abort_tag = (uint32_t) iocbq->iotag;
9685         xritag = iocbq->sli4_xritag;
9686         /* words0-2 bpl convert bde */
9687         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9688                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9689                                 sizeof(struct ulp_bde64);
9690                 bpl  = (struct ulp_bde64 *)
9691                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9692                 if (!bpl)
9693                         return IOCB_ERROR;
9694
9695                 /* Should already be byte swapped. */
9696                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
9697                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
9698                 /* swap the size field back to the cpu so we
9699                  * can assign it to the sgl.
9700                  */
9701                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
9702                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9703                 total_len = 0;
9704                 for (i = 0; i < numBdes; i++) {
9705                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
9706                         total_len += bde.tus.f.bdeSize;
9707                 }
9708         } else
9709                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
9710
9711         iocbq->iocb.ulpIoTag = iocbq->iotag;
9712         cmnd = iocbq->iocb.ulpCommand;
9713
9714         switch (iocbq->iocb.ulpCommand) {
9715         case CMD_ELS_REQUEST64_CR:
9716                 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9717                         ndlp = iocbq->context_un.ndlp;
9718                 else
9719                         ndlp = (struct lpfc_nodelist *)iocbq->context1;
9720                 if (!iocbq->iocb.ulpLe) {
9721                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9722                                 "2007 Only Limited Edition cmd Format"
9723                                 " supported 0x%x\n",
9724                                 iocbq->iocb.ulpCommand);
9725                         return IOCB_ERROR;
9726                 }
9727
9728                 wqe->els_req.payload_len = xmit_len;
9729                 /* Els_reguest64 has a TMO */
9730                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9731                         iocbq->iocb.ulpTimeout);
9732                 /* Need a VF for word 4 set the vf bit*/
9733                 bf_set(els_req64_vf, &wqe->els_req, 0);
9734                 /* And a VFID for word 12 */
9735                 bf_set(els_req64_vfid, &wqe->els_req, 0);
9736                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9737                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9738                        iocbq->iocb.ulpContext);
9739                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9740                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
9741                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
9742                 if (command_type == ELS_COMMAND_FIP)
9743                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9744                                         >> LPFC_FIP_ELS_ID_SHIFT);
9745                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9746                                         iocbq->context2)->virt);
9747                 if_type = bf_get(lpfc_sli_intf_if_type,
9748                                         &phba->sli4_hba.sli_intf);
9749                 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
9750                         if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
9751                                 *pcmd == ELS_CMD_SCR ||
9752                                 *pcmd == ELS_CMD_RDF ||
9753                                 *pcmd == ELS_CMD_RSCN_XMT ||
9754                                 *pcmd == ELS_CMD_FDISC ||
9755                                 *pcmd == ELS_CMD_LOGO ||
9756                                 *pcmd == ELS_CMD_PLOGI)) {
9757                                 bf_set(els_req64_sp, &wqe->els_req, 1);
9758                                 bf_set(els_req64_sid, &wqe->els_req,
9759                                         iocbq->vport->fc_myDID);
9760                                 if ((*pcmd == ELS_CMD_FLOGI) &&
9761                                         !(phba->fc_topology ==
9762                                                 LPFC_TOPOLOGY_LOOP))
9763                                         bf_set(els_req64_sid, &wqe->els_req, 0);
9764                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9765                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9766                                         phba->vpi_ids[iocbq->vport->vpi]);
9767                         } else if (pcmd && iocbq->context1) {
9768                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9769                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9770                                         phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9771                         }
9772                 }
9773                 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9774                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9775                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9776                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9777                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9778                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9779                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9780                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
9781                 wqe->els_req.max_response_payload_len = total_len - xmit_len;
9782                 break;
9783         case CMD_XMIT_SEQUENCE64_CX:
9784                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9785                        iocbq->iocb.un.ulpWord[3]);
9786                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
9787                        iocbq->iocb.unsli3.rcvsli3.ox_id);
9788                 /* The entire sequence is transmitted for this IOCB */
9789                 xmit_len = total_len;
9790                 cmnd = CMD_XMIT_SEQUENCE64_CR;
9791                 if (phba->link_flag & LS_LOOPBACK_MODE)
9792                         bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
9793                 fallthrough;
9794         case CMD_XMIT_SEQUENCE64_CR:
9795                 /* word3 iocb=io_tag32 wqe=reserved */
9796                 wqe->xmit_sequence.rsvd3 = 0;
9797                 /* word4 relative_offset memcpy */
9798                 /* word5 r_ctl/df_ctl memcpy */
9799                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9800                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9801                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9802                        LPFC_WQE_IOD_WRITE);
9803                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9804                        LPFC_WQE_LENLOC_WORD12);
9805                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
9806                 wqe->xmit_sequence.xmit_len = xmit_len;
9807                 command_type = OTHER_COMMAND;
9808                 break;
9809         case CMD_XMIT_BCAST64_CN:
9810                 /* word3 iocb=iotag32 wqe=seq_payload_len */
9811                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
9812                 /* word4 iocb=rsvd wqe=rsvd */
9813                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9814                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
9815                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
9816                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9817                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9818                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9819                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9820                        LPFC_WQE_LENLOC_WORD3);
9821                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
9822                 break;
9823         case CMD_FCP_IWRITE64_CR:
9824                 command_type = FCP_COMMAND_DATA_OUT;
9825                 /* word3 iocb=iotag wqe=payload_offset_len */
9826                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9827                 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9828                        xmit_len + sizeof(struct fcp_rsp));
9829                 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9830                        0);
9831                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9832                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9833                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9834                        iocbq->iocb.ulpFCP2Rcvy);
9835                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9836                 /* Always open the exchange */
9837                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9838                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9839                        LPFC_WQE_LENLOC_WORD4);
9840                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
9841                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
9842                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9843                         bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
9844                         bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9845                         if (iocbq->priority) {
9846                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9847                                        (iocbq->priority << 1));
9848                         } else {
9849                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9850                                        (phba->cfg_XLanePriority << 1));
9851                         }
9852                 }
9853                 /* Note, word 10 is already initialized to 0 */
9854
9855                 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9856                 if (phba->cfg_enable_pbde)
9857                         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9858                 else
9859                         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9860
9861                 if (phba->fcp_embed_io) {
9862                         struct lpfc_io_buf *lpfc_cmd;
9863                         struct sli4_sge *sgl;
9864                         struct fcp_cmnd *fcp_cmnd;
9865                         uint32_t *ptr;
9866
9867                         /* 128 byte wqe support here */
9868
9869                         lpfc_cmd = iocbq->context1;
9870                         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
9871                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9872
9873                         /* Word 0-2 - FCP_CMND */
9874                         wqe->generic.bde.tus.f.bdeFlags =
9875                                 BUFF_TYPE_BDE_IMMED;
9876                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9877                         wqe->generic.bde.addrHigh = 0;
9878                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9879
9880                         bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9881                         bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
9882
9883                         /* Word 22-29  FCP CMND Payload */
9884                         ptr = &wqe->words[22];
9885                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9886                 }
9887                 break;
9888         case CMD_FCP_IREAD64_CR:
9889                 /* word3 iocb=iotag wqe=payload_offset_len */
9890                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9891                 bf_set(payload_offset_len, &wqe->fcp_iread,
9892                        xmit_len + sizeof(struct fcp_rsp));
9893                 bf_set(cmd_buff_len, &wqe->fcp_iread,
9894                        0);
9895                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9896                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9897                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9898                        iocbq->iocb.ulpFCP2Rcvy);
9899                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
9900                 /* Always open the exchange */
9901                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9902                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9903                        LPFC_WQE_LENLOC_WORD4);
9904                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
9905                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
9906                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9907                         bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
9908                         bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9909                         if (iocbq->priority) {
9910                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9911                                        (iocbq->priority << 1));
9912                         } else {
9913                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9914                                        (phba->cfg_XLanePriority << 1));
9915                         }
9916                 }
9917                 /* Note, word 10 is already initialized to 0 */
9918
9919                 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9920                 if (phba->cfg_enable_pbde)
9921                         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9922                 else
9923                         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9924
9925                 if (phba->fcp_embed_io) {
9926                         struct lpfc_io_buf *lpfc_cmd;
9927                         struct sli4_sge *sgl;
9928                         struct fcp_cmnd *fcp_cmnd;
9929                         uint32_t *ptr;
9930
9931                         /* 128 byte wqe support here */
9932
9933                         lpfc_cmd = iocbq->context1;
9934                         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
9935                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9936
9937                         /* Word 0-2 - FCP_CMND */
9938                         wqe->generic.bde.tus.f.bdeFlags =
9939                                 BUFF_TYPE_BDE_IMMED;
9940                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9941                         wqe->generic.bde.addrHigh = 0;
9942                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9943
9944                         bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9945                         bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
9946
9947                         /* Word 22-29  FCP CMND Payload */
9948                         ptr = &wqe->words[22];
9949                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9950                 }
9951                 break;
9952         case CMD_FCP_ICMND64_CR:
9953                 /* word3 iocb=iotag wqe=payload_offset_len */
9954                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9955                 bf_set(payload_offset_len, &wqe->fcp_icmd,
9956                        xmit_len + sizeof(struct fcp_rsp));
9957                 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9958                        0);
9959                 /* word3 iocb=IO_TAG wqe=reserved */
9960                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
9961                 /* Always open the exchange */
9962                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9963                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9964                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9965                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9966                        LPFC_WQE_LENLOC_NONE);
9967                 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9968                        iocbq->iocb.ulpFCP2Rcvy);
9969                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9970                         bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
9971                         bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9972                         if (iocbq->priority) {
9973                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9974                                        (iocbq->priority << 1));
9975                         } else {
9976                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9977                                        (phba->cfg_XLanePriority << 1));
9978                         }
9979                 }
9980                 /* Note, word 10 is already initialized to 0 */
9981
9982                 if (phba->fcp_embed_io) {
9983                         struct lpfc_io_buf *lpfc_cmd;
9984                         struct sli4_sge *sgl;
9985                         struct fcp_cmnd *fcp_cmnd;
9986                         uint32_t *ptr;
9987
9988                         /* 128 byte wqe support here */
9989
9990                         lpfc_cmd = iocbq->context1;
9991                         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
9992                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9993
9994                         /* Word 0-2 - FCP_CMND */
9995                         wqe->generic.bde.tus.f.bdeFlags =
9996                                 BUFF_TYPE_BDE_IMMED;
9997                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9998                         wqe->generic.bde.addrHigh = 0;
9999                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
10000
10001                         bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
10002                         bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
10003
10004                         /* Word 22-29  FCP CMND Payload */
10005                         ptr = &wqe->words[22];
10006                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10007                 }
10008                 break;
10009         case CMD_GEN_REQUEST64_CR:
10010                 /* For this command calculate the xmit length of the
10011                  * request bde.
10012                  */
10013                 xmit_len = 0;
10014                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
10015                         sizeof(struct ulp_bde64);
10016                 for (i = 0; i < numBdes; i++) {
10017                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
10018                         if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
10019                                 break;
10020                         xmit_len += bde.tus.f.bdeSize;
10021                 }
10022                 /* word3 iocb=IO_TAG wqe=request_payload_len */
10023                 wqe->gen_req.request_payload_len = xmit_len;
10024                 /* word4 iocb=parameter wqe=relative_offset memcpy */
10025                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
10026                 /* word6 context tag copied in memcpy */
10027                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
10028                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
10029                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10030                                 "2015 Invalid CT %x command 0x%x\n",
10031                                 ct, iocbq->iocb.ulpCommand);
10032                         return IOCB_ERROR;
10033                 }
10034                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
10035                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
10036                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
10037                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
10038                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
10039                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
10040                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
10041                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
10042                 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
10043                 command_type = OTHER_COMMAND;
10044                 break;
10045         case CMD_XMIT_ELS_RSP64_CX:
10046                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
10047                 /* words0-2 BDE memcpy */
10048                 /* word3 iocb=iotag32 wqe=response_payload_len */
10049                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
10050                 /* word4 */
10051                 wqe->xmit_els_rsp.word4 = 0;
10052                 /* word5 iocb=rsvd wge=did */
10053                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
10054                          iocbq->iocb.un.xseq64.xmit_els_remoteID);
10055
10056                 if_type = bf_get(lpfc_sli_intf_if_type,
10057                                         &phba->sli4_hba.sli_intf);
10058                 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
10059                         if (iocbq->vport->fc_flag & FC_PT2PT) {
10060                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
10061                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
10062                                         iocbq->vport->fc_myDID);
10063                                 if (iocbq->vport->fc_myDID == Fabric_DID) {
10064                                         bf_set(wqe_els_did,
10065                                                 &wqe->xmit_els_rsp.wqe_dest, 0);
10066                                 }
10067                         }
10068                 }
10069                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
10070                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10071                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
10072                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
10073                        iocbq->iocb.unsli3.rcvsli3.ox_id);
10074                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
10075                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
10076                                phba->vpi_ids[iocbq->vport->vpi]);
10077                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
10078                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
10079                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
10080                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
10081                        LPFC_WQE_LENLOC_WORD3);
10082                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
10083                 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
10084                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
10085                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
10086                                         iocbq->context2)->virt);
10087                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
10088                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
10089                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
10090                                         iocbq->vport->fc_myDID);
10091                                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
10092                                 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
10093                                         phba->vpi_ids[phba->pport->vpi]);
10094                 }
10095                 command_type = OTHER_COMMAND;
10096                 break;
10097         case CMD_CLOSE_XRI_CN:
10098         case CMD_ABORT_XRI_CN:
10099         case CMD_ABORT_XRI_CX:
10100                 /* words 0-2 memcpy should be 0 rserved */
10101                 /* port will send abts */
10102                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
10103                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
10104                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
10105                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
10106                 } else
10107                         fip = 0;
10108
10109                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
10110                         /*
10111                          * The link is down, or the command was ELS_FIP
10112                          * so the fw does not need to send abts
10113                          * on the wire.
10114                          */
10115                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
10116                 else
10117                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
10118                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
10119                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
10120                 wqe->abort_cmd.rsrvd5 = 0;
10121                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
10122                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10123                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
10124                 /*
10125                  * The abort handler will send us CMD_ABORT_XRI_CN or
10126                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
10127                  */
10128                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10129                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
10130                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
10131                        LPFC_WQE_LENLOC_NONE);
10132                 cmnd = CMD_ABORT_XRI_CX;
10133                 command_type = OTHER_COMMAND;
10134                 xritag = 0;
10135                 break;
10136         case CMD_XMIT_BLS_RSP64_CX:
10137                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
10138                 /* As BLS ABTS RSP WQE is very different from other WQEs,
10139                  * we re-construct this WQE here based on information in
10140                  * iocbq from scratch.
10141                  */
10142                 memset(wqe, 0, sizeof(*wqe));
10143                 /* OX_ID is invariable to who sent ABTS to CT exchange */
10144                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
10145                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
10146                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
10147                     LPFC_ABTS_UNSOL_INT) {
10148                         /* ABTS sent by initiator to CT exchange, the
10149                          * RX_ID field will be filled with the newly
10150                          * allocated responder XRI.
10151                          */
10152                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10153                                iocbq->sli4_xritag);
10154                 } else {
10155                         /* ABTS sent by responder to CT exchange, the
10156                          * RX_ID field will be filled with the responder
10157                          * RX_ID from ABTS.
10158                          */
10159                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10160                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
10161                 }
10162                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
10163                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
10164
10165                 /* Use CT=VPI */
10166                 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
10167                         ndlp->nlp_DID);
10168                 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
10169                         iocbq->iocb.ulpContext);
10170                 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
10171                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
10172                         phba->vpi_ids[phba->pport->vpi]);
10173                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
10174                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
10175                        LPFC_WQE_LENLOC_NONE);
10176                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
10177                 command_type = OTHER_COMMAND;
10178                 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
10179                         bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
10180                                bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
10181                         bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
10182                                bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
10183                         bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
10184                                bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
10185                 }
10186
10187                 break;
10188         case CMD_SEND_FRAME:
10189                 bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_SEND_FRAME);
10190                 bf_set(wqe_sof, &wqe->generic.wqe_com, 0x2E); /* SOF byte */
10191                 bf_set(wqe_eof, &wqe->generic.wqe_com, 0x41); /* EOF byte */
10192                 bf_set(wqe_lenloc, &wqe->generic.wqe_com, 1);
10193                 bf_set(wqe_xbl, &wqe->generic.wqe_com, 1);
10194                 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10195                 bf_set(wqe_xc, &wqe->generic.wqe_com, 1);
10196                 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, 0xA);
10197                 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10198                 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10199                 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10200                 return 0;
10201         case CMD_XRI_ABORTED_CX:
10202         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
10203         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
10204         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
10205         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
10206         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
10207         default:
10208                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10209                                 "2014 Invalid command 0x%x\n",
10210                                 iocbq->iocb.ulpCommand);
10211                 return IOCB_ERROR;
10212         }
10213
10214         if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
10215                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
10216         else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
10217                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
10218         else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
10219                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
10220         iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
10221                               LPFC_IO_DIF_INSERT);
10222         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10223         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10224         wqe->generic.wqe_com.abort_tag = abort_tag;
10225         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
10226         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
10227         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
10228         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10229         return 0;
10230 }
10231
10232 /**
10233  * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb
10234  * @phba: Pointer to HBA context object.
10235  * @ring_number: SLI ring number to issue wqe on.
10236  * @piocb: Pointer to command iocb.
10237  * @flag: Flag indicating if this command can be put into txq.
10238  *
10239  * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to
10240  * send  an iocb command to an HBA with SLI-4 interface spec.
10241  *
10242  * This function takes the hbalock before invoking the lockless version.
10243  * The function will return success after it successfully submit the wqe to
10244  * firmware or after adding to the txq.
10245  **/
10246 static int
10247 __lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,
10248                            struct lpfc_iocbq *piocb, uint32_t flag)
10249 {
10250         unsigned long iflags;
10251         int rc;
10252
10253         spin_lock_irqsave(&phba->hbalock, iflags);
10254         rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);
10255         spin_unlock_irqrestore(&phba->hbalock, iflags);
10256
10257         return rc;
10258 }
10259
10260 /**
10261  * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe
10262  * @phba: Pointer to HBA context object.
10263  * @ring_number: SLI ring number to issue wqe on.
10264  * @piocb: Pointer to command iocb.
10265  * @flag: Flag indicating if this command can be put into txq.
10266  *
10267  * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue
10268  * an wqe command to an HBA with SLI-4 interface spec.
10269  *
10270  * This function is a lockless version. The function will return success
10271  * after it successfully submit the wqe to firmware or after adding to the
10272  * txq.
10273  **/
10274 static int
10275 __lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,
10276                            struct lpfc_iocbq *piocb, uint32_t flag)
10277 {
10278         int rc;
10279         struct lpfc_io_buf *lpfc_cmd =
10280                 (struct lpfc_io_buf *)piocb->context1;
10281         union lpfc_wqe128 *wqe = &piocb->wqe;
10282         struct sli4_sge *sgl;
10283
10284         /* 128 byte wqe support here */
10285         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
10286
10287         if (phba->fcp_embed_io) {
10288                 struct fcp_cmnd *fcp_cmnd;
10289                 u32 *ptr;
10290
10291                 fcp_cmnd = lpfc_cmd->fcp_cmnd;
10292
10293                 /* Word 0-2 - FCP_CMND */
10294                 wqe->generic.bde.tus.f.bdeFlags =
10295                         BUFF_TYPE_BDE_IMMED;
10296                 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10297                 wqe->generic.bde.addrHigh = 0;
10298                 wqe->generic.bde.addrLow =  88;  /* Word 22 */
10299
10300                 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10301                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10302
10303                 /* Word 22-29  FCP CMND Payload */
10304                 ptr = &wqe->words[22];
10305                 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10306         } else {
10307                 /* Word 0-2 - Inline BDE */
10308                 wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
10309                 wqe->generic.bde.tus.f.bdeSize = sizeof(struct fcp_cmnd);
10310                 wqe->generic.bde.addrHigh = sgl->addr_hi;
10311                 wqe->generic.bde.addrLow =  sgl->addr_lo;
10312
10313                 /* Word 10 */
10314                 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10315                 bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
10316         }
10317
10318         rc = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);
10319         return rc;
10320 }
10321
10322 /**
10323  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
10324  * @phba: Pointer to HBA context object.
10325  * @ring_number: SLI ring number to issue iocb on.
10326  * @piocb: Pointer to command iocb.
10327  * @flag: Flag indicating if this command can be put into txq.
10328  *
10329  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
10330  * an iocb command to an HBA with SLI-4 interface spec.
10331  *
10332  * This function is called with ringlock held. The function will return success
10333  * after it successfully submit the iocb to firmware or after adding to the
10334  * txq.
10335  **/
10336 static int
10337 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
10338                          struct lpfc_iocbq *piocb, uint32_t flag)
10339 {
10340         struct lpfc_sglq *sglq;
10341         union lpfc_wqe128 wqe;
10342         struct lpfc_queue *wq;
10343         struct lpfc_sli_ring *pring;
10344
10345         /* Get the WQ */
10346         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
10347             (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
10348                 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
10349         } else {
10350                 wq = phba->sli4_hba.els_wq;
10351         }
10352
10353         /* Get corresponding ring */
10354         pring = wq->pring;
10355
10356         /*
10357          * The WQE can be either 64 or 128 bytes,
10358          */
10359
10360         lockdep_assert_held(&pring->ring_lock);
10361
10362         if (piocb->sli4_xritag == NO_XRI) {
10363                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10364                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
10365                         sglq = NULL;
10366                 else {
10367                         if (!list_empty(&pring->txq)) {
10368                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
10369                                         __lpfc_sli_ringtx_put(phba,
10370                                                 pring, piocb);
10371                                         return IOCB_SUCCESS;
10372                                 } else {
10373                                         return IOCB_BUSY;
10374                                 }
10375                         } else {
10376                                 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
10377                                 if (!sglq) {
10378                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
10379                                                 __lpfc_sli_ringtx_put(phba,
10380                                                                 pring,
10381                                                                 piocb);
10382                                                 return IOCB_SUCCESS;
10383                                         } else
10384                                                 return IOCB_BUSY;
10385                                 }
10386                         }
10387                 }
10388         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
10389                 /* These IO's already have an XRI and a mapped sgl. */
10390                 sglq = NULL;
10391         }
10392         else {
10393                 /*
10394                  * This is a continuation of a commandi,(CX) so this
10395                  * sglq is on the active list
10396                  */
10397                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
10398                 if (!sglq)
10399                         return IOCB_ERROR;
10400         }
10401
10402         if (sglq) {
10403                 piocb->sli4_lxritag = sglq->sli4_lxritag;
10404                 piocb->sli4_xritag = sglq->sli4_xritag;
10405                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
10406                         return IOCB_ERROR;
10407         }
10408
10409         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
10410                 return IOCB_ERROR;
10411
10412         if (lpfc_sli4_wq_put(wq, &wqe))
10413                 return IOCB_ERROR;
10414         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
10415
10416         return 0;
10417 }
10418
10419 /**
10420  * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o
10421  *
10422  * This routine wraps the actual fcp i/o function for issusing WQE for sli-4
10423  * or IOCB for sli-3  function.
10424  * pointer from the lpfc_hba struct.
10425  *
10426  * Return codes:
10427  * IOCB_ERROR - Error
10428  * IOCB_SUCCESS - Success
10429  * IOCB_BUSY - Busy
10430  **/
10431 int
10432 lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,
10433                       struct lpfc_iocbq *piocb, uint32_t flag)
10434 {
10435         return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);
10436 }
10437
10438 /*
10439  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
10440  *
10441  * This routine wraps the actual lockless version for issusing IOCB function
10442  * pointer from the lpfc_hba struct.
10443  *
10444  * Return codes:
10445  * IOCB_ERROR - Error
10446  * IOCB_SUCCESS - Success
10447  * IOCB_BUSY - Busy
10448  **/
10449 int
10450 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10451                 struct lpfc_iocbq *piocb, uint32_t flag)
10452 {
10453         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10454 }
10455
10456 /**
10457  * lpfc_sli_api_table_setup - Set up sli api function jump table
10458  * @phba: The hba struct for which this call is being executed.
10459  * @dev_grp: The HBA PCI-Device group number.
10460  *
10461  * This routine sets up the SLI interface API function jump table in @phba
10462  * struct.
10463  * Returns: 0 - success, -ENODEV - failure.
10464  **/
10465 int
10466 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
10467 {
10468
10469         switch (dev_grp) {
10470         case LPFC_PCI_DEV_LP:
10471                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
10472                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
10473                 phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;
10474                 break;
10475         case LPFC_PCI_DEV_OC:
10476                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
10477                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
10478                 phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;
10479                 break;
10480         default:
10481                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10482                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
10483                                 dev_grp);
10484                 return -ENODEV;
10485         }
10486         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
10487         return 0;
10488 }
10489
10490 /**
10491  * lpfc_sli4_calc_ring - Calculates which ring to use
10492  * @phba: Pointer to HBA context object.
10493  * @piocb: Pointer to command iocb.
10494  *
10495  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
10496  * hba_wqidx, thus we need to calculate the corresponding ring.
10497  * Since ABORTS must go on the same WQ of the command they are
10498  * aborting, we use command's hba_wqidx.
10499  */
10500 struct lpfc_sli_ring *
10501 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
10502 {
10503         struct lpfc_io_buf *lpfc_cmd;
10504
10505         if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
10506                 if (unlikely(!phba->sli4_hba.hdwq))
10507                         return NULL;
10508                 /*
10509                  * for abort iocb hba_wqidx should already
10510                  * be setup based on what work queue we used.
10511                  */
10512                 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
10513                         lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
10514                         piocb->hba_wqidx = lpfc_cmd->hdwq_no;
10515                 }
10516                 return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
10517         } else {
10518                 if (unlikely(!phba->sli4_hba.els_wq))
10519                         return NULL;
10520                 piocb->hba_wqidx = 0;
10521                 return phba->sli4_hba.els_wq->pring;
10522         }
10523 }
10524
10525 /**
10526  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
10527  * @phba: Pointer to HBA context object.
10528  * @ring_number: Ring number
10529  * @piocb: Pointer to command iocb.
10530  * @flag: Flag indicating if this command can be put into txq.
10531  *
10532  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10533  * function. This function gets the hbalock and calls
10534  * __lpfc_sli_issue_iocb function and will return the error returned
10535  * by __lpfc_sli_issue_iocb function. This wrapper is used by
10536  * functions which do not hold hbalock.
10537  **/
10538 int
10539 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10540                     struct lpfc_iocbq *piocb, uint32_t flag)
10541 {
10542         struct lpfc_sli_ring *pring;
10543         struct lpfc_queue *eq;
10544         unsigned long iflags;
10545         int rc;
10546
10547         if (phba->sli_rev == LPFC_SLI_REV4) {
10548                 eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
10549
10550                 pring = lpfc_sli4_calc_ring(phba, piocb);
10551                 if (unlikely(pring == NULL))
10552                         return IOCB_ERROR;
10553
10554                 spin_lock_irqsave(&pring->ring_lock, iflags);
10555                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10556                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10557
10558                 lpfc_sli4_poll_eq(eq, LPFC_POLL_FASTPATH);
10559         } else {
10560                 /* For now, SLI2/3 will still use hbalock */
10561                 spin_lock_irqsave(&phba->hbalock, iflags);
10562                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10563                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10564         }
10565         return rc;
10566 }
10567
10568 /**
10569  * lpfc_extra_ring_setup - Extra ring setup function
10570  * @phba: Pointer to HBA context object.
10571  *
10572  * This function is called while driver attaches with the
10573  * HBA to setup the extra ring. The extra ring is used
10574  * only when driver needs to support target mode functionality
10575  * or IP over FC functionalities.
10576  *
10577  * This function is called with no lock held. SLI3 only.
10578  **/
10579 static int
10580 lpfc_extra_ring_setup( struct lpfc_hba *phba)
10581 {
10582         struct lpfc_sli *psli;
10583         struct lpfc_sli_ring *pring;
10584
10585         psli = &phba->sli;
10586
10587         /* Adjust cmd/rsp ring iocb entries more evenly */
10588
10589         /* Take some away from the FCP ring */
10590         pring = &psli->sli3_ring[LPFC_FCP_RING];
10591         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10592         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10593         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10594         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10595
10596         /* and give them to the extra ring */
10597         pring = &psli->sli3_ring[LPFC_EXTRA_RING];
10598
10599         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10600         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10601         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10602         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10603
10604         /* Setup default profile for this ring */
10605         pring->iotag_max = 4096;
10606         pring->num_mask = 1;
10607         pring->prt[0].profile = 0;      /* Mask 0 */
10608         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10609         pring->prt[0].type = phba->cfg_multi_ring_type;
10610         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10611         return 0;
10612 }
10613
10614 static void
10615 lpfc_sli_post_recovery_event(struct lpfc_hba *phba,
10616                              struct lpfc_nodelist *ndlp)
10617 {
10618         unsigned long iflags;
10619         struct lpfc_work_evt  *evtp = &ndlp->recovery_evt;
10620
10621         spin_lock_irqsave(&phba->hbalock, iflags);
10622         if (!list_empty(&evtp->evt_listp)) {
10623                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10624                 return;
10625         }
10626
10627         /* Incrementing the reference count until the queued work is done. */
10628         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
10629         if (!evtp->evt_arg1) {
10630                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10631                 return;
10632         }
10633         evtp->evt = LPFC_EVT_RECOVER_PORT;
10634         list_add_tail(&evtp->evt_listp, &phba->work_list);
10635         spin_unlock_irqrestore(&phba->hbalock, iflags);
10636
10637         lpfc_worker_wake_up(phba);
10638 }
10639
10640 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10641  * @phba: Pointer to HBA context object.
10642  * @iocbq: Pointer to iocb object.
10643  *
10644  * The async_event handler calls this routine when it receives
10645  * an ASYNC_STATUS_CN event from the port.  The port generates
10646  * this event when an Abort Sequence request to an rport fails
10647  * twice in succession.  The abort could be originated by the
10648  * driver or by the port.  The ABTS could have been for an ELS
10649  * or FCP IO.  The port only generates this event when an ABTS
10650  * fails to complete after one retry.
10651  */
10652 static void
10653 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10654                           struct lpfc_iocbq *iocbq)
10655 {
10656         struct lpfc_nodelist *ndlp = NULL;
10657         uint16_t rpi = 0, vpi = 0;
10658         struct lpfc_vport *vport = NULL;
10659
10660         /* The rpi in the ulpContext is vport-sensitive. */
10661         vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10662         rpi = iocbq->iocb.ulpContext;
10663
10664         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10665                         "3092 Port generated ABTS async event "
10666                         "on vpi %d rpi %d status 0x%x\n",
10667                         vpi, rpi, iocbq->iocb.ulpStatus);
10668
10669         vport = lpfc_find_vport_by_vpid(phba, vpi);
10670         if (!vport)
10671                 goto err_exit;
10672         ndlp = lpfc_findnode_rpi(vport, rpi);
10673         if (!ndlp)
10674                 goto err_exit;
10675
10676         if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10677                 lpfc_sli_abts_recover_port(vport, ndlp);
10678         return;
10679
10680  err_exit:
10681         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10682                         "3095 Event Context not found, no "
10683                         "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10684                         iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10685                         vpi, rpi);
10686 }
10687
10688 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10689  * @phba: pointer to HBA context object.
10690  * @ndlp: nodelist pointer for the impacted rport.
10691  * @axri: pointer to the wcqe containing the failed exchange.
10692  *
10693  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10694  * port.  The port generates this event when an abort exchange request to an
10695  * rport fails twice in succession with no reply.  The abort could be originated
10696  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
10697  */
10698 void
10699 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10700                            struct lpfc_nodelist *ndlp,
10701                            struct sli4_wcqe_xri_aborted *axri)
10702 {
10703         uint32_t ext_status = 0;
10704
10705         if (!ndlp) {
10706                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10707                                 "3115 Node Context not found, driver "
10708                                 "ignoring abts err event\n");
10709                 return;
10710         }
10711
10712         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10713                         "3116 Port generated FCP XRI ABORT event on "
10714                         "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
10715                         ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
10716                         bf_get(lpfc_wcqe_xa_xri, axri),
10717                         bf_get(lpfc_wcqe_xa_status, axri),
10718                         axri->parameter);
10719
10720         /*
10721          * Catch the ABTS protocol failure case.  Older OCe FW releases returned
10722          * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10723          * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10724          */
10725         ext_status = axri->parameter & IOERR_PARAM_MASK;
10726         if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10727             ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
10728                 lpfc_sli_post_recovery_event(phba, ndlp);
10729 }
10730
10731 /**
10732  * lpfc_sli_async_event_handler - ASYNC iocb handler function
10733  * @phba: Pointer to HBA context object.
10734  * @pring: Pointer to driver SLI ring object.
10735  * @iocbq: Pointer to iocb object.
10736  *
10737  * This function is called by the slow ring event handler
10738  * function when there is an ASYNC event iocb in the ring.
10739  * This function is called with no lock held.
10740  * Currently this function handles only temperature related
10741  * ASYNC events. The function decodes the temperature sensor
10742  * event message and posts events for the management applications.
10743  **/
10744 static void
10745 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10746         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10747 {
10748         IOCB_t *icmd;
10749         uint16_t evt_code;
10750         struct temp_event temp_event_data;
10751         struct Scsi_Host *shost;
10752         uint32_t *iocb_w;
10753
10754         icmd = &iocbq->iocb;
10755         evt_code = icmd->un.asyncstat.evt_code;
10756
10757         switch (evt_code) {
10758         case ASYNC_TEMP_WARN:
10759         case ASYNC_TEMP_SAFE:
10760                 temp_event_data.data = (uint32_t) icmd->ulpContext;
10761                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10762                 if (evt_code == ASYNC_TEMP_WARN) {
10763                         temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10764                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10765                                 "0347 Adapter is very hot, please take "
10766                                 "corrective action. temperature : %d Celsius\n",
10767                                 (uint32_t) icmd->ulpContext);
10768                 } else {
10769                         temp_event_data.event_code = LPFC_NORMAL_TEMP;
10770                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10771                                 "0340 Adapter temperature is OK now. "
10772                                 "temperature : %d Celsius\n",
10773                                 (uint32_t) icmd->ulpContext);
10774                 }
10775
10776                 /* Send temperature change event to applications */
10777                 shost = lpfc_shost_from_vport(phba->pport);
10778                 fc_host_post_vendor_event(shost, fc_get_event_number(),
10779                         sizeof(temp_event_data), (char *) &temp_event_data,
10780                         LPFC_NL_VENDOR_ID);
10781                 break;
10782         case ASYNC_STATUS_CN:
10783                 lpfc_sli_abts_err_handler(phba, iocbq);
10784                 break;
10785         default:
10786                 iocb_w = (uint32_t *) icmd;
10787                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10788                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
10789                         " evt_code 0x%x\n"
10790                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
10791                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
10792                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
10793                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
10794                         pring->ringno, icmd->un.asyncstat.evt_code,
10795                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10796                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10797                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10798                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10799
10800                 break;
10801         }
10802 }
10803
10804
10805 /**
10806  * lpfc_sli4_setup - SLI ring setup function
10807  * @phba: Pointer to HBA context object.
10808  *
10809  * lpfc_sli_setup sets up rings of the SLI interface with
10810  * number of iocbs per ring and iotags. This function is
10811  * called while driver attach to the HBA and before the
10812  * interrupts are enabled. So there is no need for locking.
10813  *
10814  * This function always returns 0.
10815  **/
10816 int
10817 lpfc_sli4_setup(struct lpfc_hba *phba)
10818 {
10819         struct lpfc_sli_ring *pring;
10820
10821         pring = phba->sli4_hba.els_wq->pring;
10822         pring->num_mask = LPFC_MAX_RING_MASK;
10823         pring->prt[0].profile = 0;      /* Mask 0 */
10824         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10825         pring->prt[0].type = FC_TYPE_ELS;
10826         pring->prt[0].lpfc_sli_rcv_unsol_event =
10827             lpfc_els_unsol_event;
10828         pring->prt[1].profile = 0;      /* Mask 1 */
10829         pring->prt[1].rctl = FC_RCTL_ELS_REP;
10830         pring->prt[1].type = FC_TYPE_ELS;
10831         pring->prt[1].lpfc_sli_rcv_unsol_event =
10832             lpfc_els_unsol_event;
10833         pring->prt[2].profile = 0;      /* Mask 2 */
10834         /* NameServer Inquiry */
10835         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10836         /* NameServer */
10837         pring->prt[2].type = FC_TYPE_CT;
10838         pring->prt[2].lpfc_sli_rcv_unsol_event =
10839             lpfc_ct_unsol_event;
10840         pring->prt[3].profile = 0;      /* Mask 3 */
10841         /* NameServer response */
10842         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10843         /* NameServer */
10844         pring->prt[3].type = FC_TYPE_CT;
10845         pring->prt[3].lpfc_sli_rcv_unsol_event =
10846             lpfc_ct_unsol_event;
10847         return 0;
10848 }
10849
10850 /**
10851  * lpfc_sli_setup - SLI ring setup function
10852  * @phba: Pointer to HBA context object.
10853  *
10854  * lpfc_sli_setup sets up rings of the SLI interface with
10855  * number of iocbs per ring and iotags. This function is
10856  * called while driver attach to the HBA and before the
10857  * interrupts are enabled. So there is no need for locking.
10858  *
10859  * This function always returns 0. SLI3 only.
10860  **/
10861 int
10862 lpfc_sli_setup(struct lpfc_hba *phba)
10863 {
10864         int i, totiocbsize = 0;
10865         struct lpfc_sli *psli = &phba->sli;
10866         struct lpfc_sli_ring *pring;
10867
10868         psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
10869         psli->sli_flag = 0;
10870
10871         psli->iocbq_lookup = NULL;
10872         psli->iocbq_lookup_len = 0;
10873         psli->last_iotag = 0;
10874
10875         for (i = 0; i < psli->num_rings; i++) {
10876                 pring = &psli->sli3_ring[i];
10877                 switch (i) {
10878                 case LPFC_FCP_RING:     /* ring 0 - FCP */
10879                         /* numCiocb and numRiocb are used in config_port */
10880                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10881                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10882                         pring->sli.sli3.numCiocb +=
10883                                 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10884                         pring->sli.sli3.numRiocb +=
10885                                 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10886                         pring->sli.sli3.numCiocb +=
10887                                 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10888                         pring->sli.sli3.numRiocb +=
10889                                 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10890                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10891                                                         SLI3_IOCB_CMD_SIZE :
10892                                                         SLI2_IOCB_CMD_SIZE;
10893                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10894                                                         SLI3_IOCB_RSP_SIZE :
10895                                                         SLI2_IOCB_RSP_SIZE;
10896                         pring->iotag_ctr = 0;
10897                         pring->iotag_max =
10898                             (phba->cfg_hba_queue_depth * 2);
10899                         pring->fast_iotag = pring->iotag_max;
10900                         pring->num_mask = 0;
10901                         break;
10902                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
10903                         /* numCiocb and numRiocb are used in config_port */
10904                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10905                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10906                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10907                                                         SLI3_IOCB_CMD_SIZE :
10908                                                         SLI2_IOCB_CMD_SIZE;
10909                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10910                                                         SLI3_IOCB_RSP_SIZE :
10911                                                         SLI2_IOCB_RSP_SIZE;
10912                         pring->iotag_max = phba->cfg_hba_queue_depth;
10913                         pring->num_mask = 0;
10914                         break;
10915                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
10916                         /* numCiocb and numRiocb are used in config_port */
10917                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10918                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10919                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10920                                                         SLI3_IOCB_CMD_SIZE :
10921                                                         SLI2_IOCB_CMD_SIZE;
10922                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10923                                                         SLI3_IOCB_RSP_SIZE :
10924                                                         SLI2_IOCB_RSP_SIZE;
10925                         pring->fast_iotag = 0;
10926                         pring->iotag_ctr = 0;
10927                         pring->iotag_max = 4096;
10928                         pring->lpfc_sli_rcv_async_status =
10929                                 lpfc_sli_async_event_handler;
10930                         pring->num_mask = LPFC_MAX_RING_MASK;
10931                         pring->prt[0].profile = 0;      /* Mask 0 */
10932                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10933                         pring->prt[0].type = FC_TYPE_ELS;
10934                         pring->prt[0].lpfc_sli_rcv_unsol_event =
10935                             lpfc_els_unsol_event;
10936                         pring->prt[1].profile = 0;      /* Mask 1 */
10937                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
10938                         pring->prt[1].type = FC_TYPE_ELS;
10939                         pring->prt[1].lpfc_sli_rcv_unsol_event =
10940                             lpfc_els_unsol_event;
10941                         pring->prt[2].profile = 0;      /* Mask 2 */
10942                         /* NameServer Inquiry */
10943                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10944                         /* NameServer */
10945                         pring->prt[2].type = FC_TYPE_CT;
10946                         pring->prt[2].lpfc_sli_rcv_unsol_event =
10947                             lpfc_ct_unsol_event;
10948                         pring->prt[3].profile = 0;      /* Mask 3 */
10949                         /* NameServer response */
10950                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10951                         /* NameServer */
10952                         pring->prt[3].type = FC_TYPE_CT;
10953                         pring->prt[3].lpfc_sli_rcv_unsol_event =
10954                             lpfc_ct_unsol_event;
10955                         break;
10956                 }
10957                 totiocbsize += (pring->sli.sli3.numCiocb *
10958                         pring->sli.sli3.sizeCiocb) +
10959                         (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
10960         }
10961         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
10962                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
10963                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10964                        "SLI2 SLIM Data: x%x x%lx\n",
10965                        phba->brd_no, totiocbsize,
10966                        (unsigned long) MAX_SLIM_IOCB_SIZE);
10967         }
10968         if (phba->cfg_multi_ring_support == 2)
10969                 lpfc_extra_ring_setup(phba);
10970
10971         return 0;
10972 }
10973
10974 /**
10975  * lpfc_sli4_queue_init - Queue initialization function
10976  * @phba: Pointer to HBA context object.
10977  *
10978  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
10979  * ring. This function also initializes ring indices of each ring.
10980  * This function is called during the initialization of the SLI
10981  * interface of an HBA.
10982  * This function is called with no lock held and always returns
10983  * 1.
10984  **/
10985 void
10986 lpfc_sli4_queue_init(struct lpfc_hba *phba)
10987 {
10988         struct lpfc_sli *psli;
10989         struct lpfc_sli_ring *pring;
10990         int i;
10991
10992         psli = &phba->sli;
10993         spin_lock_irq(&phba->hbalock);
10994         INIT_LIST_HEAD(&psli->mboxq);
10995         INIT_LIST_HEAD(&psli->mboxq_cmpl);
10996         /* Initialize list headers for txq and txcmplq as double linked lists */
10997         for (i = 0; i < phba->cfg_hdw_queue; i++) {
10998                 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
10999                 pring->flag = 0;
11000                 pring->ringno = LPFC_FCP_RING;
11001                 pring->txcmplq_cnt = 0;
11002                 INIT_LIST_HEAD(&pring->txq);
11003                 INIT_LIST_HEAD(&pring->txcmplq);
11004                 INIT_LIST_HEAD(&pring->iocb_continueq);
11005                 spin_lock_init(&pring->ring_lock);
11006         }
11007         pring = phba->sli4_hba.els_wq->pring;
11008         pring->flag = 0;
11009         pring->ringno = LPFC_ELS_RING;
11010         pring->txcmplq_cnt = 0;
11011         INIT_LIST_HEAD(&pring->txq);
11012         INIT_LIST_HEAD(&pring->txcmplq);
11013         INIT_LIST_HEAD(&pring->iocb_continueq);
11014         spin_lock_init(&pring->ring_lock);
11015
11016         if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11017                 pring = phba->sli4_hba.nvmels_wq->pring;
11018                 pring->flag = 0;
11019                 pring->ringno = LPFC_ELS_RING;
11020                 pring->txcmplq_cnt = 0;
11021                 INIT_LIST_HEAD(&pring->txq);
11022                 INIT_LIST_HEAD(&pring->txcmplq);
11023                 INIT_LIST_HEAD(&pring->iocb_continueq);
11024                 spin_lock_init(&pring->ring_lock);
11025         }
11026
11027         spin_unlock_irq(&phba->hbalock);
11028 }
11029
11030 /**
11031  * lpfc_sli_queue_init - Queue initialization function
11032  * @phba: Pointer to HBA context object.
11033  *
11034  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
11035  * ring. This function also initializes ring indices of each ring.
11036  * This function is called during the initialization of the SLI
11037  * interface of an HBA.
11038  * This function is called with no lock held and always returns
11039  * 1.
11040  **/
11041 void
11042 lpfc_sli_queue_init(struct lpfc_hba *phba)
11043 {
11044         struct lpfc_sli *psli;
11045         struct lpfc_sli_ring *pring;
11046         int i;
11047
11048         psli = &phba->sli;
11049         spin_lock_irq(&phba->hbalock);
11050         INIT_LIST_HEAD(&psli->mboxq);
11051         INIT_LIST_HEAD(&psli->mboxq_cmpl);
11052         /* Initialize list headers for txq and txcmplq as double linked lists */
11053         for (i = 0; i < psli->num_rings; i++) {
11054                 pring = &psli->sli3_ring[i];
11055                 pring->ringno = i;
11056                 pring->sli.sli3.next_cmdidx  = 0;
11057                 pring->sli.sli3.local_getidx = 0;
11058                 pring->sli.sli3.cmdidx = 0;
11059                 INIT_LIST_HEAD(&pring->iocb_continueq);
11060                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
11061                 INIT_LIST_HEAD(&pring->postbufq);
11062                 pring->flag = 0;
11063                 INIT_LIST_HEAD(&pring->txq);
11064                 INIT_LIST_HEAD(&pring->txcmplq);
11065                 spin_lock_init(&pring->ring_lock);
11066         }
11067         spin_unlock_irq(&phba->hbalock);
11068 }
11069
11070 /**
11071  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
11072  * @phba: Pointer to HBA context object.
11073  *
11074  * This routine flushes the mailbox command subsystem. It will unconditionally
11075  * flush all the mailbox commands in the three possible stages in the mailbox
11076  * command sub-system: pending mailbox command queue; the outstanding mailbox
11077  * command; and completed mailbox command queue. It is caller's responsibility
11078  * to make sure that the driver is in the proper state to flush the mailbox
11079  * command sub-system. Namely, the posting of mailbox commands into the
11080  * pending mailbox command queue from the various clients must be stopped;
11081  * either the HBA is in a state that it will never works on the outstanding
11082  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
11083  * mailbox command has been completed.
11084  **/
11085 static void
11086 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
11087 {
11088         LIST_HEAD(completions);
11089         struct lpfc_sli *psli = &phba->sli;
11090         LPFC_MBOXQ_t *pmb;
11091         unsigned long iflag;
11092
11093         /* Disable softirqs, including timers from obtaining phba->hbalock */
11094         local_bh_disable();
11095
11096         /* Flush all the mailbox commands in the mbox system */
11097         spin_lock_irqsave(&phba->hbalock, iflag);
11098
11099         /* The pending mailbox command queue */
11100         list_splice_init(&phba->sli.mboxq, &completions);
11101         /* The outstanding active mailbox command */
11102         if (psli->mbox_active) {
11103                 list_add_tail(&psli->mbox_active->list, &completions);
11104                 psli->mbox_active = NULL;
11105                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11106         }
11107         /* The completed mailbox command queue */
11108         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
11109         spin_unlock_irqrestore(&phba->hbalock, iflag);
11110
11111         /* Enable softirqs again, done with phba->hbalock */
11112         local_bh_enable();
11113
11114         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
11115         while (!list_empty(&completions)) {
11116                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
11117                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
11118                 if (pmb->mbox_cmpl)
11119                         pmb->mbox_cmpl(phba, pmb);
11120         }
11121 }
11122
11123 /**
11124  * lpfc_sli_host_down - Vport cleanup function
11125  * @vport: Pointer to virtual port object.
11126  *
11127  * lpfc_sli_host_down is called to clean up the resources
11128  * associated with a vport before destroying virtual
11129  * port data structures.
11130  * This function does following operations:
11131  * - Free discovery resources associated with this virtual
11132  *   port.
11133  * - Free iocbs associated with this virtual port in
11134  *   the txq.
11135  * - Send abort for all iocb commands associated with this
11136  *   vport in txcmplq.
11137  *
11138  * This function is called with no lock held and always returns 1.
11139  **/
11140 int
11141 lpfc_sli_host_down(struct lpfc_vport *vport)
11142 {
11143         LIST_HEAD(completions);
11144         struct lpfc_hba *phba = vport->phba;
11145         struct lpfc_sli *psli = &phba->sli;
11146         struct lpfc_queue *qp = NULL;
11147         struct lpfc_sli_ring *pring;
11148         struct lpfc_iocbq *iocb, *next_iocb;
11149         int i;
11150         unsigned long flags = 0;
11151         uint16_t prev_pring_flag;
11152
11153         lpfc_cleanup_discovery_resources(vport);
11154
11155         spin_lock_irqsave(&phba->hbalock, flags);
11156
11157         /*
11158          * Error everything on the txq since these iocbs
11159          * have not been given to the FW yet.
11160          * Also issue ABTS for everything on the txcmplq
11161          */
11162         if (phba->sli_rev != LPFC_SLI_REV4) {
11163                 for (i = 0; i < psli->num_rings; i++) {
11164                         pring = &psli->sli3_ring[i];
11165                         prev_pring_flag = pring->flag;
11166                         /* Only slow rings */
11167                         if (pring->ringno == LPFC_ELS_RING) {
11168                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11169                                 /* Set the lpfc data pending flag */
11170                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
11171                         }
11172                         list_for_each_entry_safe(iocb, next_iocb,
11173                                                  &pring->txq, list) {
11174                                 if (iocb->vport != vport)
11175                                         continue;
11176                                 list_move_tail(&iocb->list, &completions);
11177                         }
11178                         list_for_each_entry_safe(iocb, next_iocb,
11179                                                  &pring->txcmplq, list) {
11180                                 if (iocb->vport != vport)
11181                                         continue;
11182                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11183                                                            NULL);
11184                         }
11185                         pring->flag = prev_pring_flag;
11186                 }
11187         } else {
11188                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11189                         pring = qp->pring;
11190                         if (!pring)
11191                                 continue;
11192                         if (pring == phba->sli4_hba.els_wq->pring) {
11193                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11194                                 /* Set the lpfc data pending flag */
11195                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
11196                         }
11197                         prev_pring_flag = pring->flag;
11198                         spin_lock(&pring->ring_lock);
11199                         list_for_each_entry_safe(iocb, next_iocb,
11200                                                  &pring->txq, list) {
11201                                 if (iocb->vport != vport)
11202                                         continue;
11203                                 list_move_tail(&iocb->list, &completions);
11204                         }
11205                         spin_unlock(&pring->ring_lock);
11206                         list_for_each_entry_safe(iocb, next_iocb,
11207                                                  &pring->txcmplq, list) {
11208                                 if (iocb->vport != vport)
11209                                         continue;
11210                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11211                                                            NULL);
11212                         }
11213                         pring->flag = prev_pring_flag;
11214                 }
11215         }
11216         spin_unlock_irqrestore(&phba->hbalock, flags);
11217
11218         /* Cancel all the IOCBs from the completions list */
11219         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11220                               IOERR_SLI_DOWN);
11221         return 1;
11222 }
11223
11224 /**
11225  * lpfc_sli_hba_down - Resource cleanup function for the HBA
11226  * @phba: Pointer to HBA context object.
11227  *
11228  * This function cleans up all iocb, buffers, mailbox commands
11229  * while shutting down the HBA. This function is called with no
11230  * lock held and always returns 1.
11231  * This function does the following to cleanup driver resources:
11232  * - Free discovery resources for each virtual port
11233  * - Cleanup any pending fabric iocbs
11234  * - Iterate through the iocb txq and free each entry
11235  *   in the list.
11236  * - Free up any buffer posted to the HBA
11237  * - Free mailbox commands in the mailbox queue.
11238  **/
11239 int
11240 lpfc_sli_hba_down(struct lpfc_hba *phba)
11241 {
11242         LIST_HEAD(completions);
11243         struct lpfc_sli *psli = &phba->sli;
11244         struct lpfc_queue *qp = NULL;
11245         struct lpfc_sli_ring *pring;
11246         struct lpfc_dmabuf *buf_ptr;
11247         unsigned long flags = 0;
11248         int i;
11249
11250         /* Shutdown the mailbox command sub-system */
11251         lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
11252
11253         lpfc_hba_down_prep(phba);
11254
11255         /* Disable softirqs, including timers from obtaining phba->hbalock */
11256         local_bh_disable();
11257
11258         lpfc_fabric_abort_hba(phba);
11259
11260         spin_lock_irqsave(&phba->hbalock, flags);
11261
11262         /*
11263          * Error everything on the txq since these iocbs
11264          * have not been given to the FW yet.
11265          */
11266         if (phba->sli_rev != LPFC_SLI_REV4) {
11267                 for (i = 0; i < psli->num_rings; i++) {
11268                         pring = &psli->sli3_ring[i];
11269                         /* Only slow rings */
11270                         if (pring->ringno == LPFC_ELS_RING) {
11271                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11272                                 /* Set the lpfc data pending flag */
11273                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
11274                         }
11275                         list_splice_init(&pring->txq, &completions);
11276                 }
11277         } else {
11278                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11279                         pring = qp->pring;
11280                         if (!pring)
11281                                 continue;
11282                         spin_lock(&pring->ring_lock);
11283                         list_splice_init(&pring->txq, &completions);
11284                         spin_unlock(&pring->ring_lock);
11285                         if (pring == phba->sli4_hba.els_wq->pring) {
11286                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11287                                 /* Set the lpfc data pending flag */
11288                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
11289                         }
11290                 }
11291         }
11292         spin_unlock_irqrestore(&phba->hbalock, flags);
11293
11294         /* Cancel all the IOCBs from the completions list */
11295         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11296                               IOERR_SLI_DOWN);
11297
11298         spin_lock_irqsave(&phba->hbalock, flags);
11299         list_splice_init(&phba->elsbuf, &completions);
11300         phba->elsbuf_cnt = 0;
11301         phba->elsbuf_prev_cnt = 0;
11302         spin_unlock_irqrestore(&phba->hbalock, flags);
11303
11304         while (!list_empty(&completions)) {
11305                 list_remove_head(&completions, buf_ptr,
11306                         struct lpfc_dmabuf, list);
11307                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
11308                 kfree(buf_ptr);
11309         }
11310
11311         /* Enable softirqs again, done with phba->hbalock */
11312         local_bh_enable();
11313
11314         /* Return any active mbox cmds */
11315         del_timer_sync(&psli->mbox_tmo);
11316
11317         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
11318         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11319         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
11320
11321         return 1;
11322 }
11323
11324 /**
11325  * lpfc_sli_pcimem_bcopy - SLI memory copy function
11326  * @srcp: Source memory pointer.
11327  * @destp: Destination memory pointer.
11328  * @cnt: Number of words required to be copied.
11329  *
11330  * This function is used for copying data between driver memory
11331  * and the SLI memory. This function also changes the endianness
11332  * of each word if native endianness is different from SLI
11333  * endianness. This function can be called with or without
11334  * lock.
11335  **/
11336 void
11337 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
11338 {
11339         uint32_t *src = srcp;
11340         uint32_t *dest = destp;
11341         uint32_t ldata;
11342         int i;
11343
11344         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
11345                 ldata = *src;
11346                 ldata = le32_to_cpu(ldata);
11347                 *dest = ldata;
11348                 src++;
11349                 dest++;
11350         }
11351 }
11352
11353
11354 /**
11355  * lpfc_sli_bemem_bcopy - SLI memory copy function
11356  * @srcp: Source memory pointer.
11357  * @destp: Destination memory pointer.
11358  * @cnt: Number of words required to be copied.
11359  *
11360  * This function is used for copying data between a data structure
11361  * with big endian representation to local endianness.
11362  * This function can be called with or without lock.
11363  **/
11364 void
11365 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
11366 {
11367         uint32_t *src = srcp;
11368         uint32_t *dest = destp;
11369         uint32_t ldata;
11370         int i;
11371
11372         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
11373                 ldata = *src;
11374                 ldata = be32_to_cpu(ldata);
11375                 *dest = ldata;
11376                 src++;
11377                 dest++;
11378         }
11379 }
11380
11381 /**
11382  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
11383  * @phba: Pointer to HBA context object.
11384  * @pring: Pointer to driver SLI ring object.
11385  * @mp: Pointer to driver buffer object.
11386  *
11387  * This function is called with no lock held.
11388  * It always return zero after adding the buffer to the postbufq
11389  * buffer list.
11390  **/
11391 int
11392 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11393                          struct lpfc_dmabuf *mp)
11394 {
11395         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
11396            later */
11397         spin_lock_irq(&phba->hbalock);
11398         list_add_tail(&mp->list, &pring->postbufq);
11399         pring->postbufq_cnt++;
11400         spin_unlock_irq(&phba->hbalock);
11401         return 0;
11402 }
11403
11404 /**
11405  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
11406  * @phba: Pointer to HBA context object.
11407  *
11408  * When HBQ is enabled, buffers are searched based on tags. This function
11409  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
11410  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
11411  * does not conflict with tags of buffer posted for unsolicited events.
11412  * The function returns the allocated tag. The function is called with
11413  * no locks held.
11414  **/
11415 uint32_t
11416 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
11417 {
11418         spin_lock_irq(&phba->hbalock);
11419         phba->buffer_tag_count++;
11420         /*
11421          * Always set the QUE_BUFTAG_BIT to distiguish between
11422          * a tag assigned by HBQ.
11423          */
11424         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
11425         spin_unlock_irq(&phba->hbalock);
11426         return phba->buffer_tag_count;
11427 }
11428
11429 /**
11430  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
11431  * @phba: Pointer to HBA context object.
11432  * @pring: Pointer to driver SLI ring object.
11433  * @tag: Buffer tag.
11434  *
11435  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
11436  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
11437  * iocb is posted to the response ring with the tag of the buffer.
11438  * This function searches the pring->postbufq list using the tag
11439  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
11440  * iocb. If the buffer is found then lpfc_dmabuf object of the
11441  * buffer is returned to the caller else NULL is returned.
11442  * This function is called with no lock held.
11443  **/
11444 struct lpfc_dmabuf *
11445 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11446                         uint32_t tag)
11447 {
11448         struct lpfc_dmabuf *mp, *next_mp;
11449         struct list_head *slp = &pring->postbufq;
11450
11451         /* Search postbufq, from the beginning, looking for a match on tag */
11452         spin_lock_irq(&phba->hbalock);
11453         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
11454                 if (mp->buffer_tag == tag) {
11455                         list_del_init(&mp->list);
11456                         pring->postbufq_cnt--;
11457                         spin_unlock_irq(&phba->hbalock);
11458                         return mp;
11459                 }
11460         }
11461
11462         spin_unlock_irq(&phba->hbalock);
11463         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11464                         "0402 Cannot find virtual addr for buffer tag on "
11465                         "ring %d Data x%lx x%px x%px x%x\n",
11466                         pring->ringno, (unsigned long) tag,
11467                         slp->next, slp->prev, pring->postbufq_cnt);
11468
11469         return NULL;
11470 }
11471
11472 /**
11473  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
11474  * @phba: Pointer to HBA context object.
11475  * @pring: Pointer to driver SLI ring object.
11476  * @phys: DMA address of the buffer.
11477  *
11478  * This function searches the buffer list using the dma_address
11479  * of unsolicited event to find the driver's lpfc_dmabuf object
11480  * corresponding to the dma_address. The function returns the
11481  * lpfc_dmabuf object if a buffer is found else it returns NULL.
11482  * This function is called by the ct and els unsolicited event
11483  * handlers to get the buffer associated with the unsolicited
11484  * event.
11485  *
11486  * This function is called with no lock held.
11487  **/
11488 struct lpfc_dmabuf *
11489 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11490                          dma_addr_t phys)
11491 {
11492         struct lpfc_dmabuf *mp, *next_mp;
11493         struct list_head *slp = &pring->postbufq;
11494
11495         /* Search postbufq, from the beginning, looking for a match on phys */
11496         spin_lock_irq(&phba->hbalock);
11497         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
11498                 if (mp->phys == phys) {
11499                         list_del_init(&mp->list);
11500                         pring->postbufq_cnt--;
11501                         spin_unlock_irq(&phba->hbalock);
11502                         return mp;
11503                 }
11504         }
11505
11506         spin_unlock_irq(&phba->hbalock);
11507         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11508                         "0410 Cannot find virtual addr for mapped buf on "
11509                         "ring %d Data x%llx x%px x%px x%x\n",
11510                         pring->ringno, (unsigned long long)phys,
11511                         slp->next, slp->prev, pring->postbufq_cnt);
11512         return NULL;
11513 }
11514
11515 /**
11516  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
11517  * @phba: Pointer to HBA context object.
11518  * @cmdiocb: Pointer to driver command iocb object.
11519  * @rspiocb: Pointer to driver response iocb object.
11520  *
11521  * This function is the completion handler for the abort iocbs for
11522  * ELS commands. This function is called from the ELS ring event
11523  * handler with no lock held. This function frees memory resources
11524  * associated with the abort iocb.
11525  **/
11526 static void
11527 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11528                         struct lpfc_iocbq *rspiocb)
11529 {
11530         IOCB_t *irsp = &rspiocb->iocb;
11531         uint16_t abort_iotag, abort_context;
11532         struct lpfc_iocbq *abort_iocb = NULL;
11533
11534         if (irsp->ulpStatus) {
11535
11536                 /*
11537                  * Assume that the port already completed and returned, or
11538                  * will return the iocb. Just Log the message.
11539                  */
11540                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11541                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11542
11543                 spin_lock_irq(&phba->hbalock);
11544                 if (phba->sli_rev < LPFC_SLI_REV4) {
11545                         if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11546                             irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11547                             irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11548                                 spin_unlock_irq(&phba->hbalock);
11549                                 goto release_iocb;
11550                         }
11551                         if (abort_iotag != 0 &&
11552                                 abort_iotag <= phba->sli.last_iotag)
11553                                 abort_iocb =
11554                                         phba->sli.iocbq_lookup[abort_iotag];
11555                 } else
11556                         /* For sli4 the abort_tag is the XRI,
11557                          * so the abort routine puts the iotag  of the iocb
11558                          * being aborted in the context field of the abort
11559                          * IOCB.
11560                          */
11561                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
11562
11563                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
11564                                 "0327 Cannot abort els iocb x%px "
11565                                 "with tag %x context %x, abort status %x, "
11566                                 "abort code %x\n",
11567                                 abort_iocb, abort_iotag, abort_context,
11568                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
11569
11570                 spin_unlock_irq(&phba->hbalock);
11571         }
11572 release_iocb:
11573         lpfc_sli_release_iocbq(phba, cmdiocb);
11574         return;
11575 }
11576
11577 /**
11578  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
11579  * @phba: Pointer to HBA context object.
11580  * @cmdiocb: Pointer to driver command iocb object.
11581  * @rspiocb: Pointer to driver response iocb object.
11582  *
11583  * The function is called from SLI ring event handler with no
11584  * lock held. This function is the completion handler for ELS commands
11585  * which are aborted. The function frees memory resources used for
11586  * the aborted ELS commands.
11587  **/
11588 static void
11589 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11590                      struct lpfc_iocbq *rspiocb)
11591 {
11592         IOCB_t *irsp = &rspiocb->iocb;
11593
11594         /* ELS cmd tag <ulpIoTag> completes */
11595         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11596                         "0139 Ignoring ELS cmd tag x%x completion Data: "
11597                         "x%x x%x x%x\n",
11598                         irsp->ulpIoTag, irsp->ulpStatus,
11599                         irsp->un.ulpWord[4], irsp->ulpTimeout);
11600         lpfc_nlp_put((struct lpfc_nodelist *)cmdiocb->context1);
11601         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11602                 lpfc_ct_free_iocb(phba, cmdiocb);
11603         else
11604                 lpfc_els_free_iocb(phba, cmdiocb);
11605 }
11606
11607 /**
11608  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11609  * @phba: Pointer to HBA context object.
11610  * @pring: Pointer to driver SLI ring object.
11611  * @cmdiocb: Pointer to driver command iocb object.
11612  * @cmpl: completion function.
11613  *
11614  * This function issues an abort iocb for the provided command iocb. In case
11615  * of unloading, the abort iocb will not be issued to commands on the ELS
11616  * ring. Instead, the callback function shall be changed to those commands
11617  * so that nothing happens when them finishes. This function is called with
11618  * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS
11619  * when the command iocb is an abort request.
11620  *
11621  **/
11622 int
11623 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11624                            struct lpfc_iocbq *cmdiocb, void *cmpl)
11625 {
11626         struct lpfc_vport *vport = cmdiocb->vport;
11627         struct lpfc_iocbq *abtsiocbp;
11628         IOCB_t *icmd = NULL;
11629         IOCB_t *iabt = NULL;
11630         int retval = IOCB_ERROR;
11631         unsigned long iflags;
11632         struct lpfc_nodelist *ndlp;
11633
11634         /*
11635          * There are certain command types we don't want to abort.  And we
11636          * don't want to abort commands that are already in the process of
11637          * being aborted.
11638          */
11639         icmd = &cmdiocb->iocb;
11640         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11641             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11642             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11643                 return IOCB_ABORTING;
11644
11645         if (!pring) {
11646                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11647                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11648                 else
11649                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11650                 return retval;
11651         }
11652
11653         /*
11654          * If we're unloading, don't abort iocb on the ELS ring, but change
11655          * the callback so that nothing happens when it finishes.
11656          */
11657         if ((vport->load_flag & FC_UNLOADING) &&
11658             pring->ringno == LPFC_ELS_RING) {
11659                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11660                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11661                 else
11662                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11663                 return retval;
11664         }
11665
11666         /* issue ABTS for this IOCB based on iotag */
11667         abtsiocbp = __lpfc_sli_get_iocbq(phba);
11668         if (abtsiocbp == NULL)
11669                 return IOCB_NORESOURCE;
11670
11671         /* This signals the response to set the correct status
11672          * before calling the completion handler
11673          */
11674         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11675
11676         iabt = &abtsiocbp->iocb;
11677         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11678         iabt->un.acxri.abortContextTag = icmd->ulpContext;
11679         if (phba->sli_rev == LPFC_SLI_REV4) {
11680                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
11681                 if (pring->ringno == LPFC_ELS_RING)
11682                         iabt->un.acxri.abortContextTag = cmdiocb->iotag;
11683         } else {
11684                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
11685                 if (pring->ringno == LPFC_ELS_RING) {
11686                         ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11687                         iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11688                 }
11689         }
11690         iabt->ulpLe = 1;
11691         iabt->ulpClass = icmd->ulpClass;
11692
11693         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11694         abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
11695         if (cmdiocb->iocb_flag & LPFC_IO_FCP) {
11696                 abtsiocbp->iocb_flag |= LPFC_IO_FCP;
11697                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
11698         }
11699         if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11700                 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
11701
11702         if (phba->link_state >= LPFC_LINK_UP)
11703                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11704         else
11705                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11706
11707         if (cmpl)
11708                 abtsiocbp->iocb_cmpl = cmpl;
11709         else
11710                 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
11711         abtsiocbp->vport = vport;
11712
11713         if (phba->sli_rev == LPFC_SLI_REV4) {
11714                 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11715                 if (unlikely(pring == NULL))
11716                         goto abort_iotag_exit;
11717                 /* Note: both hbalock and ring_lock need to be set here */
11718                 spin_lock_irqsave(&pring->ring_lock, iflags);
11719                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11720                         abtsiocbp, 0);
11721                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11722         } else {
11723                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11724                         abtsiocbp, 0);
11725         }
11726
11727 abort_iotag_exit:
11728
11729         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11730                          "0339 Abort xri x%x, original iotag x%x, "
11731                          "abort cmd iotag x%x retval x%x\n",
11732                          iabt->un.acxri.abortIoTag,
11733                          iabt->un.acxri.abortContextTag,
11734                          abtsiocbp->iotag, retval);
11735
11736         if (retval) {
11737                 cmdiocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11738                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
11739         }
11740
11741         /*
11742          * Caller to this routine should check for IOCB_ERROR
11743          * and handle it properly.  This routine no longer removes
11744          * iocb off txcmplq and call compl in case of IOCB_ERROR.
11745          */
11746         return retval;
11747 }
11748
11749 /**
11750  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11751  * @phba: pointer to lpfc HBA data structure.
11752  *
11753  * This routine will abort all pending and outstanding iocbs to an HBA.
11754  **/
11755 void
11756 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11757 {
11758         struct lpfc_sli *psli = &phba->sli;
11759         struct lpfc_sli_ring *pring;
11760         struct lpfc_queue *qp = NULL;
11761         int i;
11762
11763         if (phba->sli_rev != LPFC_SLI_REV4) {
11764                 for (i = 0; i < psli->num_rings; i++) {
11765                         pring = &psli->sli3_ring[i];
11766                         lpfc_sli_abort_iocb_ring(phba, pring);
11767                 }
11768                 return;
11769         }
11770         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11771                 pring = qp->pring;
11772                 if (!pring)
11773                         continue;
11774                 lpfc_sli_abort_iocb_ring(phba, pring);
11775         }
11776 }
11777
11778 /**
11779  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
11780  * @iocbq: Pointer to driver iocb object.
11781  * @vport: Pointer to driver virtual port object.
11782  * @tgt_id: SCSI ID of the target.
11783  * @lun_id: LUN ID of the scsi device.
11784  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11785  *
11786  * This function acts as an iocb filter for functions which abort or count
11787  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11788  * 0 if the filtering criteria is met for the given iocb and will return
11789  * 1 if the filtering criteria is not met.
11790  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11791  * given iocb is for the SCSI device specified by vport, tgt_id and
11792  * lun_id parameter.
11793  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
11794  * given iocb is for the SCSI target specified by vport and tgt_id
11795  * parameters.
11796  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11797  * given iocb is for the SCSI host associated with the given vport.
11798  * This function is called with no locks held.
11799  **/
11800 static int
11801 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11802                            uint16_t tgt_id, uint64_t lun_id,
11803                            lpfc_ctx_cmd ctx_cmd)
11804 {
11805         struct lpfc_io_buf *lpfc_cmd;
11806         int rc = 1;
11807
11808         if (iocbq->vport != vport)
11809                 return rc;
11810
11811         if (!(iocbq->iocb_flag &  LPFC_IO_FCP) ||
11812             !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
11813                 return rc;
11814
11815         lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
11816
11817         if (lpfc_cmd->pCmd == NULL)
11818                 return rc;
11819
11820         switch (ctx_cmd) {
11821         case LPFC_CTX_LUN:
11822                 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
11823                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11824                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
11825                         rc = 0;
11826                 break;
11827         case LPFC_CTX_TGT:
11828                 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
11829                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
11830                         rc = 0;
11831                 break;
11832         case LPFC_CTX_HOST:
11833                 rc = 0;
11834                 break;
11835         default:
11836                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
11837                         __func__, ctx_cmd);
11838                 break;
11839         }
11840
11841         return rc;
11842 }
11843
11844 /**
11845  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
11846  * @vport: Pointer to virtual port.
11847  * @tgt_id: SCSI ID of the target.
11848  * @lun_id: LUN ID of the scsi device.
11849  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11850  *
11851  * This function returns number of FCP commands pending for the vport.
11852  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11853  * commands pending on the vport associated with SCSI device specified
11854  * by tgt_id and lun_id parameters.
11855  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11856  * commands pending on the vport associated with SCSI target specified
11857  * by tgt_id parameter.
11858  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11859  * commands pending on the vport.
11860  * This function returns the number of iocbs which satisfy the filter.
11861  * This function is called without any lock held.
11862  **/
11863 int
11864 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11865                   lpfc_ctx_cmd ctx_cmd)
11866 {
11867         struct lpfc_hba *phba = vport->phba;
11868         struct lpfc_iocbq *iocbq;
11869         int sum, i;
11870
11871         spin_lock_irq(&phba->hbalock);
11872         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11873                 iocbq = phba->sli.iocbq_lookup[i];
11874
11875                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11876                                                 ctx_cmd) == 0)
11877                         sum++;
11878         }
11879         spin_unlock_irq(&phba->hbalock);
11880
11881         return sum;
11882 }
11883
11884 /**
11885  * lpfc_sli4_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
11886  * @phba: Pointer to HBA context object
11887  * @cmdiocb: Pointer to command iocb object.
11888  * @wcqe: pointer to the complete wcqe
11889  *
11890  * This function is called when an aborted FCP iocb completes. This
11891  * function is called by the ring event handler with no lock held.
11892  * This function frees the iocb. It is called for sli-4 adapters.
11893  **/
11894 void
11895 lpfc_sli4_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11896                          struct lpfc_wcqe_complete *wcqe)
11897 {
11898         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11899                         "3017 ABORT_XRI_CN completing on rpi x%x "
11900                         "original iotag x%x, abort cmd iotag x%x "
11901                         "status 0x%x, reason 0x%x\n",
11902                         cmdiocb->iocb.un.acxri.abortContextTag,
11903                         cmdiocb->iocb.un.acxri.abortIoTag,
11904                         cmdiocb->iotag,
11905                         (bf_get(lpfc_wcqe_c_status, wcqe)
11906                         & LPFC_IOCB_STATUS_MASK),
11907                         wcqe->parameter);
11908         lpfc_sli_release_iocbq(phba, cmdiocb);
11909 }
11910
11911 /**
11912  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
11913  * @phba: Pointer to HBA context object
11914  * @cmdiocb: Pointer to command iocb object.
11915  * @rspiocb: Pointer to response iocb object.
11916  *
11917  * This function is called when an aborted FCP iocb completes. This
11918  * function is called by the ring event handler with no lock held.
11919  * This function frees the iocb.
11920  **/
11921 void
11922 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11923                         struct lpfc_iocbq *rspiocb)
11924 {
11925         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11926                         "3096 ABORT_XRI_CN completing on rpi x%x "
11927                         "original iotag x%x, abort cmd iotag x%x "
11928                         "status 0x%x, reason 0x%x\n",
11929                         cmdiocb->iocb.un.acxri.abortContextTag,
11930                         cmdiocb->iocb.un.acxri.abortIoTag,
11931                         cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11932                         rspiocb->iocb.un.ulpWord[4]);
11933         lpfc_sli_release_iocbq(phba, cmdiocb);
11934         return;
11935 }
11936
11937 /**
11938  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
11939  * @vport: Pointer to virtual port.
11940  * @pring: Pointer to driver SLI ring object.
11941  * @tgt_id: SCSI ID of the target.
11942  * @lun_id: LUN ID of the scsi device.
11943  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11944  *
11945  * This function sends an abort command for every SCSI command
11946  * associated with the given virtual port pending on the ring
11947  * filtered by lpfc_sli_validate_fcp_iocb function.
11948  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11949  * FCP iocbs associated with lun specified by tgt_id and lun_id
11950  * parameters
11951  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11952  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11953  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11954  * FCP iocbs associated with virtual port.
11955  * This function returns number of iocbs it failed to abort.
11956  * This function is called with no locks held.
11957  **/
11958 int
11959 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11960                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
11961 {
11962         struct lpfc_hba *phba = vport->phba;
11963         struct lpfc_iocbq *iocbq;
11964         int errcnt = 0, ret_val = 0;
11965         unsigned long iflags;
11966         int i;
11967
11968         /* all I/Os are in process of being flushed */
11969         if (phba->hba_flag & HBA_IOQ_FLUSH)
11970                 return errcnt;
11971
11972         for (i = 1; i <= phba->sli.last_iotag; i++) {
11973                 iocbq = phba->sli.iocbq_lookup[i];
11974
11975                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11976                                                abort_cmd) != 0)
11977                         continue;
11978
11979                 spin_lock_irqsave(&phba->hbalock, iflags);
11980                 ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,
11981                                                      lpfc_sli_abort_fcp_cmpl);
11982                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11983                 if (ret_val != IOCB_SUCCESS)
11984                         errcnt++;
11985         }
11986
11987         return errcnt;
11988 }
11989
11990 /**
11991  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11992  * @vport: Pointer to virtual port.
11993  * @pring: Pointer to driver SLI ring object.
11994  * @tgt_id: SCSI ID of the target.
11995  * @lun_id: LUN ID of the scsi device.
11996  * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11997  *
11998  * This function sends an abort command for every SCSI command
11999  * associated with the given virtual port pending on the ring
12000  * filtered by lpfc_sli_validate_fcp_iocb function.
12001  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
12002  * FCP iocbs associated with lun specified by tgt_id and lun_id
12003  * parameters
12004  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
12005  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12006  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
12007  * FCP iocbs associated with virtual port.
12008  * This function returns number of iocbs it aborted .
12009  * This function is called with no locks held right after a taskmgmt
12010  * command is sent.
12011  **/
12012 int
12013 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
12014                         uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
12015 {
12016         struct lpfc_hba *phba = vport->phba;
12017         struct lpfc_io_buf *lpfc_cmd;
12018         struct lpfc_iocbq *abtsiocbq;
12019         struct lpfc_nodelist *ndlp;
12020         struct lpfc_iocbq *iocbq;
12021         IOCB_t *icmd;
12022         int sum, i, ret_val;
12023         unsigned long iflags;
12024         struct lpfc_sli_ring *pring_s4 = NULL;
12025
12026         spin_lock_irqsave(&phba->hbalock, iflags);
12027
12028         /* all I/Os are in process of being flushed */
12029         if (phba->hba_flag & HBA_IOQ_FLUSH) {
12030                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12031                 return 0;
12032         }
12033         sum = 0;
12034
12035         for (i = 1; i <= phba->sli.last_iotag; i++) {
12036                 iocbq = phba->sli.iocbq_lookup[i];
12037
12038                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12039                                                cmd) != 0)
12040                         continue;
12041
12042                 /* Guard against IO completion being called at same time */
12043                 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12044                 spin_lock(&lpfc_cmd->buf_lock);
12045
12046                 if (!lpfc_cmd->pCmd) {
12047                         spin_unlock(&lpfc_cmd->buf_lock);
12048                         continue;
12049                 }
12050
12051                 if (phba->sli_rev == LPFC_SLI_REV4) {
12052                         pring_s4 =
12053                             phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
12054                         if (!pring_s4) {
12055                                 spin_unlock(&lpfc_cmd->buf_lock);
12056                                 continue;
12057                         }
12058                         /* Note: both hbalock and ring_lock must be set here */
12059                         spin_lock(&pring_s4->ring_lock);
12060                 }
12061
12062                 /*
12063                  * If the iocbq is already being aborted, don't take a second
12064                  * action, but do count it.
12065                  */
12066                 if ((iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
12067                     !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
12068                         if (phba->sli_rev == LPFC_SLI_REV4)
12069                                 spin_unlock(&pring_s4->ring_lock);
12070                         spin_unlock(&lpfc_cmd->buf_lock);
12071                         continue;
12072                 }
12073
12074                 /* issue ABTS for this IOCB based on iotag */
12075                 abtsiocbq = __lpfc_sli_get_iocbq(phba);
12076                 if (!abtsiocbq) {
12077                         if (phba->sli_rev == LPFC_SLI_REV4)
12078                                 spin_unlock(&pring_s4->ring_lock);
12079                         spin_unlock(&lpfc_cmd->buf_lock);
12080                         continue;
12081                 }
12082
12083                 icmd = &iocbq->iocb;
12084                 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
12085                 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
12086                 if (phba->sli_rev == LPFC_SLI_REV4)
12087                         abtsiocbq->iocb.un.acxri.abortIoTag =
12088                                                          iocbq->sli4_xritag;
12089                 else
12090                         abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
12091                 abtsiocbq->iocb.ulpLe = 1;
12092                 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
12093                 abtsiocbq->vport = vport;
12094
12095                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
12096                 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
12097                 if (iocbq->iocb_flag & LPFC_IO_FCP)
12098                         abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
12099                 if (iocbq->iocb_flag & LPFC_IO_FOF)
12100                         abtsiocbq->iocb_flag |= LPFC_IO_FOF;
12101
12102                 ndlp = lpfc_cmd->rdata->pnode;
12103
12104                 if (lpfc_is_link_up(phba) &&
12105                     (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
12106                         abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
12107                 else
12108                         abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
12109
12110                 /* Setup callback routine and issue the command. */
12111                 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
12112
12113                 /*
12114                  * Indicate the IO is being aborted by the driver and set
12115                  * the caller's flag into the aborted IO.
12116                  */
12117                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
12118
12119                 if (phba->sli_rev == LPFC_SLI_REV4) {
12120                         ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
12121                                                         abtsiocbq, 0);
12122                         spin_unlock(&pring_s4->ring_lock);
12123                 } else {
12124                         ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
12125                                                         abtsiocbq, 0);
12126                 }
12127
12128                 spin_unlock(&lpfc_cmd->buf_lock);
12129
12130                 if (ret_val == IOCB_ERROR)
12131                         __lpfc_sli_release_iocbq(phba, abtsiocbq);
12132                 else
12133                         sum++;
12134         }
12135         spin_unlock_irqrestore(&phba->hbalock, iflags);
12136         return sum;
12137 }
12138
12139 /**
12140  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
12141  * @phba: Pointer to HBA context object.
12142  * @cmdiocbq: Pointer to command iocb.
12143  * @rspiocbq: Pointer to response iocb.
12144  *
12145  * This function is the completion handler for iocbs issued using
12146  * lpfc_sli_issue_iocb_wait function. This function is called by the
12147  * ring event handler function without any lock held. This function
12148  * can be called from both worker thread context and interrupt
12149  * context. This function also can be called from other thread which
12150  * cleans up the SLI layer objects.
12151  * This function copy the contents of the response iocb to the
12152  * response iocb memory object provided by the caller of
12153  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
12154  * sleeps for the iocb completion.
12155  **/
12156 static void
12157 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
12158                         struct lpfc_iocbq *cmdiocbq,
12159                         struct lpfc_iocbq *rspiocbq)
12160 {
12161         wait_queue_head_t *pdone_q;
12162         unsigned long iflags;
12163         struct lpfc_io_buf *lpfc_cmd;
12164
12165         spin_lock_irqsave(&phba->hbalock, iflags);
12166         if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
12167
12168                 /*
12169                  * A time out has occurred for the iocb.  If a time out
12170                  * completion handler has been supplied, call it.  Otherwise,
12171                  * just free the iocbq.
12172                  */
12173
12174                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12175                 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
12176                 cmdiocbq->wait_iocb_cmpl = NULL;
12177                 if (cmdiocbq->iocb_cmpl)
12178                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
12179                 else
12180                         lpfc_sli_release_iocbq(phba, cmdiocbq);
12181                 return;
12182         }
12183
12184         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
12185         if (cmdiocbq->context2 && rspiocbq)
12186                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
12187                        &rspiocbq->iocb, sizeof(IOCB_t));
12188
12189         /* Set the exchange busy flag for task management commands */
12190         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
12191                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
12192                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
12193                         cur_iocbq);
12194                 if (rspiocbq && (rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY))
12195                         lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
12196                 else
12197                         lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
12198         }
12199
12200         pdone_q = cmdiocbq->context_un.wait_queue;
12201         if (pdone_q)
12202                 wake_up(pdone_q);
12203         spin_unlock_irqrestore(&phba->hbalock, iflags);
12204         return;
12205 }
12206
12207 /**
12208  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
12209  * @phba: Pointer to HBA context object..
12210  * @piocbq: Pointer to command iocb.
12211  * @flag: Flag to test.
12212  *
12213  * This routine grabs the hbalock and then test the iocb_flag to
12214  * see if the passed in flag is set.
12215  * Returns:
12216  * 1 if flag is set.
12217  * 0 if flag is not set.
12218  **/
12219 static int
12220 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
12221                  struct lpfc_iocbq *piocbq, uint32_t flag)
12222 {
12223         unsigned long iflags;
12224         int ret;
12225
12226         spin_lock_irqsave(&phba->hbalock, iflags);
12227         ret = piocbq->iocb_flag & flag;
12228         spin_unlock_irqrestore(&phba->hbalock, iflags);
12229         return ret;
12230
12231 }
12232
12233 /**
12234  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
12235  * @phba: Pointer to HBA context object..
12236  * @ring_number: Ring number
12237  * @piocb: Pointer to command iocb.
12238  * @prspiocbq: Pointer to response iocb.
12239  * @timeout: Timeout in number of seconds.
12240  *
12241  * This function issues the iocb to firmware and waits for the
12242  * iocb to complete. The iocb_cmpl field of the shall be used
12243  * to handle iocbs which time out. If the field is NULL, the
12244  * function shall free the iocbq structure.  If more clean up is
12245  * needed, the caller is expected to provide a completion function
12246  * that will provide the needed clean up.  If the iocb command is
12247  * not completed within timeout seconds, the function will either
12248  * free the iocbq structure (if iocb_cmpl == NULL) or execute the
12249  * completion function set in the iocb_cmpl field and then return
12250  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
12251  * resources if this function returns IOCB_TIMEDOUT.
12252  * The function waits for the iocb completion using an
12253  * non-interruptible wait.
12254  * This function will sleep while waiting for iocb completion.
12255  * So, this function should not be called from any context which
12256  * does not allow sleeping. Due to the same reason, this function
12257  * cannot be called with interrupt disabled.
12258  * This function assumes that the iocb completions occur while
12259  * this function sleep. So, this function cannot be called from
12260  * the thread which process iocb completion for this ring.
12261  * This function clears the iocb_flag of the iocb object before
12262  * issuing the iocb and the iocb completion handler sets this
12263  * flag and wakes this thread when the iocb completes.
12264  * The contents of the response iocb will be copied to prspiocbq
12265  * by the completion handler when the command completes.
12266  * This function returns IOCB_SUCCESS when success.
12267  * This function is called with no lock held.
12268  **/
12269 int
12270 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
12271                          uint32_t ring_number,
12272                          struct lpfc_iocbq *piocb,
12273                          struct lpfc_iocbq *prspiocbq,
12274                          uint32_t timeout)
12275 {
12276         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
12277         long timeleft, timeout_req = 0;
12278         int retval = IOCB_SUCCESS;
12279         uint32_t creg_val;
12280         struct lpfc_iocbq *iocb;
12281         int txq_cnt = 0;
12282         int txcmplq_cnt = 0;
12283         struct lpfc_sli_ring *pring;
12284         unsigned long iflags;
12285         bool iocb_completed = true;
12286
12287         if (phba->sli_rev >= LPFC_SLI_REV4)
12288                 pring = lpfc_sli4_calc_ring(phba, piocb);
12289         else
12290                 pring = &phba->sli.sli3_ring[ring_number];
12291         /*
12292          * If the caller has provided a response iocbq buffer, then context2
12293          * is NULL or its an error.
12294          */
12295         if (prspiocbq) {
12296                 if (piocb->context2)
12297                         return IOCB_ERROR;
12298                 piocb->context2 = prspiocbq;
12299         }
12300
12301         piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
12302         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
12303         piocb->context_un.wait_queue = &done_q;
12304         piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
12305
12306         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
12307                 if (lpfc_readl(phba->HCregaddr, &creg_val))
12308                         return IOCB_ERROR;
12309                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
12310                 writel(creg_val, phba->HCregaddr);
12311                 readl(phba->HCregaddr); /* flush */
12312         }
12313
12314         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
12315                                      SLI_IOCB_RET_IOCB);
12316         if (retval == IOCB_SUCCESS) {
12317                 timeout_req = msecs_to_jiffies(timeout * 1000);
12318                 timeleft = wait_event_timeout(done_q,
12319                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
12320                                 timeout_req);
12321                 spin_lock_irqsave(&phba->hbalock, iflags);
12322                 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
12323
12324                         /*
12325                          * IOCB timed out.  Inform the wake iocb wait
12326                          * completion function and set local status
12327                          */
12328
12329                         iocb_completed = false;
12330                         piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
12331                 }
12332                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12333                 if (iocb_completed) {
12334                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12335                                         "0331 IOCB wake signaled\n");
12336                         /* Note: we are not indicating if the IOCB has a success
12337                          * status or not - that's for the caller to check.
12338                          * IOCB_SUCCESS means just that the command was sent and
12339                          * completed. Not that it completed successfully.
12340                          * */
12341                 } else if (timeleft == 0) {
12342                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12343                                         "0338 IOCB wait timeout error - no "
12344                                         "wake response Data x%x\n", timeout);
12345                         retval = IOCB_TIMEDOUT;
12346                 } else {
12347                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12348                                         "0330 IOCB wake NOT set, "
12349                                         "Data x%x x%lx\n",
12350                                         timeout, (timeleft / jiffies));
12351                         retval = IOCB_TIMEDOUT;
12352                 }
12353         } else if (retval == IOCB_BUSY) {
12354                 if (phba->cfg_log_verbose & LOG_SLI) {
12355                         list_for_each_entry(iocb, &pring->txq, list) {
12356                                 txq_cnt++;
12357                         }
12358                         list_for_each_entry(iocb, &pring->txcmplq, list) {
12359                                 txcmplq_cnt++;
12360                         }
12361                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12362                                 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
12363                                 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
12364                 }
12365                 return retval;
12366         } else {
12367                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12368                                 "0332 IOCB wait issue failed, Data x%x\n",
12369                                 retval);
12370                 retval = IOCB_ERROR;
12371         }
12372
12373         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
12374                 if (lpfc_readl(phba->HCregaddr, &creg_val))
12375                         return IOCB_ERROR;
12376                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
12377                 writel(creg_val, phba->HCregaddr);
12378                 readl(phba->HCregaddr); /* flush */
12379         }
12380
12381         if (prspiocbq)
12382                 piocb->context2 = NULL;
12383
12384         piocb->context_un.wait_queue = NULL;
12385         piocb->iocb_cmpl = NULL;
12386         return retval;
12387 }
12388
12389 /**
12390  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
12391  * @phba: Pointer to HBA context object.
12392  * @pmboxq: Pointer to driver mailbox object.
12393  * @timeout: Timeout in number of seconds.
12394  *
12395  * This function issues the mailbox to firmware and waits for the
12396  * mailbox command to complete. If the mailbox command is not
12397  * completed within timeout seconds, it returns MBX_TIMEOUT.
12398  * The function waits for the mailbox completion using an
12399  * interruptible wait. If the thread is woken up due to a
12400  * signal, MBX_TIMEOUT error is returned to the caller. Caller
12401  * should not free the mailbox resources, if this function returns
12402  * MBX_TIMEOUT.
12403  * This function will sleep while waiting for mailbox completion.
12404  * So, this function should not be called from any context which
12405  * does not allow sleeping. Due to the same reason, this function
12406  * cannot be called with interrupt disabled.
12407  * This function assumes that the mailbox completion occurs while
12408  * this function sleep. So, this function cannot be called from
12409  * the worker thread which processes mailbox completion.
12410  * This function is called in the context of HBA management
12411  * applications.
12412  * This function returns MBX_SUCCESS when successful.
12413  * This function is called with no lock held.
12414  **/
12415 int
12416 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
12417                          uint32_t timeout)
12418 {
12419         struct completion mbox_done;
12420         int retval;
12421         unsigned long flag;
12422
12423         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
12424         /* setup wake call as IOCB callback */
12425         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
12426
12427         /* setup context3 field to pass wait_queue pointer to wake function  */
12428         init_completion(&mbox_done);
12429         pmboxq->context3 = &mbox_done;
12430         /* now issue the command */
12431         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
12432         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
12433                 wait_for_completion_timeout(&mbox_done,
12434                                             msecs_to_jiffies(timeout * 1000));
12435
12436                 spin_lock_irqsave(&phba->hbalock, flag);
12437                 pmboxq->context3 = NULL;
12438                 /*
12439                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
12440                  * else do not free the resources.
12441                  */
12442                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
12443                         retval = MBX_SUCCESS;
12444                 } else {
12445                         retval = MBX_TIMEOUT;
12446                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12447                 }
12448                 spin_unlock_irqrestore(&phba->hbalock, flag);
12449         }
12450         return retval;
12451 }
12452
12453 /**
12454  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
12455  * @phba: Pointer to HBA context.
12456  * @mbx_action: Mailbox shutdown options.
12457  *
12458  * This function is called to shutdown the driver's mailbox sub-system.
12459  * It first marks the mailbox sub-system is in a block state to prevent
12460  * the asynchronous mailbox command from issued off the pending mailbox
12461  * command queue. If the mailbox command sub-system shutdown is due to
12462  * HBA error conditions such as EEH or ERATT, this routine shall invoke
12463  * the mailbox sub-system flush routine to forcefully bring down the
12464  * mailbox sub-system. Otherwise, if it is due to normal condition (such
12465  * as with offline or HBA function reset), this routine will wait for the
12466  * outstanding mailbox command to complete before invoking the mailbox
12467  * sub-system flush routine to gracefully bring down mailbox sub-system.
12468  **/
12469 void
12470 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
12471 {
12472         struct lpfc_sli *psli = &phba->sli;
12473         unsigned long timeout;
12474
12475         if (mbx_action == LPFC_MBX_NO_WAIT) {
12476                 /* delay 100ms for port state */
12477                 msleep(100);
12478                 lpfc_sli_mbox_sys_flush(phba);
12479                 return;
12480         }
12481         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
12482
12483         /* Disable softirqs, including timers from obtaining phba->hbalock */
12484         local_bh_disable();
12485
12486         spin_lock_irq(&phba->hbalock);
12487         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
12488
12489         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
12490                 /* Determine how long we might wait for the active mailbox
12491                  * command to be gracefully completed by firmware.
12492                  */
12493                 if (phba->sli.mbox_active)
12494                         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12495                                                 phba->sli.mbox_active) *
12496                                                 1000) + jiffies;
12497                 spin_unlock_irq(&phba->hbalock);
12498
12499                 /* Enable softirqs again, done with phba->hbalock */
12500                 local_bh_enable();
12501
12502                 while (phba->sli.mbox_active) {
12503                         /* Check active mailbox complete status every 2ms */
12504                         msleep(2);
12505                         if (time_after(jiffies, timeout))
12506                                 /* Timeout, let the mailbox flush routine to
12507                                  * forcefully release active mailbox command
12508                                  */
12509                                 break;
12510                 }
12511         } else {
12512                 spin_unlock_irq(&phba->hbalock);
12513
12514                 /* Enable softirqs again, done with phba->hbalock */
12515                 local_bh_enable();
12516         }
12517
12518         lpfc_sli_mbox_sys_flush(phba);
12519 }
12520
12521 /**
12522  * lpfc_sli_eratt_read - read sli-3 error attention events
12523  * @phba: Pointer to HBA context.
12524  *
12525  * This function is called to read the SLI3 device error attention registers
12526  * for possible error attention events. The caller must hold the hostlock
12527  * with spin_lock_irq().
12528  *
12529  * This function returns 1 when there is Error Attention in the Host Attention
12530  * Register and returns 0 otherwise.
12531  **/
12532 static int
12533 lpfc_sli_eratt_read(struct lpfc_hba *phba)
12534 {
12535         uint32_t ha_copy;
12536
12537         /* Read chip Host Attention (HA) register */
12538         if (lpfc_readl(phba->HAregaddr, &ha_copy))
12539                 goto unplug_err;
12540
12541         if (ha_copy & HA_ERATT) {
12542                 /* Read host status register to retrieve error event */
12543                 if (lpfc_sli_read_hs(phba))
12544                         goto unplug_err;
12545
12546                 /* Check if there is a deferred error condition is active */
12547                 if ((HS_FFER1 & phba->work_hs) &&
12548                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
12549                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
12550                         phba->hba_flag |= DEFER_ERATT;
12551                         /* Clear all interrupt enable conditions */
12552                         writel(0, phba->HCregaddr);
12553                         readl(phba->HCregaddr);
12554                 }
12555
12556                 /* Set the driver HA work bitmap */
12557                 phba->work_ha |= HA_ERATT;
12558                 /* Indicate polling handles this ERATT */
12559                 phba->hba_flag |= HBA_ERATT_HANDLED;
12560                 return 1;
12561         }
12562         return 0;
12563
12564 unplug_err:
12565         /* Set the driver HS work bitmap */
12566         phba->work_hs |= UNPLUG_ERR;
12567         /* Set the driver HA work bitmap */
12568         phba->work_ha |= HA_ERATT;
12569         /* Indicate polling handles this ERATT */
12570         phba->hba_flag |= HBA_ERATT_HANDLED;
12571         return 1;
12572 }
12573
12574 /**
12575  * lpfc_sli4_eratt_read - read sli-4 error attention events
12576  * @phba: Pointer to HBA context.
12577  *
12578  * This function is called to read the SLI4 device error attention registers
12579  * for possible error attention events. The caller must hold the hostlock
12580  * with spin_lock_irq().
12581  *
12582  * This function returns 1 when there is Error Attention in the Host Attention
12583  * Register and returns 0 otherwise.
12584  **/
12585 static int
12586 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12587 {
12588         uint32_t uerr_sta_hi, uerr_sta_lo;
12589         uint32_t if_type, portsmphr;
12590         struct lpfc_register portstat_reg;
12591
12592         /*
12593          * For now, use the SLI4 device internal unrecoverable error
12594          * registers for error attention. This can be changed later.
12595          */
12596         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12597         switch (if_type) {
12598         case LPFC_SLI_INTF_IF_TYPE_0:
12599                 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12600                         &uerr_sta_lo) ||
12601                         lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12602                         &uerr_sta_hi)) {
12603                         phba->work_hs |= UNPLUG_ERR;
12604                         phba->work_ha |= HA_ERATT;
12605                         phba->hba_flag |= HBA_ERATT_HANDLED;
12606                         return 1;
12607                 }
12608                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12609                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12610                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12611                                         "1423 HBA Unrecoverable error: "
12612                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12613                                         "ue_mask_lo_reg=0x%x, "
12614                                         "ue_mask_hi_reg=0x%x\n",
12615                                         uerr_sta_lo, uerr_sta_hi,
12616                                         phba->sli4_hba.ue_mask_lo,
12617                                         phba->sli4_hba.ue_mask_hi);
12618                         phba->work_status[0] = uerr_sta_lo;
12619                         phba->work_status[1] = uerr_sta_hi;
12620                         phba->work_ha |= HA_ERATT;
12621                         phba->hba_flag |= HBA_ERATT_HANDLED;
12622                         return 1;
12623                 }
12624                 break;
12625         case LPFC_SLI_INTF_IF_TYPE_2:
12626         case LPFC_SLI_INTF_IF_TYPE_6:
12627                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12628                         &portstat_reg.word0) ||
12629                         lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12630                         &portsmphr)){
12631                         phba->work_hs |= UNPLUG_ERR;
12632                         phba->work_ha |= HA_ERATT;
12633                         phba->hba_flag |= HBA_ERATT_HANDLED;
12634                         return 1;
12635                 }
12636                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12637                         phba->work_status[0] =
12638                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12639                         phba->work_status[1] =
12640                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12641                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12642                                         "2885 Port Status Event: "
12643                                         "port status reg 0x%x, "
12644                                         "port smphr reg 0x%x, "
12645                                         "error 1=0x%x, error 2=0x%x\n",
12646                                         portstat_reg.word0,
12647                                         portsmphr,
12648                                         phba->work_status[0],
12649                                         phba->work_status[1]);
12650                         phba->work_ha |= HA_ERATT;
12651                         phba->hba_flag |= HBA_ERATT_HANDLED;
12652                         return 1;
12653                 }
12654                 break;
12655         case LPFC_SLI_INTF_IF_TYPE_1:
12656         default:
12657                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12658                                 "2886 HBA Error Attention on unsupported "
12659                                 "if type %d.", if_type);
12660                 return 1;
12661         }
12662
12663         return 0;
12664 }
12665
12666 /**
12667  * lpfc_sli_check_eratt - check error attention events
12668  * @phba: Pointer to HBA context.
12669  *
12670  * This function is called from timer soft interrupt context to check HBA's
12671  * error attention register bit for error attention events.
12672  *
12673  * This function returns 1 when there is Error Attention in the Host Attention
12674  * Register and returns 0 otherwise.
12675  **/
12676 int
12677 lpfc_sli_check_eratt(struct lpfc_hba *phba)
12678 {
12679         uint32_t ha_copy;
12680
12681         /* If somebody is waiting to handle an eratt, don't process it
12682          * here. The brdkill function will do this.
12683          */
12684         if (phba->link_flag & LS_IGNORE_ERATT)
12685                 return 0;
12686
12687         /* Check if interrupt handler handles this ERATT */
12688         spin_lock_irq(&phba->hbalock);
12689         if (phba->hba_flag & HBA_ERATT_HANDLED) {
12690                 /* Interrupt handler has handled ERATT */
12691                 spin_unlock_irq(&phba->hbalock);
12692                 return 0;
12693         }
12694
12695         /*
12696          * If there is deferred error attention, do not check for error
12697          * attention
12698          */
12699         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12700                 spin_unlock_irq(&phba->hbalock);
12701                 return 0;
12702         }
12703
12704         /* If PCI channel is offline, don't process it */
12705         if (unlikely(pci_channel_offline(phba->pcidev))) {
12706                 spin_unlock_irq(&phba->hbalock);
12707                 return 0;
12708         }
12709
12710         switch (phba->sli_rev) {
12711         case LPFC_SLI_REV2:
12712         case LPFC_SLI_REV3:
12713                 /* Read chip Host Attention (HA) register */
12714                 ha_copy = lpfc_sli_eratt_read(phba);
12715                 break;
12716         case LPFC_SLI_REV4:
12717                 /* Read device Uncoverable Error (UERR) registers */
12718                 ha_copy = lpfc_sli4_eratt_read(phba);
12719                 break;
12720         default:
12721                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12722                                 "0299 Invalid SLI revision (%d)\n",
12723                                 phba->sli_rev);
12724                 ha_copy = 0;
12725                 break;
12726         }
12727         spin_unlock_irq(&phba->hbalock);
12728
12729         return ha_copy;
12730 }
12731
12732 /**
12733  * lpfc_intr_state_check - Check device state for interrupt handling
12734  * @phba: Pointer to HBA context.
12735  *
12736  * This inline routine checks whether a device or its PCI slot is in a state
12737  * that the interrupt should be handled.
12738  *
12739  * This function returns 0 if the device or the PCI slot is in a state that
12740  * interrupt should be handled, otherwise -EIO.
12741  */
12742 static inline int
12743 lpfc_intr_state_check(struct lpfc_hba *phba)
12744 {
12745         /* If the pci channel is offline, ignore all the interrupts */
12746         if (unlikely(pci_channel_offline(phba->pcidev)))
12747                 return -EIO;
12748
12749         /* Update device level interrupt statistics */
12750         phba->sli.slistat.sli_intr++;
12751
12752         /* Ignore all interrupts during initialization. */
12753         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12754                 return -EIO;
12755
12756         return 0;
12757 }
12758
12759 /**
12760  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
12761  * @irq: Interrupt number.
12762  * @dev_id: The device context pointer.
12763  *
12764  * This function is directly called from the PCI layer as an interrupt
12765  * service routine when device with SLI-3 interface spec is enabled with
12766  * MSI-X multi-message interrupt mode and there are slow-path events in
12767  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12768  * interrupt mode, this function is called as part of the device-level
12769  * interrupt handler. When the PCI slot is in error recovery or the HBA
12770  * is undergoing initialization, the interrupt handler will not process
12771  * the interrupt. The link attention and ELS ring attention events are
12772  * handled by the worker thread. The interrupt handler signals the worker
12773  * thread and returns for these events. This function is called without
12774  * any lock held. It gets the hbalock to access and update SLI data
12775  * structures.
12776  *
12777  * This function returns IRQ_HANDLED when interrupt is handled else it
12778  * returns IRQ_NONE.
12779  **/
12780 irqreturn_t
12781 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
12782 {
12783         struct lpfc_hba  *phba;
12784         uint32_t ha_copy, hc_copy;
12785         uint32_t work_ha_copy;
12786         unsigned long status;
12787         unsigned long iflag;
12788         uint32_t control;
12789
12790         MAILBOX_t *mbox, *pmbox;
12791         struct lpfc_vport *vport;
12792         struct lpfc_nodelist *ndlp;
12793         struct lpfc_dmabuf *mp;
12794         LPFC_MBOXQ_t *pmb;
12795         int rc;
12796
12797         /*
12798          * Get the driver's phba structure from the dev_id and
12799          * assume the HBA is not interrupting.
12800          */
12801         phba = (struct lpfc_hba *)dev_id;
12802
12803         if (unlikely(!phba))
12804                 return IRQ_NONE;
12805
12806         /*
12807          * Stuff needs to be attented to when this function is invoked as an
12808          * individual interrupt handler in MSI-X multi-message interrupt mode
12809          */
12810         if (phba->intr_type == MSIX) {
12811                 /* Check device state for handling interrupt */
12812                 if (lpfc_intr_state_check(phba))
12813                         return IRQ_NONE;
12814                 /* Need to read HA REG for slow-path events */
12815                 spin_lock_irqsave(&phba->hbalock, iflag);
12816                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12817                         goto unplug_error;
12818                 /* If somebody is waiting to handle an eratt don't process it
12819                  * here. The brdkill function will do this.
12820                  */
12821                 if (phba->link_flag & LS_IGNORE_ERATT)
12822                         ha_copy &= ~HA_ERATT;
12823                 /* Check the need for handling ERATT in interrupt handler */
12824                 if (ha_copy & HA_ERATT) {
12825                         if (phba->hba_flag & HBA_ERATT_HANDLED)
12826                                 /* ERATT polling has handled ERATT */
12827                                 ha_copy &= ~HA_ERATT;
12828                         else
12829                                 /* Indicate interrupt handler handles ERATT */
12830                                 phba->hba_flag |= HBA_ERATT_HANDLED;
12831                 }
12832
12833                 /*
12834                  * If there is deferred error attention, do not check for any
12835                  * interrupt.
12836                  */
12837                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12838                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12839                         return IRQ_NONE;
12840                 }
12841
12842                 /* Clear up only attention source related to slow-path */
12843                 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12844                         goto unplug_error;
12845
12846                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12847                         HC_LAINT_ENA | HC_ERINT_ENA),
12848                         phba->HCregaddr);
12849                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12850                         phba->HAregaddr);
12851                 writel(hc_copy, phba->HCregaddr);
12852                 readl(phba->HAregaddr); /* flush */
12853                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12854         } else
12855                 ha_copy = phba->ha_copy;
12856
12857         work_ha_copy = ha_copy & phba->work_ha_mask;
12858
12859         if (work_ha_copy) {
12860                 if (work_ha_copy & HA_LATT) {
12861                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12862                                 /*
12863                                  * Turn off Link Attention interrupts
12864                                  * until CLEAR_LA done
12865                                  */
12866                                 spin_lock_irqsave(&phba->hbalock, iflag);
12867                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
12868                                 if (lpfc_readl(phba->HCregaddr, &control))
12869                                         goto unplug_error;
12870                                 control &= ~HC_LAINT_ENA;
12871                                 writel(control, phba->HCregaddr);
12872                                 readl(phba->HCregaddr); /* flush */
12873                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12874                         }
12875                         else
12876                                 work_ha_copy &= ~HA_LATT;
12877                 }
12878
12879                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
12880                         /*
12881                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12882                          * the only slow ring.
12883                          */
12884                         status = (work_ha_copy &
12885                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
12886                         status >>= (4*LPFC_ELS_RING);
12887                         if (status & HA_RXMASK) {
12888                                 spin_lock_irqsave(&phba->hbalock, iflag);
12889                                 if (lpfc_readl(phba->HCregaddr, &control))
12890                                         goto unplug_error;
12891
12892                                 lpfc_debugfs_slow_ring_trc(phba,
12893                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
12894                                 control, status,
12895                                 (uint32_t)phba->sli.slistat.sli_intr);
12896
12897                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
12898                                         lpfc_debugfs_slow_ring_trc(phba,
12899                                                 "ISR Disable ring:"
12900                                                 "pwork:x%x hawork:x%x wait:x%x",
12901                                                 phba->work_ha, work_ha_copy,
12902                                                 (uint32_t)((unsigned long)
12903                                                 &phba->work_waitq));
12904
12905                                         control &=
12906                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
12907                                         writel(control, phba->HCregaddr);
12908                                         readl(phba->HCregaddr); /* flush */
12909                                 }
12910                                 else {
12911                                         lpfc_debugfs_slow_ring_trc(phba,
12912                                                 "ISR slow ring:   pwork:"
12913                                                 "x%x hawork:x%x wait:x%x",
12914                                                 phba->work_ha, work_ha_copy,
12915                                                 (uint32_t)((unsigned long)
12916                                                 &phba->work_waitq));
12917                                 }
12918                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12919                         }
12920                 }
12921                 spin_lock_irqsave(&phba->hbalock, iflag);
12922                 if (work_ha_copy & HA_ERATT) {
12923                         if (lpfc_sli_read_hs(phba))
12924                                 goto unplug_error;
12925                         /*
12926                          * Check if there is a deferred error condition
12927                          * is active
12928                          */
12929                         if ((HS_FFER1 & phba->work_hs) &&
12930                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
12931                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
12932                                   phba->work_hs)) {
12933                                 phba->hba_flag |= DEFER_ERATT;
12934                                 /* Clear all interrupt enable conditions */
12935                                 writel(0, phba->HCregaddr);
12936                                 readl(phba->HCregaddr);
12937                         }
12938                 }
12939
12940                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
12941                         pmb = phba->sli.mbox_active;
12942                         pmbox = &pmb->u.mb;
12943                         mbox = phba->mbox;
12944                         vport = pmb->vport;
12945
12946                         /* First check out the status word */
12947                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12948                         if (pmbox->mbxOwner != OWN_HOST) {
12949                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12950                                 /*
12951                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
12952                                  * mbxStatus <status>
12953                                  */
12954                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12955                                                 "(%d):0304 Stray Mailbox "
12956                                                 "Interrupt mbxCommand x%x "
12957                                                 "mbxStatus x%x\n",
12958                                                 (vport ? vport->vpi : 0),
12959                                                 pmbox->mbxCommand,
12960                                                 pmbox->mbxStatus);
12961                                 /* clear mailbox attention bit */
12962                                 work_ha_copy &= ~HA_MBATT;
12963                         } else {
12964                                 phba->sli.mbox_active = NULL;
12965                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12966                                 phba->last_completion_time = jiffies;
12967                                 del_timer(&phba->sli.mbox_tmo);
12968                                 if (pmb->mbox_cmpl) {
12969                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
12970                                                         MAILBOX_CMD_SIZE);
12971                                         if (pmb->out_ext_byte_len &&
12972                                                 pmb->ctx_buf)
12973                                                 lpfc_sli_pcimem_bcopy(
12974                                                 phba->mbox_ext,
12975                                                 pmb->ctx_buf,
12976                                                 pmb->out_ext_byte_len);
12977                                 }
12978                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12979                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12980
12981                                         lpfc_debugfs_disc_trc(vport,
12982                                                 LPFC_DISC_TRC_MBOX_VPORT,
12983                                                 "MBOX dflt rpi: : "
12984                                                 "status:x%x rpi:x%x",
12985                                                 (uint32_t)pmbox->mbxStatus,
12986                                                 pmbox->un.varWords[0], 0);
12987
12988                                         if (!pmbox->mbxStatus) {
12989                                                 mp = (struct lpfc_dmabuf *)
12990                                                         (pmb->ctx_buf);
12991                                                 ndlp = (struct lpfc_nodelist *)
12992                                                         pmb->ctx_ndlp;
12993
12994                                                 /* Reg_LOGIN of dflt RPI was
12995                                                  * successful. new lets get
12996                                                  * rid of the RPI using the
12997                                                  * same mbox buffer.
12998                                                  */
12999                                                 lpfc_unreg_login(phba,
13000                                                         vport->vpi,
13001                                                         pmbox->un.varWords[0],
13002                                                         pmb);
13003                                                 pmb->mbox_cmpl =
13004                                                         lpfc_mbx_cmpl_dflt_rpi;
13005                                                 pmb->ctx_buf = mp;
13006                                                 pmb->ctx_ndlp = ndlp;
13007                                                 pmb->vport = vport;
13008                                                 rc = lpfc_sli_issue_mbox(phba,
13009                                                                 pmb,
13010                                                                 MBX_NOWAIT);
13011                                                 if (rc != MBX_BUSY)
13012                                                         lpfc_printf_log(phba,
13013                                                         KERN_ERR,
13014                                                         LOG_TRACE_EVENT,
13015                                                         "0350 rc should have"
13016                                                         "been MBX_BUSY\n");
13017                                                 if (rc != MBX_NOT_FINISHED)
13018                                                         goto send_current_mbox;
13019                                         }
13020                                 }
13021                                 spin_lock_irqsave(
13022                                                 &phba->pport->work_port_lock,
13023                                                 iflag);
13024                                 phba->pport->work_port_events &=
13025                                         ~WORKER_MBOX_TMO;
13026                                 spin_unlock_irqrestore(
13027                                                 &phba->pport->work_port_lock,
13028                                                 iflag);
13029                                 lpfc_mbox_cmpl_put(phba, pmb);
13030                         }
13031                 } else
13032                         spin_unlock_irqrestore(&phba->hbalock, iflag);
13033
13034                 if ((work_ha_copy & HA_MBATT) &&
13035                     (phba->sli.mbox_active == NULL)) {
13036 send_current_mbox:
13037                         /* Process next mailbox command if there is one */
13038                         do {
13039                                 rc = lpfc_sli_issue_mbox(phba, NULL,
13040                                                          MBX_NOWAIT);
13041                         } while (rc == MBX_NOT_FINISHED);
13042                         if (rc != MBX_SUCCESS)
13043                                 lpfc_printf_log(phba, KERN_ERR,
13044                                                 LOG_TRACE_EVENT,
13045                                                 "0349 rc should be "
13046                                                 "MBX_SUCCESS\n");
13047                 }
13048
13049                 spin_lock_irqsave(&phba->hbalock, iflag);
13050                 phba->work_ha |= work_ha_copy;
13051                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13052                 lpfc_worker_wake_up(phba);
13053         }
13054         return IRQ_HANDLED;
13055 unplug_error:
13056         spin_unlock_irqrestore(&phba->hbalock, iflag);
13057         return IRQ_HANDLED;
13058
13059 } /* lpfc_sli_sp_intr_handler */
13060
13061 /**
13062  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
13063  * @irq: Interrupt number.
13064  * @dev_id: The device context pointer.
13065  *
13066  * This function is directly called from the PCI layer as an interrupt
13067  * service routine when device with SLI-3 interface spec is enabled with
13068  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13069  * ring event in the HBA. However, when the device is enabled with either
13070  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13071  * device-level interrupt handler. When the PCI slot is in error recovery
13072  * or the HBA is undergoing initialization, the interrupt handler will not
13073  * process the interrupt. The SCSI FCP fast-path ring event are handled in
13074  * the intrrupt context. This function is called without any lock held.
13075  * It gets the hbalock to access and update SLI data structures.
13076  *
13077  * This function returns IRQ_HANDLED when interrupt is handled else it
13078  * returns IRQ_NONE.
13079  **/
13080 irqreturn_t
13081 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
13082 {
13083         struct lpfc_hba  *phba;
13084         uint32_t ha_copy;
13085         unsigned long status;
13086         unsigned long iflag;
13087         struct lpfc_sli_ring *pring;
13088
13089         /* Get the driver's phba structure from the dev_id and
13090          * assume the HBA is not interrupting.
13091          */
13092         phba = (struct lpfc_hba *) dev_id;
13093
13094         if (unlikely(!phba))
13095                 return IRQ_NONE;
13096
13097         /*
13098          * Stuff needs to be attented to when this function is invoked as an
13099          * individual interrupt handler in MSI-X multi-message interrupt mode
13100          */
13101         if (phba->intr_type == MSIX) {
13102                 /* Check device state for handling interrupt */
13103                 if (lpfc_intr_state_check(phba))
13104                         return IRQ_NONE;
13105                 /* Need to read HA REG for FCP ring and other ring events */
13106                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
13107                         return IRQ_HANDLED;
13108                 /* Clear up only attention source related to fast-path */
13109                 spin_lock_irqsave(&phba->hbalock, iflag);
13110                 /*
13111                  * If there is deferred error attention, do not check for
13112                  * any interrupt.
13113                  */
13114                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13115                         spin_unlock_irqrestore(&phba->hbalock, iflag);
13116                         return IRQ_NONE;
13117                 }
13118                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
13119                         phba->HAregaddr);
13120                 readl(phba->HAregaddr); /* flush */
13121                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13122         } else
13123                 ha_copy = phba->ha_copy;
13124
13125         /*
13126          * Process all events on FCP ring. Take the optimized path for FCP IO.
13127          */
13128         ha_copy &= ~(phba->work_ha_mask);
13129
13130         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13131         status >>= (4*LPFC_FCP_RING);
13132         pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
13133         if (status & HA_RXMASK)
13134                 lpfc_sli_handle_fast_ring_event(phba, pring, status);
13135
13136         if (phba->cfg_multi_ring_support == 2) {
13137                 /*
13138                  * Process all events on extra ring. Take the optimized path
13139                  * for extra ring IO.
13140                  */
13141                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
13142                 status >>= (4*LPFC_EXTRA_RING);
13143                 if (status & HA_RXMASK) {
13144                         lpfc_sli_handle_fast_ring_event(phba,
13145                                         &phba->sli.sli3_ring[LPFC_EXTRA_RING],
13146                                         status);
13147                 }
13148         }
13149         return IRQ_HANDLED;
13150 }  /* lpfc_sli_fp_intr_handler */
13151
13152 /**
13153  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
13154  * @irq: Interrupt number.
13155  * @dev_id: The device context pointer.
13156  *
13157  * This function is the HBA device-level interrupt handler to device with
13158  * SLI-3 interface spec, called from the PCI layer when either MSI or
13159  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
13160  * requires driver attention. This function invokes the slow-path interrupt
13161  * attention handling function and fast-path interrupt attention handling
13162  * function in turn to process the relevant HBA attention events. This
13163  * function is called without any lock held. It gets the hbalock to access
13164  * and update SLI data structures.
13165  *
13166  * This function returns IRQ_HANDLED when interrupt is handled, else it
13167  * returns IRQ_NONE.
13168  **/
13169 irqreturn_t
13170 lpfc_sli_intr_handler(int irq, void *dev_id)
13171 {
13172         struct lpfc_hba  *phba;
13173         irqreturn_t sp_irq_rc, fp_irq_rc;
13174         unsigned long status1, status2;
13175         uint32_t hc_copy;
13176
13177         /*
13178          * Get the driver's phba structure from the dev_id and
13179          * assume the HBA is not interrupting.
13180          */
13181         phba = (struct lpfc_hba *) dev_id;
13182
13183         if (unlikely(!phba))
13184                 return IRQ_NONE;
13185
13186         /* Check device state for handling interrupt */
13187         if (lpfc_intr_state_check(phba))
13188                 return IRQ_NONE;
13189
13190         spin_lock(&phba->hbalock);
13191         if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
13192                 spin_unlock(&phba->hbalock);
13193                 return IRQ_HANDLED;
13194         }
13195
13196         if (unlikely(!phba->ha_copy)) {
13197                 spin_unlock(&phba->hbalock);
13198                 return IRQ_NONE;
13199         } else if (phba->ha_copy & HA_ERATT) {
13200                 if (phba->hba_flag & HBA_ERATT_HANDLED)
13201                         /* ERATT polling has handled ERATT */
13202                         phba->ha_copy &= ~HA_ERATT;
13203                 else
13204                         /* Indicate interrupt handler handles ERATT */
13205                         phba->hba_flag |= HBA_ERATT_HANDLED;
13206         }
13207
13208         /*
13209          * If there is deferred error attention, do not check for any interrupt.
13210          */
13211         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13212                 spin_unlock(&phba->hbalock);
13213                 return IRQ_NONE;
13214         }
13215
13216         /* Clear attention sources except link and error attentions */
13217         if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
13218                 spin_unlock(&phba->hbalock);
13219                 return IRQ_HANDLED;
13220         }
13221         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
13222                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
13223                 phba->HCregaddr);
13224         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
13225         writel(hc_copy, phba->HCregaddr);
13226         readl(phba->HAregaddr); /* flush */
13227         spin_unlock(&phba->hbalock);
13228
13229         /*
13230          * Invokes slow-path host attention interrupt handling as appropriate.
13231          */
13232
13233         /* status of events with mailbox and link attention */
13234         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
13235
13236         /* status of events with ELS ring */
13237         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
13238         status2 >>= (4*LPFC_ELS_RING);
13239
13240         if (status1 || (status2 & HA_RXMASK))
13241                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
13242         else
13243                 sp_irq_rc = IRQ_NONE;
13244
13245         /*
13246          * Invoke fast-path host attention interrupt handling as appropriate.
13247          */
13248
13249         /* status of events with FCP ring */
13250         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13251         status1 >>= (4*LPFC_FCP_RING);
13252
13253         /* status of events with extra ring */
13254         if (phba->cfg_multi_ring_support == 2) {
13255                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
13256                 status2 >>= (4*LPFC_EXTRA_RING);
13257         } else
13258                 status2 = 0;
13259
13260         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
13261                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
13262         else
13263                 fp_irq_rc = IRQ_NONE;
13264
13265         /* Return device-level interrupt handling status */
13266         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
13267 }  /* lpfc_sli_intr_handler */
13268
13269 /**
13270  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
13271  * @phba: pointer to lpfc hba data structure.
13272  *
13273  * This routine is invoked by the worker thread to process all the pending
13274  * SLI4 els abort xri events.
13275  **/
13276 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
13277 {
13278         struct lpfc_cq_event *cq_event;
13279         unsigned long iflags;
13280
13281         /* First, declare the els xri abort event has been handled */
13282         spin_lock_irqsave(&phba->hbalock, iflags);
13283         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
13284         spin_unlock_irqrestore(&phba->hbalock, iflags);
13285
13286         /* Now, handle all the els xri abort events */
13287         spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
13288         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
13289                 /* Get the first event from the head of the event queue */
13290                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
13291                                  cq_event, struct lpfc_cq_event, list);
13292                 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
13293                                        iflags);
13294                 /* Notify aborted XRI for ELS work queue */
13295                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
13296
13297                 /* Free the event processed back to the free pool */
13298                 lpfc_sli4_cq_event_release(phba, cq_event);
13299                 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
13300                                   iflags);
13301         }
13302         spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
13303 }
13304
13305 /**
13306  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
13307  * @phba: pointer to lpfc hba data structure
13308  * @pIocbIn: pointer to the rspiocbq
13309  * @pIocbOut: pointer to the cmdiocbq
13310  * @wcqe: pointer to the complete wcqe
13311  *
13312  * This routine transfers the fields of a command iocbq to a response iocbq
13313  * by copying all the IOCB fields from command iocbq and transferring the
13314  * completion status information from the complete wcqe.
13315  **/
13316 static void
13317 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
13318                               struct lpfc_iocbq *pIocbIn,
13319                               struct lpfc_iocbq *pIocbOut,
13320                               struct lpfc_wcqe_complete *wcqe)
13321 {
13322         int numBdes, i;
13323         unsigned long iflags;
13324         uint32_t status, max_response;
13325         struct lpfc_dmabuf *dmabuf;
13326         struct ulp_bde64 *bpl, bde;
13327         size_t offset = offsetof(struct lpfc_iocbq, iocb);
13328
13329         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
13330                sizeof(struct lpfc_iocbq) - offset);
13331         /* Map WCQE parameters into irspiocb parameters */
13332         status = bf_get(lpfc_wcqe_c_status, wcqe);
13333         pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
13334         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
13335                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
13336                         pIocbIn->iocb.un.fcpi.fcpi_parm =
13337                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
13338                                         wcqe->total_data_placed;
13339                 else
13340                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
13341         else {
13342                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
13343                 switch (pIocbOut->iocb.ulpCommand) {
13344                 case CMD_ELS_REQUEST64_CR:
13345                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
13346                         bpl  = (struct ulp_bde64 *)dmabuf->virt;
13347                         bde.tus.w = le32_to_cpu(bpl[1].tus.w);
13348                         max_response = bde.tus.f.bdeSize;
13349                         break;
13350                 case CMD_GEN_REQUEST64_CR:
13351                         max_response = 0;
13352                         if (!pIocbOut->context3)
13353                                 break;
13354                         numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
13355                                         sizeof(struct ulp_bde64);
13356                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
13357                         bpl = (struct ulp_bde64 *)dmabuf->virt;
13358                         for (i = 0; i < numBdes; i++) {
13359                                 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
13360                                 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
13361                                         max_response += bde.tus.f.bdeSize;
13362                         }
13363                         break;
13364                 default:
13365                         max_response = wcqe->total_data_placed;
13366                         break;
13367                 }
13368                 if (max_response < wcqe->total_data_placed)
13369                         pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
13370                 else
13371                         pIocbIn->iocb.un.genreq64.bdl.bdeSize =
13372                                 wcqe->total_data_placed;
13373         }
13374
13375         /* Convert BG errors for completion status */
13376         if (status == CQE_STATUS_DI_ERROR) {
13377                 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
13378
13379                 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
13380                         pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
13381                 else
13382                         pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
13383
13384                 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
13385                 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
13386                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13387                                 BGS_GUARD_ERR_MASK;
13388                 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
13389                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13390                                 BGS_APPTAG_ERR_MASK;
13391                 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
13392                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13393                                 BGS_REFTAG_ERR_MASK;
13394
13395                 /* Check to see if there was any good data before the error */
13396                 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
13397                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13398                                 BGS_HI_WATER_MARK_PRESENT_MASK;
13399                         pIocbIn->iocb.unsli3.sli3_bg.bghm =
13400                                 wcqe->total_data_placed;
13401                 }
13402
13403                 /*
13404                 * Set ALL the error bits to indicate we don't know what
13405                 * type of error it is.
13406                 */
13407                 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
13408                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13409                                 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
13410                                 BGS_GUARD_ERR_MASK);
13411         }
13412
13413         /* Pick up HBA exchange busy condition */
13414         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
13415                 spin_lock_irqsave(&phba->hbalock, iflags);
13416                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
13417                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13418         }
13419 }
13420
13421 /**
13422  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
13423  * @phba: Pointer to HBA context object.
13424  * @irspiocbq: Pointer to work-queue completion queue entry.
13425  *
13426  * This routine handles an ELS work-queue completion event and construct
13427  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
13428  * discovery engine to handle.
13429  *
13430  * Return: Pointer to the receive IOCBQ, NULL otherwise.
13431  **/
13432 static struct lpfc_iocbq *
13433 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
13434                                struct lpfc_iocbq *irspiocbq)
13435 {
13436         struct lpfc_sli_ring *pring;
13437         struct lpfc_iocbq *cmdiocbq;
13438         struct lpfc_wcqe_complete *wcqe;
13439         unsigned long iflags;
13440
13441         pring = lpfc_phba_elsring(phba);
13442         if (unlikely(!pring))
13443                 return NULL;
13444
13445         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
13446         pring->stats.iocb_event++;
13447         /* Look up the ELS command IOCB and create pseudo response IOCB */
13448         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13449                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13450         if (unlikely(!cmdiocbq)) {
13451                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13452                                 "0386 ELS complete with no corresponding "
13453                                 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
13454                                 wcqe->word0, wcqe->total_data_placed,
13455                                 wcqe->parameter, wcqe->word3);
13456                 lpfc_sli_release_iocbq(phba, irspiocbq);
13457                 return NULL;
13458         }
13459
13460         spin_lock_irqsave(&pring->ring_lock, iflags);
13461         /* Put the iocb back on the txcmplq */
13462         lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13463         spin_unlock_irqrestore(&pring->ring_lock, iflags);
13464
13465         /* Fake the irspiocbq and copy necessary response information */
13466         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
13467
13468         return irspiocbq;
13469 }
13470
13471 inline struct lpfc_cq_event *
13472 lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13473 {
13474         struct lpfc_cq_event *cq_event;
13475
13476         /* Allocate a new internal CQ_EVENT entry */
13477         cq_event = lpfc_sli4_cq_event_alloc(phba);
13478         if (!cq_event) {
13479                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13480                                 "0602 Failed to alloc CQ_EVENT entry\n");
13481                 return NULL;
13482         }
13483
13484         /* Move the CQE into the event */
13485         memcpy(&cq_event->cqe, entry, size);
13486         return cq_event;
13487 }
13488
13489 /**
13490  * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
13491  * @phba: Pointer to HBA context object.
13492  * @mcqe: Pointer to mailbox completion queue entry.
13493  *
13494  * This routine process a mailbox completion queue entry with asynchronous
13495  * event.
13496  *
13497  * Return: true if work posted to worker thread, otherwise false.
13498  **/
13499 static bool
13500 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13501 {
13502         struct lpfc_cq_event *cq_event;
13503         unsigned long iflags;
13504
13505         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13506                         "0392 Async Event: word0:x%x, word1:x%x, "
13507                         "word2:x%x, word3:x%x\n", mcqe->word0,
13508                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13509
13510         cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13511         if (!cq_event)
13512                 return false;
13513
13514         spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
13515         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13516         spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
13517
13518         /* Set the async event flag */
13519         spin_lock_irqsave(&phba->hbalock, iflags);
13520         phba->hba_flag |= ASYNC_EVENT;
13521         spin_unlock_irqrestore(&phba->hbalock, iflags);
13522
13523         return true;
13524 }
13525
13526 /**
13527  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13528  * @phba: Pointer to HBA context object.
13529  * @mcqe: Pointer to mailbox completion queue entry.
13530  *
13531  * This routine process a mailbox completion queue entry with mailbox
13532  * completion event.
13533  *
13534  * Return: true if work posted to worker thread, otherwise false.
13535  **/
13536 static bool
13537 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13538 {
13539         uint32_t mcqe_status;
13540         MAILBOX_t *mbox, *pmbox;
13541         struct lpfc_mqe *mqe;
13542         struct lpfc_vport *vport;
13543         struct lpfc_nodelist *ndlp;
13544         struct lpfc_dmabuf *mp;
13545         unsigned long iflags;
13546         LPFC_MBOXQ_t *pmb;
13547         bool workposted = false;
13548         int rc;
13549
13550         /* If not a mailbox complete MCQE, out by checking mailbox consume */
13551         if (!bf_get(lpfc_trailer_completed, mcqe))
13552                 goto out_no_mqe_complete;
13553
13554         /* Get the reference to the active mbox command */
13555         spin_lock_irqsave(&phba->hbalock, iflags);
13556         pmb = phba->sli.mbox_active;
13557         if (unlikely(!pmb)) {
13558                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13559                                 "1832 No pending MBOX command to handle\n");
13560                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13561                 goto out_no_mqe_complete;
13562         }
13563         spin_unlock_irqrestore(&phba->hbalock, iflags);
13564         mqe = &pmb->u.mqe;
13565         pmbox = (MAILBOX_t *)&pmb->u.mqe;
13566         mbox = phba->mbox;
13567         vport = pmb->vport;
13568
13569         /* Reset heartbeat timer */
13570         phba->last_completion_time = jiffies;
13571         del_timer(&phba->sli.mbox_tmo);
13572
13573         /* Move mbox data to caller's mailbox region, do endian swapping */
13574         if (pmb->mbox_cmpl && mbox)
13575                 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
13576
13577         /*
13578          * For mcqe errors, conditionally move a modified error code to
13579          * the mbox so that the error will not be missed.
13580          */
13581         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13582         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13583                 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13584                         bf_set(lpfc_mqe_status, mqe,
13585                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
13586         }
13587         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13588                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13589                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13590                                       "MBOX dflt rpi: status:x%x rpi:x%x",
13591                                       mcqe_status,
13592                                       pmbox->un.varWords[0], 0);
13593                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
13594                         mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13595                         ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
13596                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
13597                          * RID of the PPI using the same mbox buffer.
13598                          */
13599                         lpfc_unreg_login(phba, vport->vpi,
13600                                          pmbox->un.varWords[0], pmb);
13601                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
13602                         pmb->ctx_buf = mp;
13603
13604                         /* No reference taken here.  This is a default
13605                          * RPI reg/immediate unreg cycle. The reference was
13606                          * taken in the reg rpi path and is released when
13607                          * this mailbox completes.
13608                          */
13609                         pmb->ctx_ndlp = ndlp;
13610                         pmb->vport = vport;
13611                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13612                         if (rc != MBX_BUSY)
13613                                 lpfc_printf_log(phba, KERN_ERR,
13614                                                 LOG_TRACE_EVENT,
13615                                                 "0385 rc should "
13616                                                 "have been MBX_BUSY\n");
13617                         if (rc != MBX_NOT_FINISHED)
13618                                 goto send_current_mbox;
13619                 }
13620         }
13621         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13622         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13623         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13624
13625         /* There is mailbox completion work to do */
13626         spin_lock_irqsave(&phba->hbalock, iflags);
13627         __lpfc_mbox_cmpl_put(phba, pmb);
13628         phba->work_ha |= HA_MBATT;
13629         spin_unlock_irqrestore(&phba->hbalock, iflags);
13630         workposted = true;
13631
13632 send_current_mbox:
13633         spin_lock_irqsave(&phba->hbalock, iflags);
13634         /* Release the mailbox command posting token */
13635         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13636         /* Setting active mailbox pointer need to be in sync to flag clear */
13637         phba->sli.mbox_active = NULL;
13638         if (bf_get(lpfc_trailer_consumed, mcqe))
13639                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13640         spin_unlock_irqrestore(&phba->hbalock, iflags);
13641         /* Wake up worker thread to post the next pending mailbox command */
13642         lpfc_worker_wake_up(phba);
13643         return workposted;
13644
13645 out_no_mqe_complete:
13646         spin_lock_irqsave(&phba->hbalock, iflags);
13647         if (bf_get(lpfc_trailer_consumed, mcqe))
13648                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13649         spin_unlock_irqrestore(&phba->hbalock, iflags);
13650         return false;
13651 }
13652
13653 /**
13654  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13655  * @phba: Pointer to HBA context object.
13656  * @cq: Pointer to associated CQ
13657  * @cqe: Pointer to mailbox completion queue entry.
13658  *
13659  * This routine process a mailbox completion queue entry, it invokes the
13660  * proper mailbox complete handling or asynchronous event handling routine
13661  * according to the MCQE's async bit.
13662  *
13663  * Return: true if work posted to worker thread, otherwise false.
13664  **/
13665 static bool
13666 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13667                          struct lpfc_cqe *cqe)
13668 {
13669         struct lpfc_mcqe mcqe;
13670         bool workposted;
13671
13672         cq->CQ_mbox++;
13673
13674         /* Copy the mailbox MCQE and convert endian order as needed */
13675         lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
13676
13677         /* Invoke the proper event handling routine */
13678         if (!bf_get(lpfc_trailer_async, &mcqe))
13679                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13680         else
13681                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13682         return workposted;
13683 }
13684
13685 /**
13686  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13687  * @phba: Pointer to HBA context object.
13688  * @cq: Pointer to associated CQ
13689  * @wcqe: Pointer to work-queue completion queue entry.
13690  *
13691  * This routine handles an ELS work-queue completion event.
13692  *
13693  * Return: true if work posted to worker thread, otherwise false.
13694  **/
13695 static bool
13696 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13697                              struct lpfc_wcqe_complete *wcqe)
13698 {
13699         struct lpfc_iocbq *irspiocbq;
13700         unsigned long iflags;
13701         struct lpfc_sli_ring *pring = cq->pring;
13702         int txq_cnt = 0;
13703         int txcmplq_cnt = 0;
13704
13705         /* Check for response status */
13706         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13707                 /* Log the error status */
13708                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13709                                 "0357 ELS CQE error: status=x%x: "
13710                                 "CQE: %08x %08x %08x %08x\n",
13711                                 bf_get(lpfc_wcqe_c_status, wcqe),
13712                                 wcqe->word0, wcqe->total_data_placed,
13713                                 wcqe->parameter, wcqe->word3);
13714         }
13715
13716         /* Get an irspiocbq for later ELS response processing use */
13717         irspiocbq = lpfc_sli_get_iocbq(phba);
13718         if (!irspiocbq) {
13719                 if (!list_empty(&pring->txq))
13720                         txq_cnt++;
13721                 if (!list_empty(&pring->txcmplq))
13722                         txcmplq_cnt++;
13723                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13724                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13725                         "els_txcmplq_cnt=%d\n",
13726                         txq_cnt, phba->iocb_cnt,
13727                         txcmplq_cnt);
13728                 return false;
13729         }
13730
13731         /* Save off the slow-path queue event for work thread to process */
13732         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
13733         spin_lock_irqsave(&phba->hbalock, iflags);
13734         list_add_tail(&irspiocbq->cq_event.list,
13735                       &phba->sli4_hba.sp_queue_event);
13736         phba->hba_flag |= HBA_SP_QUEUE_EVT;
13737         spin_unlock_irqrestore(&phba->hbalock, iflags);
13738
13739         return true;
13740 }
13741
13742 /**
13743  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13744  * @phba: Pointer to HBA context object.
13745  * @wcqe: Pointer to work-queue completion queue entry.
13746  *
13747  * This routine handles slow-path WQ entry consumed event by invoking the
13748  * proper WQ release routine to the slow-path WQ.
13749  **/
13750 static void
13751 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13752                              struct lpfc_wcqe_release *wcqe)
13753 {
13754         /* sanity check on queue memory */
13755         if (unlikely(!phba->sli4_hba.els_wq))
13756                 return;
13757         /* Check for the slow-path ELS work queue */
13758         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13759                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13760                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13761         else
13762                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13763                                 "2579 Slow-path wqe consume event carries "
13764                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13765                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13766                                 phba->sli4_hba.els_wq->queue_id);
13767 }
13768
13769 /**
13770  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13771  * @phba: Pointer to HBA context object.
13772  * @cq: Pointer to a WQ completion queue.
13773  * @wcqe: Pointer to work-queue completion queue entry.
13774  *
13775  * This routine handles an XRI abort event.
13776  *
13777  * Return: true if work posted to worker thread, otherwise false.
13778  **/
13779 static bool
13780 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13781                                    struct lpfc_queue *cq,
13782                                    struct sli4_wcqe_xri_aborted *wcqe)
13783 {
13784         bool workposted = false;
13785         struct lpfc_cq_event *cq_event;
13786         unsigned long iflags;
13787
13788         switch (cq->subtype) {
13789         case LPFC_IO:
13790                 lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
13791                 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
13792                         /* Notify aborted XRI for NVME work queue */
13793                         if (phba->nvmet_support)
13794                                 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13795                 }
13796                 workposted = false;
13797                 break;
13798         case LPFC_NVME_LS: /* NVME LS uses ELS resources */
13799         case LPFC_ELS:
13800                 cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));
13801                 if (!cq_event) {
13802                         workposted = false;
13803                         break;
13804                 }
13805                 cq_event->hdwq = cq->hdwq;
13806                 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
13807                                   iflags);
13808                 list_add_tail(&cq_event->list,
13809                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13810                 /* Set the els xri abort event flag */
13811                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13812                 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
13813                                        iflags);
13814                 workposted = true;
13815                 break;
13816         default:
13817                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13818                                 "0603 Invalid CQ subtype %d: "
13819                                 "%08x %08x %08x %08x\n",
13820                                 cq->subtype, wcqe->word0, wcqe->parameter,
13821                                 wcqe->word2, wcqe->word3);
13822                 workposted = false;
13823                 break;
13824         }
13825         return workposted;
13826 }
13827
13828 #define FC_RCTL_MDS_DIAGS       0xF4
13829
13830 /**
13831  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
13832  * @phba: Pointer to HBA context object.
13833  * @rcqe: Pointer to receive-queue completion queue entry.
13834  *
13835  * This routine process a receive-queue completion queue entry.
13836  *
13837  * Return: true if work posted to worker thread, otherwise false.
13838  **/
13839 static bool
13840 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13841 {
13842         bool workposted = false;
13843         struct fc_frame_header *fc_hdr;
13844         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13845         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
13846         struct lpfc_nvmet_tgtport *tgtp;
13847         struct hbq_dmabuf *dma_buf;
13848         uint32_t status, rq_id;
13849         unsigned long iflags;
13850
13851         /* sanity check on queue memory */
13852         if (unlikely(!hrq) || unlikely(!drq))
13853                 return workposted;
13854
13855         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13856                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13857         else
13858                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13859         if (rq_id != hrq->queue_id)
13860                 goto out;
13861
13862         status = bf_get(lpfc_rcqe_status, rcqe);
13863         switch (status) {
13864         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13865                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13866                                 "2537 Receive Frame Truncated!!\n");
13867                 fallthrough;
13868         case FC_STATUS_RQ_SUCCESS:
13869                 spin_lock_irqsave(&phba->hbalock, iflags);
13870                 lpfc_sli4_rq_release(hrq, drq);
13871                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13872                 if (!dma_buf) {
13873                         hrq->RQ_no_buf_found++;
13874                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13875                         goto out;
13876                 }
13877                 hrq->RQ_rcv_buf++;
13878                 hrq->RQ_buf_posted--;
13879                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
13880
13881                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13882
13883                 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
13884                     fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
13885                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13886                         /* Handle MDS Loopback frames */
13887                         if  (!(phba->pport->load_flag & FC_UNLOADING))
13888                                 lpfc_sli4_handle_mds_loopback(phba->pport,
13889                                                               dma_buf);
13890                         else
13891                                 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13892                         break;
13893                 }
13894
13895                 /* save off the frame for the work thread to process */
13896                 list_add_tail(&dma_buf->cq_event.list,
13897                               &phba->sli4_hba.sp_queue_event);
13898                 /* Frame received */
13899                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
13900                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13901                 workposted = true;
13902                 break;
13903         case FC_STATUS_INSUFF_BUF_FRM_DISC:
13904                 if (phba->nvmet_support) {
13905                         tgtp = phba->targetport->private;
13906                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13907                                         "6402 RQE Error x%x, posted %d err_cnt "
13908                                         "%d: %x %x %x\n",
13909                                         status, hrq->RQ_buf_posted,
13910                                         hrq->RQ_no_posted_buf,
13911                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
13912                                         atomic_read(&tgtp->rcv_fcp_cmd_out),
13913                                         atomic_read(&tgtp->xmt_fcp_release));
13914                 }
13915                 fallthrough;
13916
13917         case FC_STATUS_INSUFF_BUF_NEED_BUF:
13918                 hrq->RQ_no_posted_buf++;
13919                 /* Post more buffers if possible */
13920                 spin_lock_irqsave(&phba->hbalock, iflags);
13921                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13922                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13923                 workposted = true;
13924                 break;
13925         }
13926 out:
13927         return workposted;
13928 }
13929
13930 /**
13931  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13932  * @phba: Pointer to HBA context object.
13933  * @cq: Pointer to the completion queue.
13934  * @cqe: Pointer to a completion queue entry.
13935  *
13936  * This routine process a slow-path work-queue or receive queue completion queue
13937  * entry.
13938  *
13939  * Return: true if work posted to worker thread, otherwise false.
13940  **/
13941 static bool
13942 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13943                          struct lpfc_cqe *cqe)
13944 {
13945         struct lpfc_cqe cqevt;
13946         bool workposted = false;
13947
13948         /* Copy the work queue CQE and convert endian order if needed */
13949         lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
13950
13951         /* Check and process for different type of WCQE and dispatch */
13952         switch (bf_get(lpfc_cqe_code, &cqevt)) {
13953         case CQE_CODE_COMPL_WQE:
13954                 /* Process the WQ/RQ complete event */
13955                 phba->last_completion_time = jiffies;
13956                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
13957                                 (struct lpfc_wcqe_complete *)&cqevt);
13958                 break;
13959         case CQE_CODE_RELEASE_WQE:
13960                 /* Process the WQ release event */
13961                 lpfc_sli4_sp_handle_rel_wcqe(phba,
13962                                 (struct lpfc_wcqe_release *)&cqevt);
13963                 break;
13964         case CQE_CODE_XRI_ABORTED:
13965                 /* Process the WQ XRI abort event */
13966                 phba->last_completion_time = jiffies;
13967                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13968                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
13969                 break;
13970         case CQE_CODE_RECEIVE:
13971         case CQE_CODE_RECEIVE_V1:
13972                 /* Process the RQ event */
13973                 phba->last_completion_time = jiffies;
13974                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
13975                                 (struct lpfc_rcqe *)&cqevt);
13976                 break;
13977         default:
13978                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13979                                 "0388 Not a valid WCQE code: x%x\n",
13980                                 bf_get(lpfc_cqe_code, &cqevt));
13981                 break;
13982         }
13983         return workposted;
13984 }
13985
13986 /**
13987  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13988  * @phba: Pointer to HBA context object.
13989  * @eqe: Pointer to fast-path event queue entry.
13990  * @speq: Pointer to slow-path event queue.
13991  *
13992  * This routine process a event queue entry from the slow-path event queue.
13993  * It will check the MajorCode and MinorCode to determine this is for a
13994  * completion event on a completion queue, if not, an error shall be logged
13995  * and just return. Otherwise, it will get to the corresponding completion
13996  * queue and process all the entries on that completion queue, rearm the
13997  * completion queue, and then return.
13998  *
13999  **/
14000 static void
14001 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14002         struct lpfc_queue *speq)
14003 {
14004         struct lpfc_queue *cq = NULL, *childq;
14005         uint16_t cqid;
14006         int ret = 0;
14007
14008         /* Get the reference to the corresponding CQ */
14009         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14010
14011         list_for_each_entry(childq, &speq->child_list, list) {
14012                 if (childq->queue_id == cqid) {
14013                         cq = childq;
14014                         break;
14015                 }
14016         }
14017         if (unlikely(!cq)) {
14018                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
14019                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14020                                         "0365 Slow-path CQ identifier "
14021                                         "(%d) does not exist\n", cqid);
14022                 return;
14023         }
14024
14025         /* Save EQ associated with this CQ */
14026         cq->assoc_qp = speq;
14027
14028         if (is_kdump_kernel())
14029                 ret = queue_work(phba->wq, &cq->spwork);
14030         else
14031                 ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
14032
14033         if (!ret)
14034                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14035                                 "0390 Cannot schedule queue work "
14036                                 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14037                                 cqid, cq->queue_id, raw_smp_processor_id());
14038 }
14039
14040 /**
14041  * __lpfc_sli4_process_cq - Process elements of a CQ
14042  * @phba: Pointer to HBA context object.
14043  * @cq: Pointer to CQ to be processed
14044  * @handler: Routine to process each cqe
14045  * @delay: Pointer to usdelay to set in case of rescheduling of the handler
14046  * @poll_mode: Polling mode we were called from
14047  *
14048  * This routine processes completion queue entries in a CQ. While a valid
14049  * queue element is found, the handler is called. During processing checks
14050  * are made for periodic doorbell writes to let the hardware know of
14051  * element consumption.
14052  *
14053  * If the max limit on cqes to process is hit, or there are no more valid
14054  * entries, the loop stops. If we processed a sufficient number of elements,
14055  * meaning there is sufficient load, rather than rearming and generating
14056  * another interrupt, a cq rescheduling delay will be set. A delay of 0
14057  * indicates no rescheduling.
14058  *
14059  * Returns True if work scheduled, False otherwise.
14060  **/
14061 static bool
14062 __lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
14063         bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
14064                         struct lpfc_cqe *), unsigned long *delay,
14065                         enum lpfc_poll_mode poll_mode)
14066 {
14067         struct lpfc_cqe *cqe;
14068         bool workposted = false;
14069         int count = 0, consumed = 0;
14070         bool arm = true;
14071
14072         /* default - no reschedule */
14073         *delay = 0;
14074
14075         if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
14076                 goto rearm_and_exit;
14077
14078         /* Process all the entries to the CQ */
14079         cq->q_flag = 0;
14080         cqe = lpfc_sli4_cq_get(cq);
14081         while (cqe) {
14082                 workposted |= handler(phba, cq, cqe);
14083                 __lpfc_sli4_consume_cqe(phba, cq, cqe);
14084
14085                 consumed++;
14086                 if (!(++count % cq->max_proc_limit))
14087                         break;
14088
14089                 if (!(count % cq->notify_interval)) {
14090                         phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14091                                                 LPFC_QUEUE_NOARM);
14092                         consumed = 0;
14093                         cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
14094                 }
14095
14096                 if (count == LPFC_NVMET_CQ_NOTIFY)
14097                         cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
14098
14099                 cqe = lpfc_sli4_cq_get(cq);
14100         }
14101         if (count >= phba->cfg_cq_poll_threshold) {
14102                 *delay = 1;
14103                 arm = false;
14104         }
14105
14106         /* Note: complete the irq_poll softirq before rearming CQ */
14107         if (poll_mode == LPFC_IRQ_POLL)
14108                 irq_poll_complete(&cq->iop);
14109
14110         /* Track the max number of CQEs processed in 1 EQ */
14111         if (count > cq->CQ_max_cqe)
14112                 cq->CQ_max_cqe = count;
14113
14114         cq->assoc_qp->EQ_cqe_cnt += count;
14115
14116         /* Catch the no cq entry condition */
14117         if (unlikely(count == 0))
14118                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14119                                 "0369 No entry from completion queue "
14120                                 "qid=%d\n", cq->queue_id);
14121
14122         xchg(&cq->queue_claimed, 0);
14123
14124 rearm_and_exit:
14125         phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14126                         arm ?  LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
14127
14128         return workposted;
14129 }
14130
14131 /**
14132  * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
14133  * @cq: pointer to CQ to process
14134  *
14135  * This routine calls the cq processing routine with a handler specific
14136  * to the type of queue bound to it.
14137  *
14138  * The CQ routine returns two values: the first is the calling status,
14139  * which indicates whether work was queued to the  background discovery
14140  * thread. If true, the routine should wakeup the discovery thread;
14141  * the second is the delay parameter. If non-zero, rather than rearming
14142  * the CQ and yet another interrupt, the CQ handler should be queued so
14143  * that it is processed in a subsequent polling action. The value of
14144  * the delay indicates when to reschedule it.
14145  **/
14146 static void
14147 __lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
14148 {
14149         struct lpfc_hba *phba = cq->phba;
14150         unsigned long delay;
14151         bool workposted = false;
14152         int ret = 0;
14153
14154         /* Process and rearm the CQ */
14155         switch (cq->type) {
14156         case LPFC_MCQ:
14157                 workposted |= __lpfc_sli4_process_cq(phba, cq,
14158                                                 lpfc_sli4_sp_handle_mcqe,
14159                                                 &delay, LPFC_QUEUE_WORK);
14160                 break;
14161         case LPFC_WCQ:
14162                 if (cq->subtype == LPFC_IO)
14163                         workposted |= __lpfc_sli4_process_cq(phba, cq,
14164                                                 lpfc_sli4_fp_handle_cqe,
14165                                                 &delay, LPFC_QUEUE_WORK);
14166                 else
14167                         workposted |= __lpfc_sli4_process_cq(phba, cq,
14168                                                 lpfc_sli4_sp_handle_cqe,
14169                                                 &delay, LPFC_QUEUE_WORK);
14170                 break;
14171         default:
14172                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14173                                 "0370 Invalid completion queue type (%d)\n",
14174                                 cq->type);
14175                 return;
14176         }
14177
14178         if (delay) {
14179                 if (is_kdump_kernel())
14180                         ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
14181                                                 delay);
14182                 else
14183                         ret = queue_delayed_work_on(cq->chann, phba->wq,
14184                                                 &cq->sched_spwork, delay);
14185                 if (!ret)
14186                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14187                                 "0394 Cannot schedule queue work "
14188                                 "for cqid=%d on CPU %d\n",
14189                                 cq->queue_id, cq->chann);
14190         }
14191
14192         /* wake up worker thread if there are works to be done */
14193         if (workposted)
14194                 lpfc_worker_wake_up(phba);
14195 }
14196
14197 /**
14198  * lpfc_sli4_sp_process_cq - slow-path work handler when started by
14199  *   interrupt
14200  * @work: pointer to work element
14201  *
14202  * translates from the work handler and calls the slow-path handler.
14203  **/
14204 static void
14205 lpfc_sli4_sp_process_cq(struct work_struct *work)
14206 {
14207         struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
14208
14209         __lpfc_sli4_sp_process_cq(cq);
14210 }
14211
14212 /**
14213  * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
14214  * @work: pointer to work element
14215  *
14216  * translates from the work handler and calls the slow-path handler.
14217  **/
14218 static void
14219 lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
14220 {
14221         struct lpfc_queue *cq = container_of(to_delayed_work(work),
14222                                         struct lpfc_queue, sched_spwork);
14223
14224         __lpfc_sli4_sp_process_cq(cq);
14225 }
14226
14227 /**
14228  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
14229  * @phba: Pointer to HBA context object.
14230  * @cq: Pointer to associated CQ
14231  * @wcqe: Pointer to work-queue completion queue entry.
14232  *
14233  * This routine process a fast-path work queue completion entry from fast-path
14234  * event queue for FCP command response completion.
14235  **/
14236 static void
14237 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14238                              struct lpfc_wcqe_complete *wcqe)
14239 {
14240         struct lpfc_sli_ring *pring = cq->pring;
14241         struct lpfc_iocbq *cmdiocbq;
14242         struct lpfc_iocbq irspiocbq;
14243         unsigned long iflags;
14244
14245         /* Check for response status */
14246         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14247                 /* If resource errors reported from HBA, reduce queue
14248                  * depth of the SCSI device.
14249                  */
14250                 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
14251                      IOSTAT_LOCAL_REJECT)) &&
14252                     ((wcqe->parameter & IOERR_PARAM_MASK) ==
14253                      IOERR_NO_RESOURCES))
14254                         phba->lpfc_rampdown_queue_depth(phba);
14255
14256                 /* Log the cmpl status */
14257                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14258                                 "0373 FCP CQE cmpl: status=x%x: "
14259                                 "CQE: %08x %08x %08x %08x\n",
14260                                 bf_get(lpfc_wcqe_c_status, wcqe),
14261                                 wcqe->word0, wcqe->total_data_placed,
14262                                 wcqe->parameter, wcqe->word3);
14263         }
14264
14265         /* Look up the FCP command IOCB and create pseudo response IOCB */
14266         spin_lock_irqsave(&pring->ring_lock, iflags);
14267         pring->stats.iocb_event++;
14268         spin_unlock_irqrestore(&pring->ring_lock, iflags);
14269         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14270                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
14271         if (unlikely(!cmdiocbq)) {
14272                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14273                                 "0374 FCP complete with no corresponding "
14274                                 "cmdiocb: iotag (%d)\n",
14275                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
14276                 return;
14277         }
14278 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
14279         cmdiocbq->isr_timestamp = cq->isr_timestamp;
14280 #endif
14281         if (cmdiocbq->iocb_cmpl == NULL) {
14282                 if (cmdiocbq->wqe_cmpl) {
14283                         /* For FCP the flag is cleared in wqe_cmpl */
14284                         if (!(cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
14285                             cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
14286                                 spin_lock_irqsave(&phba->hbalock, iflags);
14287                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
14288                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
14289                         }
14290
14291                         /* Pass the cmd_iocb and the wcqe to the upper layer */
14292                         (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
14293                         return;
14294                 }
14295                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14296                                 "0375 FCP cmdiocb not callback function "
14297                                 "iotag: (%d)\n",
14298                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
14299                 return;
14300         }
14301
14302         /* Only SLI4 non-IO commands stil use IOCB */
14303         /* Fake the irspiocb and copy necessary response information */
14304         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
14305
14306         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
14307                 spin_lock_irqsave(&phba->hbalock, iflags);
14308                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
14309                 spin_unlock_irqrestore(&phba->hbalock, iflags);
14310         }
14311
14312         /* Pass the cmd_iocb and the rsp state to the upper layer */
14313         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
14314 }
14315
14316 /**
14317  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
14318  * @phba: Pointer to HBA context object.
14319  * @cq: Pointer to completion queue.
14320  * @wcqe: Pointer to work-queue completion queue entry.
14321  *
14322  * This routine handles an fast-path WQ entry consumed event by invoking the
14323  * proper WQ release routine to the slow-path WQ.
14324  **/
14325 static void
14326 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14327                              struct lpfc_wcqe_release *wcqe)
14328 {
14329         struct lpfc_queue *childwq;
14330         bool wqid_matched = false;
14331         uint16_t hba_wqid;
14332
14333         /* Check for fast-path FCP work queue release */
14334         hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
14335         list_for_each_entry(childwq, &cq->child_list, list) {
14336                 if (childwq->queue_id == hba_wqid) {
14337                         lpfc_sli4_wq_release(childwq,
14338                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
14339                         if (childwq->q_flag & HBA_NVMET_WQFULL)
14340                                 lpfc_nvmet_wqfull_process(phba, childwq);
14341                         wqid_matched = true;
14342                         break;
14343                 }
14344         }
14345         /* Report warning log message if no match found */
14346         if (wqid_matched != true)
14347                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14348                                 "2580 Fast-path wqe consume event carries "
14349                                 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
14350 }
14351
14352 /**
14353  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
14354  * @phba: Pointer to HBA context object.
14355  * @cq: Pointer to completion queue.
14356  * @rcqe: Pointer to receive-queue completion queue entry.
14357  *
14358  * This routine process a receive-queue completion queue entry.
14359  *
14360  * Return: true if work posted to worker thread, otherwise false.
14361  **/
14362 static bool
14363 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14364                             struct lpfc_rcqe *rcqe)
14365 {
14366         bool workposted = false;
14367         struct lpfc_queue *hrq;
14368         struct lpfc_queue *drq;
14369         struct rqb_dmabuf *dma_buf;
14370         struct fc_frame_header *fc_hdr;
14371         struct lpfc_nvmet_tgtport *tgtp;
14372         uint32_t status, rq_id;
14373         unsigned long iflags;
14374         uint32_t fctl, idx;
14375
14376         if ((phba->nvmet_support == 0) ||
14377             (phba->sli4_hba.nvmet_cqset == NULL))
14378                 return workposted;
14379
14380         idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
14381         hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
14382         drq = phba->sli4_hba.nvmet_mrq_data[idx];
14383
14384         /* sanity check on queue memory */
14385         if (unlikely(!hrq) || unlikely(!drq))
14386                 return workposted;
14387
14388         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14389                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14390         else
14391                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14392
14393         if ((phba->nvmet_support == 0) ||
14394             (rq_id != hrq->queue_id))
14395                 return workposted;
14396
14397         status = bf_get(lpfc_rcqe_status, rcqe);
14398         switch (status) {
14399         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
14400                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14401                                 "6126 Receive Frame Truncated!!\n");
14402                 fallthrough;
14403         case FC_STATUS_RQ_SUCCESS:
14404                 spin_lock_irqsave(&phba->hbalock, iflags);
14405                 lpfc_sli4_rq_release(hrq, drq);
14406                 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
14407                 if (!dma_buf) {
14408                         hrq->RQ_no_buf_found++;
14409                         spin_unlock_irqrestore(&phba->hbalock, iflags);
14410                         goto out;
14411                 }
14412                 spin_unlock_irqrestore(&phba->hbalock, iflags);
14413                 hrq->RQ_rcv_buf++;
14414                 hrq->RQ_buf_posted--;
14415                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14416
14417                 /* Just some basic sanity checks on FCP Command frame */
14418                 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
14419                         fc_hdr->fh_f_ctl[1] << 8 |
14420                         fc_hdr->fh_f_ctl[2]);
14421                 if (((fctl &
14422                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
14423                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
14424                     (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
14425                         goto drop;
14426
14427                 if (fc_hdr->fh_type == FC_TYPE_FCP) {
14428                         dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
14429                         lpfc_nvmet_unsol_fcp_event(
14430                                 phba, idx, dma_buf, cq->isr_timestamp,
14431                                 cq->q_flag & HBA_NVMET_CQ_NOTIFY);
14432                         return false;
14433                 }
14434 drop:
14435                 lpfc_rq_buf_free(phba, &dma_buf->hbuf);
14436                 break;
14437         case FC_STATUS_INSUFF_BUF_FRM_DISC:
14438                 if (phba->nvmet_support) {
14439                         tgtp = phba->targetport->private;
14440                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14441                                         "6401 RQE Error x%x, posted %d err_cnt "
14442                                         "%d: %x %x %x\n",
14443                                         status, hrq->RQ_buf_posted,
14444                                         hrq->RQ_no_posted_buf,
14445                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
14446                                         atomic_read(&tgtp->rcv_fcp_cmd_out),
14447                                         atomic_read(&tgtp->xmt_fcp_release));
14448                 }
14449                 fallthrough;
14450
14451         case FC_STATUS_INSUFF_BUF_NEED_BUF:
14452                 hrq->RQ_no_posted_buf++;
14453                 /* Post more buffers if possible */
14454                 break;
14455         }
14456 out:
14457         return workposted;
14458 }
14459
14460 /**
14461  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
14462  * @phba: adapter with cq
14463  * @cq: Pointer to the completion queue.
14464  * @cqe: Pointer to fast-path completion queue entry.
14465  *
14466  * This routine process a fast-path work queue completion entry from fast-path
14467  * event queue for FCP command response completion.
14468  *
14469  * Return: true if work posted to worker thread, otherwise false.
14470  **/
14471 static bool
14472 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14473                          struct lpfc_cqe *cqe)
14474 {
14475         struct lpfc_wcqe_release wcqe;
14476         bool workposted = false;
14477
14478         /* Copy the work queue CQE and convert endian order if needed */
14479         lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
14480
14481         /* Check and process for different type of WCQE and dispatch */
14482         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
14483         case CQE_CODE_COMPL_WQE:
14484         case CQE_CODE_NVME_ERSP:
14485                 cq->CQ_wq++;
14486                 /* Process the WQ complete event */
14487                 phba->last_completion_time = jiffies;
14488                 if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
14489                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
14490                                 (struct lpfc_wcqe_complete *)&wcqe);
14491                 break;
14492         case CQE_CODE_RELEASE_WQE:
14493                 cq->CQ_release_wqe++;
14494                 /* Process the WQ release event */
14495                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
14496                                 (struct lpfc_wcqe_release *)&wcqe);
14497                 break;
14498         case CQE_CODE_XRI_ABORTED:
14499                 cq->CQ_xri_aborted++;
14500                 /* Process the WQ XRI abort event */
14501                 phba->last_completion_time = jiffies;
14502                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14503                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
14504                 break;
14505         case CQE_CODE_RECEIVE_V1:
14506         case CQE_CODE_RECEIVE:
14507                 phba->last_completion_time = jiffies;
14508                 if (cq->subtype == LPFC_NVMET) {
14509                         workposted = lpfc_sli4_nvmet_handle_rcqe(
14510                                 phba, cq, (struct lpfc_rcqe *)&wcqe);
14511                 }
14512                 break;
14513         default:
14514                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14515                                 "0144 Not a valid CQE code: x%x\n",
14516                                 bf_get(lpfc_wcqe_c_code, &wcqe));
14517                 break;
14518         }
14519         return workposted;
14520 }
14521
14522 /**
14523  * lpfc_sli4_sched_cq_work - Schedules cq work
14524  * @phba: Pointer to HBA context object.
14525  * @cq: Pointer to CQ
14526  * @cqid: CQ ID
14527  *
14528  * This routine checks the poll mode of the CQ corresponding to
14529  * cq->chann, then either schedules a softirq or queue_work to complete
14530  * cq work.
14531  *
14532  * queue_work path is taken if in NVMET mode, or if poll_mode is in
14533  * LPFC_QUEUE_WORK mode.  Otherwise, softirq path is taken.
14534  *
14535  **/
14536 static void lpfc_sli4_sched_cq_work(struct lpfc_hba *phba,
14537                                     struct lpfc_queue *cq, uint16_t cqid)
14538 {
14539         int ret = 0;
14540
14541         switch (cq->poll_mode) {
14542         case LPFC_IRQ_POLL:
14543                 irq_poll_sched(&cq->iop);
14544                 break;
14545         case LPFC_QUEUE_WORK:
14546         default:
14547                 if (is_kdump_kernel())
14548                         ret = queue_work(phba->wq, &cq->irqwork);
14549                 else
14550                         ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
14551                 if (!ret)
14552                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14553                                         "0383 Cannot schedule queue work "
14554                                         "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14555                                         cqid, cq->queue_id,
14556                                         raw_smp_processor_id());
14557         }
14558 }
14559
14560 /**
14561  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
14562  * @phba: Pointer to HBA context object.
14563  * @eq: Pointer to the queue structure.
14564  * @eqe: Pointer to fast-path event queue entry.
14565  *
14566  * This routine process a event queue entry from the fast-path event queue.
14567  * It will check the MajorCode and MinorCode to determine this is for a
14568  * completion event on a completion queue, if not, an error shall be logged
14569  * and just return. Otherwise, it will get to the corresponding completion
14570  * queue and process all the entries on the completion queue, rearm the
14571  * completion queue, and then return.
14572  **/
14573 static void
14574 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
14575                          struct lpfc_eqe *eqe)
14576 {
14577         struct lpfc_queue *cq = NULL;
14578         uint32_t qidx = eq->hdwq;
14579         uint16_t cqid, id;
14580
14581         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
14582                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14583                                 "0366 Not a valid completion "
14584                                 "event: majorcode=x%x, minorcode=x%x\n",
14585                                 bf_get_le32(lpfc_eqe_major_code, eqe),
14586                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
14587                 return;
14588         }
14589
14590         /* Get the reference to the corresponding CQ */
14591         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14592
14593         /* Use the fast lookup method first */
14594         if (cqid <= phba->sli4_hba.cq_max) {
14595                 cq = phba->sli4_hba.cq_lookup[cqid];
14596                 if (cq)
14597                         goto  work_cq;
14598         }
14599
14600         /* Next check for NVMET completion */
14601         if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14602                 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14603                 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14604                         /* Process NVMET unsol rcv */
14605                         cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14606                         goto  process_cq;
14607                 }
14608         }
14609
14610         if (phba->sli4_hba.nvmels_cq &&
14611             (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14612                 /* Process NVME unsol rcv */
14613                 cq = phba->sli4_hba.nvmels_cq;
14614         }
14615
14616         /* Otherwise this is a Slow path event */
14617         if (cq == NULL) {
14618                 lpfc_sli4_sp_handle_eqe(phba, eqe,
14619                                         phba->sli4_hba.hdwq[qidx].hba_eq);
14620                 return;
14621         }
14622
14623 process_cq:
14624         if (unlikely(cqid != cq->queue_id)) {
14625                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14626                                 "0368 Miss-matched fast-path completion "
14627                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14628                                 cqid, cq->queue_id);
14629                 return;
14630         }
14631
14632 work_cq:
14633 #if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
14634         if (phba->ktime_on)
14635                 cq->isr_timestamp = ktime_get_ns();
14636         else
14637                 cq->isr_timestamp = 0;
14638 #endif
14639         lpfc_sli4_sched_cq_work(phba, cq, cqid);
14640 }
14641
14642 /**
14643  * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14644  * @cq: Pointer to CQ to be processed
14645  * @poll_mode: Enum lpfc_poll_state to determine poll mode
14646  *
14647  * This routine calls the cq processing routine with the handler for
14648  * fast path CQEs.
14649  *
14650  * The CQ routine returns two values: the first is the calling status,
14651  * which indicates whether work was queued to the  background discovery
14652  * thread. If true, the routine should wakeup the discovery thread;
14653  * the second is the delay parameter. If non-zero, rather than rearming
14654  * the CQ and yet another interrupt, the CQ handler should be queued so
14655  * that it is processed in a subsequent polling action. The value of
14656  * the delay indicates when to reschedule it.
14657  **/
14658 static void
14659 __lpfc_sli4_hba_process_cq(struct lpfc_queue *cq,
14660                            enum lpfc_poll_mode poll_mode)
14661 {
14662         struct lpfc_hba *phba = cq->phba;
14663         unsigned long delay;
14664         bool workposted = false;
14665         int ret = 0;
14666
14667         /* process and rearm the CQ */
14668         workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
14669                                              &delay, poll_mode);
14670
14671         if (delay) {
14672                 if (is_kdump_kernel())
14673                         ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
14674                                                 delay);
14675                 else
14676                         ret = queue_delayed_work_on(cq->chann, phba->wq,
14677                                                 &cq->sched_irqwork, delay);
14678                 if (!ret)
14679                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14680                                         "0367 Cannot schedule queue work "
14681                                         "for cqid=%d on CPU %d\n",
14682                                         cq->queue_id, cq->chann);
14683         }
14684
14685         /* wake up worker thread if there are works to be done */
14686         if (workposted)
14687                 lpfc_worker_wake_up(phba);
14688 }
14689
14690 /**
14691  * lpfc_sli4_hba_process_cq - fast-path work handler when started by
14692  *   interrupt
14693  * @work: pointer to work element
14694  *
14695  * translates from the work handler and calls the fast-path handler.
14696  **/
14697 static void
14698 lpfc_sli4_hba_process_cq(struct work_struct *work)
14699 {
14700         struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
14701
14702         __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
14703 }
14704
14705 /**
14706  * lpfc_sli4_hba_process_cq - fast-path work handler when started by timer
14707  * @work: pointer to work element
14708  *
14709  * translates from the work handler and calls the fast-path handler.
14710  **/
14711 static void
14712 lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
14713 {
14714         struct lpfc_queue *cq = container_of(to_delayed_work(work),
14715                                         struct lpfc_queue, sched_irqwork);
14716
14717         __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
14718 }
14719
14720 /**
14721  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
14722  * @irq: Interrupt number.
14723  * @dev_id: The device context pointer.
14724  *
14725  * This function is directly called from the PCI layer as an interrupt
14726  * service routine when device with SLI-4 interface spec is enabled with
14727  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14728  * ring event in the HBA. However, when the device is enabled with either
14729  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14730  * device-level interrupt handler. When the PCI slot is in error recovery
14731  * or the HBA is undergoing initialization, the interrupt handler will not
14732  * process the interrupt. The SCSI FCP fast-path ring event are handled in
14733  * the intrrupt context. This function is called without any lock held.
14734  * It gets the hbalock to access and update SLI data structures. Note that,
14735  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14736  * equal to that of FCP CQ index.
14737  *
14738  * The link attention and ELS ring attention events are handled
14739  * by the worker thread. The interrupt handler signals the worker thread
14740  * and returns for these events. This function is called without any lock
14741  * held. It gets the hbalock to access and update SLI data structures.
14742  *
14743  * This function returns IRQ_HANDLED when interrupt is handled else it
14744  * returns IRQ_NONE.
14745  **/
14746 irqreturn_t
14747 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
14748 {
14749         struct lpfc_hba *phba;
14750         struct lpfc_hba_eq_hdl *hba_eq_hdl;
14751         struct lpfc_queue *fpeq;
14752         unsigned long iflag;
14753         int ecount = 0;
14754         int hba_eqidx;
14755         struct lpfc_eq_intr_info *eqi;
14756
14757         /* Get the driver's phba structure from the dev_id */
14758         hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14759         phba = hba_eq_hdl->phba;
14760         hba_eqidx = hba_eq_hdl->idx;
14761
14762         if (unlikely(!phba))
14763                 return IRQ_NONE;
14764         if (unlikely(!phba->sli4_hba.hdwq))
14765                 return IRQ_NONE;
14766
14767         /* Get to the EQ struct associated with this vector */
14768         fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
14769         if (unlikely(!fpeq))
14770                 return IRQ_NONE;
14771
14772         /* Check device state for handling interrupt */
14773         if (unlikely(lpfc_intr_state_check(phba))) {
14774                 /* Check again for link_state with lock held */
14775                 spin_lock_irqsave(&phba->hbalock, iflag);
14776                 if (phba->link_state < LPFC_LINK_DOWN)
14777                         /* Flush, clear interrupt, and rearm the EQ */
14778                         lpfc_sli4_eqcq_flush(phba, fpeq);
14779                 spin_unlock_irqrestore(&phba->hbalock, iflag);
14780                 return IRQ_NONE;
14781         }
14782
14783         eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
14784         eqi->icnt++;
14785
14786         fpeq->last_cpu = raw_smp_processor_id();
14787
14788         if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
14789             fpeq->q_flag & HBA_EQ_DELAY_CHK &&
14790             phba->cfg_auto_imax &&
14791             fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
14792             phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
14793                 lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
14794
14795         /* process and rearm the EQ */
14796         ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
14797
14798         if (unlikely(ecount == 0)) {
14799                 fpeq->EQ_no_entry++;
14800                 if (phba->intr_type == MSIX)
14801                         /* MSI-X treated interrupt served as no EQ share INT */
14802                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14803                                         "0358 MSI-X interrupt with no EQE\n");
14804                 else
14805                         /* Non MSI-X treated on interrupt as EQ share INT */
14806                         return IRQ_NONE;
14807         }
14808
14809         return IRQ_HANDLED;
14810 } /* lpfc_sli4_fp_intr_handler */
14811
14812 /**
14813  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14814  * @irq: Interrupt number.
14815  * @dev_id: The device context pointer.
14816  *
14817  * This function is the device-level interrupt handler to device with SLI-4
14818  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14819  * interrupt mode is enabled and there is an event in the HBA which requires
14820  * driver attention. This function invokes the slow-path interrupt attention
14821  * handling function and fast-path interrupt attention handling function in
14822  * turn to process the relevant HBA attention events. This function is called
14823  * without any lock held. It gets the hbalock to access and update SLI data
14824  * structures.
14825  *
14826  * This function returns IRQ_HANDLED when interrupt is handled, else it
14827  * returns IRQ_NONE.
14828  **/
14829 irqreturn_t
14830 lpfc_sli4_intr_handler(int irq, void *dev_id)
14831 {
14832         struct lpfc_hba  *phba;
14833         irqreturn_t hba_irq_rc;
14834         bool hba_handled = false;
14835         int qidx;
14836
14837         /* Get the driver's phba structure from the dev_id */
14838         phba = (struct lpfc_hba *)dev_id;
14839
14840         if (unlikely(!phba))
14841                 return IRQ_NONE;
14842
14843         /*
14844          * Invoke fast-path host attention interrupt handling as appropriate.
14845          */
14846         for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
14847                 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
14848                                         &phba->sli4_hba.hba_eq_hdl[qidx]);
14849                 if (hba_irq_rc == IRQ_HANDLED)
14850                         hba_handled |= true;
14851         }
14852
14853         return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
14854 } /* lpfc_sli4_intr_handler */
14855
14856 void lpfc_sli4_poll_hbtimer(struct timer_list *t)
14857 {
14858         struct lpfc_hba *phba = from_timer(phba, t, cpuhp_poll_timer);
14859         struct lpfc_queue *eq;
14860         int i = 0;
14861
14862         rcu_read_lock();
14863
14864         list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
14865                 i += lpfc_sli4_poll_eq(eq, LPFC_POLL_SLOWPATH);
14866         if (!list_empty(&phba->poll_list))
14867                 mod_timer(&phba->cpuhp_poll_timer,
14868                           jiffies + msecs_to_jiffies(LPFC_POLL_HB));
14869
14870         rcu_read_unlock();
14871 }
14872
14873 inline int lpfc_sli4_poll_eq(struct lpfc_queue *eq, uint8_t path)
14874 {
14875         struct lpfc_hba *phba = eq->phba;
14876         int i = 0;
14877
14878         /*
14879          * Unlocking an irq is one of the entry point to check
14880          * for re-schedule, but we are good for io submission
14881          * path as midlayer does a get_cpu to glue us in. Flush
14882          * out the invalidate queue so we can see the updated
14883          * value for flag.
14884          */
14885         smp_rmb();
14886
14887         if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
14888                 /* We will not likely get the completion for the caller
14889                  * during this iteration but i guess that's fine.
14890                  * Future io's coming on this eq should be able to
14891                  * pick it up.  As for the case of single io's, they
14892                  * will be handled through a sched from polling timer
14893                  * function which is currently triggered every 1msec.
14894                  */
14895                 i = lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM);
14896
14897         return i;
14898 }
14899
14900 static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
14901 {
14902         struct lpfc_hba *phba = eq->phba;
14903
14904         /* kickstart slowpath processing if needed */
14905         if (list_empty(&phba->poll_list))
14906                 mod_timer(&phba->cpuhp_poll_timer,
14907                           jiffies + msecs_to_jiffies(LPFC_POLL_HB));
14908
14909         list_add_rcu(&eq->_poll_list, &phba->poll_list);
14910         synchronize_rcu();
14911 }
14912
14913 static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
14914 {
14915         struct lpfc_hba *phba = eq->phba;
14916
14917         /* Disable slowpath processing for this eq.  Kick start the eq
14918          * by RE-ARMING the eq's ASAP
14919          */
14920         list_del_rcu(&eq->_poll_list);
14921         synchronize_rcu();
14922
14923         if (list_empty(&phba->poll_list))
14924                 del_timer_sync(&phba->cpuhp_poll_timer);
14925 }
14926
14927 void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
14928 {
14929         struct lpfc_queue *eq, *next;
14930
14931         list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
14932                 list_del(&eq->_poll_list);
14933
14934         INIT_LIST_HEAD(&phba->poll_list);
14935         synchronize_rcu();
14936 }
14937
14938 static inline void
14939 __lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
14940 {
14941         if (mode == eq->mode)
14942                 return;
14943         /*
14944          * currently this function is only called during a hotplug
14945          * event and the cpu on which this function is executing
14946          * is going offline.  By now the hotplug has instructed
14947          * the scheduler to remove this cpu from cpu active mask.
14948          * So we don't need to work about being put aside by the
14949          * scheduler for a high priority process.  Yes, the inte-
14950          * rrupts could come but they are known to retire ASAP.
14951          */
14952
14953         /* Disable polling in the fastpath */
14954         WRITE_ONCE(eq->mode, mode);
14955         /* flush out the store buffer */
14956         smp_wmb();
14957
14958         /*
14959          * Add this eq to the polling list and start polling. For
14960          * a grace period both interrupt handler and poller will
14961          * try to process the eq _but_ that's fine.  We have a
14962          * synchronization mechanism in place (queue_claimed) to
14963          * deal with it.  This is just a draining phase for int-
14964          * errupt handler (not eq's) as we have guranteed through
14965          * barrier that all the CPUs have seen the new CQ_POLLED
14966          * state. which will effectively disable the REARMING of
14967          * the EQ.  The whole idea is eq's die off eventually as
14968          * we are not rearming EQ's anymore.
14969          */
14970         mode ? lpfc_sli4_add_to_poll_list(eq) :
14971                lpfc_sli4_remove_from_poll_list(eq);
14972 }
14973
14974 void lpfc_sli4_start_polling(struct lpfc_queue *eq)
14975 {
14976         __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
14977 }
14978
14979 void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
14980 {
14981         struct lpfc_hba *phba = eq->phba;
14982
14983         __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
14984
14985         /* Kick start for the pending io's in h/w.
14986          * Once we switch back to interrupt processing on a eq
14987          * the io path completion will only arm eq's when it
14988          * receives a completion.  But since eq's are in disa-
14989          * rmed state it doesn't receive a completion.  This
14990          * creates a deadlock scenaro.
14991          */
14992         phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
14993 }
14994
14995 /**
14996  * lpfc_sli4_queue_free - free a queue structure and associated memory
14997  * @queue: The queue structure to free.
14998  *
14999  * This function frees a queue structure and the DMAable memory used for
15000  * the host resident queue. This function must be called after destroying the
15001  * queue on the HBA.
15002  **/
15003 void
15004 lpfc_sli4_queue_free(struct lpfc_queue *queue)
15005 {
15006         struct lpfc_dmabuf *dmabuf;
15007
15008         if (!queue)
15009                 return;
15010
15011         if (!list_empty(&queue->wq_list))
15012                 list_del(&queue->wq_list);
15013
15014         while (!list_empty(&queue->page_list)) {
15015                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
15016                                  list);
15017                 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
15018                                   dmabuf->virt, dmabuf->phys);
15019                 kfree(dmabuf);
15020         }
15021         if (queue->rqbp) {
15022                 lpfc_free_rq_buffer(queue->phba, queue);
15023                 kfree(queue->rqbp);
15024         }
15025
15026         if (!list_empty(&queue->cpu_list))
15027                 list_del(&queue->cpu_list);
15028
15029         kfree(queue);
15030         return;
15031 }
15032
15033 /**
15034  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
15035  * @phba: The HBA that this queue is being created on.
15036  * @page_size: The size of a queue page
15037  * @entry_size: The size of each queue entry for this queue.
15038  * @entry_count: The number of entries that this queue will handle.
15039  * @cpu: The cpu that will primarily utilize this queue.
15040  *
15041  * This function allocates a queue structure and the DMAable memory used for
15042  * the host resident queue. This function must be called before creating the
15043  * queue on the HBA.
15044  **/
15045 struct lpfc_queue *
15046 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
15047                       uint32_t entry_size, uint32_t entry_count, int cpu)
15048 {
15049         struct lpfc_queue *queue;
15050         struct lpfc_dmabuf *dmabuf;
15051         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15052         uint16_t x, pgcnt;
15053
15054         if (!phba->sli4_hba.pc_sli4_params.supported)
15055                 hw_page_size = page_size;
15056
15057         pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
15058
15059         /* If needed, Adjust page count to match the max the adapter supports */
15060         if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
15061                 pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
15062
15063         queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
15064                              GFP_KERNEL, cpu_to_node(cpu));
15065         if (!queue)
15066                 return NULL;
15067
15068         INIT_LIST_HEAD(&queue->list);
15069         INIT_LIST_HEAD(&queue->_poll_list);
15070         INIT_LIST_HEAD(&queue->wq_list);
15071         INIT_LIST_HEAD(&queue->wqfull_list);
15072         INIT_LIST_HEAD(&queue->page_list);
15073         INIT_LIST_HEAD(&queue->child_list);
15074         INIT_LIST_HEAD(&queue->cpu_list);
15075
15076         /* Set queue parameters now.  If the system cannot provide memory
15077          * resources, the free routine needs to know what was allocated.
15078          */
15079         queue->page_count = pgcnt;
15080         queue->q_pgs = (void **)&queue[1];
15081         queue->entry_cnt_per_pg = hw_page_size / entry_size;
15082         queue->entry_size = entry_size;
15083         queue->entry_count = entry_count;
15084         queue->page_size = hw_page_size;
15085         queue->phba = phba;
15086
15087         for (x = 0; x < queue->page_count; x++) {
15088                 dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
15089                                       dev_to_node(&phba->pcidev->dev));
15090                 if (!dmabuf)
15091                         goto out_fail;
15092                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
15093                                                   hw_page_size, &dmabuf->phys,
15094                                                   GFP_KERNEL);
15095                 if (!dmabuf->virt) {
15096                         kfree(dmabuf);
15097                         goto out_fail;
15098                 }
15099                 dmabuf->buffer_tag = x;
15100                 list_add_tail(&dmabuf->list, &queue->page_list);
15101                 /* use lpfc_sli4_qe to index a paritcular entry in this page */
15102                 queue->q_pgs[x] = dmabuf->virt;
15103         }
15104         INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
15105         INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
15106         INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
15107         INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
15108
15109         /* notify_interval will be set during q creation */
15110
15111         return queue;
15112 out_fail:
15113         lpfc_sli4_queue_free(queue);
15114         return NULL;
15115 }
15116
15117 /**
15118  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
15119  * @phba: HBA structure that indicates port to create a queue on.
15120  * @pci_barset: PCI BAR set flag.
15121  *
15122  * This function shall perform iomap of the specified PCI BAR address to host
15123  * memory address if not already done so and return it. The returned host
15124  * memory address can be NULL.
15125  */
15126 static void __iomem *
15127 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
15128 {
15129         if (!phba->pcidev)
15130                 return NULL;
15131
15132         switch (pci_barset) {
15133         case WQ_PCI_BAR_0_AND_1:
15134                 return phba->pci_bar0_memmap_p;
15135         case WQ_PCI_BAR_2_AND_3:
15136                 return phba->pci_bar2_memmap_p;
15137         case WQ_PCI_BAR_4_AND_5:
15138                 return phba->pci_bar4_memmap_p;
15139         default:
15140                 break;
15141         }
15142         return NULL;
15143 }
15144
15145 /**
15146  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
15147  * @phba: HBA structure that EQs are on.
15148  * @startq: The starting EQ index to modify
15149  * @numq: The number of EQs (consecutive indexes) to modify
15150  * @usdelay: amount of delay
15151  *
15152  * This function revises the EQ delay on 1 or more EQs. The EQ delay
15153  * is set either by writing to a register (if supported by the SLI Port)
15154  * or by mailbox command. The mailbox command allows several EQs to be
15155  * updated at once.
15156  *
15157  * The @phba struct is used to send a mailbox command to HBA. The @startq
15158  * is used to get the starting EQ index to change. The @numq value is
15159  * used to specify how many consecutive EQ indexes, starting at EQ index,
15160  * are to be changed. This function is asynchronous and will wait for any
15161  * mailbox commands to finish before returning.
15162  *
15163  * On success this function will return a zero. If unable to allocate
15164  * enough memory this function will return -ENOMEM. If a mailbox command
15165  * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
15166  * have had their delay multipler changed.
15167  **/
15168 void
15169 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
15170                          uint32_t numq, uint32_t usdelay)
15171 {
15172         struct lpfc_mbx_modify_eq_delay *eq_delay;
15173         LPFC_MBOXQ_t *mbox;
15174         struct lpfc_queue *eq;
15175         int cnt = 0, rc, length;
15176         uint32_t shdr_status, shdr_add_status;
15177         uint32_t dmult;
15178         int qidx;
15179         union lpfc_sli4_cfg_shdr *shdr;
15180
15181         if (startq >= phba->cfg_irq_chann)
15182                 return;
15183
15184         if (usdelay > 0xFFFF) {
15185                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
15186                                 "6429 usdelay %d too large. Scaled down to "
15187                                 "0xFFFF.\n", usdelay);
15188                 usdelay = 0xFFFF;
15189         }
15190
15191         /* set values by EQ_DELAY register if supported */
15192         if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
15193                 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
15194                         eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
15195                         if (!eq)
15196                                 continue;
15197
15198                         lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
15199
15200                         if (++cnt >= numq)
15201                                 break;
15202                 }
15203                 return;
15204         }
15205
15206         /* Otherwise, set values by mailbox cmd */
15207
15208         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15209         if (!mbox) {
15210                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15211                                 "6428 Failed allocating mailbox cmd buffer."
15212                                 " EQ delay was not set.\n");
15213                 return;
15214         }
15215         length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
15216                   sizeof(struct lpfc_sli4_cfg_mhdr));
15217         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15218                          LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
15219                          length, LPFC_SLI4_MBX_EMBED);
15220         eq_delay = &mbox->u.mqe.un.eq_delay;
15221
15222         /* Calculate delay multiper from maximum interrupt per second */
15223         dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
15224         if (dmult)
15225                 dmult--;
15226         if (dmult > LPFC_DMULT_MAX)
15227                 dmult = LPFC_DMULT_MAX;
15228
15229         for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
15230                 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
15231                 if (!eq)
15232                         continue;
15233                 eq->q_mode = usdelay;
15234                 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
15235                 eq_delay->u.request.eq[cnt].phase = 0;
15236                 eq_delay->u.request.eq[cnt].delay_multi = dmult;
15237
15238                 if (++cnt >= numq)
15239                         break;
15240         }
15241         eq_delay->u.request.num_eq = cnt;
15242
15243         mbox->vport = phba->pport;
15244         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15245         mbox->ctx_buf = NULL;
15246         mbox->ctx_ndlp = NULL;
15247         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15248         shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
15249         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15250         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15251         if (shdr_status || shdr_add_status || rc) {
15252                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15253                                 "2512 MODIFY_EQ_DELAY mailbox failed with "
15254                                 "status x%x add_status x%x, mbx status x%x\n",
15255                                 shdr_status, shdr_add_status, rc);
15256         }
15257         mempool_free(mbox, phba->mbox_mem_pool);
15258         return;
15259 }
15260
15261 /**
15262  * lpfc_eq_create - Create an Event Queue on the HBA
15263  * @phba: HBA structure that indicates port to create a queue on.
15264  * @eq: The queue structure to use to create the event queue.
15265  * @imax: The maximum interrupt per second limit.
15266  *
15267  * This function creates an event queue, as detailed in @eq, on a port,
15268  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
15269  *
15270  * The @phba struct is used to send mailbox command to HBA. The @eq struct
15271  * is used to get the entry count and entry size that are necessary to
15272  * determine the number of pages to allocate and use for this queue. This
15273  * function will send the EQ_CREATE mailbox command to the HBA to setup the
15274  * event queue. This function is asynchronous and will wait for the mailbox
15275  * command to finish before continuing.
15276  *
15277  * On success this function will return a zero. If unable to allocate enough
15278  * memory this function will return -ENOMEM. If the queue create mailbox command
15279  * fails this function will return -ENXIO.
15280  **/
15281 int
15282 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
15283 {
15284         struct lpfc_mbx_eq_create *eq_create;
15285         LPFC_MBOXQ_t *mbox;
15286         int rc, length, status = 0;
15287         struct lpfc_dmabuf *dmabuf;
15288         uint32_t shdr_status, shdr_add_status;
15289         union lpfc_sli4_cfg_shdr *shdr;
15290         uint16_t dmult;
15291         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15292
15293         /* sanity check on queue memory */
15294         if (!eq)
15295                 return -ENODEV;
15296         if (!phba->sli4_hba.pc_sli4_params.supported)
15297                 hw_page_size = SLI4_PAGE_SIZE;
15298
15299         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15300         if (!mbox)
15301                 return -ENOMEM;
15302         length = (sizeof(struct lpfc_mbx_eq_create) -
15303                   sizeof(struct lpfc_sli4_cfg_mhdr));
15304         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15305                          LPFC_MBOX_OPCODE_EQ_CREATE,
15306                          length, LPFC_SLI4_MBX_EMBED);
15307         eq_create = &mbox->u.mqe.un.eq_create;
15308         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
15309         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
15310                eq->page_count);
15311         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
15312                LPFC_EQE_SIZE);
15313         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
15314
15315         /* Use version 2 of CREATE_EQ if eqav is set */
15316         if (phba->sli4_hba.pc_sli4_params.eqav) {
15317                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15318                        LPFC_Q_CREATE_VERSION_2);
15319                 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
15320                        phba->sli4_hba.pc_sli4_params.eqav);
15321         }
15322
15323         /* don't setup delay multiplier using EQ_CREATE */
15324         dmult = 0;
15325         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
15326                dmult);
15327         switch (eq->entry_count) {
15328         default:
15329                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15330                                 "0360 Unsupported EQ count. (%d)\n",
15331                                 eq->entry_count);
15332                 if (eq->entry_count < 256) {
15333                         status = -EINVAL;
15334                         goto out;
15335                 }
15336                 fallthrough;    /* otherwise default to smallest count */
15337         case 256:
15338                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15339                        LPFC_EQ_CNT_256);
15340                 break;
15341         case 512:
15342                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15343                        LPFC_EQ_CNT_512);
15344                 break;
15345         case 1024:
15346                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15347                        LPFC_EQ_CNT_1024);
15348                 break;
15349         case 2048:
15350                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15351                        LPFC_EQ_CNT_2048);
15352                 break;
15353         case 4096:
15354                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15355                        LPFC_EQ_CNT_4096);
15356                 break;
15357         }
15358         list_for_each_entry(dmabuf, &eq->page_list, list) {
15359                 memset(dmabuf->virt, 0, hw_page_size);
15360                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15361                                         putPaddrLow(dmabuf->phys);
15362                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15363                                         putPaddrHigh(dmabuf->phys);
15364         }
15365         mbox->vport = phba->pport;
15366         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15367         mbox->ctx_buf = NULL;
15368         mbox->ctx_ndlp = NULL;
15369         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15370         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15371         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15372         if (shdr_status || shdr_add_status || rc) {
15373                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15374                                 "2500 EQ_CREATE mailbox failed with "
15375                                 "status x%x add_status x%x, mbx status x%x\n",
15376                                 shdr_status, shdr_add_status, rc);
15377                 status = -ENXIO;
15378         }
15379         eq->type = LPFC_EQ;
15380         eq->subtype = LPFC_NONE;
15381         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
15382         if (eq->queue_id == 0xFFFF)
15383                 status = -ENXIO;
15384         eq->host_index = 0;
15385         eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
15386         eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
15387 out:
15388         mempool_free(mbox, phba->mbox_mem_pool);
15389         return status;
15390 }
15391
15392 static int lpfc_cq_poll_hdler(struct irq_poll *iop, int budget)
15393 {
15394         struct lpfc_queue *cq = container_of(iop, struct lpfc_queue, iop);
15395
15396         __lpfc_sli4_hba_process_cq(cq, LPFC_IRQ_POLL);
15397
15398         return 1;
15399 }
15400
15401 /**
15402  * lpfc_cq_create - Create a Completion Queue on the HBA
15403  * @phba: HBA structure that indicates port to create a queue on.
15404  * @cq: The queue structure to use to create the completion queue.
15405  * @eq: The event queue to bind this completion queue to.
15406  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
15407  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
15408  *
15409  * This function creates a completion queue, as detailed in @wq, on a port,
15410  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
15411  *
15412  * The @phba struct is used to send mailbox command to HBA. The @cq struct
15413  * is used to get the entry count and entry size that are necessary to
15414  * determine the number of pages to allocate and use for this queue. The @eq
15415  * is used to indicate which event queue to bind this completion queue to. This
15416  * function will send the CQ_CREATE mailbox command to the HBA to setup the
15417  * completion queue. This function is asynchronous and will wait for the mailbox
15418  * command to finish before continuing.
15419  *
15420  * On success this function will return a zero. If unable to allocate enough
15421  * memory this function will return -ENOMEM. If the queue create mailbox command
15422  * fails this function will return -ENXIO.
15423  **/
15424 int
15425 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
15426                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
15427 {
15428         struct lpfc_mbx_cq_create *cq_create;
15429         struct lpfc_dmabuf *dmabuf;
15430         LPFC_MBOXQ_t *mbox;
15431         int rc, length, status = 0;
15432         uint32_t shdr_status, shdr_add_status;
15433         union lpfc_sli4_cfg_shdr *shdr;
15434
15435         /* sanity check on queue memory */
15436         if (!cq || !eq)
15437                 return -ENODEV;
15438
15439         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15440         if (!mbox)
15441                 return -ENOMEM;
15442         length = (sizeof(struct lpfc_mbx_cq_create) -
15443                   sizeof(struct lpfc_sli4_cfg_mhdr));
15444         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15445                          LPFC_MBOX_OPCODE_CQ_CREATE,
15446                          length, LPFC_SLI4_MBX_EMBED);
15447         cq_create = &mbox->u.mqe.un.cq_create;
15448         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
15449         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
15450                     cq->page_count);
15451         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
15452         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
15453         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15454                phba->sli4_hba.pc_sli4_params.cqv);
15455         if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
15456                 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
15457                        (cq->page_size / SLI4_PAGE_SIZE));
15458                 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
15459                        eq->queue_id);
15460                 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
15461                        phba->sli4_hba.pc_sli4_params.cqav);
15462         } else {
15463                 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
15464                        eq->queue_id);
15465         }
15466         switch (cq->entry_count) {
15467         case 2048:
15468         case 4096:
15469                 if (phba->sli4_hba.pc_sli4_params.cqv ==
15470                     LPFC_Q_CREATE_VERSION_2) {
15471                         cq_create->u.request.context.lpfc_cq_context_count =
15472                                 cq->entry_count;
15473                         bf_set(lpfc_cq_context_count,
15474                                &cq_create->u.request.context,
15475                                LPFC_CQ_CNT_WORD7);
15476                         break;
15477                 }
15478                 fallthrough;
15479         default:
15480                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15481                                 "0361 Unsupported CQ count: "
15482                                 "entry cnt %d sz %d pg cnt %d\n",
15483                                 cq->entry_count, cq->entry_size,
15484                                 cq->page_count);
15485                 if (cq->entry_count < 256) {
15486                         status = -EINVAL;
15487                         goto out;
15488                 }
15489                 fallthrough;    /* otherwise default to smallest count */
15490         case 256:
15491                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15492                        LPFC_CQ_CNT_256);
15493                 break;
15494         case 512:
15495                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15496                        LPFC_CQ_CNT_512);
15497                 break;
15498         case 1024:
15499                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15500                        LPFC_CQ_CNT_1024);
15501                 break;
15502         }
15503         list_for_each_entry(dmabuf, &cq->page_list, list) {
15504                 memset(dmabuf->virt, 0, cq->page_size);
15505                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15506                                         putPaddrLow(dmabuf->phys);
15507                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15508                                         putPaddrHigh(dmabuf->phys);
15509         }
15510         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15511
15512         /* The IOCTL status is embedded in the mailbox subheader. */
15513         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15514         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15515         if (shdr_status || shdr_add_status || rc) {
15516                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15517                                 "2501 CQ_CREATE mailbox failed with "
15518                                 "status x%x add_status x%x, mbx status x%x\n",
15519                                 shdr_status, shdr_add_status, rc);
15520                 status = -ENXIO;
15521                 goto out;
15522         }
15523         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
15524         if (cq->queue_id == 0xFFFF) {
15525                 status = -ENXIO;
15526                 goto out;
15527         }
15528         /* link the cq onto the parent eq child list */
15529         list_add_tail(&cq->list, &eq->child_list);
15530         /* Set up completion queue's type and subtype */
15531         cq->type = type;
15532         cq->subtype = subtype;
15533         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
15534         cq->assoc_qid = eq->queue_id;
15535         cq->assoc_qp = eq;
15536         cq->host_index = 0;
15537         cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15538         cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
15539
15540         if (cq->queue_id > phba->sli4_hba.cq_max)
15541                 phba->sli4_hba.cq_max = cq->queue_id;
15542
15543         irq_poll_init(&cq->iop, LPFC_IRQ_POLL_WEIGHT, lpfc_cq_poll_hdler);
15544 out:
15545         mempool_free(mbox, phba->mbox_mem_pool);
15546         return status;
15547 }
15548
15549 /**
15550  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
15551  * @phba: HBA structure that indicates port to create a queue on.
15552  * @cqp: The queue structure array to use to create the completion queues.
15553  * @hdwq: The hardware queue array  with the EQ to bind completion queues to.
15554  * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
15555  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
15556  *
15557  * This function creates a set of  completion queue, s to support MRQ
15558  * as detailed in @cqp, on a port,
15559  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
15560  *
15561  * The @phba struct is used to send mailbox command to HBA. The @cq struct
15562  * is used to get the entry count and entry size that are necessary to
15563  * determine the number of pages to allocate and use for this queue. The @eq
15564  * is used to indicate which event queue to bind this completion queue to. This
15565  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
15566  * completion queue. This function is asynchronous and will wait for the mailbox
15567  * command to finish before continuing.
15568  *
15569  * On success this function will return a zero. If unable to allocate enough
15570  * memory this function will return -ENOMEM. If the queue create mailbox command
15571  * fails this function will return -ENXIO.
15572  **/
15573 int
15574 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
15575                    struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
15576                    uint32_t subtype)
15577 {
15578         struct lpfc_queue *cq;
15579         struct lpfc_queue *eq;
15580         struct lpfc_mbx_cq_create_set *cq_set;
15581         struct lpfc_dmabuf *dmabuf;
15582         LPFC_MBOXQ_t *mbox;
15583         int rc, length, alloclen, status = 0;
15584         int cnt, idx, numcq, page_idx = 0;
15585         uint32_t shdr_status, shdr_add_status;
15586         union lpfc_sli4_cfg_shdr *shdr;
15587         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15588
15589         /* sanity check on queue memory */
15590         numcq = phba->cfg_nvmet_mrq;
15591         if (!cqp || !hdwq || !numcq)
15592                 return -ENODEV;
15593
15594         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15595         if (!mbox)
15596                 return -ENOMEM;
15597
15598         length = sizeof(struct lpfc_mbx_cq_create_set);
15599         length += ((numcq * cqp[0]->page_count) *
15600                    sizeof(struct dma_address));
15601         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15602                         LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
15603                         LPFC_SLI4_MBX_NEMBED);
15604         if (alloclen < length) {
15605                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15606                                 "3098 Allocated DMA memory size (%d) is "
15607                                 "less than the requested DMA memory size "
15608                                 "(%d)\n", alloclen, length);
15609                 status = -ENOMEM;
15610                 goto out;
15611         }
15612         cq_set = mbox->sge_array->addr[0];
15613         shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
15614         bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
15615
15616         for (idx = 0; idx < numcq; idx++) {
15617                 cq = cqp[idx];
15618                 eq = hdwq[idx].hba_eq;
15619                 if (!cq || !eq) {
15620                         status = -ENOMEM;
15621                         goto out;
15622                 }
15623                 if (!phba->sli4_hba.pc_sli4_params.supported)
15624                         hw_page_size = cq->page_size;
15625
15626                 switch (idx) {
15627                 case 0:
15628                         bf_set(lpfc_mbx_cq_create_set_page_size,
15629                                &cq_set->u.request,
15630                                (hw_page_size / SLI4_PAGE_SIZE));
15631                         bf_set(lpfc_mbx_cq_create_set_num_pages,
15632                                &cq_set->u.request, cq->page_count);
15633                         bf_set(lpfc_mbx_cq_create_set_evt,
15634                                &cq_set->u.request, 1);
15635                         bf_set(lpfc_mbx_cq_create_set_valid,
15636                                &cq_set->u.request, 1);
15637                         bf_set(lpfc_mbx_cq_create_set_cqe_size,
15638                                &cq_set->u.request, 0);
15639                         bf_set(lpfc_mbx_cq_create_set_num_cq,
15640                                &cq_set->u.request, numcq);
15641                         bf_set(lpfc_mbx_cq_create_set_autovalid,
15642                                &cq_set->u.request,
15643                                phba->sli4_hba.pc_sli4_params.cqav);
15644                         switch (cq->entry_count) {
15645                         case 2048:
15646                         case 4096:
15647                                 if (phba->sli4_hba.pc_sli4_params.cqv ==
15648                                     LPFC_Q_CREATE_VERSION_2) {
15649                                         bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15650                                                &cq_set->u.request,
15651                                                 cq->entry_count);
15652                                         bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15653                                                &cq_set->u.request,
15654                                                LPFC_CQ_CNT_WORD7);
15655                                         break;
15656                                 }
15657                                 fallthrough;
15658                         default:
15659                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15660                                                 "3118 Bad CQ count. (%d)\n",
15661                                                 cq->entry_count);
15662                                 if (cq->entry_count < 256) {
15663                                         status = -EINVAL;
15664                                         goto out;
15665                                 }
15666                                 fallthrough;    /* otherwise default to smallest */
15667                         case 256:
15668                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15669                                        &cq_set->u.request, LPFC_CQ_CNT_256);
15670                                 break;
15671                         case 512:
15672                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15673                                        &cq_set->u.request, LPFC_CQ_CNT_512);
15674                                 break;
15675                         case 1024:
15676                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15677                                        &cq_set->u.request, LPFC_CQ_CNT_1024);
15678                                 break;
15679                         }
15680                         bf_set(lpfc_mbx_cq_create_set_eq_id0,
15681                                &cq_set->u.request, eq->queue_id);
15682                         break;
15683                 case 1:
15684                         bf_set(lpfc_mbx_cq_create_set_eq_id1,
15685                                &cq_set->u.request, eq->queue_id);
15686                         break;
15687                 case 2:
15688                         bf_set(lpfc_mbx_cq_create_set_eq_id2,
15689                                &cq_set->u.request, eq->queue_id);
15690                         break;
15691                 case 3:
15692                         bf_set(lpfc_mbx_cq_create_set_eq_id3,
15693                                &cq_set->u.request, eq->queue_id);
15694                         break;
15695                 case 4:
15696                         bf_set(lpfc_mbx_cq_create_set_eq_id4,
15697                                &cq_set->u.request, eq->queue_id);
15698                         break;
15699                 case 5:
15700                         bf_set(lpfc_mbx_cq_create_set_eq_id5,
15701                                &cq_set->u.request, eq->queue_id);
15702                         break;
15703                 case 6:
15704                         bf_set(lpfc_mbx_cq_create_set_eq_id6,
15705                                &cq_set->u.request, eq->queue_id);
15706                         break;
15707                 case 7:
15708                         bf_set(lpfc_mbx_cq_create_set_eq_id7,
15709                                &cq_set->u.request, eq->queue_id);
15710                         break;
15711                 case 8:
15712                         bf_set(lpfc_mbx_cq_create_set_eq_id8,
15713                                &cq_set->u.request, eq->queue_id);
15714                         break;
15715                 case 9:
15716                         bf_set(lpfc_mbx_cq_create_set_eq_id9,
15717                                &cq_set->u.request, eq->queue_id);
15718                         break;
15719                 case 10:
15720                         bf_set(lpfc_mbx_cq_create_set_eq_id10,
15721                                &cq_set->u.request, eq->queue_id);
15722                         break;
15723                 case 11:
15724                         bf_set(lpfc_mbx_cq_create_set_eq_id11,
15725                                &cq_set->u.request, eq->queue_id);
15726                         break;
15727                 case 12:
15728                         bf_set(lpfc_mbx_cq_create_set_eq_id12,
15729                                &cq_set->u.request, eq->queue_id);
15730                         break;
15731                 case 13:
15732                         bf_set(lpfc_mbx_cq_create_set_eq_id13,
15733                                &cq_set->u.request, eq->queue_id);
15734                         break;
15735                 case 14:
15736                         bf_set(lpfc_mbx_cq_create_set_eq_id14,
15737                                &cq_set->u.request, eq->queue_id);
15738                         break;
15739                 case 15:
15740                         bf_set(lpfc_mbx_cq_create_set_eq_id15,
15741                                &cq_set->u.request, eq->queue_id);
15742                         break;
15743                 }
15744
15745                 /* link the cq onto the parent eq child list */
15746                 list_add_tail(&cq->list, &eq->child_list);
15747                 /* Set up completion queue's type and subtype */
15748                 cq->type = type;
15749                 cq->subtype = subtype;
15750                 cq->assoc_qid = eq->queue_id;
15751                 cq->assoc_qp = eq;
15752                 cq->host_index = 0;
15753                 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15754                 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
15755                                          cq->entry_count);
15756                 cq->chann = idx;
15757
15758                 rc = 0;
15759                 list_for_each_entry(dmabuf, &cq->page_list, list) {
15760                         memset(dmabuf->virt, 0, hw_page_size);
15761                         cnt = page_idx + dmabuf->buffer_tag;
15762                         cq_set->u.request.page[cnt].addr_lo =
15763                                         putPaddrLow(dmabuf->phys);
15764                         cq_set->u.request.page[cnt].addr_hi =
15765                                         putPaddrHigh(dmabuf->phys);
15766                         rc++;
15767                 }
15768                 page_idx += rc;
15769         }
15770
15771         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15772
15773         /* The IOCTL status is embedded in the mailbox subheader. */
15774         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15775         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15776         if (shdr_status || shdr_add_status || rc) {
15777                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15778                                 "3119 CQ_CREATE_SET mailbox failed with "
15779                                 "status x%x add_status x%x, mbx status x%x\n",
15780                                 shdr_status, shdr_add_status, rc);
15781                 status = -ENXIO;
15782                 goto out;
15783         }
15784         rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15785         if (rc == 0xFFFF) {
15786                 status = -ENXIO;
15787                 goto out;
15788         }
15789
15790         for (idx = 0; idx < numcq; idx++) {
15791                 cq = cqp[idx];
15792                 cq->queue_id = rc + idx;
15793                 if (cq->queue_id > phba->sli4_hba.cq_max)
15794                         phba->sli4_hba.cq_max = cq->queue_id;
15795         }
15796
15797 out:
15798         lpfc_sli4_mbox_cmd_free(phba, mbox);
15799         return status;
15800 }
15801
15802 /**
15803  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
15804  * @phba: HBA structure that indicates port to create a queue on.
15805  * @mq: The queue structure to use to create the mailbox queue.
15806  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15807  * @cq: The completion queue to associate with this cq.
15808  *
15809  * This function provides failback (fb) functionality when the
15810  * mq_create_ext fails on older FW generations.  It's purpose is identical
15811  * to mq_create_ext otherwise.
15812  *
15813  * This routine cannot fail as all attributes were previously accessed and
15814  * initialized in mq_create_ext.
15815  **/
15816 static void
15817 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15818                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
15819 {
15820         struct lpfc_mbx_mq_create *mq_create;
15821         struct lpfc_dmabuf *dmabuf;
15822         int length;
15823
15824         length = (sizeof(struct lpfc_mbx_mq_create) -
15825                   sizeof(struct lpfc_sli4_cfg_mhdr));
15826         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15827                          LPFC_MBOX_OPCODE_MQ_CREATE,
15828                          length, LPFC_SLI4_MBX_EMBED);
15829         mq_create = &mbox->u.mqe.un.mq_create;
15830         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
15831                mq->page_count);
15832         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
15833                cq->queue_id);
15834         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15835         switch (mq->entry_count) {
15836         case 16:
15837                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15838                        LPFC_MQ_RING_SIZE_16);
15839                 break;
15840         case 32:
15841                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15842                        LPFC_MQ_RING_SIZE_32);
15843                 break;
15844         case 64:
15845                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15846                        LPFC_MQ_RING_SIZE_64);
15847                 break;
15848         case 128:
15849                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15850                        LPFC_MQ_RING_SIZE_128);
15851                 break;
15852         }
15853         list_for_each_entry(dmabuf, &mq->page_list, list) {
15854                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15855                         putPaddrLow(dmabuf->phys);
15856                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15857                         putPaddrHigh(dmabuf->phys);
15858         }
15859 }
15860
15861 /**
15862  * lpfc_mq_create - Create a mailbox Queue on the HBA
15863  * @phba: HBA structure that indicates port to create a queue on.
15864  * @mq: The queue structure to use to create the mailbox queue.
15865  * @cq: The completion queue to associate with this cq.
15866  * @subtype: The queue's subtype.
15867  *
15868  * This function creates a mailbox queue, as detailed in @mq, on a port,
15869  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15870  *
15871  * The @phba struct is used to send mailbox command to HBA. The @cq struct
15872  * is used to get the entry count and entry size that are necessary to
15873  * determine the number of pages to allocate and use for this queue. This
15874  * function will send the MQ_CREATE mailbox command to the HBA to setup the
15875  * mailbox queue. This function is asynchronous and will wait for the mailbox
15876  * command to finish before continuing.
15877  *
15878  * On success this function will return a zero. If unable to allocate enough
15879  * memory this function will return -ENOMEM. If the queue create mailbox command
15880  * fails this function will return -ENXIO.
15881  **/
15882 int32_t
15883 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15884                struct lpfc_queue *cq, uint32_t subtype)
15885 {
15886         struct lpfc_mbx_mq_create *mq_create;
15887         struct lpfc_mbx_mq_create_ext *mq_create_ext;
15888         struct lpfc_dmabuf *dmabuf;
15889         LPFC_MBOXQ_t *mbox;
15890         int rc, length, status = 0;
15891         uint32_t shdr_status, shdr_add_status;
15892         union lpfc_sli4_cfg_shdr *shdr;
15893         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15894
15895         /* sanity check on queue memory */
15896         if (!mq || !cq)
15897                 return -ENODEV;
15898         if (!phba->sli4_hba.pc_sli4_params.supported)
15899                 hw_page_size = SLI4_PAGE_SIZE;
15900
15901         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15902         if (!mbox)
15903                 return -ENOMEM;
15904         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15905                   sizeof(struct lpfc_sli4_cfg_mhdr));
15906         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15907                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15908                          length, LPFC_SLI4_MBX_EMBED);
15909
15910         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
15911         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
15912         bf_set(lpfc_mbx_mq_create_ext_num_pages,
15913                &mq_create_ext->u.request, mq->page_count);
15914         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15915                &mq_create_ext->u.request, 1);
15916         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
15917                &mq_create_ext->u.request, 1);
15918         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15919                &mq_create_ext->u.request, 1);
15920         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15921                &mq_create_ext->u.request, 1);
15922         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15923                &mq_create_ext->u.request, 1);
15924         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
15925         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15926                phba->sli4_hba.pc_sli4_params.mqv);
15927         if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15928                 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15929                        cq->queue_id);
15930         else
15931                 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15932                        cq->queue_id);
15933         switch (mq->entry_count) {
15934         default:
15935                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15936                                 "0362 Unsupported MQ count. (%d)\n",
15937                                 mq->entry_count);
15938                 if (mq->entry_count < 16) {
15939                         status = -EINVAL;
15940                         goto out;
15941                 }
15942                 fallthrough;    /* otherwise default to smallest count */
15943         case 16:
15944                 bf_set(lpfc_mq_context_ring_size,
15945                        &mq_create_ext->u.request.context,
15946                        LPFC_MQ_RING_SIZE_16);
15947                 break;
15948         case 32:
15949                 bf_set(lpfc_mq_context_ring_size,
15950                        &mq_create_ext->u.request.context,
15951                        LPFC_MQ_RING_SIZE_32);
15952                 break;
15953         case 64:
15954                 bf_set(lpfc_mq_context_ring_size,
15955                        &mq_create_ext->u.request.context,
15956                        LPFC_MQ_RING_SIZE_64);
15957                 break;
15958         case 128:
15959                 bf_set(lpfc_mq_context_ring_size,
15960                        &mq_create_ext->u.request.context,
15961                        LPFC_MQ_RING_SIZE_128);
15962                 break;
15963         }
15964         list_for_each_entry(dmabuf, &mq->page_list, list) {
15965                 memset(dmabuf->virt, 0, hw_page_size);
15966                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15967                                         putPaddrLow(dmabuf->phys);
15968                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
15969                                         putPaddrHigh(dmabuf->phys);
15970         }
15971         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15972         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15973                               &mq_create_ext->u.response);
15974         if (rc != MBX_SUCCESS) {
15975                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15976                                 "2795 MQ_CREATE_EXT failed with "
15977                                 "status x%x. Failback to MQ_CREATE.\n",
15978                                 rc);
15979                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15980                 mq_create = &mbox->u.mqe.un.mq_create;
15981                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15982                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15983                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15984                                       &mq_create->u.response);
15985         }
15986
15987         /* The IOCTL status is embedded in the mailbox subheader. */
15988         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15989         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15990         if (shdr_status || shdr_add_status || rc) {
15991                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15992                                 "2502 MQ_CREATE mailbox failed with "
15993                                 "status x%x add_status x%x, mbx status x%x\n",
15994                                 shdr_status, shdr_add_status, rc);
15995                 status = -ENXIO;
15996                 goto out;
15997         }
15998         if (mq->queue_id == 0xFFFF) {
15999                 status = -ENXIO;
16000                 goto out;
16001         }
16002         mq->type = LPFC_MQ;
16003         mq->assoc_qid = cq->queue_id;
16004         mq->subtype = subtype;
16005         mq->host_index = 0;
16006         mq->hba_index = 0;
16007
16008         /* link the mq onto the parent cq child list */
16009         list_add_tail(&mq->list, &cq->child_list);
16010 out:
16011         mempool_free(mbox, phba->mbox_mem_pool);
16012         return status;
16013 }
16014
16015 /**
16016  * lpfc_wq_create - Create a Work Queue on the HBA
16017  * @phba: HBA structure that indicates port to create a queue on.
16018  * @wq: The queue structure to use to create the work queue.
16019  * @cq: The completion queue to bind this work queue to.
16020  * @subtype: The subtype of the work queue indicating its functionality.
16021  *
16022  * This function creates a work queue, as detailed in @wq, on a port, described
16023  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
16024  *
16025  * The @phba struct is used to send mailbox command to HBA. The @wq struct
16026  * is used to get the entry count and entry size that are necessary to
16027  * determine the number of pages to allocate and use for this queue. The @cq
16028  * is used to indicate which completion queue to bind this work queue to. This
16029  * function will send the WQ_CREATE mailbox command to the HBA to setup the
16030  * work queue. This function is asynchronous and will wait for the mailbox
16031  * command to finish before continuing.
16032  *
16033  * On success this function will return a zero. If unable to allocate enough
16034  * memory this function will return -ENOMEM. If the queue create mailbox command
16035  * fails this function will return -ENXIO.
16036  **/
16037 int
16038 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
16039                struct lpfc_queue *cq, uint32_t subtype)
16040 {
16041         struct lpfc_mbx_wq_create *wq_create;
16042         struct lpfc_dmabuf *dmabuf;
16043         LPFC_MBOXQ_t *mbox;
16044         int rc, length, status = 0;
16045         uint32_t shdr_status, shdr_add_status;
16046         union lpfc_sli4_cfg_shdr *shdr;
16047         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16048         struct dma_address *page;
16049         void __iomem *bar_memmap_p;
16050         uint32_t db_offset;
16051         uint16_t pci_barset;
16052         uint8_t dpp_barset;
16053         uint32_t dpp_offset;
16054         uint8_t wq_create_version;
16055 #ifdef CONFIG_X86
16056         unsigned long pg_addr;
16057 #endif
16058
16059         /* sanity check on queue memory */
16060         if (!wq || !cq)
16061                 return -ENODEV;
16062         if (!phba->sli4_hba.pc_sli4_params.supported)
16063                 hw_page_size = wq->page_size;
16064
16065         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16066         if (!mbox)
16067                 return -ENOMEM;
16068         length = (sizeof(struct lpfc_mbx_wq_create) -
16069                   sizeof(struct lpfc_sli4_cfg_mhdr));
16070         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16071                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
16072                          length, LPFC_SLI4_MBX_EMBED);
16073         wq_create = &mbox->u.mqe.un.wq_create;
16074         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
16075         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
16076                     wq->page_count);
16077         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
16078                     cq->queue_id);
16079
16080         /* wqv is the earliest version supported, NOT the latest */
16081         bf_set(lpfc_mbox_hdr_version, &shdr->request,
16082                phba->sli4_hba.pc_sli4_params.wqv);
16083
16084         if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
16085             (wq->page_size > SLI4_PAGE_SIZE))
16086                 wq_create_version = LPFC_Q_CREATE_VERSION_1;
16087         else
16088                 wq_create_version = LPFC_Q_CREATE_VERSION_0;
16089
16090         switch (wq_create_version) {
16091         case LPFC_Q_CREATE_VERSION_1:
16092                 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
16093                        wq->entry_count);
16094                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16095                        LPFC_Q_CREATE_VERSION_1);
16096
16097                 switch (wq->entry_size) {
16098                 default:
16099                 case 64:
16100                         bf_set(lpfc_mbx_wq_create_wqe_size,
16101                                &wq_create->u.request_1,
16102                                LPFC_WQ_WQE_SIZE_64);
16103                         break;
16104                 case 128:
16105                         bf_set(lpfc_mbx_wq_create_wqe_size,
16106                                &wq_create->u.request_1,
16107                                LPFC_WQ_WQE_SIZE_128);
16108                         break;
16109                 }
16110                 /* Request DPP by default */
16111                 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
16112                 bf_set(lpfc_mbx_wq_create_page_size,
16113                        &wq_create->u.request_1,
16114                        (wq->page_size / SLI4_PAGE_SIZE));
16115                 page = wq_create->u.request_1.page;
16116                 break;
16117         default:
16118                 page = wq_create->u.request.page;
16119                 break;
16120         }
16121
16122         list_for_each_entry(dmabuf, &wq->page_list, list) {
16123                 memset(dmabuf->virt, 0, hw_page_size);
16124                 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
16125                 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
16126         }
16127
16128         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16129                 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
16130
16131         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16132         /* The IOCTL status is embedded in the mailbox subheader. */
16133         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16134         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16135         if (shdr_status || shdr_add_status || rc) {
16136                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16137                                 "2503 WQ_CREATE mailbox failed with "
16138                                 "status x%x add_status x%x, mbx status x%x\n",
16139                                 shdr_status, shdr_add_status, rc);
16140                 status = -ENXIO;
16141                 goto out;
16142         }
16143
16144         if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
16145                 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
16146                                         &wq_create->u.response);
16147         else
16148                 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
16149                                         &wq_create->u.response_1);
16150
16151         if (wq->queue_id == 0xFFFF) {
16152                 status = -ENXIO;
16153                 goto out;
16154         }
16155
16156         wq->db_format = LPFC_DB_LIST_FORMAT;
16157         if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
16158                 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16159                         wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
16160                                                &wq_create->u.response);
16161                         if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
16162                             (wq->db_format != LPFC_DB_RING_FORMAT)) {
16163                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16164                                                 "3265 WQ[%d] doorbell format "
16165                                                 "not supported: x%x\n",
16166                                                 wq->queue_id, wq->db_format);
16167                                 status = -EINVAL;
16168                                 goto out;
16169                         }
16170                         pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
16171                                             &wq_create->u.response);
16172                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16173                                                                    pci_barset);
16174                         if (!bar_memmap_p) {
16175                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16176                                                 "3263 WQ[%d] failed to memmap "
16177                                                 "pci barset:x%x\n",
16178                                                 wq->queue_id, pci_barset);
16179                                 status = -ENOMEM;
16180                                 goto out;
16181                         }
16182                         db_offset = wq_create->u.response.doorbell_offset;
16183                         if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
16184                             (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
16185                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16186                                                 "3252 WQ[%d] doorbell offset "
16187                                                 "not supported: x%x\n",
16188                                                 wq->queue_id, db_offset);
16189                                 status = -EINVAL;
16190                                 goto out;
16191                         }
16192                         wq->db_regaddr = bar_memmap_p + db_offset;
16193                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16194                                         "3264 WQ[%d]: barset:x%x, offset:x%x, "
16195                                         "format:x%x\n", wq->queue_id,
16196                                         pci_barset, db_offset, wq->db_format);
16197                 } else
16198                         wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
16199         } else {
16200                 /* Check if DPP was honored by the firmware */
16201                 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
16202                                     &wq_create->u.response_1);
16203                 if (wq->dpp_enable) {
16204                         pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
16205                                             &wq_create->u.response_1);
16206                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16207                                                                    pci_barset);
16208                         if (!bar_memmap_p) {
16209                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16210                                                 "3267 WQ[%d] failed to memmap "
16211                                                 "pci barset:x%x\n",
16212                                                 wq->queue_id, pci_barset);
16213                                 status = -ENOMEM;
16214                                 goto out;
16215                         }
16216                         db_offset = wq_create->u.response_1.doorbell_offset;
16217                         wq->db_regaddr = bar_memmap_p + db_offset;
16218                         wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
16219                                             &wq_create->u.response_1);
16220                         dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
16221                                             &wq_create->u.response_1);
16222                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16223                                                                    dpp_barset);
16224                         if (!bar_memmap_p) {
16225                                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16226                                                 "3268 WQ[%d] failed to memmap "
16227                                                 "pci barset:x%x\n",
16228                                                 wq->queue_id, dpp_barset);
16229                                 status = -ENOMEM;
16230                                 goto out;
16231                         }
16232                         dpp_offset = wq_create->u.response_1.dpp_offset;
16233                         wq->dpp_regaddr = bar_memmap_p + dpp_offset;
16234                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16235                                         "3271 WQ[%d]: barset:x%x, offset:x%x, "
16236                                         "dpp_id:x%x dpp_barset:x%x "
16237                                         "dpp_offset:x%x\n",
16238                                         wq->queue_id, pci_barset, db_offset,
16239                                         wq->dpp_id, dpp_barset, dpp_offset);
16240
16241 #ifdef CONFIG_X86
16242                         /* Enable combined writes for DPP aperture */
16243                         pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
16244                         rc = set_memory_wc(pg_addr, 1);
16245                         if (rc) {
16246                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16247                                         "3272 Cannot setup Combined "
16248                                         "Write on WQ[%d] - disable DPP\n",
16249                                         wq->queue_id);
16250                                 phba->cfg_enable_dpp = 0;
16251                         }
16252 #else
16253                         phba->cfg_enable_dpp = 0;
16254 #endif
16255                 } else
16256                         wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
16257         }
16258         wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
16259         if (wq->pring == NULL) {
16260                 status = -ENOMEM;
16261                 goto out;
16262         }
16263         wq->type = LPFC_WQ;
16264         wq->assoc_qid = cq->queue_id;
16265         wq->subtype = subtype;
16266         wq->host_index = 0;
16267         wq->hba_index = 0;
16268         wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
16269
16270         /* link the wq onto the parent cq child list */
16271         list_add_tail(&wq->list, &cq->child_list);
16272 out:
16273         mempool_free(mbox, phba->mbox_mem_pool);
16274         return status;
16275 }
16276
16277 /**
16278  * lpfc_rq_create - Create a Receive Queue on the HBA
16279  * @phba: HBA structure that indicates port to create a queue on.
16280  * @hrq: The queue structure to use to create the header receive queue.
16281  * @drq: The queue structure to use to create the data receive queue.
16282  * @cq: The completion queue to bind this work queue to.
16283  * @subtype: The subtype of the work queue indicating its functionality.
16284  *
16285  * This function creates a receive buffer queue pair , as detailed in @hrq and
16286  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
16287  * to the HBA.
16288  *
16289  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
16290  * struct is used to get the entry count that is necessary to determine the
16291  * number of pages to use for this queue. The @cq is used to indicate which
16292  * completion queue to bind received buffers that are posted to these queues to.
16293  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
16294  * receive queue pair. This function is asynchronous and will wait for the
16295  * mailbox command to finish before continuing.
16296  *
16297  * On success this function will return a zero. If unable to allocate enough
16298  * memory this function will return -ENOMEM. If the queue create mailbox command
16299  * fails this function will return -ENXIO.
16300  **/
16301 int
16302 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16303                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
16304 {
16305         struct lpfc_mbx_rq_create *rq_create;
16306         struct lpfc_dmabuf *dmabuf;
16307         LPFC_MBOXQ_t *mbox;
16308         int rc, length, status = 0;
16309         uint32_t shdr_status, shdr_add_status;
16310         union lpfc_sli4_cfg_shdr *shdr;
16311         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16312         void __iomem *bar_memmap_p;
16313         uint32_t db_offset;
16314         uint16_t pci_barset;
16315
16316         /* sanity check on queue memory */
16317         if (!hrq || !drq || !cq)
16318                 return -ENODEV;
16319         if (!phba->sli4_hba.pc_sli4_params.supported)
16320                 hw_page_size = SLI4_PAGE_SIZE;
16321
16322         if (hrq->entry_count != drq->entry_count)
16323                 return -EINVAL;
16324         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16325         if (!mbox)
16326                 return -ENOMEM;
16327         length = (sizeof(struct lpfc_mbx_rq_create) -
16328                   sizeof(struct lpfc_sli4_cfg_mhdr));
16329         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16330                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
16331                          length, LPFC_SLI4_MBX_EMBED);
16332         rq_create = &mbox->u.mqe.un.rq_create;
16333         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
16334         bf_set(lpfc_mbox_hdr_version, &shdr->request,
16335                phba->sli4_hba.pc_sli4_params.rqv);
16336         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
16337                 bf_set(lpfc_rq_context_rqe_count_1,
16338                        &rq_create->u.request.context,
16339                        hrq->entry_count);
16340                 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
16341                 bf_set(lpfc_rq_context_rqe_size,
16342                        &rq_create->u.request.context,
16343                        LPFC_RQE_SIZE_8);
16344                 bf_set(lpfc_rq_context_page_size,
16345                        &rq_create->u.request.context,
16346                        LPFC_RQ_PAGE_SIZE_4096);
16347         } else {
16348                 switch (hrq->entry_count) {
16349                 default:
16350                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16351                                         "2535 Unsupported RQ count. (%d)\n",
16352                                         hrq->entry_count);
16353                         if (hrq->entry_count < 512) {
16354                                 status = -EINVAL;
16355                                 goto out;
16356                         }
16357                         fallthrough;    /* otherwise default to smallest count */
16358                 case 512:
16359                         bf_set(lpfc_rq_context_rqe_count,
16360                                &rq_create->u.request.context,
16361                                LPFC_RQ_RING_SIZE_512);
16362                         break;
16363                 case 1024:
16364                         bf_set(lpfc_rq_context_rqe_count,
16365                                &rq_create->u.request.context,
16366                                LPFC_RQ_RING_SIZE_1024);
16367                         break;
16368                 case 2048:
16369                         bf_set(lpfc_rq_context_rqe_count,
16370                                &rq_create->u.request.context,
16371                                LPFC_RQ_RING_SIZE_2048);
16372                         break;
16373                 case 4096:
16374                         bf_set(lpfc_rq_context_rqe_count,
16375                                &rq_create->u.request.context,
16376                                LPFC_RQ_RING_SIZE_4096);
16377                         break;
16378                 }
16379                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
16380                        LPFC_HDR_BUF_SIZE);
16381         }
16382         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
16383                cq->queue_id);
16384         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
16385                hrq->page_count);
16386         list_for_each_entry(dmabuf, &hrq->page_list, list) {
16387                 memset(dmabuf->virt, 0, hw_page_size);
16388                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16389                                         putPaddrLow(dmabuf->phys);
16390                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16391                                         putPaddrHigh(dmabuf->phys);
16392         }
16393         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16394                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
16395
16396         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16397         /* The IOCTL status is embedded in the mailbox subheader. */
16398         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16399         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16400         if (shdr_status || shdr_add_status || rc) {
16401                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16402                                 "2504 RQ_CREATE mailbox failed with "
16403                                 "status x%x add_status x%x, mbx status x%x\n",
16404                                 shdr_status, shdr_add_status, rc);
16405                 status = -ENXIO;
16406                 goto out;
16407         }
16408         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16409         if (hrq->queue_id == 0xFFFF) {
16410                 status = -ENXIO;
16411                 goto out;
16412         }
16413
16414         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16415                 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
16416                                         &rq_create->u.response);
16417                 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
16418                     (hrq->db_format != LPFC_DB_RING_FORMAT)) {
16419                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16420                                         "3262 RQ [%d] doorbell format not "
16421                                         "supported: x%x\n", hrq->queue_id,
16422                                         hrq->db_format);
16423                         status = -EINVAL;
16424                         goto out;
16425                 }
16426
16427                 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
16428                                     &rq_create->u.response);
16429                 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
16430                 if (!bar_memmap_p) {
16431                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16432                                         "3269 RQ[%d] failed to memmap pci "
16433                                         "barset:x%x\n", hrq->queue_id,
16434                                         pci_barset);
16435                         status = -ENOMEM;
16436                         goto out;
16437                 }
16438
16439                 db_offset = rq_create->u.response.doorbell_offset;
16440                 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
16441                     (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
16442                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16443                                         "3270 RQ[%d] doorbell offset not "
16444                                         "supported: x%x\n", hrq->queue_id,
16445                                         db_offset);
16446                         status = -EINVAL;
16447                         goto out;
16448                 }
16449                 hrq->db_regaddr = bar_memmap_p + db_offset;
16450                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16451                                 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
16452                                 "format:x%x\n", hrq->queue_id, pci_barset,
16453                                 db_offset, hrq->db_format);
16454         } else {
16455                 hrq->db_format = LPFC_DB_RING_FORMAT;
16456                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16457         }
16458         hrq->type = LPFC_HRQ;
16459         hrq->assoc_qid = cq->queue_id;
16460         hrq->subtype = subtype;
16461         hrq->host_index = 0;
16462         hrq->hba_index = 0;
16463         hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
16464
16465         /* now create the data queue */
16466         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16467                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
16468                          length, LPFC_SLI4_MBX_EMBED);
16469         bf_set(lpfc_mbox_hdr_version, &shdr->request,
16470                phba->sli4_hba.pc_sli4_params.rqv);
16471         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
16472                 bf_set(lpfc_rq_context_rqe_count_1,
16473                        &rq_create->u.request.context, hrq->entry_count);
16474                 if (subtype == LPFC_NVMET)
16475                         rq_create->u.request.context.buffer_size =
16476                                 LPFC_NVMET_DATA_BUF_SIZE;
16477                 else
16478                         rq_create->u.request.context.buffer_size =
16479                                 LPFC_DATA_BUF_SIZE;
16480                 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
16481                        LPFC_RQE_SIZE_8);
16482                 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
16483                        (PAGE_SIZE/SLI4_PAGE_SIZE));
16484         } else {
16485                 switch (drq->entry_count) {
16486                 default:
16487                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16488                                         "2536 Unsupported RQ count. (%d)\n",
16489                                         drq->entry_count);
16490                         if (drq->entry_count < 512) {
16491                                 status = -EINVAL;
16492                                 goto out;
16493                         }
16494                         fallthrough;    /* otherwise default to smallest count */
16495                 case 512:
16496                         bf_set(lpfc_rq_context_rqe_count,
16497                                &rq_create->u.request.context,
16498                                LPFC_RQ_RING_SIZE_512);
16499                         break;
16500                 case 1024:
16501                         bf_set(lpfc_rq_context_rqe_count,
16502                                &rq_create->u.request.context,
16503                                LPFC_RQ_RING_SIZE_1024);
16504                         break;
16505                 case 2048:
16506                         bf_set(lpfc_rq_context_rqe_count,
16507                                &rq_create->u.request.context,
16508                                LPFC_RQ_RING_SIZE_2048);
16509                         break;
16510                 case 4096:
16511                         bf_set(lpfc_rq_context_rqe_count,
16512                                &rq_create->u.request.context,
16513                                LPFC_RQ_RING_SIZE_4096);
16514                         break;
16515                 }
16516                 if (subtype == LPFC_NVMET)
16517                         bf_set(lpfc_rq_context_buf_size,
16518                                &rq_create->u.request.context,
16519                                LPFC_NVMET_DATA_BUF_SIZE);
16520                 else
16521                         bf_set(lpfc_rq_context_buf_size,
16522                                &rq_create->u.request.context,
16523                                LPFC_DATA_BUF_SIZE);
16524         }
16525         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
16526                cq->queue_id);
16527         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
16528                drq->page_count);
16529         list_for_each_entry(dmabuf, &drq->page_list, list) {
16530                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16531                                         putPaddrLow(dmabuf->phys);
16532                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16533                                         putPaddrHigh(dmabuf->phys);
16534         }
16535         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16536                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
16537         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16538         /* The IOCTL status is embedded in the mailbox subheader. */
16539         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
16540         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16541         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16542         if (shdr_status || shdr_add_status || rc) {
16543                 status = -ENXIO;
16544                 goto out;
16545         }
16546         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16547         if (drq->queue_id == 0xFFFF) {
16548                 status = -ENXIO;
16549                 goto out;
16550         }
16551         drq->type = LPFC_DRQ;
16552         drq->assoc_qid = cq->queue_id;
16553         drq->subtype = subtype;
16554         drq->host_index = 0;
16555         drq->hba_index = 0;
16556         drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
16557
16558         /* link the header and data RQs onto the parent cq child list */
16559         list_add_tail(&hrq->list, &cq->child_list);
16560         list_add_tail(&drq->list, &cq->child_list);
16561
16562 out:
16563         mempool_free(mbox, phba->mbox_mem_pool);
16564         return status;
16565 }
16566
16567 /**
16568  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
16569  * @phba: HBA structure that indicates port to create a queue on.
16570  * @hrqp: The queue structure array to use to create the header receive queues.
16571  * @drqp: The queue structure array to use to create the data receive queues.
16572  * @cqp: The completion queue array to bind these receive queues to.
16573  * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16574  *
16575  * This function creates a receive buffer queue pair , as detailed in @hrq and
16576  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
16577  * to the HBA.
16578  *
16579  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
16580  * struct is used to get the entry count that is necessary to determine the
16581  * number of pages to use for this queue. The @cq is used to indicate which
16582  * completion queue to bind received buffers that are posted to these queues to.
16583  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
16584  * receive queue pair. This function is asynchronous and will wait for the
16585  * mailbox command to finish before continuing.
16586  *
16587  * On success this function will return a zero. If unable to allocate enough
16588  * memory this function will return -ENOMEM. If the queue create mailbox command
16589  * fails this function will return -ENXIO.
16590  **/
16591 int
16592 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
16593                 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
16594                 uint32_t subtype)
16595 {
16596         struct lpfc_queue *hrq, *drq, *cq;
16597         struct lpfc_mbx_rq_create_v2 *rq_create;
16598         struct lpfc_dmabuf *dmabuf;
16599         LPFC_MBOXQ_t *mbox;
16600         int rc, length, alloclen, status = 0;
16601         int cnt, idx, numrq, page_idx = 0;
16602         uint32_t shdr_status, shdr_add_status;
16603         union lpfc_sli4_cfg_shdr *shdr;
16604         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16605
16606         numrq = phba->cfg_nvmet_mrq;
16607         /* sanity check on array memory */
16608         if (!hrqp || !drqp || !cqp || !numrq)
16609                 return -ENODEV;
16610         if (!phba->sli4_hba.pc_sli4_params.supported)
16611                 hw_page_size = SLI4_PAGE_SIZE;
16612
16613         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16614         if (!mbox)
16615                 return -ENOMEM;
16616
16617         length = sizeof(struct lpfc_mbx_rq_create_v2);
16618         length += ((2 * numrq * hrqp[0]->page_count) *
16619                    sizeof(struct dma_address));
16620
16621         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16622                                     LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
16623                                     LPFC_SLI4_MBX_NEMBED);
16624         if (alloclen < length) {
16625                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16626                                 "3099 Allocated DMA memory size (%d) is "
16627                                 "less than the requested DMA memory size "
16628                                 "(%d)\n", alloclen, length);
16629                 status = -ENOMEM;
16630                 goto out;
16631         }
16632
16633
16634
16635         rq_create = mbox->sge_array->addr[0];
16636         shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
16637
16638         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
16639         cnt = 0;
16640
16641         for (idx = 0; idx < numrq; idx++) {
16642                 hrq = hrqp[idx];
16643                 drq = drqp[idx];
16644                 cq  = cqp[idx];
16645
16646                 /* sanity check on queue memory */
16647                 if (!hrq || !drq || !cq) {
16648                         status = -ENODEV;
16649                         goto out;
16650                 }
16651
16652                 if (hrq->entry_count != drq->entry_count) {
16653                         status = -EINVAL;
16654                         goto out;
16655                 }
16656
16657                 if (idx == 0) {
16658                         bf_set(lpfc_mbx_rq_create_num_pages,
16659                                &rq_create->u.request,
16660                                hrq->page_count);
16661                         bf_set(lpfc_mbx_rq_create_rq_cnt,
16662                                &rq_create->u.request, (numrq * 2));
16663                         bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
16664                                1);
16665                         bf_set(lpfc_rq_context_base_cq,
16666                                &rq_create->u.request.context,
16667                                cq->queue_id);
16668                         bf_set(lpfc_rq_context_data_size,
16669                                &rq_create->u.request.context,
16670                                LPFC_NVMET_DATA_BUF_SIZE);
16671                         bf_set(lpfc_rq_context_hdr_size,
16672                                &rq_create->u.request.context,
16673                                LPFC_HDR_BUF_SIZE);
16674                         bf_set(lpfc_rq_context_rqe_count_1,
16675                                &rq_create->u.request.context,
16676                                hrq->entry_count);
16677                         bf_set(lpfc_rq_context_rqe_size,
16678                                &rq_create->u.request.context,
16679                                LPFC_RQE_SIZE_8);
16680                         bf_set(lpfc_rq_context_page_size,
16681                                &rq_create->u.request.context,
16682                                (PAGE_SIZE/SLI4_PAGE_SIZE));
16683                 }
16684                 rc = 0;
16685                 list_for_each_entry(dmabuf, &hrq->page_list, list) {
16686                         memset(dmabuf->virt, 0, hw_page_size);
16687                         cnt = page_idx + dmabuf->buffer_tag;
16688                         rq_create->u.request.page[cnt].addr_lo =
16689                                         putPaddrLow(dmabuf->phys);
16690                         rq_create->u.request.page[cnt].addr_hi =
16691                                         putPaddrHigh(dmabuf->phys);
16692                         rc++;
16693                 }
16694                 page_idx += rc;
16695
16696                 rc = 0;
16697                 list_for_each_entry(dmabuf, &drq->page_list, list) {
16698                         memset(dmabuf->virt, 0, hw_page_size);
16699                         cnt = page_idx + dmabuf->buffer_tag;
16700                         rq_create->u.request.page[cnt].addr_lo =
16701                                         putPaddrLow(dmabuf->phys);
16702                         rq_create->u.request.page[cnt].addr_hi =
16703                                         putPaddrHigh(dmabuf->phys);
16704                         rc++;
16705                 }
16706                 page_idx += rc;
16707
16708                 hrq->db_format = LPFC_DB_RING_FORMAT;
16709                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16710                 hrq->type = LPFC_HRQ;
16711                 hrq->assoc_qid = cq->queue_id;
16712                 hrq->subtype = subtype;
16713                 hrq->host_index = 0;
16714                 hrq->hba_index = 0;
16715                 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
16716
16717                 drq->db_format = LPFC_DB_RING_FORMAT;
16718                 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16719                 drq->type = LPFC_DRQ;
16720                 drq->assoc_qid = cq->queue_id;
16721                 drq->subtype = subtype;
16722                 drq->host_index = 0;
16723                 drq->hba_index = 0;
16724                 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
16725
16726                 list_add_tail(&hrq->list, &cq->child_list);
16727                 list_add_tail(&drq->list, &cq->child_list);
16728         }
16729
16730         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16731         /* The IOCTL status is embedded in the mailbox subheader. */
16732         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16733         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16734         if (shdr_status || shdr_add_status || rc) {
16735                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16736                                 "3120 RQ_CREATE mailbox failed with "
16737                                 "status x%x add_status x%x, mbx status x%x\n",
16738                                 shdr_status, shdr_add_status, rc);
16739                 status = -ENXIO;
16740                 goto out;
16741         }
16742         rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16743         if (rc == 0xFFFF) {
16744                 status = -ENXIO;
16745                 goto out;
16746         }
16747
16748         /* Initialize all RQs with associated queue id */
16749         for (idx = 0; idx < numrq; idx++) {
16750                 hrq = hrqp[idx];
16751                 hrq->queue_id = rc + (2 * idx);
16752                 drq = drqp[idx];
16753                 drq->queue_id = rc + (2 * idx) + 1;
16754         }
16755
16756 out:
16757         lpfc_sli4_mbox_cmd_free(phba, mbox);
16758         return status;
16759 }
16760
16761 /**
16762  * lpfc_eq_destroy - Destroy an event Queue on the HBA
16763  * @phba: HBA structure that indicates port to destroy a queue on.
16764  * @eq: The queue structure associated with the queue to destroy.
16765  *
16766  * This function destroys a queue, as detailed in @eq by sending an mailbox
16767  * command, specific to the type of queue, to the HBA.
16768  *
16769  * The @eq struct is used to get the queue ID of the queue to destroy.
16770  *
16771  * On success this function will return a zero. If the queue destroy mailbox
16772  * command fails this function will return -ENXIO.
16773  **/
16774 int
16775 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16776 {
16777         LPFC_MBOXQ_t *mbox;
16778         int rc, length, status = 0;
16779         uint32_t shdr_status, shdr_add_status;
16780         union lpfc_sli4_cfg_shdr *shdr;
16781
16782         /* sanity check on queue memory */
16783         if (!eq)
16784                 return -ENODEV;
16785
16786         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16787         if (!mbox)
16788                 return -ENOMEM;
16789         length = (sizeof(struct lpfc_mbx_eq_destroy) -
16790                   sizeof(struct lpfc_sli4_cfg_mhdr));
16791         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16792                          LPFC_MBOX_OPCODE_EQ_DESTROY,
16793                          length, LPFC_SLI4_MBX_EMBED);
16794         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16795                eq->queue_id);
16796         mbox->vport = eq->phba->pport;
16797         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16798
16799         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16800         /* The IOCTL status is embedded in the mailbox subheader. */
16801         shdr = (union lpfc_sli4_cfg_shdr *)
16802                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16803         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16804         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16805         if (shdr_status || shdr_add_status || rc) {
16806                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16807                                 "2505 EQ_DESTROY mailbox failed with "
16808                                 "status x%x add_status x%x, mbx status x%x\n",
16809                                 shdr_status, shdr_add_status, rc);
16810                 status = -ENXIO;
16811         }
16812
16813         /* Remove eq from any list */
16814         list_del_init(&eq->list);
16815         mempool_free(mbox, eq->phba->mbox_mem_pool);
16816         return status;
16817 }
16818
16819 /**
16820  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16821  * @phba: HBA structure that indicates port to destroy a queue on.
16822  * @cq: The queue structure associated with the queue to destroy.
16823  *
16824  * This function destroys a queue, as detailed in @cq by sending an mailbox
16825  * command, specific to the type of queue, to the HBA.
16826  *
16827  * The @cq struct is used to get the queue ID of the queue to destroy.
16828  *
16829  * On success this function will return a zero. If the queue destroy mailbox
16830  * command fails this function will return -ENXIO.
16831  **/
16832 int
16833 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16834 {
16835         LPFC_MBOXQ_t *mbox;
16836         int rc, length, status = 0;
16837         uint32_t shdr_status, shdr_add_status;
16838         union lpfc_sli4_cfg_shdr *shdr;
16839
16840         /* sanity check on queue memory */
16841         if (!cq)
16842                 return -ENODEV;
16843         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16844         if (!mbox)
16845                 return -ENOMEM;
16846         length = (sizeof(struct lpfc_mbx_cq_destroy) -
16847                   sizeof(struct lpfc_sli4_cfg_mhdr));
16848         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16849                          LPFC_MBOX_OPCODE_CQ_DESTROY,
16850                          length, LPFC_SLI4_MBX_EMBED);
16851         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16852                cq->queue_id);
16853         mbox->vport = cq->phba->pport;
16854         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16855         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16856         /* The IOCTL status is embedded in the mailbox subheader. */
16857         shdr = (union lpfc_sli4_cfg_shdr *)
16858                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16859         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16860         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16861         if (shdr_status || shdr_add_status || rc) {
16862                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16863                                 "2506 CQ_DESTROY mailbox failed with "
16864                                 "status x%x add_status x%x, mbx status x%x\n",
16865                                 shdr_status, shdr_add_status, rc);
16866                 status = -ENXIO;
16867         }
16868         /* Remove cq from any list */
16869         list_del_init(&cq->list);
16870         mempool_free(mbox, cq->phba->mbox_mem_pool);
16871         return status;
16872 }
16873
16874 /**
16875  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16876  * @phba: HBA structure that indicates port to destroy a queue on.
16877  * @mq: The queue structure associated with the queue to destroy.
16878  *
16879  * This function destroys a queue, as detailed in @mq by sending an mailbox
16880  * command, specific to the type of queue, to the HBA.
16881  *
16882  * The @mq struct is used to get the queue ID of the queue to destroy.
16883  *
16884  * On success this function will return a zero. If the queue destroy mailbox
16885  * command fails this function will return -ENXIO.
16886  **/
16887 int
16888 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16889 {
16890         LPFC_MBOXQ_t *mbox;
16891         int rc, length, status = 0;
16892         uint32_t shdr_status, shdr_add_status;
16893         union lpfc_sli4_cfg_shdr *shdr;
16894
16895         /* sanity check on queue memory */
16896         if (!mq)
16897                 return -ENODEV;
16898         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16899         if (!mbox)
16900                 return -ENOMEM;
16901         length = (sizeof(struct lpfc_mbx_mq_destroy) -
16902                   sizeof(struct lpfc_sli4_cfg_mhdr));
16903         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16904                          LPFC_MBOX_OPCODE_MQ_DESTROY,
16905                          length, LPFC_SLI4_MBX_EMBED);
16906         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16907                mq->queue_id);
16908         mbox->vport = mq->phba->pport;
16909         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16910         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16911         /* The IOCTL status is embedded in the mailbox subheader. */
16912         shdr = (union lpfc_sli4_cfg_shdr *)
16913                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16914         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16915         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16916         if (shdr_status || shdr_add_status || rc) {
16917                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16918                                 "2507 MQ_DESTROY mailbox failed with "
16919                                 "status x%x add_status x%x, mbx status x%x\n",
16920                                 shdr_status, shdr_add_status, rc);
16921                 status = -ENXIO;
16922         }
16923         /* Remove mq from any list */
16924         list_del_init(&mq->list);
16925         mempool_free(mbox, mq->phba->mbox_mem_pool);
16926         return status;
16927 }
16928
16929 /**
16930  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16931  * @phba: HBA structure that indicates port to destroy a queue on.
16932  * @wq: The queue structure associated with the queue to destroy.
16933  *
16934  * This function destroys a queue, as detailed in @wq by sending an mailbox
16935  * command, specific to the type of queue, to the HBA.
16936  *
16937  * The @wq struct is used to get the queue ID of the queue to destroy.
16938  *
16939  * On success this function will return a zero. If the queue destroy mailbox
16940  * command fails this function will return -ENXIO.
16941  **/
16942 int
16943 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16944 {
16945         LPFC_MBOXQ_t *mbox;
16946         int rc, length, status = 0;
16947         uint32_t shdr_status, shdr_add_status;
16948         union lpfc_sli4_cfg_shdr *shdr;
16949
16950         /* sanity check on queue memory */
16951         if (!wq)
16952                 return -ENODEV;
16953         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16954         if (!mbox)
16955                 return -ENOMEM;
16956         length = (sizeof(struct lpfc_mbx_wq_destroy) -
16957                   sizeof(struct lpfc_sli4_cfg_mhdr));
16958         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16959                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16960                          length, LPFC_SLI4_MBX_EMBED);
16961         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16962                wq->queue_id);
16963         mbox->vport = wq->phba->pport;
16964         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16965         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16966         shdr = (union lpfc_sli4_cfg_shdr *)
16967                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16968         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16969         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16970         if (shdr_status || shdr_add_status || rc) {
16971                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16972                                 "2508 WQ_DESTROY mailbox failed with "
16973                                 "status x%x add_status x%x, mbx status x%x\n",
16974                                 shdr_status, shdr_add_status, rc);
16975                 status = -ENXIO;
16976         }
16977         /* Remove wq from any list */
16978         list_del_init(&wq->list);
16979         kfree(wq->pring);
16980         wq->pring = NULL;
16981         mempool_free(mbox, wq->phba->mbox_mem_pool);
16982         return status;
16983 }
16984
16985 /**
16986  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16987  * @phba: HBA structure that indicates port to destroy a queue on.
16988  * @hrq: The queue structure associated with the queue to destroy.
16989  * @drq: The queue structure associated with the queue to destroy.
16990  *
16991  * This function destroys a queue, as detailed in @rq by sending an mailbox
16992  * command, specific to the type of queue, to the HBA.
16993  *
16994  * The @rq struct is used to get the queue ID of the queue to destroy.
16995  *
16996  * On success this function will return a zero. If the queue destroy mailbox
16997  * command fails this function will return -ENXIO.
16998  **/
16999 int
17000 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17001                 struct lpfc_queue *drq)
17002 {
17003         LPFC_MBOXQ_t *mbox;
17004         int rc, length, status = 0;
17005         uint32_t shdr_status, shdr_add_status;
17006         union lpfc_sli4_cfg_shdr *shdr;
17007
17008         /* sanity check on queue memory */
17009         if (!hrq || !drq)
17010                 return -ENODEV;
17011         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
17012         if (!mbox)
17013                 return -ENOMEM;
17014         length = (sizeof(struct lpfc_mbx_rq_destroy) -
17015                   sizeof(struct lpfc_sli4_cfg_mhdr));
17016         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17017                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
17018                          length, LPFC_SLI4_MBX_EMBED);
17019         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17020                hrq->queue_id);
17021         mbox->vport = hrq->phba->pport;
17022         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17023         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
17024         /* The IOCTL status is embedded in the mailbox subheader. */
17025         shdr = (union lpfc_sli4_cfg_shdr *)
17026                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17027         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17028         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17029         if (shdr_status || shdr_add_status || rc) {
17030                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17031                                 "2509 RQ_DESTROY mailbox failed with "
17032                                 "status x%x add_status x%x, mbx status x%x\n",
17033                                 shdr_status, shdr_add_status, rc);
17034                 if (rc != MBX_TIMEOUT)
17035                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
17036                 return -ENXIO;
17037         }
17038         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17039                drq->queue_id);
17040         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
17041         shdr = (union lpfc_sli4_cfg_shdr *)
17042                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17043         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17044         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17045         if (shdr_status || shdr_add_status || rc) {
17046                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17047                                 "2510 RQ_DESTROY mailbox failed with "
17048                                 "status x%x add_status x%x, mbx status x%x\n",
17049                                 shdr_status, shdr_add_status, rc);
17050                 status = -ENXIO;
17051         }
17052         list_del_init(&hrq->list);
17053         list_del_init(&drq->list);
17054         mempool_free(mbox, hrq->phba->mbox_mem_pool);
17055         return status;
17056 }
17057
17058 /**
17059  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
17060  * @phba: The virtual port for which this call being executed.
17061  * @pdma_phys_addr0: Physical address of the 1st SGL page.
17062  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
17063  * @xritag: the xritag that ties this io to the SGL pages.
17064  *
17065  * This routine will post the sgl pages for the IO that has the xritag
17066  * that is in the iocbq structure. The xritag is assigned during iocbq
17067  * creation and persists for as long as the driver is loaded.
17068  * if the caller has fewer than 256 scatter gather segments to map then
17069  * pdma_phys_addr1 should be 0.
17070  * If the caller needs to map more than 256 scatter gather segment then
17071  * pdma_phys_addr1 should be a valid physical address.
17072  * physical address for SGLs must be 64 byte aligned.
17073  * If you are going to map 2 SGL's then the first one must have 256 entries
17074  * the second sgl can have between 1 and 256 entries.
17075  *
17076  * Return codes:
17077  *      0 - Success
17078  *      -ENXIO, -ENOMEM - Failure
17079  **/
17080 int
17081 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
17082                 dma_addr_t pdma_phys_addr0,
17083                 dma_addr_t pdma_phys_addr1,
17084                 uint16_t xritag)
17085 {
17086         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
17087         LPFC_MBOXQ_t *mbox;
17088         int rc;
17089         uint32_t shdr_status, shdr_add_status;
17090         uint32_t mbox_tmo;
17091         union lpfc_sli4_cfg_shdr *shdr;
17092
17093         if (xritag == NO_XRI) {
17094                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17095                                 "0364 Invalid param:\n");
17096                 return -EINVAL;
17097         }
17098
17099         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17100         if (!mbox)
17101                 return -ENOMEM;
17102
17103         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17104                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17105                         sizeof(struct lpfc_mbx_post_sgl_pages) -
17106                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17107
17108         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
17109                                 &mbox->u.mqe.un.post_sgl_pages;
17110         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
17111         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
17112
17113         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
17114                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
17115         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
17116                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
17117
17118         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
17119                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
17120         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
17121                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
17122         if (!phba->sli4_hba.intr_enable)
17123                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17124         else {
17125                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
17126                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17127         }
17128         /* The IOCTL status is embedded in the mailbox subheader. */
17129         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
17130         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17131         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17132         if (rc != MBX_TIMEOUT)
17133                 mempool_free(mbox, phba->mbox_mem_pool);
17134         if (shdr_status || shdr_add_status || rc) {
17135                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17136                                 "2511 POST_SGL mailbox failed with "
17137                                 "status x%x add_status x%x, mbx status x%x\n",
17138                                 shdr_status, shdr_add_status, rc);
17139         }
17140         return 0;
17141 }
17142
17143 /**
17144  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
17145  * @phba: pointer to lpfc hba data structure.
17146  *
17147  * This routine is invoked to post rpi header templates to the
17148  * HBA consistent with the SLI-4 interface spec.  This routine
17149  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17150  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17151  *
17152  * Returns
17153  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
17154  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
17155  **/
17156 static uint16_t
17157 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
17158 {
17159         unsigned long xri;
17160
17161         /*
17162          * Fetch the next logical xri.  Because this index is logical,
17163          * the driver starts at 0 each time.
17164          */
17165         spin_lock_irq(&phba->hbalock);
17166         xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
17167                                  phba->sli4_hba.max_cfg_param.max_xri, 0);
17168         if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
17169                 spin_unlock_irq(&phba->hbalock);
17170                 return NO_XRI;
17171         } else {
17172                 set_bit(xri, phba->sli4_hba.xri_bmask);
17173                 phba->sli4_hba.max_cfg_param.xri_used++;
17174         }
17175         spin_unlock_irq(&phba->hbalock);
17176         return xri;
17177 }
17178
17179 /**
17180  * lpfc_sli4_free_xri - Release an xri for reuse.
17181  * @phba: pointer to lpfc hba data structure.
17182  * @xri: xri to release.
17183  *
17184  * This routine is invoked to release an xri to the pool of
17185  * available rpis maintained by the driver.
17186  **/
17187 static void
17188 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
17189 {
17190         if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
17191                 phba->sli4_hba.max_cfg_param.xri_used--;
17192         }
17193 }
17194
17195 /**
17196  * lpfc_sli4_free_xri - Release an xri for reuse.
17197  * @phba: pointer to lpfc hba data structure.
17198  * @xri: xri to release.
17199  *
17200  * This routine is invoked to release an xri to the pool of
17201  * available rpis maintained by the driver.
17202  **/
17203 void
17204 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
17205 {
17206         spin_lock_irq(&phba->hbalock);
17207         __lpfc_sli4_free_xri(phba, xri);
17208         spin_unlock_irq(&phba->hbalock);
17209 }
17210
17211 /**
17212  * lpfc_sli4_next_xritag - Get an xritag for the io
17213  * @phba: Pointer to HBA context object.
17214  *
17215  * This function gets an xritag for the iocb. If there is no unused xritag
17216  * it will return 0xffff.
17217  * The function returns the allocated xritag if successful, else returns zero.
17218  * Zero is not a valid xritag.
17219  * The caller is not required to hold any lock.
17220  **/
17221 uint16_t
17222 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
17223 {
17224         uint16_t xri_index;
17225
17226         xri_index = lpfc_sli4_alloc_xri(phba);
17227         if (xri_index == NO_XRI)
17228                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17229                                 "2004 Failed to allocate XRI.last XRITAG is %d"
17230                                 " Max XRI is %d, Used XRI is %d\n",
17231                                 xri_index,
17232                                 phba->sli4_hba.max_cfg_param.max_xri,
17233                                 phba->sli4_hba.max_cfg_param.xri_used);
17234         return xri_index;
17235 }
17236
17237 /**
17238  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
17239  * @phba: pointer to lpfc hba data structure.
17240  * @post_sgl_list: pointer to els sgl entry list.
17241  * @post_cnt: number of els sgl entries on the list.
17242  *
17243  * This routine is invoked to post a block of driver's sgl pages to the
17244  * HBA using non-embedded mailbox command. No Lock is held. This routine
17245  * is only called when the driver is loading and after all IO has been
17246  * stopped.
17247  **/
17248 static int
17249 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
17250                             struct list_head *post_sgl_list,
17251                             int post_cnt)
17252 {
17253         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
17254         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
17255         struct sgl_page_pairs *sgl_pg_pairs;
17256         void *viraddr;
17257         LPFC_MBOXQ_t *mbox;
17258         uint32_t reqlen, alloclen, pg_pairs;
17259         uint32_t mbox_tmo;
17260         uint16_t xritag_start = 0;
17261         int rc = 0;
17262         uint32_t shdr_status, shdr_add_status;
17263         union lpfc_sli4_cfg_shdr *shdr;
17264
17265         reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
17266                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
17267         if (reqlen > SLI4_PAGE_SIZE) {
17268                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17269                                 "2559 Block sgl registration required DMA "
17270                                 "size (%d) great than a page\n", reqlen);
17271                 return -ENOMEM;
17272         }
17273
17274         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17275         if (!mbox)
17276                 return -ENOMEM;
17277
17278         /* Allocate DMA memory and set up the non-embedded mailbox command */
17279         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17280                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
17281                          LPFC_SLI4_MBX_NEMBED);
17282
17283         if (alloclen < reqlen) {
17284                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17285                                 "0285 Allocated DMA memory size (%d) is "
17286                                 "less than the requested DMA memory "
17287                                 "size (%d)\n", alloclen, reqlen);
17288                 lpfc_sli4_mbox_cmd_free(phba, mbox);
17289                 return -ENOMEM;
17290         }
17291         /* Set up the SGL pages in the non-embedded DMA pages */
17292         viraddr = mbox->sge_array->addr[0];
17293         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
17294         sgl_pg_pairs = &sgl->sgl_pg_pairs;
17295
17296         pg_pairs = 0;
17297         list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
17298                 /* Set up the sge entry */
17299                 sgl_pg_pairs->sgl_pg0_addr_lo =
17300                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
17301                 sgl_pg_pairs->sgl_pg0_addr_hi =
17302                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
17303                 sgl_pg_pairs->sgl_pg1_addr_lo =
17304                                 cpu_to_le32(putPaddrLow(0));
17305                 sgl_pg_pairs->sgl_pg1_addr_hi =
17306                                 cpu_to_le32(putPaddrHigh(0));
17307
17308                 /* Keep the first xritag on the list */
17309                 if (pg_pairs == 0)
17310                         xritag_start = sglq_entry->sli4_xritag;
17311                 sgl_pg_pairs++;
17312                 pg_pairs++;
17313         }
17314
17315         /* Complete initialization and perform endian conversion. */
17316         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
17317         bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
17318         sgl->word0 = cpu_to_le32(sgl->word0);
17319
17320         if (!phba->sli4_hba.intr_enable)
17321                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17322         else {
17323                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
17324                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17325         }
17326         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
17327         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17328         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17329         if (rc != MBX_TIMEOUT)
17330                 lpfc_sli4_mbox_cmd_free(phba, mbox);
17331         if (shdr_status || shdr_add_status || rc) {
17332                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17333                                 "2513 POST_SGL_BLOCK mailbox command failed "
17334                                 "status x%x add_status x%x mbx status x%x\n",
17335                                 shdr_status, shdr_add_status, rc);
17336                 rc = -ENXIO;
17337         }
17338         return rc;
17339 }
17340
17341 /**
17342  * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
17343  * @phba: pointer to lpfc hba data structure.
17344  * @nblist: pointer to nvme buffer list.
17345  * @count: number of scsi buffers on the list.
17346  *
17347  * This routine is invoked to post a block of @count scsi sgl pages from a
17348  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
17349  * No Lock is held.
17350  *
17351  **/
17352 static int
17353 lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
17354                             int count)
17355 {
17356         struct lpfc_io_buf *lpfc_ncmd;
17357         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
17358         struct sgl_page_pairs *sgl_pg_pairs;
17359         void *viraddr;
17360         LPFC_MBOXQ_t *mbox;
17361         uint32_t reqlen, alloclen, pg_pairs;
17362         uint32_t mbox_tmo;
17363         uint16_t xritag_start = 0;
17364         int rc = 0;
17365         uint32_t shdr_status, shdr_add_status;
17366         dma_addr_t pdma_phys_bpl1;
17367         union lpfc_sli4_cfg_shdr *shdr;
17368
17369         /* Calculate the requested length of the dma memory */
17370         reqlen = count * sizeof(struct sgl_page_pairs) +
17371                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
17372         if (reqlen > SLI4_PAGE_SIZE) {
17373                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
17374                                 "6118 Block sgl registration required DMA "
17375                                 "size (%d) great than a page\n", reqlen);
17376                 return -ENOMEM;
17377         }
17378         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17379         if (!mbox) {
17380                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17381                                 "6119 Failed to allocate mbox cmd memory\n");
17382                 return -ENOMEM;
17383         }
17384
17385         /* Allocate DMA memory and set up the non-embedded mailbox command */
17386         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17387                                     LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17388                                     reqlen, LPFC_SLI4_MBX_NEMBED);
17389
17390         if (alloclen < reqlen) {
17391                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17392                                 "6120 Allocated DMA memory size (%d) is "
17393                                 "less than the requested DMA memory "
17394                                 "size (%d)\n", alloclen, reqlen);
17395                 lpfc_sli4_mbox_cmd_free(phba, mbox);
17396                 return -ENOMEM;
17397         }
17398
17399         /* Get the first SGE entry from the non-embedded DMA memory */
17400         viraddr = mbox->sge_array->addr[0];
17401
17402         /* Set up the SGL pages in the non-embedded DMA pages */
17403         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
17404         sgl_pg_pairs = &sgl->sgl_pg_pairs;
17405
17406         pg_pairs = 0;
17407         list_for_each_entry(lpfc_ncmd, nblist, list) {
17408                 /* Set up the sge entry */
17409                 sgl_pg_pairs->sgl_pg0_addr_lo =
17410                         cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
17411                 sgl_pg_pairs->sgl_pg0_addr_hi =
17412                         cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
17413                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
17414                         pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
17415                                                 SGL_PAGE_SIZE;
17416                 else
17417                         pdma_phys_bpl1 = 0;
17418                 sgl_pg_pairs->sgl_pg1_addr_lo =
17419                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
17420                 sgl_pg_pairs->sgl_pg1_addr_hi =
17421                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
17422                 /* Keep the first xritag on the list */
17423                 if (pg_pairs == 0)
17424                         xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
17425                 sgl_pg_pairs++;
17426                 pg_pairs++;
17427         }
17428         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
17429         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
17430         /* Perform endian conversion if necessary */
17431         sgl->word0 = cpu_to_le32(sgl->word0);
17432
17433         if (!phba->sli4_hba.intr_enable) {
17434                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17435         } else {
17436                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
17437                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17438         }
17439         shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
17440         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17441         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17442         if (rc != MBX_TIMEOUT)
17443                 lpfc_sli4_mbox_cmd_free(phba, mbox);
17444         if (shdr_status || shdr_add_status || rc) {
17445                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17446                                 "6125 POST_SGL_BLOCK mailbox command failed "
17447                                 "status x%x add_status x%x mbx status x%x\n",
17448                                 shdr_status, shdr_add_status, rc);
17449                 rc = -ENXIO;
17450         }
17451         return rc;
17452 }
17453
17454 /**
17455  * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
17456  * @phba: pointer to lpfc hba data structure.
17457  * @post_nblist: pointer to the nvme buffer list.
17458  * @sb_count: number of nvme buffers.
17459  *
17460  * This routine walks a list of nvme buffers that was passed in. It attempts
17461  * to construct blocks of nvme buffer sgls which contains contiguous xris and
17462  * uses the non-embedded SGL block post mailbox commands to post to the port.
17463  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
17464  * embedded SGL post mailbox command for posting. The @post_nblist passed in
17465  * must be local list, thus no lock is needed when manipulate the list.
17466  *
17467  * Returns: 0 = failure, non-zero number of successfully posted buffers.
17468  **/
17469 int
17470 lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
17471                            struct list_head *post_nblist, int sb_count)
17472 {
17473         struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
17474         int status, sgl_size;
17475         int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
17476         dma_addr_t pdma_phys_sgl1;
17477         int last_xritag = NO_XRI;
17478         int cur_xritag;
17479         LIST_HEAD(prep_nblist);
17480         LIST_HEAD(blck_nblist);
17481         LIST_HEAD(nvme_nblist);
17482
17483         /* sanity check */
17484         if (sb_count <= 0)
17485                 return -EINVAL;
17486
17487         sgl_size = phba->cfg_sg_dma_buf_size;
17488         list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
17489                 list_del_init(&lpfc_ncmd->list);
17490                 block_cnt++;
17491                 if ((last_xritag != NO_XRI) &&
17492                     (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
17493                         /* a hole in xri block, form a sgl posting block */
17494                         list_splice_init(&prep_nblist, &blck_nblist);
17495                         post_cnt = block_cnt - 1;
17496                         /* prepare list for next posting block */
17497                         list_add_tail(&lpfc_ncmd->list, &prep_nblist);
17498                         block_cnt = 1;
17499                 } else {
17500                         /* prepare list for next posting block */
17501                         list_add_tail(&lpfc_ncmd->list, &prep_nblist);
17502                         /* enough sgls for non-embed sgl mbox command */
17503                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
17504                                 list_splice_init(&prep_nblist, &blck_nblist);
17505                                 post_cnt = block_cnt;
17506                                 block_cnt = 0;
17507                         }
17508                 }
17509                 num_posting++;
17510                 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
17511
17512                 /* end of repost sgl list condition for NVME buffers */
17513                 if (num_posting == sb_count) {
17514                         if (post_cnt == 0) {
17515                                 /* last sgl posting block */
17516                                 list_splice_init(&prep_nblist, &blck_nblist);
17517                                 post_cnt = block_cnt;
17518                         } else if (block_cnt == 1) {
17519                                 /* last single sgl with non-contiguous xri */
17520                                 if (sgl_size > SGL_PAGE_SIZE)
17521                                         pdma_phys_sgl1 =
17522                                                 lpfc_ncmd->dma_phys_sgl +
17523                                                 SGL_PAGE_SIZE;
17524                                 else
17525                                         pdma_phys_sgl1 = 0;
17526                                 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
17527                                 status = lpfc_sli4_post_sgl(
17528                                                 phba, lpfc_ncmd->dma_phys_sgl,
17529                                                 pdma_phys_sgl1, cur_xritag);
17530                                 if (status) {
17531                                         /* Post error.  Buffer unavailable. */
17532                                         lpfc_ncmd->flags |=
17533                                                 LPFC_SBUF_NOT_POSTED;
17534                                 } else {
17535                                         /* Post success. Bffer available. */
17536                                         lpfc_ncmd->flags &=
17537                                                 ~LPFC_SBUF_NOT_POSTED;
17538                                         lpfc_ncmd->status = IOSTAT_SUCCESS;
17539                                         num_posted++;
17540                                 }
17541                                 /* success, put on NVME buffer sgl list */
17542                                 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
17543                         }
17544                 }
17545
17546                 /* continue until a nembed page worth of sgls */
17547                 if (post_cnt == 0)
17548                         continue;
17549
17550                 /* post block of NVME buffer list sgls */
17551                 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
17552                                                      post_cnt);
17553
17554                 /* don't reset xirtag due to hole in xri block */
17555                 if (block_cnt == 0)
17556                         last_xritag = NO_XRI;
17557
17558                 /* reset NVME buffer post count for next round of posting */
17559                 post_cnt = 0;
17560
17561                 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
17562                 while (!list_empty(&blck_nblist)) {
17563                         list_remove_head(&blck_nblist, lpfc_ncmd,
17564                                          struct lpfc_io_buf, list);
17565                         if (status) {
17566                                 /* Post error.  Mark buffer unavailable. */
17567                                 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
17568                         } else {
17569                                 /* Post success, Mark buffer available. */
17570                                 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
17571                                 lpfc_ncmd->status = IOSTAT_SUCCESS;
17572                                 num_posted++;
17573                         }
17574                         list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
17575                 }
17576         }
17577         /* Push NVME buffers with sgl posted to the available list */
17578         lpfc_io_buf_replenish(phba, &nvme_nblist);
17579
17580         return num_posted;
17581 }
17582
17583 /**
17584  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
17585  * @phba: pointer to lpfc_hba struct that the frame was received on
17586  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17587  *
17588  * This function checks the fields in the @fc_hdr to see if the FC frame is a
17589  * valid type of frame that the LPFC driver will handle. This function will
17590  * return a zero if the frame is a valid frame or a non zero value when the
17591  * frame does not pass the check.
17592  **/
17593 static int
17594 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
17595 {
17596         /*  make rctl_names static to save stack space */
17597         struct fc_vft_header *fc_vft_hdr;
17598         uint32_t *header = (uint32_t *) fc_hdr;
17599
17600 #define FC_RCTL_MDS_DIAGS       0xF4
17601
17602         switch (fc_hdr->fh_r_ctl) {
17603         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
17604         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
17605         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
17606         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
17607         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
17608         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
17609         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
17610         case FC_RCTL_DD_CMD_STATUS:     /* command status */
17611         case FC_RCTL_ELS_REQ:   /* extended link services request */
17612         case FC_RCTL_ELS_REP:   /* extended link services reply */
17613         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
17614         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
17615         case FC_RCTL_BA_NOP:    /* basic link service NOP */
17616         case FC_RCTL_BA_ABTS:   /* basic link service abort */
17617         case FC_RCTL_BA_RMC:    /* remove connection */
17618         case FC_RCTL_BA_ACC:    /* basic accept */
17619         case FC_RCTL_BA_RJT:    /* basic reject */
17620         case FC_RCTL_BA_PRMT:
17621         case FC_RCTL_ACK_1:     /* acknowledge_1 */
17622         case FC_RCTL_ACK_0:     /* acknowledge_0 */
17623         case FC_RCTL_P_RJT:     /* port reject */
17624         case FC_RCTL_F_RJT:     /* fabric reject */
17625         case FC_RCTL_P_BSY:     /* port busy */
17626         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
17627         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
17628         case FC_RCTL_LCR:       /* link credit reset */
17629         case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
17630         case FC_RCTL_END:       /* end */
17631                 break;
17632         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
17633                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17634                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
17635                 return lpfc_fc_frame_check(phba, fc_hdr);
17636         default:
17637                 goto drop;
17638         }
17639
17640         switch (fc_hdr->fh_type) {
17641         case FC_TYPE_BLS:
17642         case FC_TYPE_ELS:
17643         case FC_TYPE_FCP:
17644         case FC_TYPE_CT:
17645         case FC_TYPE_NVME:
17646                 break;
17647         case FC_TYPE_IP:
17648         case FC_TYPE_ILS:
17649         default:
17650                 goto drop;
17651         }
17652
17653         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
17654                         "2538 Received frame rctl:x%x, type:x%x, "
17655                         "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
17656                         fc_hdr->fh_r_ctl, fc_hdr->fh_type,
17657                         be32_to_cpu(header[0]), be32_to_cpu(header[1]),
17658                         be32_to_cpu(header[2]), be32_to_cpu(header[3]),
17659                         be32_to_cpu(header[4]), be32_to_cpu(header[5]),
17660                         be32_to_cpu(header[6]));
17661         return 0;
17662 drop:
17663         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
17664                         "2539 Dropped frame rctl:x%x type:x%x\n",
17665                         fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17666         return 1;
17667 }
17668
17669 /**
17670  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
17671  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17672  *
17673  * This function processes the FC header to retrieve the VFI from the VF
17674  * header, if one exists. This function will return the VFI if one exists
17675  * or 0 if no VSAN Header exists.
17676  **/
17677 static uint32_t
17678 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
17679 {
17680         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17681
17682         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
17683                 return 0;
17684         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
17685 }
17686
17687 /**
17688  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
17689  * @phba: Pointer to the HBA structure to search for the vport on
17690  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17691  * @fcfi: The FC Fabric ID that the frame came from
17692  * @did: Destination ID to match against
17693  *
17694  * This function searches the @phba for a vport that matches the content of the
17695  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
17696  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
17697  * returns the matching vport pointer or NULL if unable to match frame to a
17698  * vport.
17699  **/
17700 static struct lpfc_vport *
17701 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
17702                        uint16_t fcfi, uint32_t did)
17703 {
17704         struct lpfc_vport **vports;
17705         struct lpfc_vport *vport = NULL;
17706         int i;
17707
17708         if (did == Fabric_DID)
17709                 return phba->pport;
17710         if ((phba->pport->fc_flag & FC_PT2PT) &&
17711                 !(phba->link_state == LPFC_HBA_READY))
17712                 return phba->pport;
17713
17714         vports = lpfc_create_vport_work_array(phba);
17715         if (vports != NULL) {
17716                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17717                         if (phba->fcf.fcfi == fcfi &&
17718                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
17719                             vports[i]->fc_myDID == did) {
17720                                 vport = vports[i];
17721                                 break;
17722                         }
17723                 }
17724         }
17725         lpfc_destroy_vport_work_array(phba, vports);
17726         return vport;
17727 }
17728
17729 /**
17730  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
17731  * @vport: The vport to work on.
17732  *
17733  * This function updates the receive sequence time stamp for this vport. The
17734  * receive sequence time stamp indicates the time that the last frame of the
17735  * the sequence that has been idle for the longest amount of time was received.
17736  * the driver uses this time stamp to indicate if any received sequences have
17737  * timed out.
17738  **/
17739 static void
17740 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17741 {
17742         struct lpfc_dmabuf *h_buf;
17743         struct hbq_dmabuf *dmabuf = NULL;
17744
17745         /* get the oldest sequence on the rcv list */
17746         h_buf = list_get_first(&vport->rcv_buffer_list,
17747                                struct lpfc_dmabuf, list);
17748         if (!h_buf)
17749                 return;
17750         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17751         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17752 }
17753
17754 /**
17755  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17756  * @vport: The vport that the received sequences were sent to.
17757  *
17758  * This function cleans up all outstanding received sequences. This is called
17759  * by the driver when a link event or user action invalidates all the received
17760  * sequences.
17761  **/
17762 void
17763 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17764 {
17765         struct lpfc_dmabuf *h_buf, *hnext;
17766         struct lpfc_dmabuf *d_buf, *dnext;
17767         struct hbq_dmabuf *dmabuf = NULL;
17768
17769         /* start with the oldest sequence on the rcv list */
17770         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17771                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17772                 list_del_init(&dmabuf->hbuf.list);
17773                 list_for_each_entry_safe(d_buf, dnext,
17774                                          &dmabuf->dbuf.list, list) {
17775                         list_del_init(&d_buf->list);
17776                         lpfc_in_buf_free(vport->phba, d_buf);
17777                 }
17778                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17779         }
17780 }
17781
17782 /**
17783  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17784  * @vport: The vport that the received sequences were sent to.
17785  *
17786  * This function determines whether any received sequences have timed out by
17787  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17788  * indicates that there is at least one timed out sequence this routine will
17789  * go through the received sequences one at a time from most inactive to most
17790  * active to determine which ones need to be cleaned up. Once it has determined
17791  * that a sequence needs to be cleaned up it will simply free up the resources
17792  * without sending an abort.
17793  **/
17794 void
17795 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17796 {
17797         struct lpfc_dmabuf *h_buf, *hnext;
17798         struct lpfc_dmabuf *d_buf, *dnext;
17799         struct hbq_dmabuf *dmabuf = NULL;
17800         unsigned long timeout;
17801         int abort_count = 0;
17802
17803         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17804                    vport->rcv_buffer_time_stamp);
17805         if (list_empty(&vport->rcv_buffer_list) ||
17806             time_before(jiffies, timeout))
17807                 return;
17808         /* start with the oldest sequence on the rcv list */
17809         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17810                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17811                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17812                            dmabuf->time_stamp);
17813                 if (time_before(jiffies, timeout))
17814                         break;
17815                 abort_count++;
17816                 list_del_init(&dmabuf->hbuf.list);
17817                 list_for_each_entry_safe(d_buf, dnext,
17818                                          &dmabuf->dbuf.list, list) {
17819                         list_del_init(&d_buf->list);
17820                         lpfc_in_buf_free(vport->phba, d_buf);
17821                 }
17822                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17823         }
17824         if (abort_count)
17825                 lpfc_update_rcv_time_stamp(vport);
17826 }
17827
17828 /**
17829  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17830  * @vport: pointer to a vitural port
17831  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17832  *
17833  * This function searches through the existing incomplete sequences that have
17834  * been sent to this @vport. If the frame matches one of the incomplete
17835  * sequences then the dbuf in the @dmabuf is added to the list of frames that
17836  * make up that sequence. If no sequence is found that matches this frame then
17837  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17838  * This function returns a pointer to the first dmabuf in the sequence list that
17839  * the frame was linked to.
17840  **/
17841 static struct hbq_dmabuf *
17842 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17843 {
17844         struct fc_frame_header *new_hdr;
17845         struct fc_frame_header *temp_hdr;
17846         struct lpfc_dmabuf *d_buf;
17847         struct lpfc_dmabuf *h_buf;
17848         struct hbq_dmabuf *seq_dmabuf = NULL;
17849         struct hbq_dmabuf *temp_dmabuf = NULL;
17850         uint8_t found = 0;
17851
17852         INIT_LIST_HEAD(&dmabuf->dbuf.list);
17853         dmabuf->time_stamp = jiffies;
17854         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17855
17856         /* Use the hdr_buf to find the sequence that this frame belongs to */
17857         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17858                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17859                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17860                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17861                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17862                         continue;
17863                 /* found a pending sequence that matches this frame */
17864                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17865                 break;
17866         }
17867         if (!seq_dmabuf) {
17868                 /*
17869                  * This indicates first frame received for this sequence.
17870                  * Queue the buffer on the vport's rcv_buffer_list.
17871                  */
17872                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17873                 lpfc_update_rcv_time_stamp(vport);
17874                 return dmabuf;
17875         }
17876         temp_hdr = seq_dmabuf->hbuf.virt;
17877         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17878                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
17879                 list_del_init(&seq_dmabuf->hbuf.list);
17880                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17881                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17882                 lpfc_update_rcv_time_stamp(vport);
17883                 return dmabuf;
17884         }
17885         /* move this sequence to the tail to indicate a young sequence */
17886         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17887         seq_dmabuf->time_stamp = jiffies;
17888         lpfc_update_rcv_time_stamp(vport);
17889         if (list_empty(&seq_dmabuf->dbuf.list)) {
17890                 temp_hdr = dmabuf->hbuf.virt;
17891                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17892                 return seq_dmabuf;
17893         }
17894         /* find the correct place in the sequence to insert this frame */
17895         d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17896         while (!found) {
17897                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17898                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17899                 /*
17900                  * If the frame's sequence count is greater than the frame on
17901                  * the list then insert the frame right after this frame
17902                  */
17903                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17904                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
17905                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
17906                         found = 1;
17907                         break;
17908                 }
17909
17910                 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17911                         break;
17912                 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
17913         }
17914
17915         if (found)
17916                 return seq_dmabuf;
17917         return NULL;
17918 }
17919
17920 /**
17921  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17922  * @vport: pointer to a vitural port
17923  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17924  *
17925  * This function tries to abort from the partially assembed sequence, described
17926  * by the information from basic abbort @dmabuf. It checks to see whether such
17927  * partially assembled sequence held by the driver. If so, it shall free up all
17928  * the frames from the partially assembled sequence.
17929  *
17930  * Return
17931  * true  -- if there is matching partially assembled sequence present and all
17932  *          the frames freed with the sequence;
17933  * false -- if there is no matching partially assembled sequence present so
17934  *          nothing got aborted in the lower layer driver
17935  **/
17936 static bool
17937 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17938                             struct hbq_dmabuf *dmabuf)
17939 {
17940         struct fc_frame_header *new_hdr;
17941         struct fc_frame_header *temp_hdr;
17942         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17943         struct hbq_dmabuf *seq_dmabuf = NULL;
17944
17945         /* Use the hdr_buf to find the sequence that matches this frame */
17946         INIT_LIST_HEAD(&dmabuf->dbuf.list);
17947         INIT_LIST_HEAD(&dmabuf->hbuf.list);
17948         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17949         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17950                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17951                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17952                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17953                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17954                         continue;
17955                 /* found a pending sequence that matches this frame */
17956                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17957                 break;
17958         }
17959
17960         /* Free up all the frames from the partially assembled sequence */
17961         if (seq_dmabuf) {
17962                 list_for_each_entry_safe(d_buf, n_buf,
17963                                          &seq_dmabuf->dbuf.list, list) {
17964                         list_del_init(&d_buf->list);
17965                         lpfc_in_buf_free(vport->phba, d_buf);
17966                 }
17967                 return true;
17968         }
17969         return false;
17970 }
17971
17972 /**
17973  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
17974  * @vport: pointer to a vitural port
17975  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17976  *
17977  * This function tries to abort from the assembed sequence from upper level
17978  * protocol, described by the information from basic abbort @dmabuf. It
17979  * checks to see whether such pending context exists at upper level protocol.
17980  * If so, it shall clean up the pending context.
17981  *
17982  * Return
17983  * true  -- if there is matching pending context of the sequence cleaned
17984  *          at ulp;
17985  * false -- if there is no matching pending context of the sequence present
17986  *          at ulp.
17987  **/
17988 static bool
17989 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17990 {
17991         struct lpfc_hba *phba = vport->phba;
17992         int handled;
17993
17994         /* Accepting abort at ulp with SLI4 only */
17995         if (phba->sli_rev < LPFC_SLI_REV4)
17996                 return false;
17997
17998         /* Register all caring upper level protocols to attend abort */
17999         handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
18000         if (handled)
18001                 return true;
18002
18003         return false;
18004 }
18005
18006 /**
18007  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
18008  * @phba: Pointer to HBA context object.
18009  * @cmd_iocbq: pointer to the command iocbq structure.
18010  * @rsp_iocbq: pointer to the response iocbq structure.
18011  *
18012  * This function handles the sequence abort response iocb command complete
18013  * event. It properly releases the memory allocated to the sequence abort
18014  * accept iocb.
18015  **/
18016 static void
18017 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
18018                              struct lpfc_iocbq *cmd_iocbq,
18019                              struct lpfc_iocbq *rsp_iocbq)
18020 {
18021         struct lpfc_nodelist *ndlp;
18022
18023         if (cmd_iocbq) {
18024                 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
18025                 lpfc_nlp_put(ndlp);
18026                 lpfc_nlp_not_used(ndlp);
18027                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
18028         }
18029
18030         /* Failure means BLS ABORT RSP did not get delivered to remote node*/
18031         if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
18032                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18033                         "3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
18034                         rsp_iocbq->iocb.ulpStatus,
18035                         rsp_iocbq->iocb.un.ulpWord[4]);
18036 }
18037
18038 /**
18039  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
18040  * @phba: Pointer to HBA context object.
18041  * @xri: xri id in transaction.
18042  *
18043  * This function validates the xri maps to the known range of XRIs allocated an
18044  * used by the driver.
18045  **/
18046 uint16_t
18047 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
18048                       uint16_t xri)
18049 {
18050         uint16_t i;
18051
18052         for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
18053                 if (xri == phba->sli4_hba.xri_ids[i])
18054                         return i;
18055         }
18056         return NO_XRI;
18057 }
18058
18059 /**
18060  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
18061  * @vport: pointer to a vitural port.
18062  * @fc_hdr: pointer to a FC frame header.
18063  * @aborted: was the partially assembled receive sequence successfully aborted
18064  *
18065  * This function sends a basic response to a previous unsol sequence abort
18066  * event after aborting the sequence handling.
18067  **/
18068 void
18069 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
18070                         struct fc_frame_header *fc_hdr, bool aborted)
18071 {
18072         struct lpfc_hba *phba = vport->phba;
18073         struct lpfc_iocbq *ctiocb = NULL;
18074         struct lpfc_nodelist *ndlp;
18075         uint16_t oxid, rxid, xri, lxri;
18076         uint32_t sid, fctl;
18077         IOCB_t *icmd;
18078         int rc;
18079
18080         if (!lpfc_is_link_up(phba))
18081                 return;
18082
18083         sid = sli4_sid_from_fc_hdr(fc_hdr);
18084         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
18085         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
18086
18087         ndlp = lpfc_findnode_did(vport, sid);
18088         if (!ndlp) {
18089                 ndlp = lpfc_nlp_init(vport, sid);
18090                 if (!ndlp) {
18091                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
18092                                          "1268 Failed to allocate ndlp for "
18093                                          "oxid:x%x SID:x%x\n", oxid, sid);
18094                         return;
18095                 }
18096                 /* Put ndlp onto pport node list */
18097                 lpfc_enqueue_node(vport, ndlp);
18098         }
18099
18100         /* Allocate buffer for rsp iocb */
18101         ctiocb = lpfc_sli_get_iocbq(phba);
18102         if (!ctiocb)
18103                 return;
18104
18105         /* Extract the F_CTL field from FC_HDR */
18106         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
18107
18108         icmd = &ctiocb->iocb;
18109         icmd->un.xseq64.bdl.bdeSize = 0;
18110         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
18111         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
18112         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
18113         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
18114
18115         /* Fill in the rest of iocb fields */
18116         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
18117         icmd->ulpBdeCount = 0;
18118         icmd->ulpLe = 1;
18119         icmd->ulpClass = CLASS3;
18120         icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
18121         ctiocb->context1 = lpfc_nlp_get(ndlp);
18122         if (!ctiocb->context1) {
18123                 lpfc_sli_release_iocbq(phba, ctiocb);
18124                 return;
18125         }
18126
18127         ctiocb->vport = phba->pport;
18128         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
18129         ctiocb->sli4_lxritag = NO_XRI;
18130         ctiocb->sli4_xritag = NO_XRI;
18131
18132         if (fctl & FC_FC_EX_CTX)
18133                 /* Exchange responder sent the abort so we
18134                  * own the oxid.
18135                  */
18136                 xri = oxid;
18137         else
18138                 xri = rxid;
18139         lxri = lpfc_sli4_xri_inrange(phba, xri);
18140         if (lxri != NO_XRI)
18141                 lpfc_set_rrq_active(phba, ndlp, lxri,
18142                         (xri == oxid) ? rxid : oxid, 0);
18143         /* For BA_ABTS from exchange responder, if the logical xri with
18144          * the oxid maps to the FCP XRI range, the port no longer has
18145          * that exchange context, send a BLS_RJT. Override the IOCB for
18146          * a BA_RJT.
18147          */
18148         if ((fctl & FC_FC_EX_CTX) &&
18149             (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
18150                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
18151                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
18152                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
18153                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
18154         }
18155
18156         /* If BA_ABTS failed to abort a partially assembled receive sequence,
18157          * the driver no longer has that exchange, send a BLS_RJT. Override
18158          * the IOCB for a BA_RJT.
18159          */
18160         if (aborted == false) {
18161                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
18162                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
18163                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
18164                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
18165         }
18166
18167         if (fctl & FC_FC_EX_CTX) {
18168                 /* ABTS sent by responder to CT exchange, construction
18169                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
18170                  * field and RX_ID from ABTS for RX_ID field.
18171                  */
18172                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
18173         } else {
18174                 /* ABTS sent by initiator to CT exchange, construction
18175                  * of BA_ACC will need to allocate a new XRI as for the
18176                  * XRI_TAG field.
18177                  */
18178                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
18179         }
18180         bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
18181         bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
18182
18183         /* Xmit CT abts response on exchange <xid> */
18184         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
18185                          "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
18186                          icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
18187
18188         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
18189         if (rc == IOCB_ERROR) {
18190                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
18191                                  "2925 Failed to issue CT ABTS RSP x%x on "
18192                                  "xri x%x, Data x%x\n",
18193                                  icmd->un.xseq64.w5.hcsw.Rctl, oxid,
18194                                  phba->link_state);
18195                 lpfc_nlp_put(ndlp);
18196                 ctiocb->context1 = NULL;
18197                 lpfc_sli_release_iocbq(phba, ctiocb);
18198         }
18199 }
18200
18201 /**
18202  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
18203  * @vport: Pointer to the vport on which this sequence was received
18204  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18205  *
18206  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
18207  * receive sequence is only partially assembed by the driver, it shall abort
18208  * the partially assembled frames for the sequence. Otherwise, if the
18209  * unsolicited receive sequence has been completely assembled and passed to
18210  * the Upper Layer Protocol (ULP), it then mark the per oxid status for the
18211  * unsolicited sequence has been aborted. After that, it will issue a basic
18212  * accept to accept the abort.
18213  **/
18214 static void
18215 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
18216                              struct hbq_dmabuf *dmabuf)
18217 {
18218         struct lpfc_hba *phba = vport->phba;
18219         struct fc_frame_header fc_hdr;
18220         uint32_t fctl;
18221         bool aborted;
18222
18223         /* Make a copy of fc_hdr before the dmabuf being released */
18224         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
18225         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
18226
18227         if (fctl & FC_FC_EX_CTX) {
18228                 /* ABTS by responder to exchange, no cleanup needed */
18229                 aborted = true;
18230         } else {
18231                 /* ABTS by initiator to exchange, need to do cleanup */
18232                 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
18233                 if (aborted == false)
18234                         aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
18235         }
18236         lpfc_in_buf_free(phba, &dmabuf->dbuf);
18237
18238         if (phba->nvmet_support) {
18239                 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
18240                 return;
18241         }
18242
18243         /* Respond with BA_ACC or BA_RJT accordingly */
18244         lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
18245 }
18246
18247 /**
18248  * lpfc_seq_complete - Indicates if a sequence is complete
18249  * @dmabuf: pointer to a dmabuf that describes the FC sequence
18250  *
18251  * This function checks the sequence, starting with the frame described by
18252  * @dmabuf, to see if all the frames associated with this sequence are present.
18253  * the frames associated with this sequence are linked to the @dmabuf using the
18254  * dbuf list. This function looks for two major things. 1) That the first frame
18255  * has a sequence count of zero. 2) There is a frame with last frame of sequence
18256  * set. 3) That there are no holes in the sequence count. The function will
18257  * return 1 when the sequence is complete, otherwise it will return 0.
18258  **/
18259 static int
18260 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
18261 {
18262         struct fc_frame_header *hdr;
18263         struct lpfc_dmabuf *d_buf;
18264         struct hbq_dmabuf *seq_dmabuf;
18265         uint32_t fctl;
18266         int seq_count = 0;
18267
18268         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18269         /* make sure first fame of sequence has a sequence count of zero */
18270         if (hdr->fh_seq_cnt != seq_count)
18271                 return 0;
18272         fctl = (hdr->fh_f_ctl[0] << 16 |
18273                 hdr->fh_f_ctl[1] << 8 |
18274                 hdr->fh_f_ctl[2]);
18275         /* If last frame of sequence we can return success. */
18276         if (fctl & FC_FC_END_SEQ)
18277                 return 1;
18278         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
18279                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18280                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18281                 /* If there is a hole in the sequence count then fail. */
18282                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
18283                         return 0;
18284                 fctl = (hdr->fh_f_ctl[0] << 16 |
18285                         hdr->fh_f_ctl[1] << 8 |
18286                         hdr->fh_f_ctl[2]);
18287                 /* If last frame of sequence we can return success. */
18288                 if (fctl & FC_FC_END_SEQ)
18289                         return 1;
18290         }
18291         return 0;
18292 }
18293
18294 /**
18295  * lpfc_prep_seq - Prep sequence for ULP processing
18296  * @vport: Pointer to the vport on which this sequence was received
18297  * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence
18298  *
18299  * This function takes a sequence, described by a list of frames, and creates
18300  * a list of iocbq structures to describe the sequence. This iocbq list will be
18301  * used to issue to the generic unsolicited sequence handler. This routine
18302  * returns a pointer to the first iocbq in the list. If the function is unable
18303  * to allocate an iocbq then it throw out the received frames that were not
18304  * able to be described and return a pointer to the first iocbq. If unable to
18305  * allocate any iocbqs (including the first) this function will return NULL.
18306  **/
18307 static struct lpfc_iocbq *
18308 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
18309 {
18310         struct hbq_dmabuf *hbq_buf;
18311         struct lpfc_dmabuf *d_buf, *n_buf;
18312         struct lpfc_iocbq *first_iocbq, *iocbq;
18313         struct fc_frame_header *fc_hdr;
18314         uint32_t sid;
18315         uint32_t len, tot_len;
18316         struct ulp_bde64 *pbde;
18317
18318         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18319         /* remove from receive buffer list */
18320         list_del_init(&seq_dmabuf->hbuf.list);
18321         lpfc_update_rcv_time_stamp(vport);
18322         /* get the Remote Port's SID */
18323         sid = sli4_sid_from_fc_hdr(fc_hdr);
18324         tot_len = 0;
18325         /* Get an iocbq struct to fill in. */
18326         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
18327         if (first_iocbq) {
18328                 /* Initialize the first IOCB. */
18329                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
18330                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
18331                 first_iocbq->vport = vport;
18332
18333                 /* Check FC Header to see what TYPE of frame we are rcv'ing */
18334                 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
18335                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
18336                         first_iocbq->iocb.un.rcvels.parmRo =
18337                                 sli4_did_from_fc_hdr(fc_hdr);
18338                         first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
18339                 } else
18340                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
18341                 first_iocbq->iocb.ulpContext = NO_XRI;
18342                 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
18343                         be16_to_cpu(fc_hdr->fh_ox_id);
18344                 /* iocbq is prepped for internal consumption.  Physical vpi. */
18345                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
18346                         vport->phba->vpi_ids[vport->vpi];
18347                 /* put the first buffer into the first IOCBq */
18348                 tot_len = bf_get(lpfc_rcqe_length,
18349                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
18350
18351                 first_iocbq->context2 = &seq_dmabuf->dbuf;
18352                 first_iocbq->context3 = NULL;
18353                 first_iocbq->iocb.ulpBdeCount = 1;
18354                 if (tot_len > LPFC_DATA_BUF_SIZE)
18355                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
18356                                                         LPFC_DATA_BUF_SIZE;
18357                 else
18358                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
18359
18360                 first_iocbq->iocb.un.rcvels.remoteID = sid;
18361
18362                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
18363         }
18364         iocbq = first_iocbq;
18365         /*
18366          * Each IOCBq can have two Buffers assigned, so go through the list
18367          * of buffers for this sequence and save two buffers in each IOCBq
18368          */
18369         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
18370                 if (!iocbq) {
18371                         lpfc_in_buf_free(vport->phba, d_buf);
18372                         continue;
18373                 }
18374                 if (!iocbq->context3) {
18375                         iocbq->context3 = d_buf;
18376                         iocbq->iocb.ulpBdeCount++;
18377                         /* We need to get the size out of the right CQE */
18378                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18379                         len = bf_get(lpfc_rcqe_length,
18380                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
18381                         pbde = (struct ulp_bde64 *)
18382                                         &iocbq->iocb.unsli3.sli3Words[4];
18383                         if (len > LPFC_DATA_BUF_SIZE)
18384                                 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
18385                         else
18386                                 pbde->tus.f.bdeSize = len;
18387
18388                         iocbq->iocb.unsli3.rcvsli3.acc_len += len;
18389                         tot_len += len;
18390                 } else {
18391                         iocbq = lpfc_sli_get_iocbq(vport->phba);
18392                         if (!iocbq) {
18393                                 if (first_iocbq) {
18394                                         first_iocbq->iocb.ulpStatus =
18395                                                         IOSTAT_FCP_RSP_ERROR;
18396                                         first_iocbq->iocb.un.ulpWord[4] =
18397                                                         IOERR_NO_RESOURCES;
18398                                 }
18399                                 lpfc_in_buf_free(vport->phba, d_buf);
18400                                 continue;
18401                         }
18402                         /* We need to get the size out of the right CQE */
18403                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18404                         len = bf_get(lpfc_rcqe_length,
18405                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
18406                         iocbq->context2 = d_buf;
18407                         iocbq->context3 = NULL;
18408                         iocbq->iocb.ulpBdeCount = 1;
18409                         if (len > LPFC_DATA_BUF_SIZE)
18410                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
18411                                                         LPFC_DATA_BUF_SIZE;
18412                         else
18413                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
18414
18415                         tot_len += len;
18416                         iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
18417
18418                         iocbq->iocb.un.rcvels.remoteID = sid;
18419                         list_add_tail(&iocbq->list, &first_iocbq->list);
18420                 }
18421         }
18422         /* Free the sequence's header buffer */
18423         if (!first_iocbq)
18424                 lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
18425
18426         return first_iocbq;
18427 }
18428
18429 static void
18430 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
18431                           struct hbq_dmabuf *seq_dmabuf)
18432 {
18433         struct fc_frame_header *fc_hdr;
18434         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
18435         struct lpfc_hba *phba = vport->phba;
18436
18437         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18438         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
18439         if (!iocbq) {
18440                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18441                                 "2707 Ring %d handler: Failed to allocate "
18442                                 "iocb Rctl x%x Type x%x received\n",
18443                                 LPFC_ELS_RING,
18444                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18445                 return;
18446         }
18447         if (!lpfc_complete_unsol_iocb(phba,
18448                                       phba->sli4_hba.els_wq->pring,
18449                                       iocbq, fc_hdr->fh_r_ctl,
18450                                       fc_hdr->fh_type))
18451                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18452                                 "2540 Ring %d handler: unexpected Rctl "
18453                                 "x%x Type x%x received\n",
18454                                 LPFC_ELS_RING,
18455                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18456
18457         /* Free iocb created in lpfc_prep_seq */
18458         list_for_each_entry_safe(curr_iocb, next_iocb,
18459                 &iocbq->list, list) {
18460                 list_del_init(&curr_iocb->list);
18461                 lpfc_sli_release_iocbq(phba, curr_iocb);
18462         }
18463         lpfc_sli_release_iocbq(phba, iocbq);
18464 }
18465
18466 static void
18467 lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
18468                             struct lpfc_iocbq *rspiocb)
18469 {
18470         struct lpfc_dmabuf *pcmd = cmdiocb->context2;
18471
18472         if (pcmd && pcmd->virt)
18473                 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
18474         kfree(pcmd);
18475         lpfc_sli_release_iocbq(phba, cmdiocb);
18476         lpfc_drain_txq(phba);
18477 }
18478
18479 static void
18480 lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
18481                               struct hbq_dmabuf *dmabuf)
18482 {
18483         struct fc_frame_header *fc_hdr;
18484         struct lpfc_hba *phba = vport->phba;
18485         struct lpfc_iocbq *iocbq = NULL;
18486         union  lpfc_wqe *wqe;
18487         struct lpfc_dmabuf *pcmd = NULL;
18488         uint32_t frame_len;
18489         int rc;
18490         unsigned long iflags;
18491
18492         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18493         frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
18494
18495         /* Send the received frame back */
18496         iocbq = lpfc_sli_get_iocbq(phba);
18497         if (!iocbq) {
18498                 /* Queue cq event and wakeup worker thread to process it */
18499                 spin_lock_irqsave(&phba->hbalock, iflags);
18500                 list_add_tail(&dmabuf->cq_event.list,
18501                               &phba->sli4_hba.sp_queue_event);
18502                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
18503                 spin_unlock_irqrestore(&phba->hbalock, iflags);
18504                 lpfc_worker_wake_up(phba);
18505                 return;
18506         }
18507
18508         /* Allocate buffer for command payload */
18509         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
18510         if (pcmd)
18511                 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
18512                                             &pcmd->phys);
18513         if (!pcmd || !pcmd->virt)
18514                 goto exit;
18515
18516         INIT_LIST_HEAD(&pcmd->list);
18517
18518         /* copyin the payload */
18519         memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
18520
18521         /* fill in BDE's for command */
18522         iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
18523         iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
18524         iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
18525         iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
18526
18527         iocbq->context2 = pcmd;
18528         iocbq->vport = vport;
18529         iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
18530         iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
18531
18532         /*
18533          * Setup rest of the iocb as though it were a WQE
18534          * Build the SEND_FRAME WQE
18535          */
18536         wqe = (union lpfc_wqe *)&iocbq->iocb;
18537
18538         wqe->send_frame.frame_len = frame_len;
18539         wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
18540         wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
18541         wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
18542         wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
18543         wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
18544         wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
18545
18546         iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
18547         iocbq->iocb.ulpLe = 1;
18548         iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
18549         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
18550         if (rc == IOCB_ERROR)
18551                 goto exit;
18552
18553         lpfc_in_buf_free(phba, &dmabuf->dbuf);
18554         return;
18555
18556 exit:
18557         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18558                         "2023 Unable to process MDS loopback frame\n");
18559         if (pcmd && pcmd->virt)
18560                 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
18561         kfree(pcmd);
18562         if (iocbq)
18563                 lpfc_sli_release_iocbq(phba, iocbq);
18564         lpfc_in_buf_free(phba, &dmabuf->dbuf);
18565 }
18566
18567 /**
18568  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
18569  * @phba: Pointer to HBA context object.
18570  * @dmabuf: Pointer to a dmabuf that describes the FC sequence.
18571  *
18572  * This function is called with no lock held. This function processes all
18573  * the received buffers and gives it to upper layers when a received buffer
18574  * indicates that it is the final frame in the sequence. The interrupt
18575  * service routine processes received buffers at interrupt contexts.
18576  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
18577  * appropriate receive function when the final frame in a sequence is received.
18578  **/
18579 void
18580 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
18581                                  struct hbq_dmabuf *dmabuf)
18582 {
18583         struct hbq_dmabuf *seq_dmabuf;
18584         struct fc_frame_header *fc_hdr;
18585         struct lpfc_vport *vport;
18586         uint32_t fcfi;
18587         uint32_t did;
18588
18589         /* Process each received buffer */
18590         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18591
18592         if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
18593             fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
18594                 vport = phba->pport;
18595                 /* Handle MDS Loopback frames */
18596                 if  (!(phba->pport->load_flag & FC_UNLOADING))
18597                         lpfc_sli4_handle_mds_loopback(vport, dmabuf);
18598                 else
18599                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
18600                 return;
18601         }
18602
18603         /* check to see if this a valid type of frame */
18604         if (lpfc_fc_frame_check(phba, fc_hdr)) {
18605                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18606                 return;
18607         }
18608
18609         if ((bf_get(lpfc_cqe_code,
18610                     &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
18611                 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
18612                               &dmabuf->cq_event.cqe.rcqe_cmpl);
18613         else
18614                 fcfi = bf_get(lpfc_rcqe_fcf_id,
18615                               &dmabuf->cq_event.cqe.rcqe_cmpl);
18616
18617         if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
18618                 vport = phba->pport;
18619                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18620                                 "2023 MDS Loopback %d bytes\n",
18621                                 bf_get(lpfc_rcqe_length,
18622                                        &dmabuf->cq_event.cqe.rcqe_cmpl));
18623                 /* Handle MDS Loopback frames */
18624                 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
18625                 return;
18626         }
18627
18628         /* d_id this frame is directed to */
18629         did = sli4_did_from_fc_hdr(fc_hdr);
18630
18631         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
18632         if (!vport) {
18633                 /* throw out the frame */
18634                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18635                 return;
18636         }
18637
18638         /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
18639         if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
18640                 (did != Fabric_DID)) {
18641                 /*
18642                  * Throw out the frame if we are not pt2pt.
18643                  * The pt2pt protocol allows for discovery frames
18644                  * to be received without a registered VPI.
18645                  */
18646                 if (!(vport->fc_flag & FC_PT2PT) ||
18647                         (phba->link_state == LPFC_HBA_READY)) {
18648                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
18649                         return;
18650                 }
18651         }
18652
18653         /* Handle the basic abort sequence (BA_ABTS) event */
18654         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
18655                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
18656                 return;
18657         }
18658
18659         /* Link this frame */
18660         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
18661         if (!seq_dmabuf) {
18662                 /* unable to add frame to vport - throw it out */
18663                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18664                 return;
18665         }
18666         /* If not last frame in sequence continue processing frames. */
18667         if (!lpfc_seq_complete(seq_dmabuf))
18668                 return;
18669
18670         /* Send the complete sequence to the upper layer protocol */
18671         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
18672 }
18673
18674 /**
18675  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
18676  * @phba: pointer to lpfc hba data structure.
18677  *
18678  * This routine is invoked to post rpi header templates to the
18679  * HBA consistent with the SLI-4 interface spec.  This routine
18680  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18681  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
18682  *
18683  * This routine does not require any locks.  It's usage is expected
18684  * to be driver load or reset recovery when the driver is
18685  * sequential.
18686  *
18687  * Return codes
18688  *      0 - successful
18689  *      -EIO - The mailbox failed to complete successfully.
18690  *      When this error occurs, the driver is not guaranteed
18691  *      to have any rpi regions posted to the device and
18692  *      must either attempt to repost the regions or take a
18693  *      fatal error.
18694  **/
18695 int
18696 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
18697 {
18698         struct lpfc_rpi_hdr *rpi_page;
18699         uint32_t rc = 0;
18700         uint16_t lrpi = 0;
18701
18702         /* SLI4 ports that support extents do not require RPI headers. */
18703         if (!phba->sli4_hba.rpi_hdrs_in_use)
18704                 goto exit;
18705         if (phba->sli4_hba.extents_in_use)
18706                 return -EIO;
18707
18708         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
18709                 /*
18710                  * Assign the rpi headers a physical rpi only if the driver
18711                  * has not initialized those resources.  A port reset only
18712                  * needs the headers posted.
18713                  */
18714                 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
18715                     LPFC_RPI_RSRC_RDY)
18716                         rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
18717
18718                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
18719                 if (rc != MBX_SUCCESS) {
18720                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18721                                         "2008 Error %d posting all rpi "
18722                                         "headers\n", rc);
18723                         rc = -EIO;
18724                         break;
18725                 }
18726         }
18727
18728  exit:
18729         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
18730                LPFC_RPI_RSRC_RDY);
18731         return rc;
18732 }
18733
18734 /**
18735  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
18736  * @phba: pointer to lpfc hba data structure.
18737  * @rpi_page:  pointer to the rpi memory region.
18738  *
18739  * This routine is invoked to post a single rpi header to the
18740  * HBA consistent with the SLI-4 interface spec.  This memory region
18741  * maps up to 64 rpi context regions.
18742  *
18743  * Return codes
18744  *      0 - successful
18745  *      -ENOMEM - No available memory
18746  *      -EIO - The mailbox failed to complete successfully.
18747  **/
18748 int
18749 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18750 {
18751         LPFC_MBOXQ_t *mboxq;
18752         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18753         uint32_t rc = 0;
18754         uint32_t shdr_status, shdr_add_status;
18755         union lpfc_sli4_cfg_shdr *shdr;
18756
18757         /* SLI4 ports that support extents do not require RPI headers. */
18758         if (!phba->sli4_hba.rpi_hdrs_in_use)
18759                 return rc;
18760         if (phba->sli4_hba.extents_in_use)
18761                 return -EIO;
18762
18763         /* The port is notified of the header region via a mailbox command. */
18764         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18765         if (!mboxq) {
18766                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18767                                 "2001 Unable to allocate memory for issuing "
18768                                 "SLI_CONFIG_SPECIAL mailbox command\n");
18769                 return -ENOMEM;
18770         }
18771
18772         /* Post all rpi memory regions to the port. */
18773         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
18774         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18775                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18776                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
18777                          sizeof(struct lpfc_sli4_cfg_mhdr),
18778                          LPFC_SLI4_MBX_EMBED);
18779
18780
18781         /* Post the physical rpi to the port for this rpi header. */
18782         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18783                rpi_page->start_rpi);
18784         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18785                hdr_tmpl, rpi_page->page_count);
18786
18787         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18788         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
18789         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18790         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18791         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18792         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18793         if (rc != MBX_TIMEOUT)
18794                 mempool_free(mboxq, phba->mbox_mem_pool);
18795         if (shdr_status || shdr_add_status || rc) {
18796                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18797                                 "2514 POST_RPI_HDR mailbox failed with "
18798                                 "status x%x add_status x%x, mbx status x%x\n",
18799                                 shdr_status, shdr_add_status, rc);
18800                 rc = -ENXIO;
18801         } else {
18802                 /*
18803                  * The next_rpi stores the next logical module-64 rpi value used
18804                  * to post physical rpis in subsequent rpi postings.
18805                  */
18806                 spin_lock_irq(&phba->hbalock);
18807                 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18808                 spin_unlock_irq(&phba->hbalock);
18809         }
18810         return rc;
18811 }
18812
18813 /**
18814  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18815  * @phba: pointer to lpfc hba data structure.
18816  *
18817  * This routine is invoked to post rpi header templates to the
18818  * HBA consistent with the SLI-4 interface spec.  This routine
18819  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18820  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
18821  *
18822  * Returns
18823  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
18824  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
18825  **/
18826 int
18827 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18828 {
18829         unsigned long rpi;
18830         uint16_t max_rpi, rpi_limit;
18831         uint16_t rpi_remaining, lrpi = 0;
18832         struct lpfc_rpi_hdr *rpi_hdr;
18833         unsigned long iflag;
18834
18835         /*
18836          * Fetch the next logical rpi.  Because this index is logical,
18837          * the  driver starts at 0 each time.
18838          */
18839         spin_lock_irqsave(&phba->hbalock, iflag);
18840         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18841         rpi_limit = phba->sli4_hba.next_rpi;
18842
18843         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18844         if (rpi >= rpi_limit)
18845                 rpi = LPFC_RPI_ALLOC_ERROR;
18846         else {
18847                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18848                 phba->sli4_hba.max_cfg_param.rpi_used++;
18849                 phba->sli4_hba.rpi_count++;
18850         }
18851         lpfc_printf_log(phba, KERN_INFO,
18852                         LOG_NODE | LOG_DISCOVERY,
18853                         "0001 Allocated rpi:x%x max:x%x lim:x%x\n",
18854                         (int) rpi, max_rpi, rpi_limit);
18855
18856         /*
18857          * Don't try to allocate more rpi header regions if the device limit
18858          * has been exhausted.
18859          */
18860         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18861             (phba->sli4_hba.rpi_count >= max_rpi)) {
18862                 spin_unlock_irqrestore(&phba->hbalock, iflag);
18863                 return rpi;
18864         }
18865
18866         /*
18867          * RPI header postings are not required for SLI4 ports capable of
18868          * extents.
18869          */
18870         if (!phba->sli4_hba.rpi_hdrs_in_use) {
18871                 spin_unlock_irqrestore(&phba->hbalock, iflag);
18872                 return rpi;
18873         }
18874
18875         /*
18876          * If the driver is running low on rpi resources, allocate another
18877          * page now.  Note that the next_rpi value is used because
18878          * it represents how many are actually in use whereas max_rpi notes
18879          * how many are supported max by the device.
18880          */
18881         rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
18882         spin_unlock_irqrestore(&phba->hbalock, iflag);
18883         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18884                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18885                 if (!rpi_hdr) {
18886                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18887                                         "2002 Error Could not grow rpi "
18888                                         "count\n");
18889                 } else {
18890                         lrpi = rpi_hdr->start_rpi;
18891                         rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
18892                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18893                 }
18894         }
18895
18896         return rpi;
18897 }
18898
18899 /**
18900  * lpfc_sli4_free_rpi - Release an rpi for reuse.
18901  * @phba: pointer to lpfc hba data structure.
18902  * @rpi: rpi to free
18903  *
18904  * This routine is invoked to release an rpi to the pool of
18905  * available rpis maintained by the driver.
18906  **/
18907 static void
18908 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18909 {
18910         /*
18911          * if the rpi value indicates a prior unreg has already
18912          * been done, skip the unreg.
18913          */
18914         if (rpi == LPFC_RPI_ALLOC_ERROR)
18915                 return;
18916
18917         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18918                 phba->sli4_hba.rpi_count--;
18919                 phba->sli4_hba.max_cfg_param.rpi_used--;
18920         } else {
18921                 lpfc_printf_log(phba, KERN_INFO,
18922                                 LOG_NODE | LOG_DISCOVERY,
18923                                 "2016 rpi %x not inuse\n",
18924                                 rpi);
18925         }
18926 }
18927
18928 /**
18929  * lpfc_sli4_free_rpi - Release an rpi for reuse.
18930  * @phba: pointer to lpfc hba data structure.
18931  * @rpi: rpi to free
18932  *
18933  * This routine is invoked to release an rpi to the pool of
18934  * available rpis maintained by the driver.
18935  **/
18936 void
18937 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18938 {
18939         spin_lock_irq(&phba->hbalock);
18940         __lpfc_sli4_free_rpi(phba, rpi);
18941         spin_unlock_irq(&phba->hbalock);
18942 }
18943
18944 /**
18945  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18946  * @phba: pointer to lpfc hba data structure.
18947  *
18948  * This routine is invoked to remove the memory region that
18949  * provided rpi via a bitmask.
18950  **/
18951 void
18952 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18953 {
18954         kfree(phba->sli4_hba.rpi_bmask);
18955         kfree(phba->sli4_hba.rpi_ids);
18956         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
18957 }
18958
18959 /**
18960  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18961  * @ndlp: pointer to lpfc nodelist data structure.
18962  * @cmpl: completion call-back.
18963  * @arg: data to load as MBox 'caller buffer information'
18964  *
18965  * This routine is invoked to remove the memory region that
18966  * provided rpi via a bitmask.
18967  **/
18968 int
18969 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18970         void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
18971 {
18972         LPFC_MBOXQ_t *mboxq;
18973         struct lpfc_hba *phba = ndlp->phba;
18974         int rc;
18975
18976         /* The port is notified of the header region via a mailbox command. */
18977         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18978         if (!mboxq)
18979                 return -ENOMEM;
18980
18981         /* Post all rpi memory regions to the port. */
18982         lpfc_resume_rpi(mboxq, ndlp);
18983         if (cmpl) {
18984                 mboxq->mbox_cmpl = cmpl;
18985                 mboxq->ctx_buf = arg;
18986                 mboxq->ctx_ndlp = ndlp;
18987         } else
18988                 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18989         mboxq->vport = ndlp->vport;
18990         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18991         if (rc == MBX_NOT_FINISHED) {
18992                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18993                                 "2010 Resume RPI Mailbox failed "
18994                                 "status %d, mbxStatus x%x\n", rc,
18995                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18996                 mempool_free(mboxq, phba->mbox_mem_pool);
18997                 return -EIO;
18998         }
18999         return 0;
19000 }
19001
19002 /**
19003  * lpfc_sli4_init_vpi - Initialize a vpi with the port
19004  * @vport: Pointer to the vport for which the vpi is being initialized
19005  *
19006  * This routine is invoked to activate a vpi with the port.
19007  *
19008  * Returns:
19009  *    0 success
19010  *    -Evalue otherwise
19011  **/
19012 int
19013 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
19014 {
19015         LPFC_MBOXQ_t *mboxq;
19016         int rc = 0;
19017         int retval = MBX_SUCCESS;
19018         uint32_t mbox_tmo;
19019         struct lpfc_hba *phba = vport->phba;
19020         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19021         if (!mboxq)
19022                 return -ENOMEM;
19023         lpfc_init_vpi(phba, mboxq, vport->vpi);
19024         mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
19025         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
19026         if (rc != MBX_SUCCESS) {
19027                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19028                                 "2022 INIT VPI Mailbox failed "
19029                                 "status %d, mbxStatus x%x\n", rc,
19030                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19031                 retval = -EIO;
19032         }
19033         if (rc != MBX_TIMEOUT)
19034                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
19035
19036         return retval;
19037 }
19038
19039 /**
19040  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
19041  * @phba: pointer to lpfc hba data structure.
19042  * @mboxq: Pointer to mailbox object.
19043  *
19044  * This routine is invoked to manually add a single FCF record. The caller
19045  * must pass a completely initialized FCF_Record.  This routine takes
19046  * care of the nonembedded mailbox operations.
19047  **/
19048 static void
19049 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
19050 {
19051         void *virt_addr;
19052         union lpfc_sli4_cfg_shdr *shdr;
19053         uint32_t shdr_status, shdr_add_status;
19054
19055         virt_addr = mboxq->sge_array->addr[0];
19056         /* The IOCTL status is embedded in the mailbox subheader. */
19057         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
19058         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19059         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19060
19061         if ((shdr_status || shdr_add_status) &&
19062                 (shdr_status != STATUS_FCF_IN_USE))
19063                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19064                         "2558 ADD_FCF_RECORD mailbox failed with "
19065                         "status x%x add_status x%x\n",
19066                         shdr_status, shdr_add_status);
19067
19068         lpfc_sli4_mbox_cmd_free(phba, mboxq);
19069 }
19070
19071 /**
19072  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
19073  * @phba: pointer to lpfc hba data structure.
19074  * @fcf_record:  pointer to the initialized fcf record to add.
19075  *
19076  * This routine is invoked to manually add a single FCF record. The caller
19077  * must pass a completely initialized FCF_Record.  This routine takes
19078  * care of the nonembedded mailbox operations.
19079  **/
19080 int
19081 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
19082 {
19083         int rc = 0;
19084         LPFC_MBOXQ_t *mboxq;
19085         uint8_t *bytep;
19086         void *virt_addr;
19087         struct lpfc_mbx_sge sge;
19088         uint32_t alloc_len, req_len;
19089         uint32_t fcfindex;
19090
19091         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19092         if (!mboxq) {
19093                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19094                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
19095                 return -ENOMEM;
19096         }
19097
19098         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
19099                   sizeof(uint32_t);
19100
19101         /* Allocate DMA memory and set up the non-embedded mailbox command */
19102         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19103                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
19104                                      req_len, LPFC_SLI4_MBX_NEMBED);
19105         if (alloc_len < req_len) {
19106                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19107                         "2523 Allocated DMA memory size (x%x) is "
19108                         "less than the requested DMA memory "
19109                         "size (x%x)\n", alloc_len, req_len);
19110                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19111                 return -ENOMEM;
19112         }
19113
19114         /*
19115          * Get the first SGE entry from the non-embedded DMA memory.  This
19116          * routine only uses a single SGE.
19117          */
19118         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
19119         virt_addr = mboxq->sge_array->addr[0];
19120         /*
19121          * Configure the FCF record for FCFI 0.  This is the driver's
19122          * hardcoded default and gets used in nonFIP mode.
19123          */
19124         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
19125         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
19126         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
19127
19128         /*
19129          * Copy the fcf_index and the FCF Record Data. The data starts after
19130          * the FCoE header plus word10. The data copy needs to be endian
19131          * correct.
19132          */
19133         bytep += sizeof(uint32_t);
19134         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
19135         mboxq->vport = phba->pport;
19136         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
19137         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19138         if (rc == MBX_NOT_FINISHED) {
19139                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19140                         "2515 ADD_FCF_RECORD mailbox failed with "
19141                         "status 0x%x\n", rc);
19142                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19143                 rc = -EIO;
19144         } else
19145                 rc = 0;
19146
19147         return rc;
19148 }
19149
19150 /**
19151  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
19152  * @phba: pointer to lpfc hba data structure.
19153  * @fcf_record:  pointer to the fcf record to write the default data.
19154  * @fcf_index: FCF table entry index.
19155  *
19156  * This routine is invoked to build the driver's default FCF record.  The
19157  * values used are hardcoded.  This routine handles memory initialization.
19158  *
19159  **/
19160 void
19161 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
19162                                 struct fcf_record *fcf_record,
19163                                 uint16_t fcf_index)
19164 {
19165         memset(fcf_record, 0, sizeof(struct fcf_record));
19166         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
19167         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
19168         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
19169         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
19170         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
19171         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
19172         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
19173         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
19174         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
19175         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
19176         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
19177         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
19178         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
19179         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
19180         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
19181         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
19182                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
19183         /* Set the VLAN bit map */
19184         if (phba->valid_vlan) {
19185                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
19186                         = 1 << (phba->vlan_id % 8);
19187         }
19188 }
19189
19190 /**
19191  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
19192  * @phba: pointer to lpfc hba data structure.
19193  * @fcf_index: FCF table entry offset.
19194  *
19195  * This routine is invoked to scan the entire FCF table by reading FCF
19196  * record and processing it one at a time starting from the @fcf_index
19197  * for initial FCF discovery or fast FCF failover rediscovery.
19198  *
19199  * Return 0 if the mailbox command is submitted successfully, none 0
19200  * otherwise.
19201  **/
19202 int
19203 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
19204 {
19205         int rc = 0, error;
19206         LPFC_MBOXQ_t *mboxq;
19207
19208         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
19209         phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
19210         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19211         if (!mboxq) {
19212                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19213                                 "2000 Failed to allocate mbox for "
19214                                 "READ_FCF cmd\n");
19215                 error = -ENOMEM;
19216                 goto fail_fcf_scan;
19217         }
19218         /* Construct the read FCF record mailbox command */
19219         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
19220         if (rc) {
19221                 error = -EINVAL;
19222                 goto fail_fcf_scan;
19223         }
19224         /* Issue the mailbox command asynchronously */
19225         mboxq->vport = phba->pport;
19226         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
19227
19228         spin_lock_irq(&phba->hbalock);
19229         phba->hba_flag |= FCF_TS_INPROG;
19230         spin_unlock_irq(&phba->hbalock);
19231
19232         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19233         if (rc == MBX_NOT_FINISHED)
19234                 error = -EIO;
19235         else {
19236                 /* Reset eligible FCF count for new scan */
19237                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
19238                         phba->fcf.eligible_fcf_cnt = 0;
19239                 error = 0;
19240         }
19241 fail_fcf_scan:
19242         if (error) {
19243                 if (mboxq)
19244                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
19245                 /* FCF scan failed, clear FCF_TS_INPROG flag */
19246                 spin_lock_irq(&phba->hbalock);
19247                 phba->hba_flag &= ~FCF_TS_INPROG;
19248                 spin_unlock_irq(&phba->hbalock);
19249         }
19250         return error;
19251 }
19252
19253 /**
19254  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
19255  * @phba: pointer to lpfc hba data structure.
19256  * @fcf_index: FCF table entry offset.
19257  *
19258  * This routine is invoked to read an FCF record indicated by @fcf_index
19259  * and to use it for FLOGI roundrobin FCF failover.
19260  *
19261  * Return 0 if the mailbox command is submitted successfully, none 0
19262  * otherwise.
19263  **/
19264 int
19265 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
19266 {
19267         int rc = 0, error;
19268         LPFC_MBOXQ_t *mboxq;
19269
19270         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19271         if (!mboxq) {
19272                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
19273                                 "2763 Failed to allocate mbox for "
19274                                 "READ_FCF cmd\n");
19275                 error = -ENOMEM;
19276                 goto fail_fcf_read;
19277         }
19278         /* Construct the read FCF record mailbox command */
19279         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
19280         if (rc) {
19281                 error = -EINVAL;
19282                 goto fail_fcf_read;
19283         }
19284         /* Issue the mailbox command asynchronously */
19285         mboxq->vport = phba->pport;
19286         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
19287         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19288         if (rc == MBX_NOT_FINISHED)
19289                 error = -EIO;
19290         else
19291                 error = 0;
19292
19293 fail_fcf_read:
19294         if (error && mboxq)
19295                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19296         return error;
19297 }
19298
19299 /**
19300  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
19301  * @phba: pointer to lpfc hba data structure.
19302  * @fcf_index: FCF table entry offset.
19303  *
19304  * This routine is invoked to read an FCF record indicated by @fcf_index to
19305  * determine whether it's eligible for FLOGI roundrobin failover list.
19306  *
19307  * Return 0 if the mailbox command is submitted successfully, none 0
19308  * otherwise.
19309  **/
19310 int
19311 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
19312 {
19313         int rc = 0, error;
19314         LPFC_MBOXQ_t *mboxq;
19315
19316         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19317         if (!mboxq) {
19318                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
19319                                 "2758 Failed to allocate mbox for "
19320                                 "READ_FCF cmd\n");
19321                                 error = -ENOMEM;
19322                                 goto fail_fcf_read;
19323         }
19324         /* Construct the read FCF record mailbox command */
19325         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
19326         if (rc) {
19327                 error = -EINVAL;
19328                 goto fail_fcf_read;
19329         }
19330         /* Issue the mailbox command asynchronously */
19331         mboxq->vport = phba->pport;
19332         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
19333         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19334         if (rc == MBX_NOT_FINISHED)
19335                 error = -EIO;
19336         else
19337                 error = 0;
19338
19339 fail_fcf_read:
19340         if (error && mboxq)
19341                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19342         return error;
19343 }
19344
19345 /**
19346  * lpfc_check_next_fcf_pri_level
19347  * @phba: pointer to the lpfc_hba struct for this port.
19348  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
19349  * routine when the rr_bmask is empty. The FCF indecies are put into the
19350  * rr_bmask based on their priority level. Starting from the highest priority
19351  * to the lowest. The most likely FCF candidate will be in the highest
19352  * priority group. When this routine is called it searches the fcf_pri list for
19353  * next lowest priority group and repopulates the rr_bmask with only those
19354  * fcf_indexes.
19355  * returns:
19356  * 1=success 0=failure
19357  **/
19358 static int
19359 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
19360 {
19361         uint16_t next_fcf_pri;
19362         uint16_t last_index;
19363         struct lpfc_fcf_pri *fcf_pri;
19364         int rc;
19365         int ret = 0;
19366
19367         last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
19368                         LPFC_SLI4_FCF_TBL_INDX_MAX);
19369         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19370                         "3060 Last IDX %d\n", last_index);
19371
19372         /* Verify the priority list has 2 or more entries */
19373         spin_lock_irq(&phba->hbalock);
19374         if (list_empty(&phba->fcf.fcf_pri_list) ||
19375             list_is_singular(&phba->fcf.fcf_pri_list)) {
19376                 spin_unlock_irq(&phba->hbalock);
19377                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
19378                         "3061 Last IDX %d\n", last_index);
19379                 return 0; /* Empty rr list */
19380         }
19381         spin_unlock_irq(&phba->hbalock);
19382
19383         next_fcf_pri = 0;
19384         /*
19385          * Clear the rr_bmask and set all of the bits that are at this
19386          * priority.
19387          */
19388         memset(phba->fcf.fcf_rr_bmask, 0,
19389                         sizeof(*phba->fcf.fcf_rr_bmask));
19390         spin_lock_irq(&phba->hbalock);
19391         list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
19392                 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
19393                         continue;
19394                 /*
19395                  * the 1st priority that has not FLOGI failed
19396                  * will be the highest.
19397                  */
19398                 if (!next_fcf_pri)
19399                         next_fcf_pri = fcf_pri->fcf_rec.priority;
19400                 spin_unlock_irq(&phba->hbalock);
19401                 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
19402                         rc = lpfc_sli4_fcf_rr_index_set(phba,
19403                                                 fcf_pri->fcf_rec.fcf_index);
19404                         if (rc)
19405                                 return 0;
19406                 }
19407                 spin_lock_irq(&phba->hbalock);
19408         }
19409         /*
19410          * if next_fcf_pri was not set above and the list is not empty then
19411          * we have failed flogis on all of them. So reset flogi failed
19412          * and start at the beginning.
19413          */
19414         if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
19415                 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
19416                         fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
19417                         /*
19418                          * the 1st priority that has not FLOGI failed
19419                          * will be the highest.
19420                          */
19421                         if (!next_fcf_pri)
19422                                 next_fcf_pri = fcf_pri->fcf_rec.priority;
19423                         spin_unlock_irq(&phba->hbalock);
19424                         if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
19425                                 rc = lpfc_sli4_fcf_rr_index_set(phba,
19426                                                 fcf_pri->fcf_rec.fcf_index);
19427                                 if (rc)
19428                                         return 0;
19429                         }
19430                         spin_lock_irq(&phba->hbalock);
19431                 }
19432         } else
19433                 ret = 1;
19434         spin_unlock_irq(&phba->hbalock);
19435
19436         return ret;
19437 }
19438 /**
19439  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
19440  * @phba: pointer to lpfc hba data structure.
19441  *
19442  * This routine is to get the next eligible FCF record index in a round
19443  * robin fashion. If the next eligible FCF record index equals to the
19444  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
19445  * shall be returned, otherwise, the next eligible FCF record's index
19446  * shall be returned.
19447  **/
19448 uint16_t
19449 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
19450 {
19451         uint16_t next_fcf_index;
19452
19453 initial_priority:
19454         /* Search start from next bit of currently registered FCF index */
19455         next_fcf_index = phba->fcf.current_rec.fcf_indx;
19456
19457 next_priority:
19458         /* Determine the next fcf index to check */
19459         next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
19460         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
19461                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
19462                                        next_fcf_index);
19463
19464         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
19465         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19466                 /*
19467                  * If we have wrapped then we need to clear the bits that
19468                  * have been tested so that we can detect when we should
19469                  * change the priority level.
19470                  */
19471                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
19472                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
19473         }
19474
19475
19476         /* Check roundrobin failover list empty condition */
19477         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
19478                 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
19479                 /*
19480                  * If next fcf index is not found check if there are lower
19481                  * Priority level fcf's in the fcf_priority list.
19482                  * Set up the rr_bmask with all of the avaiable fcf bits
19483                  * at that level and continue the selection process.
19484                  */
19485                 if (lpfc_check_next_fcf_pri_level(phba))
19486                         goto initial_priority;
19487                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
19488                                 "2844 No roundrobin failover FCF available\n");
19489
19490                 return LPFC_FCOE_FCF_NEXT_NONE;
19491         }
19492
19493         if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
19494                 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
19495                 LPFC_FCF_FLOGI_FAILED) {
19496                 if (list_is_singular(&phba->fcf.fcf_pri_list))
19497                         return LPFC_FCOE_FCF_NEXT_NONE;
19498
19499                 goto next_priority;
19500         }
19501
19502         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19503                         "2845 Get next roundrobin failover FCF (x%x)\n",
19504                         next_fcf_index);
19505
19506         return next_fcf_index;
19507 }
19508
19509 /**
19510  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
19511  * @phba: pointer to lpfc hba data structure.
19512  * @fcf_index: index into the FCF table to 'set'
19513  *
19514  * This routine sets the FCF record index in to the eligible bmask for
19515  * roundrobin failover search. It checks to make sure that the index
19516  * does not go beyond the range of the driver allocated bmask dimension
19517  * before setting the bit.
19518  *
19519  * Returns 0 if the index bit successfully set, otherwise, it returns
19520  * -EINVAL.
19521  **/
19522 int
19523 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
19524 {
19525         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19526                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
19527                                 "2610 FCF (x%x) reached driver's book "
19528                                 "keeping dimension:x%x\n",
19529                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
19530                 return -EINVAL;
19531         }
19532         /* Set the eligible FCF record index bmask */
19533         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
19534
19535         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19536                         "2790 Set FCF (x%x) to roundrobin FCF failover "
19537                         "bmask\n", fcf_index);
19538
19539         return 0;
19540 }
19541
19542 /**
19543  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
19544  * @phba: pointer to lpfc hba data structure.
19545  * @fcf_index: index into the FCF table to 'clear'
19546  *
19547  * This routine clears the FCF record index from the eligible bmask for
19548  * roundrobin failover search. It checks to make sure that the index
19549  * does not go beyond the range of the driver allocated bmask dimension
19550  * before clearing the bit.
19551  **/
19552 void
19553 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
19554 {
19555         struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
19556         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19557                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
19558                                 "2762 FCF (x%x) reached driver's book "
19559                                 "keeping dimension:x%x\n",
19560                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
19561                 return;
19562         }
19563         /* Clear the eligible FCF record index bmask */
19564         spin_lock_irq(&phba->hbalock);
19565         list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
19566                                  list) {
19567                 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
19568                         list_del_init(&fcf_pri->list);
19569                         break;
19570                 }
19571         }
19572         spin_unlock_irq(&phba->hbalock);
19573         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
19574
19575         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19576                         "2791 Clear FCF (x%x) from roundrobin failover "
19577                         "bmask\n", fcf_index);
19578 }
19579
19580 /**
19581  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
19582  * @phba: pointer to lpfc hba data structure.
19583  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
19584  *
19585  * This routine is the completion routine for the rediscover FCF table mailbox
19586  * command. If the mailbox command returned failure, it will try to stop the
19587  * FCF rediscover wait timer.
19588  **/
19589 static void
19590 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
19591 {
19592         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
19593         uint32_t shdr_status, shdr_add_status;
19594
19595         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
19596
19597         shdr_status = bf_get(lpfc_mbox_hdr_status,
19598                              &redisc_fcf->header.cfg_shdr.response);
19599         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19600                              &redisc_fcf->header.cfg_shdr.response);
19601         if (shdr_status || shdr_add_status) {
19602                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
19603                                 "2746 Requesting for FCF rediscovery failed "
19604                                 "status x%x add_status x%x\n",
19605                                 shdr_status, shdr_add_status);
19606                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
19607                         spin_lock_irq(&phba->hbalock);
19608                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
19609                         spin_unlock_irq(&phba->hbalock);
19610                         /*
19611                          * CVL event triggered FCF rediscover request failed,
19612                          * last resort to re-try current registered FCF entry.
19613                          */
19614                         lpfc_retry_pport_discovery(phba);
19615                 } else {
19616                         spin_lock_irq(&phba->hbalock);
19617                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
19618                         spin_unlock_irq(&phba->hbalock);
19619                         /*
19620                          * DEAD FCF event triggered FCF rediscover request
19621                          * failed, last resort to fail over as a link down
19622                          * to FCF registration.
19623                          */
19624                         lpfc_sli4_fcf_dead_failthrough(phba);
19625                 }
19626         } else {
19627                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19628                                 "2775 Start FCF rediscover quiescent timer\n");
19629                 /*
19630                  * Start FCF rediscovery wait timer for pending FCF
19631                  * before rescan FCF record table.
19632                  */
19633                 lpfc_fcf_redisc_wait_start_timer(phba);
19634         }
19635
19636         mempool_free(mbox, phba->mbox_mem_pool);
19637 }
19638
19639 /**
19640  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
19641  * @phba: pointer to lpfc hba data structure.
19642  *
19643  * This routine is invoked to request for rediscovery of the entire FCF table
19644  * by the port.
19645  **/
19646 int
19647 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
19648 {
19649         LPFC_MBOXQ_t *mbox;
19650         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
19651         int rc, length;
19652
19653         /* Cancel retry delay timers to all vports before FCF rediscover */
19654         lpfc_cancel_all_vport_retry_delay_timer(phba);
19655
19656         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19657         if (!mbox) {
19658                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19659                                 "2745 Failed to allocate mbox for "
19660                                 "requesting FCF rediscover.\n");
19661                 return -ENOMEM;
19662         }
19663
19664         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
19665                   sizeof(struct lpfc_sli4_cfg_mhdr));
19666         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
19667                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
19668                          length, LPFC_SLI4_MBX_EMBED);
19669
19670         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
19671         /* Set count to 0 for invalidating the entire FCF database */
19672         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
19673
19674         /* Issue the mailbox command asynchronously */
19675         mbox->vport = phba->pport;
19676         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
19677         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
19678
19679         if (rc == MBX_NOT_FINISHED) {
19680                 mempool_free(mbox, phba->mbox_mem_pool);
19681                 return -EIO;
19682         }
19683         return 0;
19684 }
19685
19686 /**
19687  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
19688  * @phba: pointer to lpfc hba data structure.
19689  *
19690  * This function is the failover routine as a last resort to the FCF DEAD
19691  * event when driver failed to perform fast FCF failover.
19692  **/
19693 void
19694 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
19695 {
19696         uint32_t link_state;
19697
19698         /*
19699          * Last resort as FCF DEAD event failover will treat this as
19700          * a link down, but save the link state because we don't want
19701          * it to be changed to Link Down unless it is already down.
19702          */
19703         link_state = phba->link_state;
19704         lpfc_linkdown(phba);
19705         phba->link_state = link_state;
19706
19707         /* Unregister FCF if no devices connected to it */
19708         lpfc_unregister_unused_fcf(phba);
19709 }
19710
19711 /**
19712  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
19713  * @phba: pointer to lpfc hba data structure.
19714  * @rgn23_data: pointer to configure region 23 data.
19715  *
19716  * This function gets SLI3 port configure region 23 data through memory dump
19717  * mailbox command. When it successfully retrieves data, the size of the data
19718  * will be returned, otherwise, 0 will be returned.
19719  **/
19720 static uint32_t
19721 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19722 {
19723         LPFC_MBOXQ_t *pmb = NULL;
19724         MAILBOX_t *mb;
19725         uint32_t offset = 0;
19726         int i, rc;
19727
19728         if (!rgn23_data)
19729                 return 0;
19730
19731         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19732         if (!pmb) {
19733                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19734                                 "2600 failed to allocate mailbox memory\n");
19735                 return 0;
19736         }
19737         mb = &pmb->u.mb;
19738
19739         do {
19740                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
19741                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
19742
19743                 if (rc != MBX_SUCCESS) {
19744                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19745                                         "2601 failed to read config "
19746                                         "region 23, rc 0x%x Status 0x%x\n",
19747                                         rc, mb->mbxStatus);
19748                         mb->un.varDmp.word_cnt = 0;
19749                 }
19750                 /*
19751                  * dump mem may return a zero when finished or we got a
19752                  * mailbox error, either way we are done.
19753                  */
19754                 if (mb->un.varDmp.word_cnt == 0)
19755                         break;
19756
19757                 i =  mb->un.varDmp.word_cnt * sizeof(uint32_t);
19758                 if (offset + i >  DMP_RGN23_SIZE)
19759                         i =  DMP_RGN23_SIZE - offset;
19760                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
19761                                       rgn23_data  + offset, i);
19762                 offset += i;
19763         } while (offset < DMP_RGN23_SIZE);
19764
19765         mempool_free(pmb, phba->mbox_mem_pool);
19766         return offset;
19767 }
19768
19769 /**
19770  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19771  * @phba: pointer to lpfc hba data structure.
19772  * @rgn23_data: pointer to configure region 23 data.
19773  *
19774  * This function gets SLI4 port configure region 23 data through memory dump
19775  * mailbox command. When it successfully retrieves data, the size of the data
19776  * will be returned, otherwise, 0 will be returned.
19777  **/
19778 static uint32_t
19779 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19780 {
19781         LPFC_MBOXQ_t *mboxq = NULL;
19782         struct lpfc_dmabuf *mp = NULL;
19783         struct lpfc_mqe *mqe;
19784         uint32_t data_length = 0;
19785         int rc;
19786
19787         if (!rgn23_data)
19788                 return 0;
19789
19790         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19791         if (!mboxq) {
19792                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19793                                 "3105 failed to allocate mailbox memory\n");
19794                 return 0;
19795         }
19796
19797         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19798                 goto out;
19799         mqe = &mboxq->u.mqe;
19800         mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
19801         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19802         if (rc)
19803                 goto out;
19804         data_length = mqe->un.mb_words[5];
19805         if (data_length == 0)
19806                 goto out;
19807         if (data_length > DMP_RGN23_SIZE) {
19808                 data_length = 0;
19809                 goto out;
19810         }
19811         lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19812 out:
19813         mempool_free(mboxq, phba->mbox_mem_pool);
19814         if (mp) {
19815                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19816                 kfree(mp);
19817         }
19818         return data_length;
19819 }
19820
19821 /**
19822  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19823  * @phba: pointer to lpfc hba data structure.
19824  *
19825  * This function read region 23 and parse TLV for port status to
19826  * decide if the user disaled the port. If the TLV indicates the
19827  * port is disabled, the hba_flag is set accordingly.
19828  **/
19829 void
19830 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19831 {
19832         uint8_t *rgn23_data = NULL;
19833         uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19834         uint32_t offset = 0;
19835
19836         /* Get adapter Region 23 data */
19837         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19838         if (!rgn23_data)
19839                 goto out;
19840
19841         if (phba->sli_rev < LPFC_SLI_REV4)
19842                 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19843         else {
19844                 if_type = bf_get(lpfc_sli_intf_if_type,
19845                                  &phba->sli4_hba.sli_intf);
19846                 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19847                         goto out;
19848                 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19849         }
19850
19851         if (!data_size)
19852                 goto out;
19853
19854         /* Check the region signature first */
19855         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19856                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19857                         "2619 Config region 23 has bad signature\n");
19858                         goto out;
19859         }
19860         offset += 4;
19861
19862         /* Check the data structure version */
19863         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19864                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19865                         "2620 Config region 23 has bad version\n");
19866                 goto out;
19867         }
19868         offset += 4;
19869
19870         /* Parse TLV entries in the region */
19871         while (offset < data_size) {
19872                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19873                         break;
19874                 /*
19875                  * If the TLV is not driver specific TLV or driver id is
19876                  * not linux driver id, skip the record.
19877                  */
19878                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19879                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19880                     (rgn23_data[offset + 3] != 0)) {
19881                         offset += rgn23_data[offset + 1] * 4 + 4;
19882                         continue;
19883                 }
19884
19885                 /* Driver found a driver specific TLV in the config region */
19886                 sub_tlv_len = rgn23_data[offset + 1] * 4;
19887                 offset += 4;
19888                 tlv_offset = 0;
19889
19890                 /*
19891                  * Search for configured port state sub-TLV.
19892                  */
19893                 while ((offset < data_size) &&
19894                         (tlv_offset < sub_tlv_len)) {
19895                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19896                                 offset += 4;
19897                                 tlv_offset += 4;
19898                                 break;
19899                         }
19900                         if (rgn23_data[offset] != PORT_STE_TYPE) {
19901                                 offset += rgn23_data[offset + 1] * 4 + 4;
19902                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19903                                 continue;
19904                         }
19905
19906                         /* This HBA contains PORT_STE configured */
19907                         if (!rgn23_data[offset + 2])
19908                                 phba->hba_flag |= LINK_DISABLED;
19909
19910                         goto out;
19911                 }
19912         }
19913
19914 out:
19915         kfree(rgn23_data);
19916         return;
19917 }
19918
19919 /**
19920  * lpfc_wr_object - write an object to the firmware
19921  * @phba: HBA structure that indicates port to create a queue on.
19922  * @dmabuf_list: list of dmabufs to write to the port.
19923  * @size: the total byte value of the objects to write to the port.
19924  * @offset: the current offset to be used to start the transfer.
19925  *
19926  * This routine will create a wr_object mailbox command to send to the port.
19927  * the mailbox command will be constructed using the dma buffers described in
19928  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19929  * BDEs that the imbedded mailbox can support. The @offset variable will be
19930  * used to indicate the starting offset of the transfer and will also return
19931  * the offset after the write object mailbox has completed. @size is used to
19932  * determine the end of the object and whether the eof bit should be set.
19933  *
19934  * Return 0 is successful and offset will contain the the new offset to use
19935  * for the next write.
19936  * Return negative value for error cases.
19937  **/
19938 int
19939 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19940                uint32_t size, uint32_t *offset)
19941 {
19942         struct lpfc_mbx_wr_object *wr_object;
19943         LPFC_MBOXQ_t *mbox;
19944         int rc = 0, i = 0;
19945         uint32_t shdr_status, shdr_add_status, shdr_change_status, shdr_csf;
19946         uint32_t mbox_tmo;
19947         struct lpfc_dmabuf *dmabuf;
19948         uint32_t written = 0;
19949         bool check_change_status = false;
19950
19951         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19952         if (!mbox)
19953                 return -ENOMEM;
19954
19955         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19956                         LPFC_MBOX_OPCODE_WRITE_OBJECT,
19957                         sizeof(struct lpfc_mbx_wr_object) -
19958                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19959
19960         wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19961         wr_object->u.request.write_offset = *offset;
19962         sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19963         wr_object->u.request.object_name[0] =
19964                 cpu_to_le32(wr_object->u.request.object_name[0]);
19965         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19966         list_for_each_entry(dmabuf, dmabuf_list, list) {
19967                 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19968                         break;
19969                 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19970                 wr_object->u.request.bde[i].addrHigh =
19971                         putPaddrHigh(dmabuf->phys);
19972                 if (written + SLI4_PAGE_SIZE >= size) {
19973                         wr_object->u.request.bde[i].tus.f.bdeSize =
19974                                 (size - written);
19975                         written += (size - written);
19976                         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
19977                         bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
19978                         check_change_status = true;
19979                 } else {
19980                         wr_object->u.request.bde[i].tus.f.bdeSize =
19981                                 SLI4_PAGE_SIZE;
19982                         written += SLI4_PAGE_SIZE;
19983                 }
19984                 i++;
19985         }
19986         wr_object->u.request.bde_count = i;
19987         bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19988         if (!phba->sli4_hba.intr_enable)
19989                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19990         else {
19991                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
19992                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19993         }
19994         /* The IOCTL status is embedded in the mailbox subheader. */
19995         shdr_status = bf_get(lpfc_mbox_hdr_status,
19996                              &wr_object->header.cfg_shdr.response);
19997         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19998                                  &wr_object->header.cfg_shdr.response);
19999         if (check_change_status) {
20000                 shdr_change_status = bf_get(lpfc_wr_object_change_status,
20001                                             &wr_object->u.response);
20002
20003                 if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
20004                     shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
20005                         shdr_csf = bf_get(lpfc_wr_object_csf,
20006                                           &wr_object->u.response);
20007                         if (shdr_csf)
20008                                 shdr_change_status =
20009                                                    LPFC_CHANGE_STATUS_PCI_RESET;
20010                 }
20011
20012                 switch (shdr_change_status) {
20013                 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
20014                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20015                                         "3198 Firmware write complete: System "
20016                                         "reboot required to instantiate\n");
20017                         break;
20018                 case (LPFC_CHANGE_STATUS_FW_RESET):
20019                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20020                                         "3199 Firmware write complete: Firmware"
20021                                         " reset required to instantiate\n");
20022                         break;
20023                 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
20024                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20025                                         "3200 Firmware write complete: Port "
20026                                         "Migration or PCI Reset required to "
20027                                         "instantiate\n");
20028                         break;
20029                 case (LPFC_CHANGE_STATUS_PCI_RESET):
20030                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20031                                         "3201 Firmware write complete: PCI "
20032                                         "Reset required to instantiate\n");
20033                         break;
20034                 default:
20035                         break;
20036                 }
20037         }
20038         if (rc != MBX_TIMEOUT)
20039                 mempool_free(mbox, phba->mbox_mem_pool);
20040         if (shdr_status || shdr_add_status || rc) {
20041                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20042                                 "3025 Write Object mailbox failed with "
20043                                 "status x%x add_status x%x, mbx status x%x\n",
20044                                 shdr_status, shdr_add_status, rc);
20045                 rc = -ENXIO;
20046                 *offset = shdr_add_status;
20047         } else
20048                 *offset += wr_object->u.response.actual_write_length;
20049         return rc;
20050 }
20051
20052 /**
20053  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
20054  * @vport: pointer to vport data structure.
20055  *
20056  * This function iterate through the mailboxq and clean up all REG_LOGIN
20057  * and REG_VPI mailbox commands associated with the vport. This function
20058  * is called when driver want to restart discovery of the vport due to
20059  * a Clear Virtual Link event.
20060  **/
20061 void
20062 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
20063 {
20064         struct lpfc_hba *phba = vport->phba;
20065         LPFC_MBOXQ_t *mb, *nextmb;
20066         struct lpfc_dmabuf *mp;
20067         struct lpfc_nodelist *ndlp;
20068         struct lpfc_nodelist *act_mbx_ndlp = NULL;
20069         LIST_HEAD(mbox_cmd_list);
20070         uint8_t restart_loop;
20071
20072         /* Clean up internally queued mailbox commands with the vport */
20073         spin_lock_irq(&phba->hbalock);
20074         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
20075                 if (mb->vport != vport)
20076                         continue;
20077
20078                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
20079                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
20080                         continue;
20081
20082                 list_del(&mb->list);
20083                 list_add_tail(&mb->list, &mbox_cmd_list);
20084         }
20085         /* Clean up active mailbox command with the vport */
20086         mb = phba->sli.mbox_active;
20087         if (mb && (mb->vport == vport)) {
20088                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
20089                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
20090                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
20091                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
20092                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
20093                         /* Put reference count for delayed processing */
20094                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
20095                         /* Unregister the RPI when mailbox complete */
20096                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
20097                 }
20098         }
20099         /* Cleanup any mailbox completions which are not yet processed */
20100         do {
20101                 restart_loop = 0;
20102                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
20103                         /*
20104                          * If this mailox is already processed or it is
20105                          * for another vport ignore it.
20106                          */
20107                         if ((mb->vport != vport) ||
20108                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
20109                                 continue;
20110
20111                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
20112                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
20113                                 continue;
20114
20115                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
20116                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
20117                                 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
20118                                 /* Unregister the RPI when mailbox complete */
20119                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
20120                                 restart_loop = 1;
20121                                 spin_unlock_irq(&phba->hbalock);
20122                                 spin_lock(&ndlp->lock);
20123                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
20124                                 spin_unlock(&ndlp->lock);
20125                                 spin_lock_irq(&phba->hbalock);
20126                                 break;
20127                         }
20128                 }
20129         } while (restart_loop);
20130
20131         spin_unlock_irq(&phba->hbalock);
20132
20133         /* Release the cleaned-up mailbox commands */
20134         while (!list_empty(&mbox_cmd_list)) {
20135                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
20136                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
20137                         mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
20138                         if (mp) {
20139                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
20140                                 kfree(mp);
20141                         }
20142                         mb->ctx_buf = NULL;
20143                         ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
20144                         mb->ctx_ndlp = NULL;
20145                         if (ndlp) {
20146                                 spin_lock(&ndlp->lock);
20147                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
20148                                 spin_unlock(&ndlp->lock);
20149                                 lpfc_nlp_put(ndlp);
20150                         }
20151                 }
20152                 mempool_free(mb, phba->mbox_mem_pool);
20153         }
20154
20155         /* Release the ndlp with the cleaned-up active mailbox command */
20156         if (act_mbx_ndlp) {
20157                 spin_lock(&act_mbx_ndlp->lock);
20158                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
20159                 spin_unlock(&act_mbx_ndlp->lock);
20160                 lpfc_nlp_put(act_mbx_ndlp);
20161         }
20162 }
20163
20164 /**
20165  * lpfc_drain_txq - Drain the txq
20166  * @phba: Pointer to HBA context object.
20167  *
20168  * This function attempt to submit IOCBs on the txq
20169  * to the adapter.  For SLI4 adapters, the txq contains
20170  * ELS IOCBs that have been deferred because the there
20171  * are no SGLs.  This congestion can occur with large
20172  * vport counts during node discovery.
20173  **/
20174
20175 uint32_t
20176 lpfc_drain_txq(struct lpfc_hba *phba)
20177 {
20178         LIST_HEAD(completions);
20179         struct lpfc_sli_ring *pring;
20180         struct lpfc_iocbq *piocbq = NULL;
20181         unsigned long iflags = 0;
20182         char *fail_msg = NULL;
20183         struct lpfc_sglq *sglq;
20184         union lpfc_wqe128 wqe;
20185         uint32_t txq_cnt = 0;
20186         struct lpfc_queue *wq;
20187
20188         if (phba->link_flag & LS_MDS_LOOPBACK) {
20189                 /* MDS WQE are posted only to first WQ*/
20190                 wq = phba->sli4_hba.hdwq[0].io_wq;
20191                 if (unlikely(!wq))
20192                         return 0;
20193                 pring = wq->pring;
20194         } else {
20195                 wq = phba->sli4_hba.els_wq;
20196                 if (unlikely(!wq))
20197                         return 0;
20198                 pring = lpfc_phba_elsring(phba);
20199         }
20200
20201         if (unlikely(!pring) || list_empty(&pring->txq))
20202                 return 0;
20203
20204         spin_lock_irqsave(&pring->ring_lock, iflags);
20205         list_for_each_entry(piocbq, &pring->txq, list) {
20206                 txq_cnt++;
20207         }
20208
20209         if (txq_cnt > pring->txq_max)
20210                 pring->txq_max = txq_cnt;
20211
20212         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20213
20214         while (!list_empty(&pring->txq)) {
20215                 spin_lock_irqsave(&pring->ring_lock, iflags);
20216
20217                 piocbq = lpfc_sli_ringtx_get(phba, pring);
20218                 if (!piocbq) {
20219                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20220                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20221                                 "2823 txq empty and txq_cnt is %d\n ",
20222                                 txq_cnt);
20223                         break;
20224                 }
20225                 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
20226                 if (!sglq) {
20227                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
20228                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20229                         break;
20230                 }
20231                 txq_cnt--;
20232
20233                 /* The xri and iocb resources secured,
20234                  * attempt to issue request
20235                  */
20236                 piocbq->sli4_lxritag = sglq->sli4_lxritag;
20237                 piocbq->sli4_xritag = sglq->sli4_xritag;
20238                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
20239                         fail_msg = "to convert bpl to sgl";
20240                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
20241                         fail_msg = "to convert iocb to wqe";
20242                 else if (lpfc_sli4_wq_put(wq, &wqe))
20243                         fail_msg = " - Wq is full";
20244                 else
20245                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
20246
20247                 if (fail_msg) {
20248                         /* Failed means we can't issue and need to cancel */
20249                         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20250                                         "2822 IOCB failed %s iotag 0x%x "
20251                                         "xri 0x%x\n",
20252                                         fail_msg,
20253                                         piocbq->iotag, piocbq->sli4_xritag);
20254                         list_add_tail(&piocbq->list, &completions);
20255                 }
20256                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20257         }
20258
20259         /* Cancel all the IOCBs that cannot be issued */
20260         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
20261                                 IOERR_SLI_ABORTED);
20262
20263         return txq_cnt;
20264 }
20265
20266 /**
20267  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
20268  * @phba: Pointer to HBA context object.
20269  * @pwqeq: Pointer to command WQE.
20270  * @sglq: Pointer to the scatter gather queue object.
20271  *
20272  * This routine converts the bpl or bde that is in the WQE
20273  * to a sgl list for the sli4 hardware. The physical address
20274  * of the bpl/bde is converted back to a virtual address.
20275  * If the WQE contains a BPL then the list of BDE's is
20276  * converted to sli4_sge's. If the WQE contains a single
20277  * BDE then it is converted to a single sli_sge.
20278  * The WQE is still in cpu endianness so the contents of
20279  * the bpl can be used without byte swapping.
20280  *
20281  * Returns valid XRI = Success, NO_XRI = Failure.
20282  */
20283 static uint16_t
20284 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
20285                  struct lpfc_sglq *sglq)
20286 {
20287         uint16_t xritag = NO_XRI;
20288         struct ulp_bde64 *bpl = NULL;
20289         struct ulp_bde64 bde;
20290         struct sli4_sge *sgl  = NULL;
20291         struct lpfc_dmabuf *dmabuf;
20292         union lpfc_wqe128 *wqe;
20293         int numBdes = 0;
20294         int i = 0;
20295         uint32_t offset = 0; /* accumulated offset in the sg request list */
20296         int inbound = 0; /* number of sg reply entries inbound from firmware */
20297         uint32_t cmd;
20298
20299         if (!pwqeq || !sglq)
20300                 return xritag;
20301
20302         sgl  = (struct sli4_sge *)sglq->sgl;
20303         wqe = &pwqeq->wqe;
20304         pwqeq->iocb.ulpIoTag = pwqeq->iotag;
20305
20306         cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
20307         if (cmd == CMD_XMIT_BLS_RSP64_WQE)
20308                 return sglq->sli4_xritag;
20309         numBdes = pwqeq->rsvd2;
20310         if (numBdes) {
20311                 /* The addrHigh and addrLow fields within the WQE
20312                  * have not been byteswapped yet so there is no
20313                  * need to swap them back.
20314                  */
20315                 if (pwqeq->context3)
20316                         dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
20317                 else
20318                         return xritag;
20319
20320                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
20321                 if (!bpl)
20322                         return xritag;
20323
20324                 for (i = 0; i < numBdes; i++) {
20325                         /* Should already be byte swapped. */
20326                         sgl->addr_hi = bpl->addrHigh;
20327                         sgl->addr_lo = bpl->addrLow;
20328
20329                         sgl->word2 = le32_to_cpu(sgl->word2);
20330                         if ((i+1) == numBdes)
20331                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
20332                         else
20333                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
20334                         /* swap the size field back to the cpu so we
20335                          * can assign it to the sgl.
20336                          */
20337                         bde.tus.w = le32_to_cpu(bpl->tus.w);
20338                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
20339                         /* The offsets in the sgl need to be accumulated
20340                          * separately for the request and reply lists.
20341                          * The request is always first, the reply follows.
20342                          */
20343                         switch (cmd) {
20344                         case CMD_GEN_REQUEST64_WQE:
20345                                 /* add up the reply sg entries */
20346                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
20347                                         inbound++;
20348                                 /* first inbound? reset the offset */
20349                                 if (inbound == 1)
20350                                         offset = 0;
20351                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
20352                                 bf_set(lpfc_sli4_sge_type, sgl,
20353                                         LPFC_SGE_TYPE_DATA);
20354                                 offset += bde.tus.f.bdeSize;
20355                                 break;
20356                         case CMD_FCP_TRSP64_WQE:
20357                                 bf_set(lpfc_sli4_sge_offset, sgl, 0);
20358                                 bf_set(lpfc_sli4_sge_type, sgl,
20359                                         LPFC_SGE_TYPE_DATA);
20360                                 break;
20361                         case CMD_FCP_TSEND64_WQE:
20362                         case CMD_FCP_TRECEIVE64_WQE:
20363                                 bf_set(lpfc_sli4_sge_type, sgl,
20364                                         bpl->tus.f.bdeFlags);
20365                                 if (i < 3)
20366                                         offset = 0;
20367                                 else
20368                                         offset += bde.tus.f.bdeSize;
20369                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
20370                                 break;
20371                         }
20372                         sgl->word2 = cpu_to_le32(sgl->word2);
20373                         bpl++;
20374                         sgl++;
20375                 }
20376         } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
20377                 /* The addrHigh and addrLow fields of the BDE have not
20378                  * been byteswapped yet so they need to be swapped
20379                  * before putting them in the sgl.
20380                  */
20381                 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
20382                 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
20383                 sgl->word2 = le32_to_cpu(sgl->word2);
20384                 bf_set(lpfc_sli4_sge_last, sgl, 1);
20385                 sgl->word2 = cpu_to_le32(sgl->word2);
20386                 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
20387         }
20388         return sglq->sli4_xritag;
20389 }
20390
20391 /**
20392  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
20393  * @phba: Pointer to HBA context object.
20394  * @qp: Pointer to HDW queue.
20395  * @pwqe: Pointer to command WQE.
20396  **/
20397 int
20398 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
20399                     struct lpfc_iocbq *pwqe)
20400 {
20401         union lpfc_wqe128 *wqe = &pwqe->wqe;
20402         struct lpfc_async_xchg_ctx *ctxp;
20403         struct lpfc_queue *wq;
20404         struct lpfc_sglq *sglq;
20405         struct lpfc_sli_ring *pring;
20406         unsigned long iflags;
20407         uint32_t ret = 0;
20408
20409         /* NVME_LS and NVME_LS ABTS requests. */
20410         if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
20411                 pring =  phba->sli4_hba.nvmels_wq->pring;
20412                 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20413                                           qp, wq_access);
20414                 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
20415                 if (!sglq) {
20416                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20417                         return WQE_BUSY;
20418                 }
20419                 pwqe->sli4_lxritag = sglq->sli4_lxritag;
20420                 pwqe->sli4_xritag = sglq->sli4_xritag;
20421                 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
20422                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20423                         return WQE_ERROR;
20424                 }
20425                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
20426                        pwqe->sli4_xritag);
20427                 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
20428                 if (ret) {
20429                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20430                         return ret;
20431                 }
20432
20433                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20434                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20435
20436                 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
20437                 return 0;
20438         }
20439
20440         /* NVME_FCREQ and NVME_ABTS requests */
20441         if (pwqe->iocb_flag & LPFC_IO_NVME ||
20442             pwqe->iocb_flag & LPFC_IO_FCP) {
20443                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
20444                 wq = qp->io_wq;
20445                 pring = wq->pring;
20446
20447                 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
20448
20449                 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20450                                           qp, wq_access);
20451                 ret = lpfc_sli4_wq_put(wq, wqe);
20452                 if (ret) {
20453                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20454                         return ret;
20455                 }
20456                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20457                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20458
20459                 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
20460                 return 0;
20461         }
20462
20463         /* NVMET requests */
20464         if (pwqe->iocb_flag & LPFC_IO_NVMET) {
20465                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
20466                 wq = qp->io_wq;
20467                 pring = wq->pring;
20468
20469                 ctxp = pwqe->context2;
20470                 sglq = ctxp->ctxbuf->sglq;
20471                 if (pwqe->sli4_xritag ==  NO_XRI) {
20472                         pwqe->sli4_lxritag = sglq->sli4_lxritag;
20473                         pwqe->sli4_xritag = sglq->sli4_xritag;
20474                 }
20475                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
20476                        pwqe->sli4_xritag);
20477                 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
20478
20479                 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20480                                           qp, wq_access);
20481                 ret = lpfc_sli4_wq_put(wq, wqe);
20482                 if (ret) {
20483                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
20484                         return ret;
20485                 }
20486                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20487                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20488
20489                 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
20490                 return 0;
20491         }
20492         return WQE_ERROR;
20493 }
20494
20495 /**
20496  * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort
20497  * @phba: Pointer to HBA context object.
20498  * @cmdiocb: Pointer to driver command iocb object.
20499  * @cmpl: completion function.
20500  *
20501  * Fill the appropriate fields for the abort WQE and call
20502  * internal routine lpfc_sli4_issue_wqe to send the WQE
20503  * This function is called with hbalock held and no ring_lock held.
20504  *
20505  * RETURNS 0 - SUCCESS
20506  **/
20507
20508 int
20509 lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
20510                             void *cmpl)
20511 {
20512         struct lpfc_vport *vport = cmdiocb->vport;
20513         struct lpfc_iocbq *abtsiocb = NULL;
20514         union lpfc_wqe128 *abtswqe;
20515         struct lpfc_io_buf *lpfc_cmd;
20516         int retval = IOCB_ERROR;
20517         u16 xritag = cmdiocb->sli4_xritag;
20518
20519         /*
20520          * The scsi command can not be in txq and it is in flight because the
20521          * pCmd is still pointing at the SCSI command we have to abort. There
20522          * is no need to search the txcmplq. Just send an abort to the FW.
20523          */
20524
20525         abtsiocb = __lpfc_sli_get_iocbq(phba);
20526         if (!abtsiocb)
20527                 return WQE_NORESOURCE;
20528
20529         /* Indicate the IO is being aborted by the driver. */
20530         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
20531
20532         abtswqe = &abtsiocb->wqe;
20533         memset(abtswqe, 0, sizeof(*abtswqe));
20534
20535         if (lpfc_is_link_up(phba))
20536                 bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);
20537         else
20538                 bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 0);
20539         bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);
20540         abtswqe->abort_cmd.rsrvd5 = 0;
20541         abtswqe->abort_cmd.wqe_com.abort_tag = xritag;
20542         bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);
20543         bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
20544         bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);
20545         bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);
20546         bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
20547         bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);
20548
20549         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
20550         abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;
20551         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
20552         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
20553                 abtsiocb->iocb_flag |= LPFC_IO_FCP;
20554         if (cmdiocb->iocb_flag & LPFC_IO_NVME)
20555                 abtsiocb->iocb_flag |= LPFC_IO_NVME;
20556         if (cmdiocb->iocb_flag & LPFC_IO_FOF)
20557                 abtsiocb->iocb_flag |= LPFC_IO_FOF;
20558         abtsiocb->vport = vport;
20559         abtsiocb->wqe_cmpl = cmpl;
20560
20561         lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);
20562         retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);
20563
20564         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
20565                          "0359 Abort xri x%x, original iotag x%x, "
20566                          "abort cmd iotag x%x retval x%x\n",
20567                          xritag, cmdiocb->iotag, abtsiocb->iotag, retval);
20568
20569         if (retval) {
20570                 cmdiocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
20571                 __lpfc_sli_release_iocbq(phba, abtsiocb);
20572         }
20573
20574         return retval;
20575 }
20576
20577 #ifdef LPFC_MXP_STAT
20578 /**
20579  * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
20580  * @phba: pointer to lpfc hba data structure.
20581  * @hwqid: belong to which HWQ.
20582  *
20583  * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
20584  * 15 seconds after a test case is running.
20585  *
20586  * The user should call lpfc_debugfs_multixripools_write before running a test
20587  * case to clear stat_snapshot_taken. Then the user starts a test case. During
20588  * test case is running, stat_snapshot_taken is incremented by 1 every time when
20589  * this routine is called from heartbeat timer. When stat_snapshot_taken is
20590  * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
20591  **/
20592 void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
20593 {
20594         struct lpfc_sli4_hdw_queue *qp;
20595         struct lpfc_multixri_pool *multixri_pool;
20596         struct lpfc_pvt_pool *pvt_pool;
20597         struct lpfc_pbl_pool *pbl_pool;
20598         u32 txcmplq_cnt;
20599
20600         qp = &phba->sli4_hba.hdwq[hwqid];
20601         multixri_pool = qp->p_multixri_pool;
20602         if (!multixri_pool)
20603                 return;
20604
20605         if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
20606                 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20607                 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20608                 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
20609
20610                 multixri_pool->stat_pbl_count = pbl_pool->count;
20611                 multixri_pool->stat_pvt_count = pvt_pool->count;
20612                 multixri_pool->stat_busy_count = txcmplq_cnt;
20613         }
20614
20615         multixri_pool->stat_snapshot_taken++;
20616 }
20617 #endif
20618
20619 /**
20620  * lpfc_adjust_pvt_pool_count - Adjust private pool count
20621  * @phba: pointer to lpfc hba data structure.
20622  * @hwqid: belong to which HWQ.
20623  *
20624  * This routine moves some XRIs from private to public pool when private pool
20625  * is not busy.
20626  **/
20627 void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
20628 {
20629         struct lpfc_multixri_pool *multixri_pool;
20630         u32 io_req_count;
20631         u32 prev_io_req_count;
20632
20633         multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20634         if (!multixri_pool)
20635                 return;
20636         io_req_count = multixri_pool->io_req_count;
20637         prev_io_req_count = multixri_pool->prev_io_req_count;
20638
20639         if (prev_io_req_count != io_req_count) {
20640                 /* Private pool is busy */
20641                 multixri_pool->prev_io_req_count = io_req_count;
20642         } else {
20643                 /* Private pool is not busy.
20644                  * Move XRIs from private to public pool.
20645                  */
20646                 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
20647         }
20648 }
20649
20650 /**
20651  * lpfc_adjust_high_watermark - Adjust high watermark
20652  * @phba: pointer to lpfc hba data structure.
20653  * @hwqid: belong to which HWQ.
20654  *
20655  * This routine sets high watermark as number of outstanding XRIs,
20656  * but make sure the new value is between xri_limit/2 and xri_limit.
20657  **/
20658 void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
20659 {
20660         u32 new_watermark;
20661         u32 watermark_max;
20662         u32 watermark_min;
20663         u32 xri_limit;
20664         u32 txcmplq_cnt;
20665         u32 abts_io_bufs;
20666         struct lpfc_multixri_pool *multixri_pool;
20667         struct lpfc_sli4_hdw_queue *qp;
20668
20669         qp = &phba->sli4_hba.hdwq[hwqid];
20670         multixri_pool = qp->p_multixri_pool;
20671         if (!multixri_pool)
20672                 return;
20673         xri_limit = multixri_pool->xri_limit;
20674
20675         watermark_max = xri_limit;
20676         watermark_min = xri_limit / 2;
20677
20678         txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
20679         abts_io_bufs = qp->abts_scsi_io_bufs;
20680         abts_io_bufs += qp->abts_nvme_io_bufs;
20681
20682         new_watermark = txcmplq_cnt + abts_io_bufs;
20683         new_watermark = min(watermark_max, new_watermark);
20684         new_watermark = max(watermark_min, new_watermark);
20685         multixri_pool->pvt_pool.high_watermark = new_watermark;
20686
20687 #ifdef LPFC_MXP_STAT
20688         multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
20689                                           new_watermark);
20690 #endif
20691 }
20692
20693 /**
20694  * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
20695  * @phba: pointer to lpfc hba data structure.
20696  * @hwqid: belong to which HWQ.
20697  *
20698  * This routine is called from hearbeat timer when pvt_pool is idle.
20699  * All free XRIs are moved from private to public pool on hwqid with 2 steps.
20700  * The first step moves (all - low_watermark) amount of XRIs.
20701  * The second step moves the rest of XRIs.
20702  **/
20703 void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
20704 {
20705         struct lpfc_pbl_pool *pbl_pool;
20706         struct lpfc_pvt_pool *pvt_pool;
20707         struct lpfc_sli4_hdw_queue *qp;
20708         struct lpfc_io_buf *lpfc_ncmd;
20709         struct lpfc_io_buf *lpfc_ncmd_next;
20710         unsigned long iflag;
20711         struct list_head tmp_list;
20712         u32 tmp_count;
20713
20714         qp = &phba->sli4_hba.hdwq[hwqid];
20715         pbl_pool = &qp->p_multixri_pool->pbl_pool;
20716         pvt_pool = &qp->p_multixri_pool->pvt_pool;
20717         tmp_count = 0;
20718
20719         lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
20720         lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
20721
20722         if (pvt_pool->count > pvt_pool->low_watermark) {
20723                 /* Step 1: move (all - low_watermark) from pvt_pool
20724                  * to pbl_pool
20725                  */
20726
20727                 /* Move low watermark of bufs from pvt_pool to tmp_list */
20728                 INIT_LIST_HEAD(&tmp_list);
20729                 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20730                                          &pvt_pool->list, list) {
20731                         list_move_tail(&lpfc_ncmd->list, &tmp_list);
20732                         tmp_count++;
20733                         if (tmp_count >= pvt_pool->low_watermark)
20734                                 break;
20735                 }
20736
20737                 /* Move all bufs from pvt_pool to pbl_pool */
20738                 list_splice_init(&pvt_pool->list, &pbl_pool->list);
20739
20740                 /* Move all bufs from tmp_list to pvt_pool */
20741                 list_splice(&tmp_list, &pvt_pool->list);
20742
20743                 pbl_pool->count += (pvt_pool->count - tmp_count);
20744                 pvt_pool->count = tmp_count;
20745         } else {
20746                 /* Step 2: move the rest from pvt_pool to pbl_pool */
20747                 list_splice_init(&pvt_pool->list, &pbl_pool->list);
20748                 pbl_pool->count += pvt_pool->count;
20749                 pvt_pool->count = 0;
20750         }
20751
20752         spin_unlock(&pvt_pool->lock);
20753         spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20754 }
20755
20756 /**
20757  * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
20758  * @phba: pointer to lpfc hba data structure
20759  * @qp: pointer to HDW queue
20760  * @pbl_pool: specified public free XRI pool
20761  * @pvt_pool: specified private free XRI pool
20762  * @count: number of XRIs to move
20763  *
20764  * This routine tries to move some free common bufs from the specified pbl_pool
20765  * to the specified pvt_pool. It might move less than count XRIs if there's not
20766  * enough in public pool.
20767  *
20768  * Return:
20769  *   true - if XRIs are successfully moved from the specified pbl_pool to the
20770  *          specified pvt_pool
20771  *   false - if the specified pbl_pool is empty or locked by someone else
20772  **/
20773 static bool
20774 _lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
20775                           struct lpfc_pbl_pool *pbl_pool,
20776                           struct lpfc_pvt_pool *pvt_pool, u32 count)
20777 {
20778         struct lpfc_io_buf *lpfc_ncmd;
20779         struct lpfc_io_buf *lpfc_ncmd_next;
20780         unsigned long iflag;
20781         int ret;
20782
20783         ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
20784         if (ret) {
20785                 if (pbl_pool->count) {
20786                         /* Move a batch of XRIs from public to private pool */
20787                         lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
20788                         list_for_each_entry_safe(lpfc_ncmd,
20789                                                  lpfc_ncmd_next,
20790                                                  &pbl_pool->list,
20791                                                  list) {
20792                                 list_move_tail(&lpfc_ncmd->list,
20793                                                &pvt_pool->list);
20794                                 pvt_pool->count++;
20795                                 pbl_pool->count--;
20796                                 count--;
20797                                 if (count == 0)
20798                                         break;
20799                         }
20800
20801                         spin_unlock(&pvt_pool->lock);
20802                         spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20803                         return true;
20804                 }
20805                 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20806         }
20807
20808         return false;
20809 }
20810
20811 /**
20812  * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
20813  * @phba: pointer to lpfc hba data structure.
20814  * @hwqid: belong to which HWQ.
20815  * @count: number of XRIs to move
20816  *
20817  * This routine tries to find some free common bufs in one of public pools with
20818  * Round Robin method. The search always starts from local hwqid, then the next
20819  * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
20820  * a batch of free common bufs are moved to private pool on hwqid.
20821  * It might move less than count XRIs if there's not enough in public pool.
20822  **/
20823 void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
20824 {
20825         struct lpfc_multixri_pool *multixri_pool;
20826         struct lpfc_multixri_pool *next_multixri_pool;
20827         struct lpfc_pvt_pool *pvt_pool;
20828         struct lpfc_pbl_pool *pbl_pool;
20829         struct lpfc_sli4_hdw_queue *qp;
20830         u32 next_hwqid;
20831         u32 hwq_count;
20832         int ret;
20833
20834         qp = &phba->sli4_hba.hdwq[hwqid];
20835         multixri_pool = qp->p_multixri_pool;
20836         pvt_pool = &multixri_pool->pvt_pool;
20837         pbl_pool = &multixri_pool->pbl_pool;
20838
20839         /* Check if local pbl_pool is available */
20840         ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
20841         if (ret) {
20842 #ifdef LPFC_MXP_STAT
20843                 multixri_pool->local_pbl_hit_count++;
20844 #endif
20845                 return;
20846         }
20847
20848         hwq_count = phba->cfg_hdw_queue;
20849
20850         /* Get the next hwqid which was found last time */
20851         next_hwqid = multixri_pool->rrb_next_hwqid;
20852
20853         do {
20854                 /* Go to next hwq */
20855                 next_hwqid = (next_hwqid + 1) % hwq_count;
20856
20857                 next_multixri_pool =
20858                         phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
20859                 pbl_pool = &next_multixri_pool->pbl_pool;
20860
20861                 /* Check if the public free xri pool is available */
20862                 ret = _lpfc_move_xri_pbl_to_pvt(
20863                         phba, qp, pbl_pool, pvt_pool, count);
20864
20865                 /* Exit while-loop if success or all hwqid are checked */
20866         } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
20867
20868         /* Starting point for the next time */
20869         multixri_pool->rrb_next_hwqid = next_hwqid;
20870
20871         if (!ret) {
20872                 /* stats: all public pools are empty*/
20873                 multixri_pool->pbl_empty_count++;
20874         }
20875
20876 #ifdef LPFC_MXP_STAT
20877         if (ret) {
20878                 if (next_hwqid == hwqid)
20879                         multixri_pool->local_pbl_hit_count++;
20880                 else
20881                         multixri_pool->other_pbl_hit_count++;
20882         }
20883 #endif
20884 }
20885
20886 /**
20887  * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
20888  * @phba: pointer to lpfc hba data structure.
20889  * @hwqid: belong to which HWQ.
20890  *
20891  * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
20892  * low watermark.
20893  **/
20894 void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
20895 {
20896         struct lpfc_multixri_pool *multixri_pool;
20897         struct lpfc_pvt_pool *pvt_pool;
20898
20899         multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20900         pvt_pool = &multixri_pool->pvt_pool;
20901
20902         if (pvt_pool->count < pvt_pool->low_watermark)
20903                 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20904 }
20905
20906 /**
20907  * lpfc_release_io_buf - Return one IO buf back to free pool
20908  * @phba: pointer to lpfc hba data structure.
20909  * @lpfc_ncmd: IO buf to be returned.
20910  * @qp: belong to which HWQ.
20911  *
20912  * This routine returns one IO buf back to free pool. If this is an urgent IO,
20913  * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
20914  * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
20915  * xri_limit.  If cfg_xri_rebalancing==0, the IO buf is returned to
20916  * lpfc_io_buf_list_put.
20917  **/
20918 void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
20919                          struct lpfc_sli4_hdw_queue *qp)
20920 {
20921         unsigned long iflag;
20922         struct lpfc_pbl_pool *pbl_pool;
20923         struct lpfc_pvt_pool *pvt_pool;
20924         struct lpfc_epd_pool *epd_pool;
20925         u32 txcmplq_cnt;
20926         u32 xri_owned;
20927         u32 xri_limit;
20928         u32 abts_io_bufs;
20929
20930         /* MUST zero fields if buffer is reused by another protocol */
20931         lpfc_ncmd->nvmeCmd = NULL;
20932         lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
20933         lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
20934
20935         if (phba->cfg_xpsgl && !phba->nvmet_support &&
20936             !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
20937                 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
20938
20939         if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
20940                 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
20941
20942         if (phba->cfg_xri_rebalancing) {
20943                 if (lpfc_ncmd->expedite) {
20944                         /* Return to expedite pool */
20945                         epd_pool = &phba->epd_pool;
20946                         spin_lock_irqsave(&epd_pool->lock, iflag);
20947                         list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
20948                         epd_pool->count++;
20949                         spin_unlock_irqrestore(&epd_pool->lock, iflag);
20950                         return;
20951                 }
20952
20953                 /* Avoid invalid access if an IO sneaks in and is being rejected
20954                  * just _after_ xri pools are destroyed in lpfc_offline.
20955                  * Nothing much can be done at this point.
20956                  */
20957                 if (!qp->p_multixri_pool)
20958                         return;
20959
20960                 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20961                 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20962
20963                 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
20964                 abts_io_bufs = qp->abts_scsi_io_bufs;
20965                 abts_io_bufs += qp->abts_nvme_io_bufs;
20966
20967                 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
20968                 xri_limit = qp->p_multixri_pool->xri_limit;
20969
20970 #ifdef LPFC_MXP_STAT
20971                 if (xri_owned <= xri_limit)
20972                         qp->p_multixri_pool->below_limit_count++;
20973                 else
20974                         qp->p_multixri_pool->above_limit_count++;
20975 #endif
20976
20977                 /* XRI goes to either public or private free xri pool
20978                  *     based on watermark and xri_limit
20979                  */
20980                 if ((pvt_pool->count < pvt_pool->low_watermark) ||
20981                     (xri_owned < xri_limit &&
20982                      pvt_pool->count < pvt_pool->high_watermark)) {
20983                         lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
20984                                                   qp, free_pvt_pool);
20985                         list_add_tail(&lpfc_ncmd->list,
20986                                       &pvt_pool->list);
20987                         pvt_pool->count++;
20988                         spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20989                 } else {
20990                         lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
20991                                                   qp, free_pub_pool);
20992                         list_add_tail(&lpfc_ncmd->list,
20993                                       &pbl_pool->list);
20994                         pbl_pool->count++;
20995                         spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20996                 }
20997         } else {
20998                 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
20999                                           qp, free_xri);
21000                 list_add_tail(&lpfc_ncmd->list,
21001                               &qp->lpfc_io_buf_list_put);
21002                 qp->put_io_bufs++;
21003                 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
21004                                        iflag);
21005         }
21006 }
21007
21008 /**
21009  * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
21010  * @phba: pointer to lpfc hba data structure.
21011  * @qp: pointer to HDW queue
21012  * @pvt_pool: pointer to private pool data structure.
21013  * @ndlp: pointer to lpfc nodelist data structure.
21014  *
21015  * This routine tries to get one free IO buf from private pool.
21016  *
21017  * Return:
21018  *   pointer to one free IO buf - if private pool is not empty
21019  *   NULL - if private pool is empty
21020  **/
21021 static struct lpfc_io_buf *
21022 lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
21023                                   struct lpfc_sli4_hdw_queue *qp,
21024                                   struct lpfc_pvt_pool *pvt_pool,
21025                                   struct lpfc_nodelist *ndlp)
21026 {
21027         struct lpfc_io_buf *lpfc_ncmd;
21028         struct lpfc_io_buf *lpfc_ncmd_next;
21029         unsigned long iflag;
21030
21031         lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
21032         list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21033                                  &pvt_pool->list, list) {
21034                 if (lpfc_test_rrq_active(
21035                         phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
21036                         continue;
21037                 list_del(&lpfc_ncmd->list);
21038                 pvt_pool->count--;
21039                 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21040                 return lpfc_ncmd;
21041         }
21042         spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21043
21044         return NULL;
21045 }
21046
21047 /**
21048  * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
21049  * @phba: pointer to lpfc hba data structure.
21050  *
21051  * This routine tries to get one free IO buf from expedite pool.
21052  *
21053  * Return:
21054  *   pointer to one free IO buf - if expedite pool is not empty
21055  *   NULL - if expedite pool is empty
21056  **/
21057 static struct lpfc_io_buf *
21058 lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
21059 {
21060         struct lpfc_io_buf *lpfc_ncmd;
21061         struct lpfc_io_buf *lpfc_ncmd_next;
21062         unsigned long iflag;
21063         struct lpfc_epd_pool *epd_pool;
21064
21065         epd_pool = &phba->epd_pool;
21066         lpfc_ncmd = NULL;
21067
21068         spin_lock_irqsave(&epd_pool->lock, iflag);
21069         if (epd_pool->count > 0) {
21070                 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21071                                          &epd_pool->list, list) {
21072                         list_del(&lpfc_ncmd->list);
21073                         epd_pool->count--;
21074                         break;
21075                 }
21076         }
21077         spin_unlock_irqrestore(&epd_pool->lock, iflag);
21078
21079         return lpfc_ncmd;
21080 }
21081
21082 /**
21083  * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
21084  * @phba: pointer to lpfc hba data structure.
21085  * @ndlp: pointer to lpfc nodelist data structure.
21086  * @hwqid: belong to which HWQ
21087  * @expedite: 1 means this request is urgent.
21088  *
21089  * This routine will do the following actions and then return a pointer to
21090  * one free IO buf.
21091  *
21092  * 1. If private free xri count is empty, move some XRIs from public to
21093  *    private pool.
21094  * 2. Get one XRI from private free xri pool.
21095  * 3. If we fail to get one from pvt_pool and this is an expedite request,
21096  *    get one free xri from expedite pool.
21097  *
21098  * Note: ndlp is only used on SCSI side for RRQ testing.
21099  *       The caller should pass NULL for ndlp on NVME side.
21100  *
21101  * Return:
21102  *   pointer to one free IO buf - if private pool is not empty
21103  *   NULL - if private pool is empty
21104  **/
21105 static struct lpfc_io_buf *
21106 lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
21107                                     struct lpfc_nodelist *ndlp,
21108                                     int hwqid, int expedite)
21109 {
21110         struct lpfc_sli4_hdw_queue *qp;
21111         struct lpfc_multixri_pool *multixri_pool;
21112         struct lpfc_pvt_pool *pvt_pool;
21113         struct lpfc_io_buf *lpfc_ncmd;
21114
21115         qp = &phba->sli4_hba.hdwq[hwqid];
21116         lpfc_ncmd = NULL;
21117         multixri_pool = qp->p_multixri_pool;
21118         pvt_pool = &multixri_pool->pvt_pool;
21119         multixri_pool->io_req_count++;
21120
21121         /* If pvt_pool is empty, move some XRIs from public to private pool */
21122         if (pvt_pool->count == 0)
21123                 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21124
21125         /* Get one XRI from private free xri pool */
21126         lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
21127
21128         if (lpfc_ncmd) {
21129                 lpfc_ncmd->hdwq = qp;
21130                 lpfc_ncmd->hdwq_no = hwqid;
21131         } else if (expedite) {
21132                 /* If we fail to get one from pvt_pool and this is an expedite
21133                  * request, get one free xri from expedite pool.
21134                  */
21135                 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
21136         }
21137
21138         return lpfc_ncmd;
21139 }
21140
21141 static inline struct lpfc_io_buf *
21142 lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
21143 {
21144         struct lpfc_sli4_hdw_queue *qp;
21145         struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
21146
21147         qp = &phba->sli4_hba.hdwq[idx];
21148         list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
21149                                  &qp->lpfc_io_buf_list_get, list) {
21150                 if (lpfc_test_rrq_active(phba, ndlp,
21151                                          lpfc_cmd->cur_iocbq.sli4_lxritag))
21152                         continue;
21153
21154                 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
21155                         continue;
21156
21157                 list_del_init(&lpfc_cmd->list);
21158                 qp->get_io_bufs--;
21159                 lpfc_cmd->hdwq = qp;
21160                 lpfc_cmd->hdwq_no = idx;
21161                 return lpfc_cmd;
21162         }
21163         return NULL;
21164 }
21165
21166 /**
21167  * lpfc_get_io_buf - Get one IO buffer from free pool
21168  * @phba: The HBA for which this call is being executed.
21169  * @ndlp: pointer to lpfc nodelist data structure.
21170  * @hwqid: belong to which HWQ
21171  * @expedite: 1 means this request is urgent.
21172  *
21173  * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
21174  * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
21175  * a IO buffer from head of @hdwq io_buf_list and returns to caller.
21176  *
21177  * Note: ndlp is only used on SCSI side for RRQ testing.
21178  *       The caller should pass NULL for ndlp on NVME side.
21179  *
21180  * Return codes:
21181  *   NULL - Error
21182  *   Pointer to lpfc_io_buf - Success
21183  **/
21184 struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
21185                                     struct lpfc_nodelist *ndlp,
21186                                     u32 hwqid, int expedite)
21187 {
21188         struct lpfc_sli4_hdw_queue *qp;
21189         unsigned long iflag;
21190         struct lpfc_io_buf *lpfc_cmd;
21191
21192         qp = &phba->sli4_hba.hdwq[hwqid];
21193         lpfc_cmd = NULL;
21194
21195         if (phba->cfg_xri_rebalancing)
21196                 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
21197                         phba, ndlp, hwqid, expedite);
21198         else {
21199                 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
21200                                           qp, alloc_xri_get);
21201                 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
21202                         lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
21203                 if (!lpfc_cmd) {
21204                         lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
21205                                           qp, alloc_xri_put);
21206                         list_splice(&qp->lpfc_io_buf_list_put,
21207                                     &qp->lpfc_io_buf_list_get);
21208                         qp->get_io_bufs += qp->put_io_bufs;
21209                         INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
21210                         qp->put_io_bufs = 0;
21211                         spin_unlock(&qp->io_buf_list_put_lock);
21212                         if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
21213                             expedite)
21214                                 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
21215                 }
21216                 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
21217         }
21218
21219         return lpfc_cmd;
21220 }
21221
21222 /**
21223  * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
21224  * @phba: The HBA for which this call is being executed.
21225  * @lpfc_buf: IO buf structure to append the SGL chunk
21226  *
21227  * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
21228  * and will allocate an SGL chunk if the pool is empty.
21229  *
21230  * Return codes:
21231  *   NULL - Error
21232  *   Pointer to sli4_hybrid_sgl - Success
21233  **/
21234 struct sli4_hybrid_sgl *
21235 lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
21236 {
21237         struct sli4_hybrid_sgl *list_entry = NULL;
21238         struct sli4_hybrid_sgl *tmp = NULL;
21239         struct sli4_hybrid_sgl *allocated_sgl = NULL;
21240         struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21241         struct list_head *buf_list = &hdwq->sgl_list;
21242         unsigned long iflags;
21243
21244         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21245
21246         if (likely(!list_empty(buf_list))) {
21247                 /* break off 1 chunk from the sgl_list */
21248                 list_for_each_entry_safe(list_entry, tmp,
21249                                          buf_list, list_node) {
21250                         list_move_tail(&list_entry->list_node,
21251                                        &lpfc_buf->dma_sgl_xtra_list);
21252                         break;
21253                 }
21254         } else {
21255                 /* allocate more */
21256                 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21257                 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
21258                                    cpu_to_node(hdwq->io_wq->chann));
21259                 if (!tmp) {
21260                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21261                                         "8353 error kmalloc memory for HDWQ "
21262                                         "%d %s\n",
21263                                         lpfc_buf->hdwq_no, __func__);
21264                         return NULL;
21265                 }
21266
21267                 tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
21268                                               GFP_ATOMIC, &tmp->dma_phys_sgl);
21269                 if (!tmp->dma_sgl) {
21270                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21271                                         "8354 error pool_alloc memory for HDWQ "
21272                                         "%d %s\n",
21273                                         lpfc_buf->hdwq_no, __func__);
21274                         kfree(tmp);
21275                         return NULL;
21276                 }
21277
21278                 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21279                 list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
21280         }
21281
21282         allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
21283                                         struct sli4_hybrid_sgl,
21284                                         list_node);
21285
21286         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21287
21288         return allocated_sgl;
21289 }
21290
21291 /**
21292  * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
21293  * @phba: The HBA for which this call is being executed.
21294  * @lpfc_buf: IO buf structure with the SGL chunk
21295  *
21296  * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
21297  *
21298  * Return codes:
21299  *   0 - Success
21300  *   -EINVAL - Error
21301  **/
21302 int
21303 lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
21304 {
21305         int rc = 0;
21306         struct sli4_hybrid_sgl *list_entry = NULL;
21307         struct sli4_hybrid_sgl *tmp = NULL;
21308         struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21309         struct list_head *buf_list = &hdwq->sgl_list;
21310         unsigned long iflags;
21311
21312         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21313
21314         if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
21315                 list_for_each_entry_safe(list_entry, tmp,
21316                                          &lpfc_buf->dma_sgl_xtra_list,
21317                                          list_node) {
21318                         list_move_tail(&list_entry->list_node,
21319                                        buf_list);
21320                 }
21321         } else {
21322                 rc = -EINVAL;
21323         }
21324
21325         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21326         return rc;
21327 }
21328
21329 /**
21330  * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
21331  * @phba: phba object
21332  * @hdwq: hdwq to cleanup sgl buff resources on
21333  *
21334  * This routine frees all SGL chunks of hdwq SGL chunk pool.
21335  *
21336  * Return codes:
21337  *   None
21338  **/
21339 void
21340 lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
21341                        struct lpfc_sli4_hdw_queue *hdwq)
21342 {
21343         struct list_head *buf_list = &hdwq->sgl_list;
21344         struct sli4_hybrid_sgl *list_entry = NULL;
21345         struct sli4_hybrid_sgl *tmp = NULL;
21346         unsigned long iflags;
21347
21348         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21349
21350         /* Free sgl pool */
21351         list_for_each_entry_safe(list_entry, tmp,
21352                                  buf_list, list_node) {
21353                 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
21354                               list_entry->dma_sgl,
21355                               list_entry->dma_phys_sgl);
21356                 list_del(&list_entry->list_node);
21357                 kfree(list_entry);
21358         }
21359
21360         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21361 }
21362
21363 /**
21364  * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
21365  * @phba: The HBA for which this call is being executed.
21366  * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
21367  *
21368  * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
21369  * and will allocate an CMD/RSP buffer if the pool is empty.
21370  *
21371  * Return codes:
21372  *   NULL - Error
21373  *   Pointer to fcp_cmd_rsp_buf - Success
21374  **/
21375 struct fcp_cmd_rsp_buf *
21376 lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21377                               struct lpfc_io_buf *lpfc_buf)
21378 {
21379         struct fcp_cmd_rsp_buf *list_entry = NULL;
21380         struct fcp_cmd_rsp_buf *tmp = NULL;
21381         struct fcp_cmd_rsp_buf *allocated_buf = NULL;
21382         struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21383         struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
21384         unsigned long iflags;
21385
21386         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21387
21388         if (likely(!list_empty(buf_list))) {
21389                 /* break off 1 chunk from the list */
21390                 list_for_each_entry_safe(list_entry, tmp,
21391                                          buf_list,
21392                                          list_node) {
21393                         list_move_tail(&list_entry->list_node,
21394                                        &lpfc_buf->dma_cmd_rsp_list);
21395                         break;
21396                 }
21397         } else {
21398                 /* allocate more */
21399                 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21400                 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
21401                                    cpu_to_node(hdwq->io_wq->chann));
21402                 if (!tmp) {
21403                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21404                                         "8355 error kmalloc memory for HDWQ "
21405                                         "%d %s\n",
21406                                         lpfc_buf->hdwq_no, __func__);
21407                         return NULL;
21408                 }
21409
21410                 tmp->fcp_cmnd = dma_pool_alloc(phba->lpfc_cmd_rsp_buf_pool,
21411                                                 GFP_ATOMIC,
21412                                                 &tmp->fcp_cmd_rsp_dma_handle);
21413
21414                 if (!tmp->fcp_cmnd) {
21415                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21416                                         "8356 error pool_alloc memory for HDWQ "
21417                                         "%d %s\n",
21418                                         lpfc_buf->hdwq_no, __func__);
21419                         kfree(tmp);
21420                         return NULL;
21421                 }
21422
21423                 tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
21424                                 sizeof(struct fcp_cmnd));
21425
21426                 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21427                 list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
21428         }
21429
21430         allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
21431                                         struct fcp_cmd_rsp_buf,
21432                                         list_node);
21433
21434         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21435
21436         return allocated_buf;
21437 }
21438
21439 /**
21440  * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
21441  * @phba: The HBA for which this call is being executed.
21442  * @lpfc_buf: IO buf structure with the CMD/RSP buf
21443  *
21444  * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
21445  *
21446  * Return codes:
21447  *   0 - Success
21448  *   -EINVAL - Error
21449  **/
21450 int
21451 lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21452                               struct lpfc_io_buf *lpfc_buf)
21453 {
21454         int rc = 0;
21455         struct fcp_cmd_rsp_buf *list_entry = NULL;
21456         struct fcp_cmd_rsp_buf *tmp = NULL;
21457         struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21458         struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
21459         unsigned long iflags;
21460
21461         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21462
21463         if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
21464                 list_for_each_entry_safe(list_entry, tmp,
21465                                          &lpfc_buf->dma_cmd_rsp_list,
21466                                          list_node) {
21467                         list_move_tail(&list_entry->list_node,
21468                                        buf_list);
21469                 }
21470         } else {
21471                 rc = -EINVAL;
21472         }
21473
21474         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21475         return rc;
21476 }
21477
21478 /**
21479  * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
21480  * @phba: phba object
21481  * @hdwq: hdwq to cleanup cmd rsp buff resources on
21482  *
21483  * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
21484  *
21485  * Return codes:
21486  *   None
21487  **/
21488 void
21489 lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21490                                struct lpfc_sli4_hdw_queue *hdwq)
21491 {
21492         struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
21493         struct fcp_cmd_rsp_buf *list_entry = NULL;
21494         struct fcp_cmd_rsp_buf *tmp = NULL;
21495         unsigned long iflags;
21496
21497         spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
21498
21499         /* Free cmd_rsp buf pool */
21500         list_for_each_entry_safe(list_entry, tmp,
21501                                  buf_list,
21502                                  list_node) {
21503                 dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
21504                               list_entry->fcp_cmnd,
21505                               list_entry->fcp_cmd_rsp_dma_handle);
21506                 list_del(&list_entry->list_node);
21507                 kfree(list_entry);
21508         }
21509
21510         spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
21511 }