]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/lpfc/lpfc_sli.c
scsi: lpfc: Fix ELS abort on SLI-3 adapters
[linux.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-2018 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 #ifdef CONFIG_X86
39 #include <asm/set_memory.h>
40 #endif
41
42 #include <linux/nvme-fc-driver.h>
43
44 #include "lpfc_hw4.h"
45 #include "lpfc_hw.h"
46 #include "lpfc_sli.h"
47 #include "lpfc_sli4.h"
48 #include "lpfc_nl.h"
49 #include "lpfc_disc.h"
50 #include "lpfc.h"
51 #include "lpfc_scsi.h"
52 #include "lpfc_nvme.h"
53 #include "lpfc_nvmet.h"
54 #include "lpfc_crtn.h"
55 #include "lpfc_logmsg.h"
56 #include "lpfc_compat.h"
57 #include "lpfc_debugfs.h"
58 #include "lpfc_vport.h"
59 #include "lpfc_version.h"
60
61 /* There are only four IOCB completion types. */
62 typedef enum _lpfc_iocb_type {
63         LPFC_UNKNOWN_IOCB,
64         LPFC_UNSOL_IOCB,
65         LPFC_SOL_IOCB,
66         LPFC_ABORT_IOCB
67 } lpfc_iocb_type;
68
69
70 /* Provide function prototypes local to this module. */
71 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
72                                   uint32_t);
73 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
74                               uint8_t *, uint32_t *);
75 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
76                                                          struct lpfc_iocbq *);
77 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
78                                       struct hbq_dmabuf *);
79 static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
80                                           struct hbq_dmabuf *dmabuf);
81 static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
82                                     struct lpfc_cqe *);
83 static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
84                                        int);
85 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
86                                      struct lpfc_eqe *eqe, uint32_t qidx);
87 static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
88 static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
89 static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
90                                    struct lpfc_sli_ring *pring,
91                                    struct lpfc_iocbq *cmdiocb);
92
93 static IOCB_t *
94 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
95 {
96         return &iocbq->iocb;
97 }
98
99 #if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
100 /**
101  * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
102  * @srcp: Source memory pointer.
103  * @destp: Destination memory pointer.
104  * @cnt: Number of words required to be copied.
105  *       Must be a multiple of sizeof(uint64_t)
106  *
107  * This function is used for copying data between driver memory
108  * and the SLI WQ. This function also changes the endianness
109  * of each word if native endianness is different from SLI
110  * endianness. This function can be called with or without
111  * lock.
112  **/
113 void
114 lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
115 {
116         uint64_t *src = srcp;
117         uint64_t *dest = destp;
118         int i;
119
120         for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
121                 *dest++ = *src++;
122 }
123 #else
124 #define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
125 #endif
126
127 /**
128  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
129  * @q: The Work Queue to operate on.
130  * @wqe: The work Queue Entry to put on the Work queue.
131  *
132  * This routine will copy the contents of @wqe to the next available entry on
133  * the @q. This function will then ring the Work Queue Doorbell to signal the
134  * HBA to start processing the Work Queue Entry. This function returns 0 if
135  * successful. If no entries are available on @q then this function will return
136  * -ENOMEM.
137  * The caller is expected to hold the hbalock when calling this routine.
138  **/
139 static int
140 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
141 {
142         union lpfc_wqe *temp_wqe;
143         struct lpfc_register doorbell;
144         uint32_t host_index;
145         uint32_t idx;
146         uint32_t i = 0;
147         uint8_t *tmp;
148         u32 if_type;
149
150         /* sanity check on queue memory */
151         if (unlikely(!q))
152                 return -ENOMEM;
153         temp_wqe = q->qe[q->host_index].wqe;
154
155         /* If the host has not yet processed the next entry then we are done */
156         idx = ((q->host_index + 1) % q->entry_count);
157         if (idx == q->hba_index) {
158                 q->WQ_overflow++;
159                 return -EBUSY;
160         }
161         q->WQ_posted++;
162         /* set consumption flag every once in a while */
163         if (!((q->host_index + 1) % q->entry_repost))
164                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
165         else
166                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
167         if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
168                 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
169         lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
170         if (q->dpp_enable && q->phba->cfg_enable_dpp) {
171                 /* write to DPP aperture taking advatage of Combined Writes */
172                 tmp = (uint8_t *)temp_wqe;
173 #ifdef __raw_writeq
174                 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
175                         __raw_writeq(*((uint64_t *)(tmp + i)),
176                                         q->dpp_regaddr + i);
177 #else
178                 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
179                         __raw_writel(*((uint32_t *)(tmp + i)),
180                                         q->dpp_regaddr + i);
181 #endif
182         }
183         /* ensure WQE bcopy and DPP flushed before doorbell write */
184         wmb();
185
186         /* Update the host index before invoking device */
187         host_index = q->host_index;
188
189         q->host_index = idx;
190
191         /* Ring Doorbell */
192         doorbell.word0 = 0;
193         if (q->db_format == LPFC_DB_LIST_FORMAT) {
194                 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
195                         bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
196                         bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
197                         bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
198                             q->dpp_id);
199                         bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
200                             q->queue_id);
201                 } else {
202                         bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
203                         bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
204
205                         /* Leave bits <23:16> clear for if_type 6 dpp */
206                         if_type = bf_get(lpfc_sli_intf_if_type,
207                                          &q->phba->sli4_hba.sli_intf);
208                         if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
209                                 bf_set(lpfc_wq_db_list_fm_index, &doorbell,
210                                        host_index);
211                 }
212         } else if (q->db_format == LPFC_DB_RING_FORMAT) {
213                 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
214                 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
215         } else {
216                 return -EINVAL;
217         }
218         writel(doorbell.word0, q->db_regaddr);
219
220         return 0;
221 }
222
223 /**
224  * lpfc_sli4_wq_release - Updates internal hba index for WQ
225  * @q: The Work Queue to operate on.
226  * @index: The index to advance the hba index to.
227  *
228  * This routine will update the HBA index of a queue to reflect consumption of
229  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
230  * an entry the host calls this function to update the queue's internal
231  * pointers. This routine returns the number of entries that were consumed by
232  * the HBA.
233  **/
234 static uint32_t
235 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
236 {
237         uint32_t released = 0;
238
239         /* sanity check on queue memory */
240         if (unlikely(!q))
241                 return 0;
242
243         if (q->hba_index == index)
244                 return 0;
245         do {
246                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
247                 released++;
248         } while (q->hba_index != index);
249         return released;
250 }
251
252 /**
253  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
254  * @q: The Mailbox Queue to operate on.
255  * @wqe: The Mailbox Queue Entry to put on the Work queue.
256  *
257  * This routine will copy the contents of @mqe to the next available entry on
258  * the @q. This function will then ring the Work Queue Doorbell to signal the
259  * HBA to start processing the Work Queue Entry. This function returns 0 if
260  * successful. If no entries are available on @q then this function will return
261  * -ENOMEM.
262  * The caller is expected to hold the hbalock when calling this routine.
263  **/
264 static uint32_t
265 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
266 {
267         struct lpfc_mqe *temp_mqe;
268         struct lpfc_register doorbell;
269
270         /* sanity check on queue memory */
271         if (unlikely(!q))
272                 return -ENOMEM;
273         temp_mqe = q->qe[q->host_index].mqe;
274
275         /* If the host has not yet processed the next entry then we are done */
276         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
277                 return -ENOMEM;
278         lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
279         /* Save off the mailbox pointer for completion */
280         q->phba->mbox = (MAILBOX_t *)temp_mqe;
281
282         /* Update the host index before invoking device */
283         q->host_index = ((q->host_index + 1) % q->entry_count);
284
285         /* Ring Doorbell */
286         doorbell.word0 = 0;
287         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
288         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
289         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
290         return 0;
291 }
292
293 /**
294  * lpfc_sli4_mq_release - Updates internal hba index for MQ
295  * @q: The Mailbox Queue to operate on.
296  *
297  * This routine will update the HBA index of a queue to reflect consumption of
298  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
299  * an entry the host calls this function to update the queue's internal
300  * pointers. This routine returns the number of entries that were consumed by
301  * the HBA.
302  **/
303 static uint32_t
304 lpfc_sli4_mq_release(struct lpfc_queue *q)
305 {
306         /* sanity check on queue memory */
307         if (unlikely(!q))
308                 return 0;
309
310         /* Clear the mailbox pointer for completion */
311         q->phba->mbox = NULL;
312         q->hba_index = ((q->hba_index + 1) % q->entry_count);
313         return 1;
314 }
315
316 /**
317  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
318  * @q: The Event Queue to get the first valid EQE from
319  *
320  * This routine will get the first valid Event Queue Entry from @q, update
321  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
322  * the Queue (no more work to do), or the Queue is full of EQEs that have been
323  * processed, but not popped back to the HBA then this routine will return NULL.
324  **/
325 static struct lpfc_eqe *
326 lpfc_sli4_eq_get(struct lpfc_queue *q)
327 {
328         struct lpfc_hba *phba;
329         struct lpfc_eqe *eqe;
330         uint32_t idx;
331
332         /* sanity check on queue memory */
333         if (unlikely(!q))
334                 return NULL;
335         phba = q->phba;
336         eqe = q->qe[q->hba_index].eqe;
337
338         /* If the next EQE is not valid then we are done */
339         if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
340                 return NULL;
341         /* If the host has not yet processed the next entry then we are done */
342         idx = ((q->hba_index + 1) % q->entry_count);
343         if (idx == q->host_index)
344                 return NULL;
345
346         q->hba_index = idx;
347         /* if the index wrapped around, toggle the valid bit */
348         if (phba->sli4_hba.pc_sli4_params.eqav && !q->hba_index)
349                 q->qe_valid = (q->qe_valid) ? 0 : 1;
350
351
352         /*
353          * insert barrier for instruction interlock : data from the hardware
354          * must have the valid bit checked before it can be copied and acted
355          * upon. Speculative instructions were allowing a bcopy at the start
356          * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
357          * after our return, to copy data before the valid bit check above
358          * was done. As such, some of the copied data was stale. The barrier
359          * ensures the check is before any data is copied.
360          */
361         mb();
362         return eqe;
363 }
364
365 /**
366  * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
367  * @q: The Event Queue to disable interrupts
368  *
369  **/
370 inline void
371 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
372 {
373         struct lpfc_register doorbell;
374
375         doorbell.word0 = 0;
376         bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
377         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
378         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
379                 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
380         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
381         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
382 }
383
384 /**
385  * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
386  * @q: The Event Queue to disable interrupts
387  *
388  **/
389 inline void
390 lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
391 {
392         struct lpfc_register doorbell;
393
394         doorbell.word0 = 0;
395         bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
396         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
397         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
398                 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
399         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
400         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
401 }
402
403 /**
404  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
405  * @q: The Event Queue that the host has completed processing for.
406  * @arm: Indicates whether the host wants to arms this CQ.
407  *
408  * This routine will mark all Event Queue Entries on @q, from the last
409  * known completed entry to the last entry that was processed, as completed
410  * by clearing the valid bit for each completion queue entry. Then it will
411  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
412  * The internal host index in the @q will be updated by this routine to indicate
413  * that the host has finished processing the entries. The @arm parameter
414  * indicates that the queue should be rearmed when ringing the doorbell.
415  *
416  * This function will return the number of EQEs that were popped.
417  **/
418 uint32_t
419 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
420 {
421         uint32_t released = 0;
422         struct lpfc_hba *phba;
423         struct lpfc_eqe *temp_eqe;
424         struct lpfc_register doorbell;
425
426         /* sanity check on queue memory */
427         if (unlikely(!q))
428                 return 0;
429         phba = q->phba;
430
431         /* while there are valid entries */
432         while (q->hba_index != q->host_index) {
433                 if (!phba->sli4_hba.pc_sli4_params.eqav) {
434                         temp_eqe = q->qe[q->host_index].eqe;
435                         bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
436                 }
437                 released++;
438                 q->host_index = ((q->host_index + 1) % q->entry_count);
439         }
440         if (unlikely(released == 0 && !arm))
441                 return 0;
442
443         /* ring doorbell for number popped */
444         doorbell.word0 = 0;
445         if (arm) {
446                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
447                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
448         }
449         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
450         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
451         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
452                         (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
453         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
454         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
455         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
456         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
457                 readl(q->phba->sli4_hba.EQDBregaddr);
458         return released;
459 }
460
461 /**
462  * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
463  * @q: The Event Queue that the host has completed processing for.
464  * @arm: Indicates whether the host wants to arms this CQ.
465  *
466  * This routine will mark all Event Queue Entries on @q, from the last
467  * known completed entry to the last entry that was processed, as completed
468  * by clearing the valid bit for each completion queue entry. Then it will
469  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
470  * The internal host index in the @q will be updated by this routine to indicate
471  * that the host has finished processing the entries. The @arm parameter
472  * indicates that the queue should be rearmed when ringing the doorbell.
473  *
474  * This function will return the number of EQEs that were popped.
475  **/
476 uint32_t
477 lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
478 {
479         uint32_t released = 0;
480         struct lpfc_hba *phba;
481         struct lpfc_eqe *temp_eqe;
482         struct lpfc_register doorbell;
483
484         /* sanity check on queue memory */
485         if (unlikely(!q))
486                 return 0;
487         phba = q->phba;
488
489         /* while there are valid entries */
490         while (q->hba_index != q->host_index) {
491                 if (!phba->sli4_hba.pc_sli4_params.eqav) {
492                         temp_eqe = q->qe[q->host_index].eqe;
493                         bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
494                 }
495                 released++;
496                 q->host_index = ((q->host_index + 1) % q->entry_count);
497         }
498         if (unlikely(released == 0 && !arm))
499                 return 0;
500
501         /* ring doorbell for number popped */
502         doorbell.word0 = 0;
503         if (arm)
504                 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
505         bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
506         bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
507         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
508         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
509         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
510                 readl(q->phba->sli4_hba.EQDBregaddr);
511         return released;
512 }
513
514 /**
515  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
516  * @q: The Completion Queue to get the first valid CQE from
517  *
518  * This routine will get the first valid Completion Queue Entry from @q, update
519  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
520  * the Queue (no more work to do), or the Queue is full of CQEs that have been
521  * processed, but not popped back to the HBA then this routine will return NULL.
522  **/
523 static struct lpfc_cqe *
524 lpfc_sli4_cq_get(struct lpfc_queue *q)
525 {
526         struct lpfc_hba *phba;
527         struct lpfc_cqe *cqe;
528         uint32_t idx;
529
530         /* sanity check on queue memory */
531         if (unlikely(!q))
532                 return NULL;
533         phba = q->phba;
534         cqe = q->qe[q->hba_index].cqe;
535
536         /* If the next CQE is not valid then we are done */
537         if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
538                 return NULL;
539         /* If the host has not yet processed the next entry then we are done */
540         idx = ((q->hba_index + 1) % q->entry_count);
541         if (idx == q->host_index)
542                 return NULL;
543
544         q->hba_index = idx;
545         /* if the index wrapped around, toggle the valid bit */
546         if (phba->sli4_hba.pc_sli4_params.cqav && !q->hba_index)
547                 q->qe_valid = (q->qe_valid) ? 0 : 1;
548
549         /*
550          * insert barrier for instruction interlock : data from the hardware
551          * must have the valid bit checked before it can be copied and acted
552          * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
553          * instructions allowing action on content before valid bit checked,
554          * add barrier here as well. May not be needed as "content" is a
555          * single 32-bit entity here (vs multi word structure for cq's).
556          */
557         mb();
558         return cqe;
559 }
560
561 /**
562  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
563  * @q: The Completion Queue that the host has completed processing for.
564  * @arm: Indicates whether the host wants to arms this CQ.
565  *
566  * This routine will mark all Completion queue entries on @q, from the last
567  * known completed entry to the last entry that was processed, as completed
568  * by clearing the valid bit for each completion queue entry. Then it will
569  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
570  * The internal host index in the @q will be updated by this routine to indicate
571  * that the host has finished processing the entries. The @arm parameter
572  * indicates that the queue should be rearmed when ringing the doorbell.
573  *
574  * This function will return the number of CQEs that were released.
575  **/
576 uint32_t
577 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
578 {
579         uint32_t released = 0;
580         struct lpfc_hba *phba;
581         struct lpfc_cqe *temp_qe;
582         struct lpfc_register doorbell;
583
584         /* sanity check on queue memory */
585         if (unlikely(!q))
586                 return 0;
587         phba = q->phba;
588
589         /* while there are valid entries */
590         while (q->hba_index != q->host_index) {
591                 if (!phba->sli4_hba.pc_sli4_params.cqav) {
592                         temp_qe = q->qe[q->host_index].cqe;
593                         bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
594                 }
595                 released++;
596                 q->host_index = ((q->host_index + 1) % q->entry_count);
597         }
598         if (unlikely(released == 0 && !arm))
599                 return 0;
600
601         /* ring doorbell for number popped */
602         doorbell.word0 = 0;
603         if (arm)
604                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
605         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
606         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
607         bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
608                         (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
609         bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
610         writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
611         return released;
612 }
613
614 /**
615  * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
616  * @q: The Completion Queue that the host has completed processing for.
617  * @arm: Indicates whether the host wants to arms this CQ.
618  *
619  * This routine will mark all Completion queue entries on @q, from the last
620  * known completed entry to the last entry that was processed, as completed
621  * by clearing the valid bit for each completion queue entry. Then it will
622  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
623  * The internal host index in the @q will be updated by this routine to indicate
624  * that the host has finished processing the entries. The @arm parameter
625  * indicates that the queue should be rearmed when ringing the doorbell.
626  *
627  * This function will return the number of CQEs that were released.
628  **/
629 uint32_t
630 lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
631 {
632         uint32_t released = 0;
633         struct lpfc_hba *phba;
634         struct lpfc_cqe *temp_qe;
635         struct lpfc_register doorbell;
636
637         /* sanity check on queue memory */
638         if (unlikely(!q))
639                 return 0;
640         phba = q->phba;
641
642         /* while there are valid entries */
643         while (q->hba_index != q->host_index) {
644                 if (!phba->sli4_hba.pc_sli4_params.cqav) {
645                         temp_qe = q->qe[q->host_index].cqe;
646                         bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
647                 }
648                 released++;
649                 q->host_index = ((q->host_index + 1) % q->entry_count);
650         }
651         if (unlikely(released == 0 && !arm))
652                 return 0;
653
654         /* ring doorbell for number popped */
655         doorbell.word0 = 0;
656         if (arm)
657                 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
658         bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
659         bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
660         writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
661         return released;
662 }
663
664 /**
665  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
666  * @q: The Header Receive Queue to operate on.
667  * @wqe: The Receive Queue Entry to put on the Receive queue.
668  *
669  * This routine will copy the contents of @wqe to the next available entry on
670  * the @q. This function will then ring the Receive Queue Doorbell to signal the
671  * HBA to start processing the Receive Queue Entry. This function returns the
672  * index that the rqe was copied to if successful. If no entries are available
673  * on @q then this function will return -ENOMEM.
674  * The caller is expected to hold the hbalock when calling this routine.
675  **/
676 int
677 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
678                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
679 {
680         struct lpfc_rqe *temp_hrqe;
681         struct lpfc_rqe *temp_drqe;
682         struct lpfc_register doorbell;
683         int hq_put_index;
684         int dq_put_index;
685
686         /* sanity check on queue memory */
687         if (unlikely(!hq) || unlikely(!dq))
688                 return -ENOMEM;
689         hq_put_index = hq->host_index;
690         dq_put_index = dq->host_index;
691         temp_hrqe = hq->qe[hq_put_index].rqe;
692         temp_drqe = dq->qe[dq_put_index].rqe;
693
694         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
695                 return -EINVAL;
696         if (hq_put_index != dq_put_index)
697                 return -EINVAL;
698         /* If the host has not yet processed the next entry then we are done */
699         if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
700                 return -EBUSY;
701         lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
702         lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
703
704         /* Update the host index to point to the next slot */
705         hq->host_index = ((hq_put_index + 1) % hq->entry_count);
706         dq->host_index = ((dq_put_index + 1) % dq->entry_count);
707         hq->RQ_buf_posted++;
708
709         /* Ring The Header Receive Queue Doorbell */
710         if (!(hq->host_index % hq->entry_repost)) {
711                 doorbell.word0 = 0;
712                 if (hq->db_format == LPFC_DB_RING_FORMAT) {
713                         bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
714                                hq->entry_repost);
715                         bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
716                 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
717                         bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
718                                hq->entry_repost);
719                         bf_set(lpfc_rq_db_list_fm_index, &doorbell,
720                                hq->host_index);
721                         bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
722                 } else {
723                         return -EINVAL;
724                 }
725                 writel(doorbell.word0, hq->db_regaddr);
726         }
727         return hq_put_index;
728 }
729
730 /**
731  * lpfc_sli4_rq_release - Updates internal hba index for RQ
732  * @q: The Header Receive Queue to operate on.
733  *
734  * This routine will update the HBA index of a queue to reflect consumption of
735  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
736  * consumed an entry the host calls this function to update the queue's
737  * internal pointers. This routine returns the number of entries that were
738  * consumed by the HBA.
739  **/
740 static uint32_t
741 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
742 {
743         /* sanity check on queue memory */
744         if (unlikely(!hq) || unlikely(!dq))
745                 return 0;
746
747         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
748                 return 0;
749         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
750         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
751         return 1;
752 }
753
754 /**
755  * lpfc_cmd_iocb - Get next command iocb entry in the ring
756  * @phba: Pointer to HBA context object.
757  * @pring: Pointer to driver SLI ring object.
758  *
759  * This function returns pointer to next command iocb entry
760  * in the command ring. The caller must hold hbalock to prevent
761  * other threads consume the next command iocb.
762  * SLI-2/SLI-3 provide different sized iocbs.
763  **/
764 static inline IOCB_t *
765 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
766 {
767         return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
768                            pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
769 }
770
771 /**
772  * lpfc_resp_iocb - Get next response iocb entry in the ring
773  * @phba: Pointer to HBA context object.
774  * @pring: Pointer to driver SLI ring object.
775  *
776  * This function returns pointer to next response iocb entry
777  * in the response ring. The caller must hold hbalock to make sure
778  * that no other thread consume the next response iocb.
779  * SLI-2/SLI-3 provide different sized iocbs.
780  **/
781 static inline IOCB_t *
782 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
783 {
784         return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
785                            pring->sli.sli3.rspidx * phba->iocb_rsp_size);
786 }
787
788 /**
789  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
790  * @phba: Pointer to HBA context object.
791  *
792  * This function is called with hbalock held. This function
793  * allocates a new driver iocb object from the iocb pool. If the
794  * allocation is successful, it returns pointer to the newly
795  * allocated iocb object else it returns NULL.
796  **/
797 struct lpfc_iocbq *
798 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
799 {
800         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
801         struct lpfc_iocbq * iocbq = NULL;
802
803         lockdep_assert_held(&phba->hbalock);
804
805         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
806         if (iocbq)
807                 phba->iocb_cnt++;
808         if (phba->iocb_cnt > phba->iocb_max)
809                 phba->iocb_max = phba->iocb_cnt;
810         return iocbq;
811 }
812
813 /**
814  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
815  * @phba: Pointer to HBA context object.
816  * @xritag: XRI value.
817  *
818  * This function clears the sglq pointer from the array of acive
819  * sglq's. The xritag that is passed in is used to index into the
820  * array. Before the xritag can be used it needs to be adjusted
821  * by subtracting the xribase.
822  *
823  * Returns sglq ponter = success, NULL = Failure.
824  **/
825 struct lpfc_sglq *
826 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
827 {
828         struct lpfc_sglq *sglq;
829
830         sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
831         phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
832         return sglq;
833 }
834
835 /**
836  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
837  * @phba: Pointer to HBA context object.
838  * @xritag: XRI value.
839  *
840  * This function returns the sglq pointer from the array of acive
841  * sglq's. The xritag that is passed in is used to index into the
842  * array. Before the xritag can be used it needs to be adjusted
843  * by subtracting the xribase.
844  *
845  * Returns sglq ponter = success, NULL = Failure.
846  **/
847 struct lpfc_sglq *
848 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
849 {
850         struct lpfc_sglq *sglq;
851
852         sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];
853         return sglq;
854 }
855
856 /**
857  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
858  * @phba: Pointer to HBA context object.
859  * @xritag: xri used in this exchange.
860  * @rrq: The RRQ to be cleared.
861  *
862  **/
863 void
864 lpfc_clr_rrq_active(struct lpfc_hba *phba,
865                     uint16_t xritag,
866                     struct lpfc_node_rrq *rrq)
867 {
868         struct lpfc_nodelist *ndlp = NULL;
869
870         if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
871                 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
872
873         /* The target DID could have been swapped (cable swap)
874          * we should use the ndlp from the findnode if it is
875          * available.
876          */
877         if ((!ndlp) && rrq->ndlp)
878                 ndlp = rrq->ndlp;
879
880         if (!ndlp)
881                 goto out;
882
883         if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
884                 rrq->send_rrq = 0;
885                 rrq->xritag = 0;
886                 rrq->rrq_stop_time = 0;
887         }
888 out:
889         mempool_free(rrq, phba->rrq_pool);
890 }
891
892 /**
893  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
894  * @phba: Pointer to HBA context object.
895  *
896  * This function is called with hbalock held. This function
897  * Checks if stop_time (ratov from setting rrq active) has
898  * been reached, if it has and the send_rrq flag is set then
899  * it will call lpfc_send_rrq. If the send_rrq flag is not set
900  * then it will just call the routine to clear the rrq and
901  * free the rrq resource.
902  * The timer is set to the next rrq that is going to expire before
903  * leaving the routine.
904  *
905  **/
906 void
907 lpfc_handle_rrq_active(struct lpfc_hba *phba)
908 {
909         struct lpfc_node_rrq *rrq;
910         struct lpfc_node_rrq *nextrrq;
911         unsigned long next_time;
912         unsigned long iflags;
913         LIST_HEAD(send_rrq);
914
915         spin_lock_irqsave(&phba->hbalock, iflags);
916         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
917         next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
918         list_for_each_entry_safe(rrq, nextrrq,
919                                  &phba->active_rrq_list, list) {
920                 if (time_after(jiffies, rrq->rrq_stop_time))
921                         list_move(&rrq->list, &send_rrq);
922                 else if (time_before(rrq->rrq_stop_time, next_time))
923                         next_time = rrq->rrq_stop_time;
924         }
925         spin_unlock_irqrestore(&phba->hbalock, iflags);
926         if ((!list_empty(&phba->active_rrq_list)) &&
927             (!(phba->pport->load_flag & FC_UNLOADING)))
928                 mod_timer(&phba->rrq_tmr, next_time);
929         list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
930                 list_del(&rrq->list);
931                 if (!rrq->send_rrq)
932                         /* this call will free the rrq */
933                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
934                 else if (lpfc_send_rrq(phba, rrq)) {
935                         /* if we send the rrq then the completion handler
936                         *  will clear the bit in the xribitmap.
937                         */
938                         lpfc_clr_rrq_active(phba, rrq->xritag,
939                                             rrq);
940                 }
941         }
942 }
943
944 /**
945  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
946  * @vport: Pointer to vport context object.
947  * @xri: The xri used in the exchange.
948  * @did: The targets DID for this exchange.
949  *
950  * returns NULL = rrq not found in the phba->active_rrq_list.
951  *         rrq = rrq for this xri and target.
952  **/
953 struct lpfc_node_rrq *
954 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
955 {
956         struct lpfc_hba *phba = vport->phba;
957         struct lpfc_node_rrq *rrq;
958         struct lpfc_node_rrq *nextrrq;
959         unsigned long iflags;
960
961         if (phba->sli_rev != LPFC_SLI_REV4)
962                 return NULL;
963         spin_lock_irqsave(&phba->hbalock, iflags);
964         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
965                 if (rrq->vport == vport && rrq->xritag == xri &&
966                                 rrq->nlp_DID == did){
967                         list_del(&rrq->list);
968                         spin_unlock_irqrestore(&phba->hbalock, iflags);
969                         return rrq;
970                 }
971         }
972         spin_unlock_irqrestore(&phba->hbalock, iflags);
973         return NULL;
974 }
975
976 /**
977  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
978  * @vport: Pointer to vport context object.
979  * @ndlp: Pointer to the lpfc_node_list structure.
980  * If ndlp is NULL Remove all active RRQs for this vport from the
981  * phba->active_rrq_list and clear the rrq.
982  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
983  **/
984 void
985 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
986
987 {
988         struct lpfc_hba *phba = vport->phba;
989         struct lpfc_node_rrq *rrq;
990         struct lpfc_node_rrq *nextrrq;
991         unsigned long iflags;
992         LIST_HEAD(rrq_list);
993
994         if (phba->sli_rev != LPFC_SLI_REV4)
995                 return;
996         if (!ndlp) {
997                 lpfc_sli4_vport_delete_els_xri_aborted(vport);
998                 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
999         }
1000         spin_lock_irqsave(&phba->hbalock, iflags);
1001         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
1002                 if ((rrq->vport == vport) && (!ndlp  || rrq->ndlp == ndlp))
1003                         list_move(&rrq->list, &rrq_list);
1004         spin_unlock_irqrestore(&phba->hbalock, iflags);
1005
1006         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1007                 list_del(&rrq->list);
1008                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1009         }
1010 }
1011
1012 /**
1013  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
1014  * @phba: Pointer to HBA context object.
1015  * @ndlp: Targets nodelist pointer for this exchange.
1016  * @xritag the xri in the bitmap to test.
1017  *
1018  * This function is called with hbalock held. This function
1019  * returns 0 = rrq not active for this xri
1020  *         1 = rrq is valid for this xri.
1021  **/
1022 int
1023 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1024                         uint16_t  xritag)
1025 {
1026         lockdep_assert_held(&phba->hbalock);
1027         if (!ndlp)
1028                 return 0;
1029         if (!ndlp->active_rrqs_xri_bitmap)
1030                 return 0;
1031         if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1032                         return 1;
1033         else
1034                 return 0;
1035 }
1036
1037 /**
1038  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1039  * @phba: Pointer to HBA context object.
1040  * @ndlp: nodelist pointer for this target.
1041  * @xritag: xri used in this exchange.
1042  * @rxid: Remote Exchange ID.
1043  * @send_rrq: Flag used to determine if we should send rrq els cmd.
1044  *
1045  * This function takes the hbalock.
1046  * The active bit is always set in the active rrq xri_bitmap even
1047  * if there is no slot avaiable for the other rrq information.
1048  *
1049  * returns 0 rrq actived for this xri
1050  *         < 0 No memory or invalid ndlp.
1051  **/
1052 int
1053 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1054                     uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
1055 {
1056         unsigned long iflags;
1057         struct lpfc_node_rrq *rrq;
1058         int empty;
1059
1060         if (!ndlp)
1061                 return -EINVAL;
1062
1063         if (!phba->cfg_enable_rrq)
1064                 return -EINVAL;
1065
1066         spin_lock_irqsave(&phba->hbalock, iflags);
1067         if (phba->pport->load_flag & FC_UNLOADING) {
1068                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1069                 goto out;
1070         }
1071
1072         /*
1073          * set the active bit even if there is no mem available.
1074          */
1075         if (NLP_CHK_FREE_REQ(ndlp))
1076                 goto out;
1077
1078         if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1079                 goto out;
1080
1081         if (!ndlp->active_rrqs_xri_bitmap)
1082                 goto out;
1083
1084         if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1085                 goto out;
1086
1087         spin_unlock_irqrestore(&phba->hbalock, iflags);
1088         rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1089         if (!rrq) {
1090                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1091                                 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1092                                 " DID:0x%x Send:%d\n",
1093                                 xritag, rxid, ndlp->nlp_DID, send_rrq);
1094                 return -EINVAL;
1095         }
1096         if (phba->cfg_enable_rrq == 1)
1097                 rrq->send_rrq = send_rrq;
1098         else
1099                 rrq->send_rrq = 0;
1100         rrq->xritag = xritag;
1101         rrq->rrq_stop_time = jiffies +
1102                                 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
1103         rrq->ndlp = ndlp;
1104         rrq->nlp_DID = ndlp->nlp_DID;
1105         rrq->vport = ndlp->vport;
1106         rrq->rxid = rxid;
1107         spin_lock_irqsave(&phba->hbalock, iflags);
1108         empty = list_empty(&phba->active_rrq_list);
1109         list_add_tail(&rrq->list, &phba->active_rrq_list);
1110         phba->hba_flag |= HBA_RRQ_ACTIVE;
1111         if (empty)
1112                 lpfc_worker_wake_up(phba);
1113         spin_unlock_irqrestore(&phba->hbalock, iflags);
1114         return 0;
1115 out:
1116         spin_unlock_irqrestore(&phba->hbalock, iflags);
1117         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1118                         "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1119                         " DID:0x%x Send:%d\n",
1120                         xritag, rxid, ndlp->nlp_DID, send_rrq);
1121         return -EINVAL;
1122 }
1123
1124 /**
1125  * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
1126  * @phba: Pointer to HBA context object.
1127  * @piocb: Pointer to the iocbq.
1128  *
1129  * This function is called with the ring lock held. This function
1130  * gets a new driver sglq object from the sglq list. If the
1131  * list is not empty then it is successful, it returns pointer to the newly
1132  * allocated sglq object else it returns NULL.
1133  **/
1134 static struct lpfc_sglq *
1135 __lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1136 {
1137         struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
1138         struct lpfc_sglq *sglq = NULL;
1139         struct lpfc_sglq *start_sglq = NULL;
1140         struct lpfc_scsi_buf *lpfc_cmd;
1141         struct lpfc_nodelist *ndlp;
1142         int found = 0;
1143
1144         lockdep_assert_held(&phba->hbalock);
1145
1146         if (piocbq->iocb_flag &  LPFC_IO_FCP) {
1147                 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1148                 ndlp = lpfc_cmd->rdata->pnode;
1149         } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
1150                         !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
1151                 ndlp = piocbq->context_un.ndlp;
1152         } else  if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1153                 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1154                         ndlp = NULL;
1155                 else
1156                         ndlp = piocbq->context_un.ndlp;
1157         } else {
1158                 ndlp = piocbq->context1;
1159         }
1160
1161         spin_lock(&phba->sli4_hba.sgl_list_lock);
1162         list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
1163         start_sglq = sglq;
1164         while (!found) {
1165                 if (!sglq)
1166                         break;
1167                 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1168                     test_bit(sglq->sli4_lxritag,
1169                     ndlp->active_rrqs_xri_bitmap)) {
1170                         /* This xri has an rrq outstanding for this DID.
1171                          * put it back in the list and get another xri.
1172                          */
1173                         list_add_tail(&sglq->list, lpfc_els_sgl_list);
1174                         sglq = NULL;
1175                         list_remove_head(lpfc_els_sgl_list, sglq,
1176                                                 struct lpfc_sglq, list);
1177                         if (sglq == start_sglq) {
1178                                 list_add_tail(&sglq->list, lpfc_els_sgl_list);
1179                                 sglq = NULL;
1180                                 break;
1181                         } else
1182                                 continue;
1183                 }
1184                 sglq->ndlp = ndlp;
1185                 found = 1;
1186                 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1187                 sglq->state = SGL_ALLOCATED;
1188         }
1189         spin_unlock(&phba->sli4_hba.sgl_list_lock);
1190         return sglq;
1191 }
1192
1193 /**
1194  * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1195  * @phba: Pointer to HBA context object.
1196  * @piocb: Pointer to the iocbq.
1197  *
1198  * This function is called with the sgl_list lock held. This function
1199  * gets a new driver sglq object from the sglq list. If the
1200  * list is not empty then it is successful, it returns pointer to the newly
1201  * allocated sglq object else it returns NULL.
1202  **/
1203 struct lpfc_sglq *
1204 __lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1205 {
1206         struct list_head *lpfc_nvmet_sgl_list;
1207         struct lpfc_sglq *sglq = NULL;
1208
1209         lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1210
1211         lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1212
1213         list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1214         if (!sglq)
1215                 return NULL;
1216         phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1217         sglq->state = SGL_ALLOCATED;
1218         return sglq;
1219 }
1220
1221 /**
1222  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
1223  * @phba: Pointer to HBA context object.
1224  *
1225  * This function is called with no lock held. This function
1226  * allocates a new driver iocb object from the iocb pool. If the
1227  * allocation is successful, it returns pointer to the newly
1228  * allocated iocb object else it returns NULL.
1229  **/
1230 struct lpfc_iocbq *
1231 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1232 {
1233         struct lpfc_iocbq * iocbq = NULL;
1234         unsigned long iflags;
1235
1236         spin_lock_irqsave(&phba->hbalock, iflags);
1237         iocbq = __lpfc_sli_get_iocbq(phba);
1238         spin_unlock_irqrestore(&phba->hbalock, iflags);
1239         return iocbq;
1240 }
1241
1242 /**
1243  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1244  * @phba: Pointer to HBA context object.
1245  * @iocbq: Pointer to driver iocb object.
1246  *
1247  * This function is called with hbalock held to release driver
1248  * iocb object to the iocb pool. The iotag in the iocb object
1249  * does not change for each use of the iocb object. This function
1250  * clears all other fields of the iocb object when it is freed.
1251  * The sqlq structure that holds the xritag and phys and virtual
1252  * mappings for the scatter gather list is retrieved from the
1253  * active array of sglq. The get of the sglq pointer also clears
1254  * the entry in the array. If the status of the IO indiactes that
1255  * this IO was aborted then the sglq entry it put on the
1256  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1257  * IO has good status or fails for any other reason then the sglq
1258  * entry is added to the free list (lpfc_els_sgl_list).
1259  **/
1260 static void
1261 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1262 {
1263         struct lpfc_sglq *sglq;
1264         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1265         unsigned long iflag = 0;
1266         struct lpfc_sli_ring *pring;
1267
1268         lockdep_assert_held(&phba->hbalock);
1269
1270         if (iocbq->sli4_xritag == NO_XRI)
1271                 sglq = NULL;
1272         else
1273                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1274
1275
1276         if (sglq)  {
1277                 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1278                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1279                                           iflag);
1280                         sglq->state = SGL_FREED;
1281                         sglq->ndlp = NULL;
1282                         list_add_tail(&sglq->list,
1283                                       &phba->sli4_hba.lpfc_nvmet_sgl_list);
1284                         spin_unlock_irqrestore(
1285                                 &phba->sli4_hba.sgl_list_lock, iflag);
1286                         goto out;
1287                 }
1288
1289                 pring = phba->sli4_hba.els_wq->pring;
1290                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1291                         (sglq->state != SGL_XRI_ABORTED)) {
1292                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1293                                           iflag);
1294                         list_add(&sglq->list,
1295                                  &phba->sli4_hba.lpfc_abts_els_sgl_list);
1296                         spin_unlock_irqrestore(
1297                                 &phba->sli4_hba.sgl_list_lock, iflag);
1298                 } else {
1299                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1300                                           iflag);
1301                         sglq->state = SGL_FREED;
1302                         sglq->ndlp = NULL;
1303                         list_add_tail(&sglq->list,
1304                                       &phba->sli4_hba.lpfc_els_sgl_list);
1305                         spin_unlock_irqrestore(
1306                                 &phba->sli4_hba.sgl_list_lock, iflag);
1307
1308                         /* Check if TXQ queue needs to be serviced */
1309                         if (!list_empty(&pring->txq))
1310                                 lpfc_worker_wake_up(phba);
1311                 }
1312         }
1313
1314 out:
1315         /*
1316          * Clean all volatile data fields, preserve iotag and node struct.
1317          */
1318         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1319         iocbq->sli4_lxritag = NO_XRI;
1320         iocbq->sli4_xritag = NO_XRI;
1321         iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1322                               LPFC_IO_NVME_LS);
1323         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1324 }
1325
1326
1327 /**
1328  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1329  * @phba: Pointer to HBA context object.
1330  * @iocbq: Pointer to driver iocb object.
1331  *
1332  * This function is called with hbalock held to release driver
1333  * iocb object to the iocb pool. The iotag in the iocb object
1334  * does not change for each use of the iocb object. This function
1335  * clears all other fields of the iocb object when it is freed.
1336  **/
1337 static void
1338 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1339 {
1340         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1341
1342         lockdep_assert_held(&phba->hbalock);
1343
1344         /*
1345          * Clean all volatile data fields, preserve iotag and node struct.
1346          */
1347         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1348         iocbq->sli4_xritag = NO_XRI;
1349         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1350 }
1351
1352 /**
1353  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1354  * @phba: Pointer to HBA context object.
1355  * @iocbq: Pointer to driver iocb object.
1356  *
1357  * This function is called with hbalock held to release driver
1358  * iocb object to the iocb pool. The iotag in the iocb object
1359  * does not change for each use of the iocb object. This function
1360  * clears all other fields of the iocb object when it is freed.
1361  **/
1362 static void
1363 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1364 {
1365         lockdep_assert_held(&phba->hbalock);
1366
1367         phba->__lpfc_sli_release_iocbq(phba, iocbq);
1368         phba->iocb_cnt--;
1369 }
1370
1371 /**
1372  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1373  * @phba: Pointer to HBA context object.
1374  * @iocbq: Pointer to driver iocb object.
1375  *
1376  * This function is called with no lock held to release the iocb to
1377  * iocb pool.
1378  **/
1379 void
1380 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1381 {
1382         unsigned long iflags;
1383
1384         /*
1385          * Clean all volatile data fields, preserve iotag and node struct.
1386          */
1387         spin_lock_irqsave(&phba->hbalock, iflags);
1388         __lpfc_sli_release_iocbq(phba, iocbq);
1389         spin_unlock_irqrestore(&phba->hbalock, iflags);
1390 }
1391
1392 /**
1393  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1394  * @phba: Pointer to HBA context object.
1395  * @iocblist: List of IOCBs.
1396  * @ulpstatus: ULP status in IOCB command field.
1397  * @ulpWord4: ULP word-4 in IOCB command field.
1398  *
1399  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1400  * on the list by invoking the complete callback function associated with the
1401  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1402  * fields.
1403  **/
1404 void
1405 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1406                       uint32_t ulpstatus, uint32_t ulpWord4)
1407 {
1408         struct lpfc_iocbq *piocb;
1409
1410         while (!list_empty(iocblist)) {
1411                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1412                 if (!piocb->iocb_cmpl)
1413                         lpfc_sli_release_iocbq(phba, piocb);
1414                 else {
1415                         piocb->iocb.ulpStatus = ulpstatus;
1416                         piocb->iocb.un.ulpWord[4] = ulpWord4;
1417                         (piocb->iocb_cmpl) (phba, piocb, piocb);
1418                 }
1419         }
1420         return;
1421 }
1422
1423 /**
1424  * lpfc_sli_iocb_cmd_type - Get the iocb type
1425  * @iocb_cmnd: iocb command code.
1426  *
1427  * This function is called by ring event handler function to get the iocb type.
1428  * This function translates the iocb command to an iocb command type used to
1429  * decide the final disposition of each completed IOCB.
1430  * The function returns
1431  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1432  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1433  * LPFC_ABORT_IOCB   if it is an abort iocb
1434  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1435  *
1436  * The caller is not required to hold any lock.
1437  **/
1438 static lpfc_iocb_type
1439 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1440 {
1441         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1442
1443         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1444                 return 0;
1445
1446         switch (iocb_cmnd) {
1447         case CMD_XMIT_SEQUENCE_CR:
1448         case CMD_XMIT_SEQUENCE_CX:
1449         case CMD_XMIT_BCAST_CN:
1450         case CMD_XMIT_BCAST_CX:
1451         case CMD_ELS_REQUEST_CR:
1452         case CMD_ELS_REQUEST_CX:
1453         case CMD_CREATE_XRI_CR:
1454         case CMD_CREATE_XRI_CX:
1455         case CMD_GET_RPI_CN:
1456         case CMD_XMIT_ELS_RSP_CX:
1457         case CMD_GET_RPI_CR:
1458         case CMD_FCP_IWRITE_CR:
1459         case CMD_FCP_IWRITE_CX:
1460         case CMD_FCP_IREAD_CR:
1461         case CMD_FCP_IREAD_CX:
1462         case CMD_FCP_ICMND_CR:
1463         case CMD_FCP_ICMND_CX:
1464         case CMD_FCP_TSEND_CX:
1465         case CMD_FCP_TRSP_CX:
1466         case CMD_FCP_TRECEIVE_CX:
1467         case CMD_FCP_AUTO_TRSP_CX:
1468         case CMD_ADAPTER_MSG:
1469         case CMD_ADAPTER_DUMP:
1470         case CMD_XMIT_SEQUENCE64_CR:
1471         case CMD_XMIT_SEQUENCE64_CX:
1472         case CMD_XMIT_BCAST64_CN:
1473         case CMD_XMIT_BCAST64_CX:
1474         case CMD_ELS_REQUEST64_CR:
1475         case CMD_ELS_REQUEST64_CX:
1476         case CMD_FCP_IWRITE64_CR:
1477         case CMD_FCP_IWRITE64_CX:
1478         case CMD_FCP_IREAD64_CR:
1479         case CMD_FCP_IREAD64_CX:
1480         case CMD_FCP_ICMND64_CR:
1481         case CMD_FCP_ICMND64_CX:
1482         case CMD_FCP_TSEND64_CX:
1483         case CMD_FCP_TRSP64_CX:
1484         case CMD_FCP_TRECEIVE64_CX:
1485         case CMD_GEN_REQUEST64_CR:
1486         case CMD_GEN_REQUEST64_CX:
1487         case CMD_XMIT_ELS_RSP64_CX:
1488         case DSSCMD_IWRITE64_CR:
1489         case DSSCMD_IWRITE64_CX:
1490         case DSSCMD_IREAD64_CR:
1491         case DSSCMD_IREAD64_CX:
1492                 type = LPFC_SOL_IOCB;
1493                 break;
1494         case CMD_ABORT_XRI_CN:
1495         case CMD_ABORT_XRI_CX:
1496         case CMD_CLOSE_XRI_CN:
1497         case CMD_CLOSE_XRI_CX:
1498         case CMD_XRI_ABORTED_CX:
1499         case CMD_ABORT_MXRI64_CN:
1500         case CMD_XMIT_BLS_RSP64_CX:
1501                 type = LPFC_ABORT_IOCB;
1502                 break;
1503         case CMD_RCV_SEQUENCE_CX:
1504         case CMD_RCV_ELS_REQ_CX:
1505         case CMD_RCV_SEQUENCE64_CX:
1506         case CMD_RCV_ELS_REQ64_CX:
1507         case CMD_ASYNC_STATUS:
1508         case CMD_IOCB_RCV_SEQ64_CX:
1509         case CMD_IOCB_RCV_ELS64_CX:
1510         case CMD_IOCB_RCV_CONT64_CX:
1511         case CMD_IOCB_RET_XRI64_CX:
1512                 type = LPFC_UNSOL_IOCB;
1513                 break;
1514         case CMD_IOCB_XMIT_MSEQ64_CR:
1515         case CMD_IOCB_XMIT_MSEQ64_CX:
1516         case CMD_IOCB_RCV_SEQ_LIST64_CX:
1517         case CMD_IOCB_RCV_ELS_LIST64_CX:
1518         case CMD_IOCB_CLOSE_EXTENDED_CN:
1519         case CMD_IOCB_ABORT_EXTENDED_CN:
1520         case CMD_IOCB_RET_HBQE64_CN:
1521         case CMD_IOCB_FCP_IBIDIR64_CR:
1522         case CMD_IOCB_FCP_IBIDIR64_CX:
1523         case CMD_IOCB_FCP_ITASKMGT64_CX:
1524         case CMD_IOCB_LOGENTRY_CN:
1525         case CMD_IOCB_LOGENTRY_ASYNC_CN:
1526                 printk("%s - Unhandled SLI-3 Command x%x\n",
1527                                 __func__, iocb_cmnd);
1528                 type = LPFC_UNKNOWN_IOCB;
1529                 break;
1530         default:
1531                 type = LPFC_UNKNOWN_IOCB;
1532                 break;
1533         }
1534
1535         return type;
1536 }
1537
1538 /**
1539  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1540  * @phba: Pointer to HBA context object.
1541  *
1542  * This function is called from SLI initialization code
1543  * to configure every ring of the HBA's SLI interface. The
1544  * caller is not required to hold any lock. This function issues
1545  * a config_ring mailbox command for each ring.
1546  * This function returns zero if successful else returns a negative
1547  * error code.
1548  **/
1549 static int
1550 lpfc_sli_ring_map(struct lpfc_hba *phba)
1551 {
1552         struct lpfc_sli *psli = &phba->sli;
1553         LPFC_MBOXQ_t *pmb;
1554         MAILBOX_t *pmbox;
1555         int i, rc, ret = 0;
1556
1557         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1558         if (!pmb)
1559                 return -ENOMEM;
1560         pmbox = &pmb->u.mb;
1561         phba->link_state = LPFC_INIT_MBX_CMDS;
1562         for (i = 0; i < psli->num_rings; i++) {
1563                 lpfc_config_ring(phba, i, pmb);
1564                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1565                 if (rc != MBX_SUCCESS) {
1566                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1567                                         "0446 Adapter failed to init (%d), "
1568                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1569                                         "ring %d\n",
1570                                         rc, pmbox->mbxCommand,
1571                                         pmbox->mbxStatus, i);
1572                         phba->link_state = LPFC_HBA_ERROR;
1573                         ret = -ENXIO;
1574                         break;
1575                 }
1576         }
1577         mempool_free(pmb, phba->mbox_mem_pool);
1578         return ret;
1579 }
1580
1581 /**
1582  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1583  * @phba: Pointer to HBA context object.
1584  * @pring: Pointer to driver SLI ring object.
1585  * @piocb: Pointer to the driver iocb object.
1586  *
1587  * This function is called with hbalock held. The function adds the
1588  * new iocb to txcmplq of the given ring. This function always returns
1589  * 0. If this function is called for ELS ring, this function checks if
1590  * there is a vport associated with the ELS command. This function also
1591  * starts els_tmofunc timer if this is an ELS command.
1592  **/
1593 static int
1594 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1595                         struct lpfc_iocbq *piocb)
1596 {
1597         lockdep_assert_held(&phba->hbalock);
1598
1599         BUG_ON(!piocb);
1600
1601         list_add_tail(&piocb->list, &pring->txcmplq);
1602         piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1603
1604         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1605            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1606            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1607                 BUG_ON(!piocb->vport);
1608                 if (!(piocb->vport->load_flag & FC_UNLOADING))
1609                         mod_timer(&piocb->vport->els_tmofunc,
1610                                   jiffies +
1611                                   msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1612         }
1613
1614         return 0;
1615 }
1616
1617 /**
1618  * lpfc_sli_ringtx_get - Get first element of the txq
1619  * @phba: Pointer to HBA context object.
1620  * @pring: Pointer to driver SLI ring object.
1621  *
1622  * This function is called with hbalock held to get next
1623  * iocb in txq of the given ring. If there is any iocb in
1624  * the txq, the function returns first iocb in the list after
1625  * removing the iocb from the list, else it returns NULL.
1626  **/
1627 struct lpfc_iocbq *
1628 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1629 {
1630         struct lpfc_iocbq *cmd_iocb;
1631
1632         lockdep_assert_held(&phba->hbalock);
1633
1634         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1635         return cmd_iocb;
1636 }
1637
1638 /**
1639  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1640  * @phba: Pointer to HBA context object.
1641  * @pring: Pointer to driver SLI ring object.
1642  *
1643  * This function is called with hbalock held and the caller must post the
1644  * iocb without releasing the lock. If the caller releases the lock,
1645  * iocb slot returned by the function is not guaranteed to be available.
1646  * The function returns pointer to the next available iocb slot if there
1647  * is available slot in the ring, else it returns NULL.
1648  * If the get index of the ring is ahead of the put index, the function
1649  * will post an error attention event to the worker thread to take the
1650  * HBA to offline state.
1651  **/
1652 static IOCB_t *
1653 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1654 {
1655         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1656         uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
1657
1658         lockdep_assert_held(&phba->hbalock);
1659
1660         if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1661            (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1662                 pring->sli.sli3.next_cmdidx = 0;
1663
1664         if (unlikely(pring->sli.sli3.local_getidx ==
1665                 pring->sli.sli3.next_cmdidx)) {
1666
1667                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
1668
1669                 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
1670                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1671                                         "0315 Ring %d issue: portCmdGet %d "
1672                                         "is bigger than cmd ring %d\n",
1673                                         pring->ringno,
1674                                         pring->sli.sli3.local_getidx,
1675                                         max_cmd_idx);
1676
1677                         phba->link_state = LPFC_HBA_ERROR;
1678                         /*
1679                          * All error attention handlers are posted to
1680                          * worker thread
1681                          */
1682                         phba->work_ha |= HA_ERATT;
1683                         phba->work_hs = HS_FFER3;
1684
1685                         lpfc_worker_wake_up(phba);
1686
1687                         return NULL;
1688                 }
1689
1690                 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
1691                         return NULL;
1692         }
1693
1694         return lpfc_cmd_iocb(phba, pring);
1695 }
1696
1697 /**
1698  * lpfc_sli_next_iotag - Get an iotag for the iocb
1699  * @phba: Pointer to HBA context object.
1700  * @iocbq: Pointer to driver iocb object.
1701  *
1702  * This function gets an iotag for the iocb. If there is no unused iotag and
1703  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1704  * array and assigns a new iotag.
1705  * The function returns the allocated iotag if successful, else returns zero.
1706  * Zero is not a valid iotag.
1707  * The caller is not required to hold any lock.
1708  **/
1709 uint16_t
1710 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1711 {
1712         struct lpfc_iocbq **new_arr;
1713         struct lpfc_iocbq **old_arr;
1714         size_t new_len;
1715         struct lpfc_sli *psli = &phba->sli;
1716         uint16_t iotag;
1717
1718         spin_lock_irq(&phba->hbalock);
1719         iotag = psli->last_iotag;
1720         if(++iotag < psli->iocbq_lookup_len) {
1721                 psli->last_iotag = iotag;
1722                 psli->iocbq_lookup[iotag] = iocbq;
1723                 spin_unlock_irq(&phba->hbalock);
1724                 iocbq->iotag = iotag;
1725                 return iotag;
1726         } else if (psli->iocbq_lookup_len < (0xffff
1727                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1728                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1729                 spin_unlock_irq(&phba->hbalock);
1730                 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
1731                                   GFP_KERNEL);
1732                 if (new_arr) {
1733                         spin_lock_irq(&phba->hbalock);
1734                         old_arr = psli->iocbq_lookup;
1735                         if (new_len <= psli->iocbq_lookup_len) {
1736                                 /* highly unprobable case */
1737                                 kfree(new_arr);
1738                                 iotag = psli->last_iotag;
1739                                 if(++iotag < psli->iocbq_lookup_len) {
1740                                         psli->last_iotag = iotag;
1741                                         psli->iocbq_lookup[iotag] = iocbq;
1742                                         spin_unlock_irq(&phba->hbalock);
1743                                         iocbq->iotag = iotag;
1744                                         return iotag;
1745                                 }
1746                                 spin_unlock_irq(&phba->hbalock);
1747                                 return 0;
1748                         }
1749                         if (psli->iocbq_lookup)
1750                                 memcpy(new_arr, old_arr,
1751                                        ((psli->last_iotag  + 1) *
1752                                         sizeof (struct lpfc_iocbq *)));
1753                         psli->iocbq_lookup = new_arr;
1754                         psli->iocbq_lookup_len = new_len;
1755                         psli->last_iotag = iotag;
1756                         psli->iocbq_lookup[iotag] = iocbq;
1757                         spin_unlock_irq(&phba->hbalock);
1758                         iocbq->iotag = iotag;
1759                         kfree(old_arr);
1760                         return iotag;
1761                 }
1762         } else
1763                 spin_unlock_irq(&phba->hbalock);
1764
1765         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1766                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1767                         psli->last_iotag);
1768
1769         return 0;
1770 }
1771
1772 /**
1773  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1774  * @phba: Pointer to HBA context object.
1775  * @pring: Pointer to driver SLI ring object.
1776  * @iocb: Pointer to iocb slot in the ring.
1777  * @nextiocb: Pointer to driver iocb object which need to be
1778  *            posted to firmware.
1779  *
1780  * This function is called with hbalock held to post a new iocb to
1781  * the firmware. This function copies the new iocb to ring iocb slot and
1782  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1783  * a completion call back for this iocb else the function will free the
1784  * iocb object.
1785  **/
1786 static void
1787 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1788                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1789 {
1790         lockdep_assert_held(&phba->hbalock);
1791         /*
1792          * Set up an iotag
1793          */
1794         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1795
1796
1797         if (pring->ringno == LPFC_ELS_RING) {
1798                 lpfc_debugfs_slow_ring_trc(phba,
1799                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1800                         *(((uint32_t *) &nextiocb->iocb) + 4),
1801                         *(((uint32_t *) &nextiocb->iocb) + 6),
1802                         *(((uint32_t *) &nextiocb->iocb) + 7));
1803         }
1804
1805         /*
1806          * Issue iocb command to adapter
1807          */
1808         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1809         wmb();
1810         pring->stats.iocb_cmd++;
1811
1812         /*
1813          * If there is no completion routine to call, we can release the
1814          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1815          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1816          */
1817         if (nextiocb->iocb_cmpl)
1818                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1819         else
1820                 __lpfc_sli_release_iocbq(phba, nextiocb);
1821
1822         /*
1823          * Let the HBA know what IOCB slot will be the next one the
1824          * driver will put a command into.
1825          */
1826         pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1827         writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1828 }
1829
1830 /**
1831  * lpfc_sli_update_full_ring - Update the chip attention register
1832  * @phba: Pointer to HBA context object.
1833  * @pring: Pointer to driver SLI ring object.
1834  *
1835  * The caller is not required to hold any lock for calling this function.
1836  * This function updates the chip attention bits for the ring to inform firmware
1837  * that there are pending work to be done for this ring and requests an
1838  * interrupt when there is space available in the ring. This function is
1839  * called when the driver is unable to post more iocbs to the ring due
1840  * to unavailability of space in the ring.
1841  **/
1842 static void
1843 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1844 {
1845         int ringno = pring->ringno;
1846
1847         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1848
1849         wmb();
1850
1851         /*
1852          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1853          * The HBA will tell us when an IOCB entry is available.
1854          */
1855         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1856         readl(phba->CAregaddr); /* flush */
1857
1858         pring->stats.iocb_cmd_full++;
1859 }
1860
1861 /**
1862  * lpfc_sli_update_ring - Update chip attention register
1863  * @phba: Pointer to HBA context object.
1864  * @pring: Pointer to driver SLI ring object.
1865  *
1866  * This function updates the chip attention register bit for the
1867  * given ring to inform HBA that there is more work to be done
1868  * in this ring. The caller is not required to hold any lock.
1869  **/
1870 static void
1871 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1872 {
1873         int ringno = pring->ringno;
1874
1875         /*
1876          * Tell the HBA that there is work to do in this ring.
1877          */
1878         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1879                 wmb();
1880                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1881                 readl(phba->CAregaddr); /* flush */
1882         }
1883 }
1884
1885 /**
1886  * lpfc_sli_resume_iocb - Process iocbs in the txq
1887  * @phba: Pointer to HBA context object.
1888  * @pring: Pointer to driver SLI ring object.
1889  *
1890  * This function is called with hbalock held to post pending iocbs
1891  * in the txq to the firmware. This function is called when driver
1892  * detects space available in the ring.
1893  **/
1894 static void
1895 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1896 {
1897         IOCB_t *iocb;
1898         struct lpfc_iocbq *nextiocb;
1899
1900         lockdep_assert_held(&phba->hbalock);
1901
1902         /*
1903          * Check to see if:
1904          *  (a) there is anything on the txq to send
1905          *  (b) link is up
1906          *  (c) link attention events can be processed (fcp ring only)
1907          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1908          */
1909
1910         if (lpfc_is_link_up(phba) &&
1911             (!list_empty(&pring->txq)) &&
1912             (pring->ringno != LPFC_FCP_RING ||
1913              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1914
1915                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1916                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1917                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1918
1919                 if (iocb)
1920                         lpfc_sli_update_ring(phba, pring);
1921                 else
1922                         lpfc_sli_update_full_ring(phba, pring);
1923         }
1924
1925         return;
1926 }
1927
1928 /**
1929  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1930  * @phba: Pointer to HBA context object.
1931  * @hbqno: HBQ number.
1932  *
1933  * This function is called with hbalock held to get the next
1934  * available slot for the given HBQ. If there is free slot
1935  * available for the HBQ it will return pointer to the next available
1936  * HBQ entry else it will return NULL.
1937  **/
1938 static struct lpfc_hbq_entry *
1939 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1940 {
1941         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1942
1943         lockdep_assert_held(&phba->hbalock);
1944
1945         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1946             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1947                 hbqp->next_hbqPutIdx = 0;
1948
1949         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1950                 uint32_t raw_index = phba->hbq_get[hbqno];
1951                 uint32_t getidx = le32_to_cpu(raw_index);
1952
1953                 hbqp->local_hbqGetIdx = getidx;
1954
1955                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1956                         lpfc_printf_log(phba, KERN_ERR,
1957                                         LOG_SLI | LOG_VPORT,
1958                                         "1802 HBQ %d: local_hbqGetIdx "
1959                                         "%u is > than hbqp->entry_count %u\n",
1960                                         hbqno, hbqp->local_hbqGetIdx,
1961                                         hbqp->entry_count);
1962
1963                         phba->link_state = LPFC_HBA_ERROR;
1964                         return NULL;
1965                 }
1966
1967                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1968                         return NULL;
1969         }
1970
1971         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1972                         hbqp->hbqPutIdx;
1973 }
1974
1975 /**
1976  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1977  * @phba: Pointer to HBA context object.
1978  *
1979  * This function is called with no lock held to free all the
1980  * hbq buffers while uninitializing the SLI interface. It also
1981  * frees the HBQ buffers returned by the firmware but not yet
1982  * processed by the upper layers.
1983  **/
1984 void
1985 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1986 {
1987         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1988         struct hbq_dmabuf *hbq_buf;
1989         unsigned long flags;
1990         int i, hbq_count;
1991
1992         hbq_count = lpfc_sli_hbq_count();
1993         /* Return all memory used by all HBQs */
1994         spin_lock_irqsave(&phba->hbalock, flags);
1995         for (i = 0; i < hbq_count; ++i) {
1996                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1997                                 &phba->hbqs[i].hbq_buffer_list, list) {
1998                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1999                         list_del(&hbq_buf->dbuf.list);
2000                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2001                 }
2002                 phba->hbqs[i].buffer_count = 0;
2003         }
2004
2005         /* Mark the HBQs not in use */
2006         phba->hbq_in_use = 0;
2007         spin_unlock_irqrestore(&phba->hbalock, flags);
2008 }
2009
2010 /**
2011  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
2012  * @phba: Pointer to HBA context object.
2013  * @hbqno: HBQ number.
2014  * @hbq_buf: Pointer to HBQ buffer.
2015  *
2016  * This function is called with the hbalock held to post a
2017  * hbq buffer to the firmware. If the function finds an empty
2018  * slot in the HBQ, it will post the buffer. The function will return
2019  * pointer to the hbq entry if it successfully post the buffer
2020  * else it will return NULL.
2021  **/
2022 static int
2023 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
2024                          struct hbq_dmabuf *hbq_buf)
2025 {
2026         lockdep_assert_held(&phba->hbalock);
2027         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2028 }
2029
2030 /**
2031  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2032  * @phba: Pointer to HBA context object.
2033  * @hbqno: HBQ number.
2034  * @hbq_buf: Pointer to HBQ buffer.
2035  *
2036  * This function is called with the hbalock held to post a hbq buffer to the
2037  * firmware. If the function finds an empty slot in the HBQ, it will post the
2038  * buffer and place it on the hbq_buffer_list. The function will return zero if
2039  * it successfully post the buffer else it will return an error.
2040  **/
2041 static int
2042 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2043                             struct hbq_dmabuf *hbq_buf)
2044 {
2045         struct lpfc_hbq_entry *hbqe;
2046         dma_addr_t physaddr = hbq_buf->dbuf.phys;
2047
2048         lockdep_assert_held(&phba->hbalock);
2049         /* Get next HBQ entry slot to use */
2050         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2051         if (hbqe) {
2052                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2053
2054                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2055                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
2056                 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
2057                 hbqe->bde.tus.f.bdeFlags = 0;
2058                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2059                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2060                                 /* Sync SLIM */
2061                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2062                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
2063                                 /* flush */
2064                 readl(phba->hbq_put + hbqno);
2065                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
2066                 return 0;
2067         } else
2068                 return -ENOMEM;
2069 }
2070
2071 /**
2072  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2073  * @phba: Pointer to HBA context object.
2074  * @hbqno: HBQ number.
2075  * @hbq_buf: Pointer to HBQ buffer.
2076  *
2077  * This function is called with the hbalock held to post an RQE to the SLI4
2078  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2079  * the hbq_buffer_list and return zero, otherwise it will return an error.
2080  **/
2081 static int
2082 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2083                             struct hbq_dmabuf *hbq_buf)
2084 {
2085         int rc;
2086         struct lpfc_rqe hrqe;
2087         struct lpfc_rqe drqe;
2088         struct lpfc_queue *hrq;
2089         struct lpfc_queue *drq;
2090
2091         if (hbqno != LPFC_ELS_HBQ)
2092                 return 1;
2093         hrq = phba->sli4_hba.hdr_rq;
2094         drq = phba->sli4_hba.dat_rq;
2095
2096         lockdep_assert_held(&phba->hbalock);
2097         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2098         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2099         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2100         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
2101         rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
2102         if (rc < 0)
2103                 return rc;
2104         hbq_buf->tag = (rc | (hbqno << 16));
2105         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2106         return 0;
2107 }
2108
2109 /* HBQ for ELS and CT traffic. */
2110 static struct lpfc_hbq_init lpfc_els_hbq = {
2111         .rn = 1,
2112         .entry_count = 256,
2113         .mask_count = 0,
2114         .profile = 0,
2115         .ring_mask = (1 << LPFC_ELS_RING),
2116         .buffer_count = 0,
2117         .init_count = 40,
2118         .add_count = 40,
2119 };
2120
2121 /* Array of HBQs */
2122 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
2123         &lpfc_els_hbq,
2124 };
2125
2126 /**
2127  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
2128  * @phba: Pointer to HBA context object.
2129  * @hbqno: HBQ number.
2130  * @count: Number of HBQ buffers to be posted.
2131  *
2132  * This function is called with no lock held to post more hbq buffers to the
2133  * given HBQ. The function returns the number of HBQ buffers successfully
2134  * posted.
2135  **/
2136 static int
2137 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2138 {
2139         uint32_t i, posted = 0;
2140         unsigned long flags;
2141         struct hbq_dmabuf *hbq_buffer;
2142         LIST_HEAD(hbq_buf_list);
2143         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
2144                 return 0;
2145
2146         if ((phba->hbqs[hbqno].buffer_count + count) >
2147             lpfc_hbq_defs[hbqno]->entry_count)
2148                 count = lpfc_hbq_defs[hbqno]->entry_count -
2149                                         phba->hbqs[hbqno].buffer_count;
2150         if (!count)
2151                 return 0;
2152         /* Allocate HBQ entries */
2153         for (i = 0; i < count; i++) {
2154                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2155                 if (!hbq_buffer)
2156                         break;
2157                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2158         }
2159         /* Check whether HBQ is still in use */
2160         spin_lock_irqsave(&phba->hbalock, flags);
2161         if (!phba->hbq_in_use)
2162                 goto err;
2163         while (!list_empty(&hbq_buf_list)) {
2164                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2165                                  dbuf.list);
2166                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2167                                       (hbqno << 16));
2168                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
2169                         phba->hbqs[hbqno].buffer_count++;
2170                         posted++;
2171                 } else
2172                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2173         }
2174         spin_unlock_irqrestore(&phba->hbalock, flags);
2175         return posted;
2176 err:
2177         spin_unlock_irqrestore(&phba->hbalock, flags);
2178         while (!list_empty(&hbq_buf_list)) {
2179                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2180                                  dbuf.list);
2181                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2182         }
2183         return 0;
2184 }
2185
2186 /**
2187  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
2188  * @phba: Pointer to HBA context object.
2189  * @qno: HBQ number.
2190  *
2191  * This function posts more buffers to the HBQ. This function
2192  * is called with no lock held. The function returns the number of HBQ entries
2193  * successfully allocated.
2194  **/
2195 int
2196 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
2197 {
2198         if (phba->sli_rev == LPFC_SLI_REV4)
2199                 return 0;
2200         else
2201                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2202                                          lpfc_hbq_defs[qno]->add_count);
2203 }
2204
2205 /**
2206  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
2207  * @phba: Pointer to HBA context object.
2208  * @qno:  HBQ queue number.
2209  *
2210  * This function is called from SLI initialization code path with
2211  * no lock held to post initial HBQ buffers to firmware. The
2212  * function returns the number of HBQ entries successfully allocated.
2213  **/
2214 static int
2215 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2216 {
2217         if (phba->sli_rev == LPFC_SLI_REV4)
2218                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2219                                         lpfc_hbq_defs[qno]->entry_count);
2220         else
2221                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2222                                          lpfc_hbq_defs[qno]->init_count);
2223 }
2224
2225 /**
2226  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2227  * @phba: Pointer to HBA context object.
2228  * @hbqno: HBQ number.
2229  *
2230  * This function removes the first hbq buffer on an hbq list and returns a
2231  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2232  **/
2233 static struct hbq_dmabuf *
2234 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2235 {
2236         struct lpfc_dmabuf *d_buf;
2237
2238         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2239         if (!d_buf)
2240                 return NULL;
2241         return container_of(d_buf, struct hbq_dmabuf, dbuf);
2242 }
2243
2244 /**
2245  * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2246  * @phba: Pointer to HBA context object.
2247  * @hbqno: HBQ number.
2248  *
2249  * This function removes the first RQ buffer on an RQ buffer list and returns a
2250  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2251  **/
2252 static struct rqb_dmabuf *
2253 lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2254 {
2255         struct lpfc_dmabuf *h_buf;
2256         struct lpfc_rqb *rqbp;
2257
2258         rqbp = hrq->rqbp;
2259         list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2260                          struct lpfc_dmabuf, list);
2261         if (!h_buf)
2262                 return NULL;
2263         rqbp->buffer_count--;
2264         return container_of(h_buf, struct rqb_dmabuf, hbuf);
2265 }
2266
2267 /**
2268  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
2269  * @phba: Pointer to HBA context object.
2270  * @tag: Tag of the hbq buffer.
2271  *
2272  * This function searches for the hbq buffer associated with the given tag in
2273  * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2274  * otherwise it returns NULL.
2275  **/
2276 static struct hbq_dmabuf *
2277 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2278 {
2279         struct lpfc_dmabuf *d_buf;
2280         struct hbq_dmabuf *hbq_buf;
2281         uint32_t hbqno;
2282
2283         hbqno = tag >> 16;
2284         if (hbqno >= LPFC_MAX_HBQS)
2285                 return NULL;
2286
2287         spin_lock_irq(&phba->hbalock);
2288         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2289                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2290                 if (hbq_buf->tag == tag) {
2291                         spin_unlock_irq(&phba->hbalock);
2292                         return hbq_buf;
2293                 }
2294         }
2295         spin_unlock_irq(&phba->hbalock);
2296         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
2297                         "1803 Bad hbq tag. Data: x%x x%x\n",
2298                         tag, phba->hbqs[tag >> 16].buffer_count);
2299         return NULL;
2300 }
2301
2302 /**
2303  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2304  * @phba: Pointer to HBA context object.
2305  * @hbq_buffer: Pointer to HBQ buffer.
2306  *
2307  * This function is called with hbalock. This function gives back
2308  * the hbq buffer to firmware. If the HBQ does not have space to
2309  * post the buffer, it will free the buffer.
2310  **/
2311 void
2312 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2313 {
2314         uint32_t hbqno;
2315
2316         if (hbq_buffer) {
2317                 hbqno = hbq_buffer->tag >> 16;
2318                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2319                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2320         }
2321 }
2322
2323 /**
2324  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2325  * @mbxCommand: mailbox command code.
2326  *
2327  * This function is called by the mailbox event handler function to verify
2328  * that the completed mailbox command is a legitimate mailbox command. If the
2329  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2330  * and the mailbox event handler will take the HBA offline.
2331  **/
2332 static int
2333 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2334 {
2335         uint8_t ret;
2336
2337         switch (mbxCommand) {
2338         case MBX_LOAD_SM:
2339         case MBX_READ_NV:
2340         case MBX_WRITE_NV:
2341         case MBX_WRITE_VPARMS:
2342         case MBX_RUN_BIU_DIAG:
2343         case MBX_INIT_LINK:
2344         case MBX_DOWN_LINK:
2345         case MBX_CONFIG_LINK:
2346         case MBX_CONFIG_RING:
2347         case MBX_RESET_RING:
2348         case MBX_READ_CONFIG:
2349         case MBX_READ_RCONFIG:
2350         case MBX_READ_SPARM:
2351         case MBX_READ_STATUS:
2352         case MBX_READ_RPI:
2353         case MBX_READ_XRI:
2354         case MBX_READ_REV:
2355         case MBX_READ_LNK_STAT:
2356         case MBX_REG_LOGIN:
2357         case MBX_UNREG_LOGIN:
2358         case MBX_CLEAR_LA:
2359         case MBX_DUMP_MEMORY:
2360         case MBX_DUMP_CONTEXT:
2361         case MBX_RUN_DIAGS:
2362         case MBX_RESTART:
2363         case MBX_UPDATE_CFG:
2364         case MBX_DOWN_LOAD:
2365         case MBX_DEL_LD_ENTRY:
2366         case MBX_RUN_PROGRAM:
2367         case MBX_SET_MASK:
2368         case MBX_SET_VARIABLE:
2369         case MBX_UNREG_D_ID:
2370         case MBX_KILL_BOARD:
2371         case MBX_CONFIG_FARP:
2372         case MBX_BEACON:
2373         case MBX_LOAD_AREA:
2374         case MBX_RUN_BIU_DIAG64:
2375         case MBX_CONFIG_PORT:
2376         case MBX_READ_SPARM64:
2377         case MBX_READ_RPI64:
2378         case MBX_REG_LOGIN64:
2379         case MBX_READ_TOPOLOGY:
2380         case MBX_WRITE_WWN:
2381         case MBX_SET_DEBUG:
2382         case MBX_LOAD_EXP_ROM:
2383         case MBX_ASYNCEVT_ENABLE:
2384         case MBX_REG_VPI:
2385         case MBX_UNREG_VPI:
2386         case MBX_HEARTBEAT:
2387         case MBX_PORT_CAPABILITIES:
2388         case MBX_PORT_IOV_CONTROL:
2389         case MBX_SLI4_CONFIG:
2390         case MBX_SLI4_REQ_FTRS:
2391         case MBX_REG_FCFI:
2392         case MBX_UNREG_FCFI:
2393         case MBX_REG_VFI:
2394         case MBX_UNREG_VFI:
2395         case MBX_INIT_VPI:
2396         case MBX_INIT_VFI:
2397         case MBX_RESUME_RPI:
2398         case MBX_READ_EVENT_LOG_STATUS:
2399         case MBX_READ_EVENT_LOG:
2400         case MBX_SECURITY_MGMT:
2401         case MBX_AUTH_PORT:
2402         case MBX_ACCESS_VDATA:
2403                 ret = mbxCommand;
2404                 break;
2405         default:
2406                 ret = MBX_SHUTDOWN;
2407                 break;
2408         }
2409         return ret;
2410 }
2411
2412 /**
2413  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2414  * @phba: Pointer to HBA context object.
2415  * @pmboxq: Pointer to mailbox command.
2416  *
2417  * This is completion handler function for mailbox commands issued from
2418  * lpfc_sli_issue_mbox_wait function. This function is called by the
2419  * mailbox event handler function with no lock held. This function
2420  * will wake up thread waiting on the wait queue pointed by context1
2421  * of the mailbox.
2422  **/
2423 void
2424 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2425 {
2426         unsigned long drvr_flag;
2427         struct completion *pmbox_done;
2428
2429         /*
2430          * If pmbox_done is empty, the driver thread gave up waiting and
2431          * continued running.
2432          */
2433         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2434         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2435         pmbox_done = (struct completion *)pmboxq->context3;
2436         if (pmbox_done)
2437                 complete(pmbox_done);
2438         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2439         return;
2440 }
2441
2442
2443 /**
2444  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2445  * @phba: Pointer to HBA context object.
2446  * @pmb: Pointer to mailbox object.
2447  *
2448  * This function is the default mailbox completion handler. It
2449  * frees the memory resources associated with the completed mailbox
2450  * command. If the completed command is a REG_LOGIN mailbox command,
2451  * this function will issue a UREG_LOGIN to re-claim the RPI.
2452  **/
2453 void
2454 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2455 {
2456         struct lpfc_vport  *vport = pmb->vport;
2457         struct lpfc_dmabuf *mp;
2458         struct lpfc_nodelist *ndlp;
2459         struct Scsi_Host *shost;
2460         uint16_t rpi, vpi;
2461         int rc;
2462
2463         mp = (struct lpfc_dmabuf *) (pmb->context1);
2464
2465         if (mp) {
2466                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2467                 kfree(mp);
2468         }
2469
2470         /*
2471          * If a REG_LOGIN succeeded  after node is destroyed or node
2472          * is in re-discovery driver need to cleanup the RPI.
2473          */
2474         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2475             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2476             !pmb->u.mb.mbxStatus) {
2477                 rpi = pmb->u.mb.un.varWords[0];
2478                 vpi = pmb->u.mb.un.varRegLogin.vpi;
2479                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2480                 pmb->vport = vport;
2481                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2482                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2483                 if (rc != MBX_NOT_FINISHED)
2484                         return;
2485         }
2486
2487         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2488                 !(phba->pport->load_flag & FC_UNLOADING) &&
2489                 !pmb->u.mb.mbxStatus) {
2490                 shost = lpfc_shost_from_vport(vport);
2491                 spin_lock_irq(shost->host_lock);
2492                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2493                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2494                 spin_unlock_irq(shost->host_lock);
2495         }
2496
2497         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2498                 ndlp = (struct lpfc_nodelist *)pmb->context2;
2499                 lpfc_nlp_put(ndlp);
2500                 pmb->context2 = NULL;
2501         }
2502
2503         /* Check security permission status on INIT_LINK mailbox command */
2504         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2505             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2506                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2507                                 "2860 SLI authentication is required "
2508                                 "for INIT_LINK but has not done yet\n");
2509
2510         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2511                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2512         else
2513                 mempool_free(pmb, phba->mbox_mem_pool);
2514 }
2515  /**
2516  * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2517  * @phba: Pointer to HBA context object.
2518  * @pmb: Pointer to mailbox object.
2519  *
2520  * This function is the unreg rpi mailbox completion handler. It
2521  * frees the memory resources associated with the completed mailbox
2522  * command. An additional refrenece is put on the ndlp to prevent
2523  * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2524  * the unreg mailbox command completes, this routine puts the
2525  * reference back.
2526  *
2527  **/
2528 void
2529 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2530 {
2531         struct lpfc_vport  *vport = pmb->vport;
2532         struct lpfc_nodelist *ndlp;
2533
2534         ndlp = pmb->context1;
2535         if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2536                 if (phba->sli_rev == LPFC_SLI_REV4 &&
2537                     (bf_get(lpfc_sli_intf_if_type,
2538                      &phba->sli4_hba.sli_intf) >=
2539                      LPFC_SLI_INTF_IF_TYPE_2)) {
2540                         if (ndlp) {
2541                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2542                                                  "0010 UNREG_LOGIN vpi:%x "
2543                                                  "rpi:%x DID:%x map:%x %p\n",
2544                                                  vport->vpi, ndlp->nlp_rpi,
2545                                                  ndlp->nlp_DID,
2546                                                  ndlp->nlp_usg_map, ndlp);
2547                                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2548                                 lpfc_nlp_put(ndlp);
2549                         }
2550                 }
2551         }
2552
2553         mempool_free(pmb, phba->mbox_mem_pool);
2554 }
2555
2556 /**
2557  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2558  * @phba: Pointer to HBA context object.
2559  *
2560  * This function is called with no lock held. This function processes all
2561  * the completed mailbox commands and gives it to upper layers. The interrupt
2562  * service routine processes mailbox completion interrupt and adds completed
2563  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2564  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2565  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2566  * function returns the mailbox commands to the upper layer by calling the
2567  * completion handler function of each mailbox.
2568  **/
2569 int
2570 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2571 {
2572         MAILBOX_t *pmbox;
2573         LPFC_MBOXQ_t *pmb;
2574         int rc;
2575         LIST_HEAD(cmplq);
2576
2577         phba->sli.slistat.mbox_event++;
2578
2579         /* Get all completed mailboxe buffers into the cmplq */
2580         spin_lock_irq(&phba->hbalock);
2581         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2582         spin_unlock_irq(&phba->hbalock);
2583
2584         /* Get a Mailbox buffer to setup mailbox commands for callback */
2585         do {
2586                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2587                 if (pmb == NULL)
2588                         break;
2589
2590                 pmbox = &pmb->u.mb;
2591
2592                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2593                         if (pmb->vport) {
2594                                 lpfc_debugfs_disc_trc(pmb->vport,
2595                                         LPFC_DISC_TRC_MBOX_VPORT,
2596                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2597                                         (uint32_t)pmbox->mbxCommand,
2598                                         pmbox->un.varWords[0],
2599                                         pmbox->un.varWords[1]);
2600                         }
2601                         else {
2602                                 lpfc_debugfs_disc_trc(phba->pport,
2603                                         LPFC_DISC_TRC_MBOX,
2604                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2605                                         (uint32_t)pmbox->mbxCommand,
2606                                         pmbox->un.varWords[0],
2607                                         pmbox->un.varWords[1]);
2608                         }
2609                 }
2610
2611                 /*
2612                  * It is a fatal error if unknown mbox command completion.
2613                  */
2614                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2615                     MBX_SHUTDOWN) {
2616                         /* Unknown mailbox command compl */
2617                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2618                                         "(%d):0323 Unknown Mailbox command "
2619                                         "x%x (x%x/x%x) Cmpl\n",
2620                                         pmb->vport ? pmb->vport->vpi : 0,
2621                                         pmbox->mbxCommand,
2622                                         lpfc_sli_config_mbox_subsys_get(phba,
2623                                                                         pmb),
2624                                         lpfc_sli_config_mbox_opcode_get(phba,
2625                                                                         pmb));
2626                         phba->link_state = LPFC_HBA_ERROR;
2627                         phba->work_hs = HS_FFER3;
2628                         lpfc_handle_eratt(phba);
2629                         continue;
2630                 }
2631
2632                 if (pmbox->mbxStatus) {
2633                         phba->sli.slistat.mbox_stat_err++;
2634                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2635                                 /* Mbox cmd cmpl error - RETRYing */
2636                                 lpfc_printf_log(phba, KERN_INFO,
2637                                         LOG_MBOX | LOG_SLI,
2638                                         "(%d):0305 Mbox cmd cmpl "
2639                                         "error - RETRYing Data: x%x "
2640                                         "(x%x/x%x) x%x x%x x%x\n",
2641                                         pmb->vport ? pmb->vport->vpi : 0,
2642                                         pmbox->mbxCommand,
2643                                         lpfc_sli_config_mbox_subsys_get(phba,
2644                                                                         pmb),
2645                                         lpfc_sli_config_mbox_opcode_get(phba,
2646                                                                         pmb),
2647                                         pmbox->mbxStatus,
2648                                         pmbox->un.varWords[0],
2649                                         pmb->vport->port_state);
2650                                 pmbox->mbxStatus = 0;
2651                                 pmbox->mbxOwner = OWN_HOST;
2652                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2653                                 if (rc != MBX_NOT_FINISHED)
2654                                         continue;
2655                         }
2656                 }
2657
2658                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2659                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2660                                 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2661                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2662                                 "x%x x%x x%x\n",
2663                                 pmb->vport ? pmb->vport->vpi : 0,
2664                                 pmbox->mbxCommand,
2665                                 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2666                                 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2667                                 pmb->mbox_cmpl,
2668                                 *((uint32_t *) pmbox),
2669                                 pmbox->un.varWords[0],
2670                                 pmbox->un.varWords[1],
2671                                 pmbox->un.varWords[2],
2672                                 pmbox->un.varWords[3],
2673                                 pmbox->un.varWords[4],
2674                                 pmbox->un.varWords[5],
2675                                 pmbox->un.varWords[6],
2676                                 pmbox->un.varWords[7],
2677                                 pmbox->un.varWords[8],
2678                                 pmbox->un.varWords[9],
2679                                 pmbox->un.varWords[10]);
2680
2681                 if (pmb->mbox_cmpl)
2682                         pmb->mbox_cmpl(phba,pmb);
2683         } while (1);
2684         return 0;
2685 }
2686
2687 /**
2688  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2689  * @phba: Pointer to HBA context object.
2690  * @pring: Pointer to driver SLI ring object.
2691  * @tag: buffer tag.
2692  *
2693  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2694  * is set in the tag the buffer is posted for a particular exchange,
2695  * the function will return the buffer without replacing the buffer.
2696  * If the buffer is for unsolicited ELS or CT traffic, this function
2697  * returns the buffer and also posts another buffer to the firmware.
2698  **/
2699 static struct lpfc_dmabuf *
2700 lpfc_sli_get_buff(struct lpfc_hba *phba,
2701                   struct lpfc_sli_ring *pring,
2702                   uint32_t tag)
2703 {
2704         struct hbq_dmabuf *hbq_entry;
2705
2706         if (tag & QUE_BUFTAG_BIT)
2707                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2708         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2709         if (!hbq_entry)
2710                 return NULL;
2711         return &hbq_entry->dbuf;
2712 }
2713
2714 /**
2715  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2716  * @phba: Pointer to HBA context object.
2717  * @pring: Pointer to driver SLI ring object.
2718  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2719  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2720  * @fch_type: the type for the first frame of the sequence.
2721  *
2722  * This function is called with no lock held. This function uses the r_ctl and
2723  * type of the received sequence to find the correct callback function to call
2724  * to process the sequence.
2725  **/
2726 static int
2727 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2728                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2729                          uint32_t fch_type)
2730 {
2731         int i;
2732
2733         switch (fch_type) {
2734         case FC_TYPE_NVME:
2735                 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
2736                 return 1;
2737         default:
2738                 break;
2739         }
2740
2741         /* unSolicited Responses */
2742         if (pring->prt[0].profile) {
2743                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2744                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2745                                                                         saveq);
2746                 return 1;
2747         }
2748         /* We must search, based on rctl / type
2749            for the right routine */
2750         for (i = 0; i < pring->num_mask; i++) {
2751                 if ((pring->prt[i].rctl == fch_r_ctl) &&
2752                     (pring->prt[i].type == fch_type)) {
2753                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2754                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2755                                                 (phba, pring, saveq);
2756                         return 1;
2757                 }
2758         }
2759         return 0;
2760 }
2761
2762 /**
2763  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2764  * @phba: Pointer to HBA context object.
2765  * @pring: Pointer to driver SLI ring object.
2766  * @saveq: Pointer to the unsolicited iocb.
2767  *
2768  * This function is called with no lock held by the ring event handler
2769  * when there is an unsolicited iocb posted to the response ring by the
2770  * firmware. This function gets the buffer associated with the iocbs
2771  * and calls the event handler for the ring. This function handles both
2772  * qring buffers and hbq buffers.
2773  * When the function returns 1 the caller can free the iocb object otherwise
2774  * upper layer functions will free the iocb objects.
2775  **/
2776 static int
2777 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2778                             struct lpfc_iocbq *saveq)
2779 {
2780         IOCB_t           * irsp;
2781         WORD5            * w5p;
2782         uint32_t           Rctl, Type;
2783         struct lpfc_iocbq *iocbq;
2784         struct lpfc_dmabuf *dmzbuf;
2785
2786         irsp = &(saveq->iocb);
2787
2788         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2789                 if (pring->lpfc_sli_rcv_async_status)
2790                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2791                 else
2792                         lpfc_printf_log(phba,
2793                                         KERN_WARNING,
2794                                         LOG_SLI,
2795                                         "0316 Ring %d handler: unexpected "
2796                                         "ASYNC_STATUS iocb received evt_code "
2797                                         "0x%x\n",
2798                                         pring->ringno,
2799                                         irsp->un.asyncstat.evt_code);
2800                 return 1;
2801         }
2802
2803         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2804                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2805                 if (irsp->ulpBdeCount > 0) {
2806                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2807                                         irsp->un.ulpWord[3]);
2808                         lpfc_in_buf_free(phba, dmzbuf);
2809                 }
2810
2811                 if (irsp->ulpBdeCount > 1) {
2812                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2813                                         irsp->unsli3.sli3Words[3]);
2814                         lpfc_in_buf_free(phba, dmzbuf);
2815                 }
2816
2817                 if (irsp->ulpBdeCount > 2) {
2818                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2819                                 irsp->unsli3.sli3Words[7]);
2820                         lpfc_in_buf_free(phba, dmzbuf);
2821                 }
2822
2823                 return 1;
2824         }
2825
2826         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2827                 if (irsp->ulpBdeCount != 0) {
2828                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2829                                                 irsp->un.ulpWord[3]);
2830                         if (!saveq->context2)
2831                                 lpfc_printf_log(phba,
2832                                         KERN_ERR,
2833                                         LOG_SLI,
2834                                         "0341 Ring %d Cannot find buffer for "
2835                                         "an unsolicited iocb. tag 0x%x\n",
2836                                         pring->ringno,
2837                                         irsp->un.ulpWord[3]);
2838                 }
2839                 if (irsp->ulpBdeCount == 2) {
2840                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2841                                                 irsp->unsli3.sli3Words[7]);
2842                         if (!saveq->context3)
2843                                 lpfc_printf_log(phba,
2844                                         KERN_ERR,
2845                                         LOG_SLI,
2846                                         "0342 Ring %d Cannot find buffer for an"
2847                                         " unsolicited iocb. tag 0x%x\n",
2848                                         pring->ringno,
2849                                         irsp->unsli3.sli3Words[7]);
2850                 }
2851                 list_for_each_entry(iocbq, &saveq->list, list) {
2852                         irsp = &(iocbq->iocb);
2853                         if (irsp->ulpBdeCount != 0) {
2854                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2855                                                         irsp->un.ulpWord[3]);
2856                                 if (!iocbq->context2)
2857                                         lpfc_printf_log(phba,
2858                                                 KERN_ERR,
2859                                                 LOG_SLI,
2860                                                 "0343 Ring %d Cannot find "
2861                                                 "buffer for an unsolicited iocb"
2862                                                 ". tag 0x%x\n", pring->ringno,
2863                                                 irsp->un.ulpWord[3]);
2864                         }
2865                         if (irsp->ulpBdeCount == 2) {
2866                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2867                                                 irsp->unsli3.sli3Words[7]);
2868                                 if (!iocbq->context3)
2869                                         lpfc_printf_log(phba,
2870                                                 KERN_ERR,
2871                                                 LOG_SLI,
2872                                                 "0344 Ring %d Cannot find "
2873                                                 "buffer for an unsolicited "
2874                                                 "iocb. tag 0x%x\n",
2875                                                 pring->ringno,
2876                                                 irsp->unsli3.sli3Words[7]);
2877                         }
2878                 }
2879         }
2880         if (irsp->ulpBdeCount != 0 &&
2881             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2882              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2883                 int found = 0;
2884
2885                 /* search continue save q for same XRI */
2886                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2887                         if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2888                                 saveq->iocb.unsli3.rcvsli3.ox_id) {
2889                                 list_add_tail(&saveq->list, &iocbq->list);
2890                                 found = 1;
2891                                 break;
2892                         }
2893                 }
2894                 if (!found)
2895                         list_add_tail(&saveq->clist,
2896                                       &pring->iocb_continue_saveq);
2897                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2898                         list_del_init(&iocbq->clist);
2899                         saveq = iocbq;
2900                         irsp = &(saveq->iocb);
2901                 } else
2902                         return 0;
2903         }
2904         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2905             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2906             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2907                 Rctl = FC_RCTL_ELS_REQ;
2908                 Type = FC_TYPE_ELS;
2909         } else {
2910                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2911                 Rctl = w5p->hcsw.Rctl;
2912                 Type = w5p->hcsw.Type;
2913
2914                 /* Firmware Workaround */
2915                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2916                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2917                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2918                         Rctl = FC_RCTL_ELS_REQ;
2919                         Type = FC_TYPE_ELS;
2920                         w5p->hcsw.Rctl = Rctl;
2921                         w5p->hcsw.Type = Type;
2922                 }
2923         }
2924
2925         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2926                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2927                                 "0313 Ring %d handler: unexpected Rctl x%x "
2928                                 "Type x%x received\n",
2929                                 pring->ringno, Rctl, Type);
2930
2931         return 1;
2932 }
2933
2934 /**
2935  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2936  * @phba: Pointer to HBA context object.
2937  * @pring: Pointer to driver SLI ring object.
2938  * @prspiocb: Pointer to response iocb object.
2939  *
2940  * This function looks up the iocb_lookup table to get the command iocb
2941  * corresponding to the given response iocb using the iotag of the
2942  * response iocb. This function is called with the hbalock held
2943  * for sli3 devices or the ring_lock for sli4 devices.
2944  * This function returns the command iocb object if it finds the command
2945  * iocb else returns NULL.
2946  **/
2947 static struct lpfc_iocbq *
2948 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2949                       struct lpfc_sli_ring *pring,
2950                       struct lpfc_iocbq *prspiocb)
2951 {
2952         struct lpfc_iocbq *cmd_iocb = NULL;
2953         uint16_t iotag;
2954         lockdep_assert_held(&phba->hbalock);
2955
2956         iotag = prspiocb->iocb.ulpIoTag;
2957
2958         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2959                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2960                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2961                         /* remove from txcmpl queue list */
2962                         list_del_init(&cmd_iocb->list);
2963                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2964                         return cmd_iocb;
2965                 }
2966         }
2967
2968         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2969                         "0317 iotag x%x is out of "
2970                         "range: max iotag x%x wd0 x%x\n",
2971                         iotag, phba->sli.last_iotag,
2972                         *(((uint32_t *) &prspiocb->iocb) + 7));
2973         return NULL;
2974 }
2975
2976 /**
2977  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2978  * @phba: Pointer to HBA context object.
2979  * @pring: Pointer to driver SLI ring object.
2980  * @iotag: IOCB tag.
2981  *
2982  * This function looks up the iocb_lookup table to get the command iocb
2983  * corresponding to the given iotag. This function is called with the
2984  * hbalock held.
2985  * This function returns the command iocb object if it finds the command
2986  * iocb else returns NULL.
2987  **/
2988 static struct lpfc_iocbq *
2989 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2990                              struct lpfc_sli_ring *pring, uint16_t iotag)
2991 {
2992         struct lpfc_iocbq *cmd_iocb = NULL;
2993
2994         lockdep_assert_held(&phba->hbalock);
2995         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2996                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2997                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2998                         /* remove from txcmpl queue list */
2999                         list_del_init(&cmd_iocb->list);
3000                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3001                         return cmd_iocb;
3002                 }
3003         }
3004
3005         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3006                         "0372 iotag x%x lookup error: max iotag (x%x) "
3007                         "iocb_flag x%x\n",
3008                         iotag, phba->sli.last_iotag,
3009                         cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3010         return NULL;
3011 }
3012
3013 /**
3014  * lpfc_sli_process_sol_iocb - process solicited iocb completion
3015  * @phba: Pointer to HBA context object.
3016  * @pring: Pointer to driver SLI ring object.
3017  * @saveq: Pointer to the response iocb to be processed.
3018  *
3019  * This function is called by the ring event handler for non-fcp
3020  * rings when there is a new response iocb in the response ring.
3021  * The caller is not required to hold any locks. This function
3022  * gets the command iocb associated with the response iocb and
3023  * calls the completion handler for the command iocb. If there
3024  * is no completion handler, the function will free the resources
3025  * associated with command iocb. If the response iocb is for
3026  * an already aborted command iocb, the status of the completion
3027  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3028  * This function always returns 1.
3029  **/
3030 static int
3031 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3032                           struct lpfc_iocbq *saveq)
3033 {
3034         struct lpfc_iocbq *cmdiocbp;
3035         int rc = 1;
3036         unsigned long iflag;
3037
3038         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
3039         if (phba->sli_rev == LPFC_SLI_REV4)
3040                 spin_lock_irqsave(&pring->ring_lock, iflag);
3041         else
3042                 spin_lock_irqsave(&phba->hbalock, iflag);
3043         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
3044         if (phba->sli_rev == LPFC_SLI_REV4)
3045                 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3046         else
3047                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3048
3049         if (cmdiocbp) {
3050                 if (cmdiocbp->iocb_cmpl) {
3051                         /*
3052                          * If an ELS command failed send an event to mgmt
3053                          * application.
3054                          */
3055                         if (saveq->iocb.ulpStatus &&
3056                              (pring->ringno == LPFC_ELS_RING) &&
3057                              (cmdiocbp->iocb.ulpCommand ==
3058                                 CMD_ELS_REQUEST64_CR))
3059                                 lpfc_send_els_failure_event(phba,
3060                                         cmdiocbp, saveq);
3061
3062                         /*
3063                          * Post all ELS completions to the worker thread.
3064                          * All other are passed to the completion callback.
3065                          */
3066                         if (pring->ringno == LPFC_ELS_RING) {
3067                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3068                                     (cmdiocbp->iocb_flag &
3069                                                         LPFC_DRIVER_ABORTED)) {
3070                                         spin_lock_irqsave(&phba->hbalock,
3071                                                           iflag);
3072                                         cmdiocbp->iocb_flag &=
3073                                                 ~LPFC_DRIVER_ABORTED;
3074                                         spin_unlock_irqrestore(&phba->hbalock,
3075                                                                iflag);
3076                                         saveq->iocb.ulpStatus =
3077                                                 IOSTAT_LOCAL_REJECT;
3078                                         saveq->iocb.un.ulpWord[4] =
3079                                                 IOERR_SLI_ABORTED;
3080
3081                                         /* Firmware could still be in progress
3082                                          * of DMAing payload, so don't free data
3083                                          * buffer till after a hbeat.
3084                                          */
3085                                         spin_lock_irqsave(&phba->hbalock,
3086                                                           iflag);
3087                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
3088                                         spin_unlock_irqrestore(&phba->hbalock,
3089                                                                iflag);
3090                                 }
3091                                 if (phba->sli_rev == LPFC_SLI_REV4) {
3092                                         if (saveq->iocb_flag &
3093                                             LPFC_EXCHANGE_BUSY) {
3094                                                 /* Set cmdiocb flag for the
3095                                                  * exchange busy so sgl (xri)
3096                                                  * will not be released until
3097                                                  * the abort xri is received
3098                                                  * from hba.
3099                                                  */
3100                                                 spin_lock_irqsave(
3101                                                         &phba->hbalock, iflag);
3102                                                 cmdiocbp->iocb_flag |=
3103                                                         LPFC_EXCHANGE_BUSY;
3104                                                 spin_unlock_irqrestore(
3105                                                         &phba->hbalock, iflag);
3106                                         }
3107                                         if (cmdiocbp->iocb_flag &
3108                                             LPFC_DRIVER_ABORTED) {
3109                                                 /*
3110                                                  * Clear LPFC_DRIVER_ABORTED
3111                                                  * bit in case it was driver
3112                                                  * initiated abort.
3113                                                  */
3114                                                 spin_lock_irqsave(
3115                                                         &phba->hbalock, iflag);
3116                                                 cmdiocbp->iocb_flag &=
3117                                                         ~LPFC_DRIVER_ABORTED;
3118                                                 spin_unlock_irqrestore(
3119                                                         &phba->hbalock, iflag);
3120                                                 cmdiocbp->iocb.ulpStatus =
3121                                                         IOSTAT_LOCAL_REJECT;
3122                                                 cmdiocbp->iocb.un.ulpWord[4] =
3123                                                         IOERR_ABORT_REQUESTED;
3124                                                 /*
3125                                                  * For SLI4, irsiocb contains
3126                                                  * NO_XRI in sli_xritag, it
3127                                                  * shall not affect releasing
3128                                                  * sgl (xri) process.
3129                                                  */
3130                                                 saveq->iocb.ulpStatus =
3131                                                         IOSTAT_LOCAL_REJECT;
3132                                                 saveq->iocb.un.ulpWord[4] =
3133                                                         IOERR_SLI_ABORTED;
3134                                                 spin_lock_irqsave(
3135                                                         &phba->hbalock, iflag);
3136                                                 saveq->iocb_flag |=
3137                                                         LPFC_DELAY_MEM_FREE;
3138                                                 spin_unlock_irqrestore(
3139                                                         &phba->hbalock, iflag);
3140                                         }
3141                                 }
3142                         }
3143                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
3144                 } else
3145                         lpfc_sli_release_iocbq(phba, cmdiocbp);
3146         } else {
3147                 /*
3148                  * Unknown initiating command based on the response iotag.
3149                  * This could be the case on the ELS ring because of
3150                  * lpfc_els_abort().
3151                  */
3152                 if (pring->ringno != LPFC_ELS_RING) {
3153                         /*
3154                          * Ring <ringno> handler: unexpected completion IoTag
3155                          * <IoTag>
3156                          */
3157                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3158                                          "0322 Ring %d handler: "
3159                                          "unexpected completion IoTag x%x "
3160                                          "Data: x%x x%x x%x x%x\n",
3161                                          pring->ringno,
3162                                          saveq->iocb.ulpIoTag,
3163                                          saveq->iocb.ulpStatus,
3164                                          saveq->iocb.un.ulpWord[4],
3165                                          saveq->iocb.ulpCommand,
3166                                          saveq->iocb.ulpContext);
3167                 }
3168         }
3169
3170         return rc;
3171 }
3172
3173 /**
3174  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
3175  * @phba: Pointer to HBA context object.
3176  * @pring: Pointer to driver SLI ring object.
3177  *
3178  * This function is called from the iocb ring event handlers when
3179  * put pointer is ahead of the get pointer for a ring. This function signal
3180  * an error attention condition to the worker thread and the worker
3181  * thread will transition the HBA to offline state.
3182  **/
3183 static void
3184 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3185 {
3186         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3187         /*
3188          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3189          * rsp ring <portRspMax>
3190          */
3191         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3192                         "0312 Ring %d handler: portRspPut %d "
3193                         "is bigger than rsp ring %d\n",
3194                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
3195                         pring->sli.sli3.numRiocb);
3196
3197         phba->link_state = LPFC_HBA_ERROR;
3198
3199         /*
3200          * All error attention handlers are posted to
3201          * worker thread
3202          */
3203         phba->work_ha |= HA_ERATT;
3204         phba->work_hs = HS_FFER3;
3205
3206         lpfc_worker_wake_up(phba);
3207
3208         return;
3209 }
3210
3211 /**
3212  * lpfc_poll_eratt - Error attention polling timer timeout handler
3213  * @ptr: Pointer to address of HBA context object.
3214  *
3215  * This function is invoked by the Error Attention polling timer when the
3216  * timer times out. It will check the SLI Error Attention register for
3217  * possible attention events. If so, it will post an Error Attention event
3218  * and wake up worker thread to process it. Otherwise, it will set up the
3219  * Error Attention polling timer for the next poll.
3220  **/
3221 void lpfc_poll_eratt(struct timer_list *t)
3222 {
3223         struct lpfc_hba *phba;
3224         uint32_t eratt = 0;
3225         uint64_t sli_intr, cnt;
3226
3227         phba = from_timer(phba, t, eratt_poll);
3228
3229         /* Here we will also keep track of interrupts per sec of the hba */
3230         sli_intr = phba->sli.slistat.sli_intr;
3231
3232         if (phba->sli.slistat.sli_prev_intr > sli_intr)
3233                 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3234                         sli_intr);
3235         else
3236                 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3237
3238         /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3239         do_div(cnt, phba->eratt_poll_interval);
3240         phba->sli.slistat.sli_ips = cnt;
3241
3242         phba->sli.slistat.sli_prev_intr = sli_intr;
3243
3244         /* Check chip HA register for error event */
3245         eratt = lpfc_sli_check_eratt(phba);
3246
3247         if (eratt)
3248                 /* Tell the worker thread there is work to do */
3249                 lpfc_worker_wake_up(phba);
3250         else
3251                 /* Restart the timer for next eratt poll */
3252                 mod_timer(&phba->eratt_poll,
3253                           jiffies +
3254                           msecs_to_jiffies(1000 * phba->eratt_poll_interval));
3255         return;
3256 }
3257
3258
3259 /**
3260  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3261  * @phba: Pointer to HBA context object.
3262  * @pring: Pointer to driver SLI ring object.
3263  * @mask: Host attention register mask for this ring.
3264  *
3265  * This function is called from the interrupt context when there is a ring
3266  * event for the fcp ring. The caller does not hold any lock.
3267  * The function processes each response iocb in the response ring until it
3268  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3269  * LE bit set. The function will call the completion handler of the command iocb
3270  * if the response iocb indicates a completion for a command iocb or it is
3271  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3272  * function if this is an unsolicited iocb.
3273  * This routine presumes LPFC_FCP_RING handling and doesn't bother
3274  * to check it explicitly.
3275  */
3276 int
3277 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3278                                 struct lpfc_sli_ring *pring, uint32_t mask)
3279 {
3280         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3281         IOCB_t *irsp = NULL;
3282         IOCB_t *entry = NULL;
3283         struct lpfc_iocbq *cmdiocbq = NULL;
3284         struct lpfc_iocbq rspiocbq;
3285         uint32_t status;
3286         uint32_t portRspPut, portRspMax;
3287         int rc = 1;
3288         lpfc_iocb_type type;
3289         unsigned long iflag;
3290         uint32_t rsp_cmpl = 0;
3291
3292         spin_lock_irqsave(&phba->hbalock, iflag);
3293         pring->stats.iocb_event++;
3294
3295         /*
3296          * The next available response entry should never exceed the maximum
3297          * entries.  If it does, treat it as an adapter hardware error.
3298          */
3299         portRspMax = pring->sli.sli3.numRiocb;
3300         portRspPut = le32_to_cpu(pgp->rspPutInx);
3301         if (unlikely(portRspPut >= portRspMax)) {
3302                 lpfc_sli_rsp_pointers_error(phba, pring);
3303                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3304                 return 1;
3305         }
3306         if (phba->fcp_ring_in_use) {
3307                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3308                 return 1;
3309         } else
3310                 phba->fcp_ring_in_use = 1;
3311
3312         rmb();
3313         while (pring->sli.sli3.rspidx != portRspPut) {
3314                 /*
3315                  * Fetch an entry off the ring and copy it into a local data
3316                  * structure.  The copy involves a byte-swap since the
3317                  * network byte order and pci byte orders are different.
3318                  */
3319                 entry = lpfc_resp_iocb(phba, pring);
3320                 phba->last_completion_time = jiffies;
3321
3322                 if (++pring->sli.sli3.rspidx >= portRspMax)
3323                         pring->sli.sli3.rspidx = 0;
3324
3325                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3326                                       (uint32_t *) &rspiocbq.iocb,
3327                                       phba->iocb_rsp_size);
3328                 INIT_LIST_HEAD(&(rspiocbq.list));
3329                 irsp = &rspiocbq.iocb;
3330
3331                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3332                 pring->stats.iocb_rsp++;
3333                 rsp_cmpl++;
3334
3335                 if (unlikely(irsp->ulpStatus)) {
3336                         /*
3337                          * If resource errors reported from HBA, reduce
3338                          * queuedepths of the SCSI device.
3339                          */
3340                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3341                             ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3342                              IOERR_NO_RESOURCES)) {
3343                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3344                                 phba->lpfc_rampdown_queue_depth(phba);
3345                                 spin_lock_irqsave(&phba->hbalock, iflag);
3346                         }
3347
3348                         /* Rsp ring <ringno> error: IOCB */
3349                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3350                                         "0336 Rsp Ring %d error: IOCB Data: "
3351                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3352                                         pring->ringno,
3353                                         irsp->un.ulpWord[0],
3354                                         irsp->un.ulpWord[1],
3355                                         irsp->un.ulpWord[2],
3356                                         irsp->un.ulpWord[3],
3357                                         irsp->un.ulpWord[4],
3358                                         irsp->un.ulpWord[5],
3359                                         *(uint32_t *)&irsp->un1,
3360                                         *((uint32_t *)&irsp->un1 + 1));
3361                 }
3362
3363                 switch (type) {
3364                 case LPFC_ABORT_IOCB:
3365                 case LPFC_SOL_IOCB:
3366                         /*
3367                          * Idle exchange closed via ABTS from port.  No iocb
3368                          * resources need to be recovered.
3369                          */
3370                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3371                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3372                                                 "0333 IOCB cmd 0x%x"
3373                                                 " processed. Skipping"
3374                                                 " completion\n",
3375                                                 irsp->ulpCommand);
3376                                 break;
3377                         }
3378
3379                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3380                                                          &rspiocbq);
3381                         if (unlikely(!cmdiocbq))
3382                                 break;
3383                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3384                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3385                         if (cmdiocbq->iocb_cmpl) {
3386                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3387                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3388                                                       &rspiocbq);
3389                                 spin_lock_irqsave(&phba->hbalock, iflag);
3390                         }
3391                         break;
3392                 case LPFC_UNSOL_IOCB:
3393                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3394                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3395                         spin_lock_irqsave(&phba->hbalock, iflag);
3396                         break;
3397                 default:
3398                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3399                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3400                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3401                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3402                                        MAX_MSG_DATA);
3403                                 dev_warn(&((phba->pcidev)->dev),
3404                                          "lpfc%d: %s\n",
3405                                          phba->brd_no, adaptermsg);
3406                         } else {
3407                                 /* Unknown IOCB command */
3408                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3409                                                 "0334 Unknown IOCB command "
3410                                                 "Data: x%x, x%x x%x x%x x%x\n",
3411                                                 type, irsp->ulpCommand,
3412                                                 irsp->ulpStatus,
3413                                                 irsp->ulpIoTag,
3414                                                 irsp->ulpContext);
3415                         }
3416                         break;
3417                 }
3418
3419                 /*
3420                  * The response IOCB has been processed.  Update the ring
3421                  * pointer in SLIM.  If the port response put pointer has not
3422                  * been updated, sync the pgp->rspPutInx and fetch the new port
3423                  * response put pointer.
3424                  */
3425                 writel(pring->sli.sli3.rspidx,
3426                         &phba->host_gp[pring->ringno].rspGetInx);
3427
3428                 if (pring->sli.sli3.rspidx == portRspPut)
3429                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3430         }
3431
3432         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3433                 pring->stats.iocb_rsp_full++;
3434                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3435                 writel(status, phba->CAregaddr);
3436                 readl(phba->CAregaddr);
3437         }
3438         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3439                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3440                 pring->stats.iocb_cmd_empty++;
3441
3442                 /* Force update of the local copy of cmdGetInx */
3443                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3444                 lpfc_sli_resume_iocb(phba, pring);
3445
3446                 if ((pring->lpfc_sli_cmd_available))
3447                         (pring->lpfc_sli_cmd_available) (phba, pring);
3448
3449         }
3450
3451         phba->fcp_ring_in_use = 0;
3452         spin_unlock_irqrestore(&phba->hbalock, iflag);
3453         return rc;
3454 }
3455
3456 /**
3457  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3458  * @phba: Pointer to HBA context object.
3459  * @pring: Pointer to driver SLI ring object.
3460  * @rspiocbp: Pointer to driver response IOCB object.
3461  *
3462  * This function is called from the worker thread when there is a slow-path
3463  * response IOCB to process. This function chains all the response iocbs until
3464  * seeing the iocb with the LE bit set. The function will call
3465  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3466  * completion of a command iocb. The function will call the
3467  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3468  * The function frees the resources or calls the completion handler if this
3469  * iocb is an abort completion. The function returns NULL when the response
3470  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3471  * this function shall chain the iocb on to the iocb_continueq and return the
3472  * response iocb passed in.
3473  **/
3474 static struct lpfc_iocbq *
3475 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3476                         struct lpfc_iocbq *rspiocbp)
3477 {
3478         struct lpfc_iocbq *saveq;
3479         struct lpfc_iocbq *cmdiocbp;
3480         struct lpfc_iocbq *next_iocb;
3481         IOCB_t *irsp = NULL;
3482         uint32_t free_saveq;
3483         uint8_t iocb_cmd_type;
3484         lpfc_iocb_type type;
3485         unsigned long iflag;
3486         int rc;
3487
3488         spin_lock_irqsave(&phba->hbalock, iflag);
3489         /* First add the response iocb to the countinueq list */
3490         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3491         pring->iocb_continueq_cnt++;
3492
3493         /* Now, determine whether the list is completed for processing */
3494         irsp = &rspiocbp->iocb;
3495         if (irsp->ulpLe) {
3496                 /*
3497                  * By default, the driver expects to free all resources
3498                  * associated with this iocb completion.
3499                  */
3500                 free_saveq = 1;
3501                 saveq = list_get_first(&pring->iocb_continueq,
3502                                        struct lpfc_iocbq, list);
3503                 irsp = &(saveq->iocb);
3504                 list_del_init(&pring->iocb_continueq);
3505                 pring->iocb_continueq_cnt = 0;
3506
3507                 pring->stats.iocb_rsp++;
3508
3509                 /*
3510                  * If resource errors reported from HBA, reduce
3511                  * queuedepths of the SCSI device.
3512                  */
3513                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3514                     ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3515                      IOERR_NO_RESOURCES)) {
3516                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3517                         phba->lpfc_rampdown_queue_depth(phba);
3518                         spin_lock_irqsave(&phba->hbalock, iflag);
3519                 }
3520
3521                 if (irsp->ulpStatus) {
3522                         /* Rsp ring <ringno> error: IOCB */
3523                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3524                                         "0328 Rsp Ring %d error: "
3525                                         "IOCB Data: "
3526                                         "x%x x%x x%x x%x "
3527                                         "x%x x%x x%x x%x "
3528                                         "x%x x%x x%x x%x "
3529                                         "x%x x%x x%x x%x\n",
3530                                         pring->ringno,
3531                                         irsp->un.ulpWord[0],
3532                                         irsp->un.ulpWord[1],
3533                                         irsp->un.ulpWord[2],
3534                                         irsp->un.ulpWord[3],
3535                                         irsp->un.ulpWord[4],
3536                                         irsp->un.ulpWord[5],
3537                                         *(((uint32_t *) irsp) + 6),
3538                                         *(((uint32_t *) irsp) + 7),
3539                                         *(((uint32_t *) irsp) + 8),
3540                                         *(((uint32_t *) irsp) + 9),
3541                                         *(((uint32_t *) irsp) + 10),
3542                                         *(((uint32_t *) irsp) + 11),
3543                                         *(((uint32_t *) irsp) + 12),
3544                                         *(((uint32_t *) irsp) + 13),
3545                                         *(((uint32_t *) irsp) + 14),
3546                                         *(((uint32_t *) irsp) + 15));
3547                 }
3548
3549                 /*
3550                  * Fetch the IOCB command type and call the correct completion
3551                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3552                  * get freed back to the lpfc_iocb_list by the discovery
3553                  * kernel thread.
3554                  */
3555                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3556                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3557                 switch (type) {
3558                 case LPFC_SOL_IOCB:
3559                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3560                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3561                         spin_lock_irqsave(&phba->hbalock, iflag);
3562                         break;
3563
3564                 case LPFC_UNSOL_IOCB:
3565                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3566                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3567                         spin_lock_irqsave(&phba->hbalock, iflag);
3568                         if (!rc)
3569                                 free_saveq = 0;
3570                         break;
3571
3572                 case LPFC_ABORT_IOCB:
3573                         cmdiocbp = NULL;
3574                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3575                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3576                                                                  saveq);
3577                         if (cmdiocbp) {
3578                                 /* Call the specified completion routine */
3579                                 if (cmdiocbp->iocb_cmpl) {
3580                                         spin_unlock_irqrestore(&phba->hbalock,
3581                                                                iflag);
3582                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3583                                                               saveq);
3584                                         spin_lock_irqsave(&phba->hbalock,
3585                                                           iflag);
3586                                 } else
3587                                         __lpfc_sli_release_iocbq(phba,
3588                                                                  cmdiocbp);
3589                         }
3590                         break;
3591
3592                 case LPFC_UNKNOWN_IOCB:
3593                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3594                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3595                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3596                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3597                                        MAX_MSG_DATA);
3598                                 dev_warn(&((phba->pcidev)->dev),
3599                                          "lpfc%d: %s\n",
3600                                          phba->brd_no, adaptermsg);
3601                         } else {
3602                                 /* Unknown IOCB command */
3603                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3604                                                 "0335 Unknown IOCB "
3605                                                 "command Data: x%x "
3606                                                 "x%x x%x x%x\n",
3607                                                 irsp->ulpCommand,
3608                                                 irsp->ulpStatus,
3609                                                 irsp->ulpIoTag,
3610                                                 irsp->ulpContext);
3611                         }
3612                         break;
3613                 }
3614
3615                 if (free_saveq) {
3616                         list_for_each_entry_safe(rspiocbp, next_iocb,
3617                                                  &saveq->list, list) {
3618                                 list_del_init(&rspiocbp->list);
3619                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3620                         }
3621                         __lpfc_sli_release_iocbq(phba, saveq);
3622                 }
3623                 rspiocbp = NULL;
3624         }
3625         spin_unlock_irqrestore(&phba->hbalock, iflag);
3626         return rspiocbp;
3627 }
3628
3629 /**
3630  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3631  * @phba: Pointer to HBA context object.
3632  * @pring: Pointer to driver SLI ring object.
3633  * @mask: Host attention register mask for this ring.
3634  *
3635  * This routine wraps the actual slow_ring event process routine from the
3636  * API jump table function pointer from the lpfc_hba struct.
3637  **/
3638 void
3639 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3640                                 struct lpfc_sli_ring *pring, uint32_t mask)
3641 {
3642         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3643 }
3644
3645 /**
3646  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3647  * @phba: Pointer to HBA context object.
3648  * @pring: Pointer to driver SLI ring object.
3649  * @mask: Host attention register mask for this ring.
3650  *
3651  * This function is called from the worker thread when there is a ring event
3652  * for non-fcp rings. The caller does not hold any lock. The function will
3653  * remove each response iocb in the response ring and calls the handle
3654  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3655  **/
3656 static void
3657 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3658                                    struct lpfc_sli_ring *pring, uint32_t mask)
3659 {
3660         struct lpfc_pgp *pgp;
3661         IOCB_t *entry;
3662         IOCB_t *irsp = NULL;
3663         struct lpfc_iocbq *rspiocbp = NULL;
3664         uint32_t portRspPut, portRspMax;
3665         unsigned long iflag;
3666         uint32_t status;
3667
3668         pgp = &phba->port_gp[pring->ringno];
3669         spin_lock_irqsave(&phba->hbalock, iflag);
3670         pring->stats.iocb_event++;
3671
3672         /*
3673          * The next available response entry should never exceed the maximum
3674          * entries.  If it does, treat it as an adapter hardware error.
3675          */
3676         portRspMax = pring->sli.sli3.numRiocb;
3677         portRspPut = le32_to_cpu(pgp->rspPutInx);
3678         if (portRspPut >= portRspMax) {
3679                 /*
3680                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3681                  * rsp ring <portRspMax>
3682                  */
3683                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3684                                 "0303 Ring %d handler: portRspPut %d "
3685                                 "is bigger than rsp ring %d\n",
3686                                 pring->ringno, portRspPut, portRspMax);
3687
3688                 phba->link_state = LPFC_HBA_ERROR;
3689                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3690
3691                 phba->work_hs = HS_FFER3;
3692                 lpfc_handle_eratt(phba);
3693
3694                 return;
3695         }
3696
3697         rmb();
3698         while (pring->sli.sli3.rspidx != portRspPut) {
3699                 /*
3700                  * Build a completion list and call the appropriate handler.
3701                  * The process is to get the next available response iocb, get
3702                  * a free iocb from the list, copy the response data into the
3703                  * free iocb, insert to the continuation list, and update the
3704                  * next response index to slim.  This process makes response
3705                  * iocb's in the ring available to DMA as fast as possible but
3706                  * pays a penalty for a copy operation.  Since the iocb is
3707                  * only 32 bytes, this penalty is considered small relative to
3708                  * the PCI reads for register values and a slim write.  When
3709                  * the ulpLe field is set, the entire Command has been
3710                  * received.
3711                  */
3712                 entry = lpfc_resp_iocb(phba, pring);
3713
3714                 phba->last_completion_time = jiffies;
3715                 rspiocbp = __lpfc_sli_get_iocbq(phba);
3716                 if (rspiocbp == NULL) {
3717                         printk(KERN_ERR "%s: out of buffers! Failing "
3718                                "completion.\n", __func__);
3719                         break;
3720                 }
3721
3722                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3723                                       phba->iocb_rsp_size);
3724                 irsp = &rspiocbp->iocb;
3725
3726                 if (++pring->sli.sli3.rspidx >= portRspMax)
3727                         pring->sli.sli3.rspidx = 0;
3728
3729                 if (pring->ringno == LPFC_ELS_RING) {
3730                         lpfc_debugfs_slow_ring_trc(phba,
3731                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
3732                                 *(((uint32_t *) irsp) + 4),
3733                                 *(((uint32_t *) irsp) + 6),
3734                                 *(((uint32_t *) irsp) + 7));
3735                 }
3736
3737                 writel(pring->sli.sli3.rspidx,
3738                         &phba->host_gp[pring->ringno].rspGetInx);
3739
3740                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3741                 /* Handle the response IOCB */
3742                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3743                 spin_lock_irqsave(&phba->hbalock, iflag);
3744
3745                 /*
3746                  * If the port response put pointer has not been updated, sync
3747                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3748                  * response put pointer.
3749                  */
3750                 if (pring->sli.sli3.rspidx == portRspPut) {
3751                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3752                 }
3753         } /* while (pring->sli.sli3.rspidx != portRspPut) */
3754
3755         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3756                 /* At least one response entry has been freed */
3757                 pring->stats.iocb_rsp_full++;
3758                 /* SET RxRE_RSP in Chip Att register */
3759                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3760                 writel(status, phba->CAregaddr);
3761                 readl(phba->CAregaddr); /* flush */
3762         }
3763         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3764                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3765                 pring->stats.iocb_cmd_empty++;
3766
3767                 /* Force update of the local copy of cmdGetInx */
3768                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3769                 lpfc_sli_resume_iocb(phba, pring);
3770
3771                 if ((pring->lpfc_sli_cmd_available))
3772                         (pring->lpfc_sli_cmd_available) (phba, pring);
3773
3774         }
3775
3776         spin_unlock_irqrestore(&phba->hbalock, iflag);
3777         return;
3778 }
3779
3780 /**
3781  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3782  * @phba: Pointer to HBA context object.
3783  * @pring: Pointer to driver SLI ring object.
3784  * @mask: Host attention register mask for this ring.
3785  *
3786  * This function is called from the worker thread when there is a pending
3787  * ELS response iocb on the driver internal slow-path response iocb worker
3788  * queue. The caller does not hold any lock. The function will remove each
3789  * response iocb from the response worker queue and calls the handle
3790  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3791  **/
3792 static void
3793 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3794                                    struct lpfc_sli_ring *pring, uint32_t mask)
3795 {
3796         struct lpfc_iocbq *irspiocbq;
3797         struct hbq_dmabuf *dmabuf;
3798         struct lpfc_cq_event *cq_event;
3799         unsigned long iflag;
3800
3801         spin_lock_irqsave(&phba->hbalock, iflag);
3802         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3803         spin_unlock_irqrestore(&phba->hbalock, iflag);
3804         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3805                 /* Get the response iocb from the head of work queue */
3806                 spin_lock_irqsave(&phba->hbalock, iflag);
3807                 list_remove_head(&phba->sli4_hba.sp_queue_event,
3808                                  cq_event, struct lpfc_cq_event, list);
3809                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3810
3811                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3812                 case CQE_CODE_COMPL_WQE:
3813                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3814                                                  cq_event);
3815                         /* Translate ELS WCQE to response IOCBQ */
3816                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3817                                                                    irspiocbq);
3818                         if (irspiocbq)
3819                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
3820                                                            irspiocbq);
3821                         break;
3822                 case CQE_CODE_RECEIVE:
3823                 case CQE_CODE_RECEIVE_V1:
3824                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
3825                                               cq_event);
3826                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
3827                         break;
3828                 default:
3829                         break;
3830                 }
3831         }
3832 }
3833
3834 /**
3835  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3836  * @phba: Pointer to HBA context object.
3837  * @pring: Pointer to driver SLI ring object.
3838  *
3839  * This function aborts all iocbs in the given ring and frees all the iocb
3840  * objects in txq. This function issues an abort iocb for all the iocb commands
3841  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3842  * the return of this function. The caller is not required to hold any locks.
3843  **/
3844 void
3845 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3846 {
3847         LIST_HEAD(completions);
3848         struct lpfc_iocbq *iocb, *next_iocb;
3849
3850         if (pring->ringno == LPFC_ELS_RING) {
3851                 lpfc_fabric_abort_hba(phba);
3852         }
3853
3854         /* Error everything on txq and txcmplq
3855          * First do the txq.
3856          */
3857         if (phba->sli_rev >= LPFC_SLI_REV4) {
3858                 spin_lock_irq(&pring->ring_lock);
3859                 list_splice_init(&pring->txq, &completions);
3860                 pring->txq_cnt = 0;
3861                 spin_unlock_irq(&pring->ring_lock);
3862
3863                 spin_lock_irq(&phba->hbalock);
3864                 /* Next issue ABTS for everything on the txcmplq */
3865                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3866                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3867                 spin_unlock_irq(&phba->hbalock);
3868         } else {
3869                 spin_lock_irq(&phba->hbalock);
3870                 list_splice_init(&pring->txq, &completions);
3871                 pring->txq_cnt = 0;
3872
3873                 /* Next issue ABTS for everything on the txcmplq */
3874                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3875                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3876                 spin_unlock_irq(&phba->hbalock);
3877         }
3878
3879         /* Cancel all the IOCBs from the completions list */
3880         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3881                               IOERR_SLI_ABORTED);
3882 }
3883
3884 /**
3885  * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3886  * @phba: Pointer to HBA context object.
3887  * @pring: Pointer to driver SLI ring object.
3888  *
3889  * This function aborts all iocbs in the given ring and frees all the iocb
3890  * objects in txq. This function issues an abort iocb for all the iocb commands
3891  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3892  * the return of this function. The caller is not required to hold any locks.
3893  **/
3894 void
3895 lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3896 {
3897         LIST_HEAD(completions);
3898         struct lpfc_iocbq *iocb, *next_iocb;
3899
3900         if (pring->ringno == LPFC_ELS_RING)
3901                 lpfc_fabric_abort_hba(phba);
3902
3903         spin_lock_irq(&phba->hbalock);
3904         /* Next issue ABTS for everything on the txcmplq */
3905         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3906                 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3907         spin_unlock_irq(&phba->hbalock);
3908 }
3909
3910
3911 /**
3912  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3913  * @phba: Pointer to HBA context object.
3914  * @pring: Pointer to driver SLI ring object.
3915  *
3916  * This function aborts all iocbs in FCP rings and frees all the iocb
3917  * objects in txq. This function issues an abort iocb for all the iocb commands
3918  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3919  * the return of this function. The caller is not required to hold any locks.
3920  **/
3921 void
3922 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3923 {
3924         struct lpfc_sli *psli = &phba->sli;
3925         struct lpfc_sli_ring  *pring;
3926         uint32_t i;
3927
3928         /* Look on all the FCP Rings for the iotag */
3929         if (phba->sli_rev >= LPFC_SLI_REV4) {
3930                 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3931                         pring = phba->sli4_hba.fcp_wq[i]->pring;
3932                         lpfc_sli_abort_iocb_ring(phba, pring);
3933                 }
3934         } else {
3935                 pring = &psli->sli3_ring[LPFC_FCP_RING];
3936                 lpfc_sli_abort_iocb_ring(phba, pring);
3937         }
3938 }
3939
3940 /**
3941  * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3942  * @phba: Pointer to HBA context object.
3943  *
3944  * This function aborts all wqes in NVME rings. This function issues an
3945  * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3946  * the txcmplq is not guaranteed to complete before the return of this
3947  * function. The caller is not required to hold any locks.
3948  **/
3949 void
3950 lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3951 {
3952         struct lpfc_sli_ring  *pring;
3953         uint32_t i;
3954
3955         if (phba->sli_rev < LPFC_SLI_REV4)
3956                 return;
3957
3958         /* Abort all IO on each NVME ring. */
3959         for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3960                 pring = phba->sli4_hba.nvme_wq[i]->pring;
3961                 lpfc_sli_abort_wqe_ring(phba, pring);
3962         }
3963 }
3964
3965
3966 /**
3967  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3968  * @phba: Pointer to HBA context object.
3969  *
3970  * This function flushes all iocbs in the fcp ring and frees all the iocb
3971  * objects in txq and txcmplq. This function will not issue abort iocbs
3972  * for all the iocb commands in txcmplq, they will just be returned with
3973  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3974  * slot has been permanently disabled.
3975  **/
3976 void
3977 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3978 {
3979         LIST_HEAD(txq);
3980         LIST_HEAD(txcmplq);
3981         struct lpfc_sli *psli = &phba->sli;
3982         struct lpfc_sli_ring  *pring;
3983         uint32_t i;
3984         struct lpfc_iocbq *piocb, *next_iocb;
3985
3986         spin_lock_irq(&phba->hbalock);
3987         /* Indicate the I/O queues are flushed */
3988         phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
3989         spin_unlock_irq(&phba->hbalock);
3990
3991         /* Look on all the FCP Rings for the iotag */
3992         if (phba->sli_rev >= LPFC_SLI_REV4) {
3993                 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3994                         pring = phba->sli4_hba.fcp_wq[i]->pring;
3995
3996                         spin_lock_irq(&pring->ring_lock);
3997                         /* Retrieve everything on txq */
3998                         list_splice_init(&pring->txq, &txq);
3999                         list_for_each_entry_safe(piocb, next_iocb,
4000                                                  &pring->txcmplq, list)
4001                                 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4002                         /* Retrieve everything on the txcmplq */
4003                         list_splice_init(&pring->txcmplq, &txcmplq);
4004                         pring->txq_cnt = 0;
4005                         pring->txcmplq_cnt = 0;
4006                         spin_unlock_irq(&pring->ring_lock);
4007
4008                         /* Flush the txq */
4009                         lpfc_sli_cancel_iocbs(phba, &txq,
4010                                               IOSTAT_LOCAL_REJECT,
4011                                               IOERR_SLI_DOWN);
4012                         /* Flush the txcmpq */
4013                         lpfc_sli_cancel_iocbs(phba, &txcmplq,
4014                                               IOSTAT_LOCAL_REJECT,
4015                                               IOERR_SLI_DOWN);
4016                 }
4017         } else {
4018                 pring = &psli->sli3_ring[LPFC_FCP_RING];
4019
4020                 spin_lock_irq(&phba->hbalock);
4021                 /* Retrieve everything on txq */
4022                 list_splice_init(&pring->txq, &txq);
4023                 list_for_each_entry_safe(piocb, next_iocb,
4024                                          &pring->txcmplq, list)
4025                         piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4026                 /* Retrieve everything on the txcmplq */
4027                 list_splice_init(&pring->txcmplq, &txcmplq);
4028                 pring->txq_cnt = 0;
4029                 pring->txcmplq_cnt = 0;
4030                 spin_unlock_irq(&phba->hbalock);
4031
4032                 /* Flush the txq */
4033                 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4034                                       IOERR_SLI_DOWN);
4035                 /* Flush the txcmpq */
4036                 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4037                                       IOERR_SLI_DOWN);
4038         }
4039 }
4040
4041 /**
4042  * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4043  * @phba: Pointer to HBA context object.
4044  *
4045  * This function flushes all wqes in the nvme rings and frees all resources
4046  * in the txcmplq. This function does not issue abort wqes for the IO
4047  * commands in txcmplq, they will just be returned with
4048  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4049  * slot has been permanently disabled.
4050  **/
4051 void
4052 lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4053 {
4054         LIST_HEAD(txcmplq);
4055         struct lpfc_sli_ring  *pring;
4056         uint32_t i;
4057         struct lpfc_iocbq *piocb, *next_iocb;
4058
4059         if (phba->sli_rev < LPFC_SLI_REV4)
4060                 return;
4061
4062         /* Hint to other driver operations that a flush is in progress. */
4063         spin_lock_irq(&phba->hbalock);
4064         phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4065         spin_unlock_irq(&phba->hbalock);
4066
4067         /* Cycle through all NVME rings and complete each IO with
4068          * a local driver reason code.  This is a flush so no
4069          * abort exchange to FW.
4070          */
4071         for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
4072                 pring = phba->sli4_hba.nvme_wq[i]->pring;
4073
4074                 spin_lock_irq(&pring->ring_lock);
4075                 list_for_each_entry_safe(piocb, next_iocb,
4076                                          &pring->txcmplq, list)
4077                         piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4078                 /* Retrieve everything on the txcmplq */
4079                 list_splice_init(&pring->txcmplq, &txcmplq);
4080                 pring->txcmplq_cnt = 0;
4081                 spin_unlock_irq(&pring->ring_lock);
4082
4083                 /* Flush the txcmpq &&&PAE */
4084                 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4085                                       IOSTAT_LOCAL_REJECT,
4086                                       IOERR_SLI_DOWN);
4087         }
4088 }
4089
4090 /**
4091  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
4092  * @phba: Pointer to HBA context object.
4093  * @mask: Bit mask to be checked.
4094  *
4095  * This function reads the host status register and compares
4096  * with the provided bit mask to check if HBA completed
4097  * the restart. This function will wait in a loop for the
4098  * HBA to complete restart. If the HBA does not restart within
4099  * 15 iterations, the function will reset the HBA again. The
4100  * function returns 1 when HBA fail to restart otherwise returns
4101  * zero.
4102  **/
4103 static int
4104 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
4105 {
4106         uint32_t status;
4107         int i = 0;
4108         int retval = 0;
4109
4110         /* Read the HBA Host Status Register */
4111         if (lpfc_readl(phba->HSregaddr, &status))
4112                 return 1;
4113
4114         /*
4115          * Check status register every 100ms for 5 retries, then every
4116          * 500ms for 5, then every 2.5 sec for 5, then reset board and
4117          * every 2.5 sec for 4.
4118          * Break our of the loop if errors occurred during init.
4119          */
4120         while (((status & mask) != mask) &&
4121                !(status & HS_FFERM) &&
4122                i++ < 20) {
4123
4124                 if (i <= 5)
4125                         msleep(10);
4126                 else if (i <= 10)
4127                         msleep(500);
4128                 else
4129                         msleep(2500);
4130
4131                 if (i == 15) {
4132                                 /* Do post */
4133                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4134                         lpfc_sli_brdrestart(phba);
4135                 }
4136                 /* Read the HBA Host Status Register */
4137                 if (lpfc_readl(phba->HSregaddr, &status)) {
4138                         retval = 1;
4139                         break;
4140                 }
4141         }
4142
4143         /* Check to see if any errors occurred during init */
4144         if ((status & HS_FFERM) || (i >= 20)) {
4145                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4146                                 "2751 Adapter failed to restart, "
4147                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4148                                 status,
4149                                 readl(phba->MBslimaddr + 0xa8),
4150                                 readl(phba->MBslimaddr + 0xac));
4151                 phba->link_state = LPFC_HBA_ERROR;
4152                 retval = 1;
4153         }
4154
4155         return retval;
4156 }
4157
4158 /**
4159  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4160  * @phba: Pointer to HBA context object.
4161  * @mask: Bit mask to be checked.
4162  *
4163  * This function checks the host status register to check if HBA is
4164  * ready. This function will wait in a loop for the HBA to be ready
4165  * If the HBA is not ready , the function will will reset the HBA PCI
4166  * function again. The function returns 1 when HBA fail to be ready
4167  * otherwise returns zero.
4168  **/
4169 static int
4170 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4171 {
4172         uint32_t status;
4173         int retval = 0;
4174
4175         /* Read the HBA Host Status Register */
4176         status = lpfc_sli4_post_status_check(phba);
4177
4178         if (status) {
4179                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4180                 lpfc_sli_brdrestart(phba);
4181                 status = lpfc_sli4_post_status_check(phba);
4182         }
4183
4184         /* Check to see if any errors occurred during init */
4185         if (status) {
4186                 phba->link_state = LPFC_HBA_ERROR;
4187                 retval = 1;
4188         } else
4189                 phba->sli4_hba.intr_enable = 0;
4190
4191         return retval;
4192 }
4193
4194 /**
4195  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4196  * @phba: Pointer to HBA context object.
4197  * @mask: Bit mask to be checked.
4198  *
4199  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4200  * from the API jump table function pointer from the lpfc_hba struct.
4201  **/
4202 int
4203 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4204 {
4205         return phba->lpfc_sli_brdready(phba, mask);
4206 }
4207
4208 #define BARRIER_TEST_PATTERN (0xdeadbeef)
4209
4210 /**
4211  * lpfc_reset_barrier - Make HBA ready for HBA reset
4212  * @phba: Pointer to HBA context object.
4213  *
4214  * This function is called before resetting an HBA. This function is called
4215  * with hbalock held and requests HBA to quiesce DMAs before a reset.
4216  **/
4217 void lpfc_reset_barrier(struct lpfc_hba *phba)
4218 {
4219         uint32_t __iomem *resp_buf;
4220         uint32_t __iomem *mbox_buf;
4221         volatile uint32_t mbox;
4222         uint32_t hc_copy, ha_copy, resp_data;
4223         int  i;
4224         uint8_t hdrtype;
4225
4226         lockdep_assert_held(&phba->hbalock);
4227
4228         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4229         if (hdrtype != 0x80 ||
4230             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4231              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4232                 return;
4233
4234         /*
4235          * Tell the other part of the chip to suspend temporarily all
4236          * its DMA activity.
4237          */
4238         resp_buf = phba->MBslimaddr;
4239
4240         /* Disable the error attention */
4241         if (lpfc_readl(phba->HCregaddr, &hc_copy))
4242                 return;
4243         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4244         readl(phba->HCregaddr); /* flush */
4245         phba->link_flag |= LS_IGNORE_ERATT;
4246
4247         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4248                 return;
4249         if (ha_copy & HA_ERATT) {
4250                 /* Clear Chip error bit */
4251                 writel(HA_ERATT, phba->HAregaddr);
4252                 phba->pport->stopped = 1;
4253         }
4254
4255         mbox = 0;
4256         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4257         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4258
4259         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4260         mbox_buf = phba->MBslimaddr;
4261         writel(mbox, mbox_buf);
4262
4263         for (i = 0; i < 50; i++) {
4264                 if (lpfc_readl((resp_buf + 1), &resp_data))
4265                         return;
4266                 if (resp_data != ~(BARRIER_TEST_PATTERN))
4267                         mdelay(1);
4268                 else
4269                         break;
4270         }
4271         resp_data = 0;
4272         if (lpfc_readl((resp_buf + 1), &resp_data))
4273                 return;
4274         if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4275                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4276                     phba->pport->stopped)
4277                         goto restore_hc;
4278                 else
4279                         goto clear_errat;
4280         }
4281
4282         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
4283         resp_data = 0;
4284         for (i = 0; i < 500; i++) {
4285                 if (lpfc_readl(resp_buf, &resp_data))
4286                         return;
4287                 if (resp_data != mbox)
4288                         mdelay(1);
4289                 else
4290                         break;
4291         }
4292
4293 clear_errat:
4294
4295         while (++i < 500) {
4296                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4297                         return;
4298                 if (!(ha_copy & HA_ERATT))
4299                         mdelay(1);
4300                 else
4301                         break;
4302         }
4303
4304         if (readl(phba->HAregaddr) & HA_ERATT) {
4305                 writel(HA_ERATT, phba->HAregaddr);
4306                 phba->pport->stopped = 1;
4307         }
4308
4309 restore_hc:
4310         phba->link_flag &= ~LS_IGNORE_ERATT;
4311         writel(hc_copy, phba->HCregaddr);
4312         readl(phba->HCregaddr); /* flush */
4313 }
4314
4315 /**
4316  * lpfc_sli_brdkill - Issue a kill_board mailbox command
4317  * @phba: Pointer to HBA context object.
4318  *
4319  * This function issues a kill_board mailbox command and waits for
4320  * the error attention interrupt. This function is called for stopping
4321  * the firmware processing. The caller is not required to hold any
4322  * locks. This function calls lpfc_hba_down_post function to free
4323  * any pending commands after the kill. The function will return 1 when it
4324  * fails to kill the board else will return 0.
4325  **/
4326 int
4327 lpfc_sli_brdkill(struct lpfc_hba *phba)
4328 {
4329         struct lpfc_sli *psli;
4330         LPFC_MBOXQ_t *pmb;
4331         uint32_t status;
4332         uint32_t ha_copy;
4333         int retval;
4334         int i = 0;
4335
4336         psli = &phba->sli;
4337
4338         /* Kill HBA */
4339         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4340                         "0329 Kill HBA Data: x%x x%x\n",
4341                         phba->pport->port_state, psli->sli_flag);
4342
4343         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4344         if (!pmb)
4345                 return 1;
4346
4347         /* Disable the error attention */
4348         spin_lock_irq(&phba->hbalock);
4349         if (lpfc_readl(phba->HCregaddr, &status)) {
4350                 spin_unlock_irq(&phba->hbalock);
4351                 mempool_free(pmb, phba->mbox_mem_pool);
4352                 return 1;
4353         }
4354         status &= ~HC_ERINT_ENA;
4355         writel(status, phba->HCregaddr);
4356         readl(phba->HCregaddr); /* flush */
4357         phba->link_flag |= LS_IGNORE_ERATT;
4358         spin_unlock_irq(&phba->hbalock);
4359
4360         lpfc_kill_board(phba, pmb);
4361         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4362         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4363
4364         if (retval != MBX_SUCCESS) {
4365                 if (retval != MBX_BUSY)
4366                         mempool_free(pmb, phba->mbox_mem_pool);
4367                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4368                                 "2752 KILL_BOARD command failed retval %d\n",
4369                                 retval);
4370                 spin_lock_irq(&phba->hbalock);
4371                 phba->link_flag &= ~LS_IGNORE_ERATT;
4372                 spin_unlock_irq(&phba->hbalock);
4373                 return 1;
4374         }
4375
4376         spin_lock_irq(&phba->hbalock);
4377         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4378         spin_unlock_irq(&phba->hbalock);
4379
4380         mempool_free(pmb, phba->mbox_mem_pool);
4381
4382         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4383          * attention every 100ms for 3 seconds. If we don't get ERATT after
4384          * 3 seconds we still set HBA_ERROR state because the status of the
4385          * board is now undefined.
4386          */
4387         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4388                 return 1;
4389         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4390                 mdelay(100);
4391                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4392                         return 1;
4393         }
4394
4395         del_timer_sync(&psli->mbox_tmo);
4396         if (ha_copy & HA_ERATT) {
4397                 writel(HA_ERATT, phba->HAregaddr);
4398                 phba->pport->stopped = 1;
4399         }
4400         spin_lock_irq(&phba->hbalock);
4401         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4402         psli->mbox_active = NULL;
4403         phba->link_flag &= ~LS_IGNORE_ERATT;
4404         spin_unlock_irq(&phba->hbalock);
4405
4406         lpfc_hba_down_post(phba);
4407         phba->link_state = LPFC_HBA_ERROR;
4408
4409         return ha_copy & HA_ERATT ? 0 : 1;
4410 }
4411
4412 /**
4413  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
4414  * @phba: Pointer to HBA context object.
4415  *
4416  * This function resets the HBA by writing HC_INITFF to the control
4417  * register. After the HBA resets, this function resets all the iocb ring
4418  * indices. This function disables PCI layer parity checking during
4419  * the reset.
4420  * This function returns 0 always.
4421  * The caller is not required to hold any locks.
4422  **/
4423 int
4424 lpfc_sli_brdreset(struct lpfc_hba *phba)
4425 {
4426         struct lpfc_sli *psli;
4427         struct lpfc_sli_ring *pring;
4428         uint16_t cfg_value;
4429         int i;
4430
4431         psli = &phba->sli;
4432
4433         /* Reset HBA */
4434         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4435                         "0325 Reset HBA Data: x%x x%x\n",
4436                         (phba->pport) ? phba->pport->port_state : 0,
4437                         psli->sli_flag);
4438
4439         /* perform board reset */
4440         phba->fc_eventTag = 0;
4441         phba->link_events = 0;
4442         if (phba->pport) {
4443                 phba->pport->fc_myDID = 0;
4444                 phba->pport->fc_prevDID = 0;
4445         }
4446
4447         /* Turn off parity checking and serr during the physical reset */
4448         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4449         pci_write_config_word(phba->pcidev, PCI_COMMAND,
4450                               (cfg_value &
4451                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4452
4453         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4454
4455         /* Now toggle INITFF bit in the Host Control Register */
4456         writel(HC_INITFF, phba->HCregaddr);
4457         mdelay(1);
4458         readl(phba->HCregaddr); /* flush */
4459         writel(0, phba->HCregaddr);
4460         readl(phba->HCregaddr); /* flush */
4461
4462         /* Restore PCI cmd register */
4463         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4464
4465         /* Initialize relevant SLI info */
4466         for (i = 0; i < psli->num_rings; i++) {
4467                 pring = &psli->sli3_ring[i];
4468                 pring->flag = 0;
4469                 pring->sli.sli3.rspidx = 0;
4470                 pring->sli.sli3.next_cmdidx  = 0;
4471                 pring->sli.sli3.local_getidx = 0;
4472                 pring->sli.sli3.cmdidx = 0;
4473                 pring->missbufcnt = 0;
4474         }
4475
4476         phba->link_state = LPFC_WARM_START;
4477         return 0;
4478 }
4479
4480 /**
4481  * lpfc_sli4_brdreset - Reset a sli-4 HBA
4482  * @phba: Pointer to HBA context object.
4483  *
4484  * This function resets a SLI4 HBA. This function disables PCI layer parity
4485  * checking during resets the device. The caller is not required to hold
4486  * any locks.
4487  *
4488  * This function returns 0 always.
4489  **/
4490 int
4491 lpfc_sli4_brdreset(struct lpfc_hba *phba)
4492 {
4493         struct lpfc_sli *psli = &phba->sli;
4494         uint16_t cfg_value;
4495         int rc = 0;
4496
4497         /* Reset HBA */
4498         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4499                         "0295 Reset HBA Data: x%x x%x x%x\n",
4500                         phba->pport->port_state, psli->sli_flag,
4501                         phba->hba_flag);
4502
4503         /* perform board reset */
4504         phba->fc_eventTag = 0;
4505         phba->link_events = 0;
4506         phba->pport->fc_myDID = 0;
4507         phba->pport->fc_prevDID = 0;
4508
4509         spin_lock_irq(&phba->hbalock);
4510         psli->sli_flag &= ~(LPFC_PROCESS_LA);
4511         phba->fcf.fcf_flag = 0;
4512         spin_unlock_irq(&phba->hbalock);
4513
4514         /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4515         if (phba->hba_flag & HBA_FW_DUMP_OP) {
4516                 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4517                 return rc;
4518         }
4519
4520         /* Now physically reset the device */
4521         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4522                         "0389 Performing PCI function reset!\n");
4523
4524         /* Turn off parity checking and serr during the physical reset */
4525         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4526         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4527                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4528
4529         /* Perform FCoE PCI function reset before freeing queue memory */
4530         rc = lpfc_pci_function_reset(phba);
4531
4532         /* Restore PCI cmd register */
4533         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4534
4535         return rc;
4536 }
4537
4538 /**
4539  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4540  * @phba: Pointer to HBA context object.
4541  *
4542  * This function is called in the SLI initialization code path to
4543  * restart the HBA. The caller is not required to hold any lock.
4544  * This function writes MBX_RESTART mailbox command to the SLIM and
4545  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4546  * function to free any pending commands. The function enables
4547  * POST only during the first initialization. The function returns zero.
4548  * The function does not guarantee completion of MBX_RESTART mailbox
4549  * command before the return of this function.
4550  **/
4551 static int
4552 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4553 {
4554         MAILBOX_t *mb;
4555         struct lpfc_sli *psli;
4556         volatile uint32_t word0;
4557         void __iomem *to_slim;
4558         uint32_t hba_aer_enabled;
4559
4560         spin_lock_irq(&phba->hbalock);
4561
4562         /* Take PCIe device Advanced Error Reporting (AER) state */
4563         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4564
4565         psli = &phba->sli;
4566
4567         /* Restart HBA */
4568         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4569                         "0337 Restart HBA Data: x%x x%x\n",
4570                         (phba->pport) ? phba->pport->port_state : 0,
4571                         psli->sli_flag);
4572
4573         word0 = 0;
4574         mb = (MAILBOX_t *) &word0;
4575         mb->mbxCommand = MBX_RESTART;
4576         mb->mbxHc = 1;
4577
4578         lpfc_reset_barrier(phba);
4579
4580         to_slim = phba->MBslimaddr;
4581         writel(*(uint32_t *) mb, to_slim);
4582         readl(to_slim); /* flush */
4583
4584         /* Only skip post after fc_ffinit is completed */
4585         if (phba->pport && phba->pport->port_state)
4586                 word0 = 1;      /* This is really setting up word1 */
4587         else
4588                 word0 = 0;      /* This is really setting up word1 */
4589         to_slim = phba->MBslimaddr + sizeof (uint32_t);
4590         writel(*(uint32_t *) mb, to_slim);
4591         readl(to_slim); /* flush */
4592
4593         lpfc_sli_brdreset(phba);
4594         if (phba->pport)
4595                 phba->pport->stopped = 0;
4596         phba->link_state = LPFC_INIT_START;
4597         phba->hba_flag = 0;
4598         spin_unlock_irq(&phba->hbalock);
4599
4600         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4601         psli->stats_start = ktime_get_seconds();
4602
4603         /* Give the INITFF and Post time to settle. */
4604         mdelay(100);
4605
4606         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4607         if (hba_aer_enabled)
4608                 pci_disable_pcie_error_reporting(phba->pcidev);
4609
4610         lpfc_hba_down_post(phba);
4611
4612         return 0;
4613 }
4614
4615 /**
4616  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4617  * @phba: Pointer to HBA context object.
4618  *
4619  * This function is called in the SLI initialization code path to restart
4620  * a SLI4 HBA. The caller is not required to hold any lock.
4621  * At the end of the function, it calls lpfc_hba_down_post function to
4622  * free any pending commands.
4623  **/
4624 static int
4625 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4626 {
4627         struct lpfc_sli *psli = &phba->sli;
4628         uint32_t hba_aer_enabled;
4629         int rc;
4630
4631         /* Restart HBA */
4632         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4633                         "0296 Restart HBA Data: x%x x%x\n",
4634                         phba->pport->port_state, psli->sli_flag);
4635
4636         /* Take PCIe device Advanced Error Reporting (AER) state */
4637         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4638
4639         rc = lpfc_sli4_brdreset(phba);
4640
4641         spin_lock_irq(&phba->hbalock);
4642         phba->pport->stopped = 0;
4643         phba->link_state = LPFC_INIT_START;
4644         phba->hba_flag = 0;
4645         spin_unlock_irq(&phba->hbalock);
4646
4647         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4648         psli->stats_start = ktime_get_seconds();
4649
4650         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4651         if (hba_aer_enabled)
4652                 pci_disable_pcie_error_reporting(phba->pcidev);
4653
4654         lpfc_hba_down_post(phba);
4655         lpfc_sli4_queue_destroy(phba);
4656
4657         return rc;
4658 }
4659
4660 /**
4661  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4662  * @phba: Pointer to HBA context object.
4663  *
4664  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4665  * API jump table function pointer from the lpfc_hba struct.
4666 **/
4667 int
4668 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4669 {
4670         return phba->lpfc_sli_brdrestart(phba);
4671 }
4672
4673 /**
4674  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4675  * @phba: Pointer to HBA context object.
4676  *
4677  * This function is called after a HBA restart to wait for successful
4678  * restart of the HBA. Successful restart of the HBA is indicated by
4679  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4680  * iteration, the function will restart the HBA again. The function returns
4681  * zero if HBA successfully restarted else returns negative error code.
4682  **/
4683 int
4684 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4685 {
4686         uint32_t status, i = 0;
4687
4688         /* Read the HBA Host Status Register */
4689         if (lpfc_readl(phba->HSregaddr, &status))
4690                 return -EIO;
4691
4692         /* Check status register to see what current state is */
4693         i = 0;
4694         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4695
4696                 /* Check every 10ms for 10 retries, then every 100ms for 90
4697                  * retries, then every 1 sec for 50 retires for a total of
4698                  * ~60 seconds before reset the board again and check every
4699                  * 1 sec for 50 retries. The up to 60 seconds before the
4700                  * board ready is required by the Falcon FIPS zeroization
4701                  * complete, and any reset the board in between shall cause
4702                  * restart of zeroization, further delay the board ready.
4703                  */
4704                 if (i++ >= 200) {
4705                         /* Adapter failed to init, timeout, status reg
4706                            <status> */
4707                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4708                                         "0436 Adapter failed to init, "
4709                                         "timeout, status reg x%x, "
4710                                         "FW Data: A8 x%x AC x%x\n", status,
4711                                         readl(phba->MBslimaddr + 0xa8),
4712                                         readl(phba->MBslimaddr + 0xac));
4713                         phba->link_state = LPFC_HBA_ERROR;
4714                         return -ETIMEDOUT;
4715                 }
4716
4717                 /* Check to see if any errors occurred during init */
4718                 if (status & HS_FFERM) {
4719                         /* ERROR: During chipset initialization */
4720                         /* Adapter failed to init, chipset, status reg
4721                            <status> */
4722                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4723                                         "0437 Adapter failed to init, "
4724                                         "chipset, status reg x%x, "
4725                                         "FW Data: A8 x%x AC x%x\n", status,
4726                                         readl(phba->MBslimaddr + 0xa8),
4727                                         readl(phba->MBslimaddr + 0xac));
4728                         phba->link_state = LPFC_HBA_ERROR;
4729                         return -EIO;
4730                 }
4731
4732                 if (i <= 10)
4733                         msleep(10);
4734                 else if (i <= 100)
4735                         msleep(100);
4736                 else
4737                         msleep(1000);
4738
4739                 if (i == 150) {
4740                         /* Do post */
4741                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4742                         lpfc_sli_brdrestart(phba);
4743                 }
4744                 /* Read the HBA Host Status Register */
4745                 if (lpfc_readl(phba->HSregaddr, &status))
4746                         return -EIO;
4747         }
4748
4749         /* Check to see if any errors occurred during init */
4750         if (status & HS_FFERM) {
4751                 /* ERROR: During chipset initialization */
4752                 /* Adapter failed to init, chipset, status reg <status> */
4753                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4754                                 "0438 Adapter failed to init, chipset, "
4755                                 "status reg x%x, "
4756                                 "FW Data: A8 x%x AC x%x\n", status,
4757                                 readl(phba->MBslimaddr + 0xa8),
4758                                 readl(phba->MBslimaddr + 0xac));
4759                 phba->link_state = LPFC_HBA_ERROR;
4760                 return -EIO;
4761         }
4762
4763         /* Clear all interrupt enable conditions */
4764         writel(0, phba->HCregaddr);
4765         readl(phba->HCregaddr); /* flush */
4766
4767         /* setup host attn register */
4768         writel(0xffffffff, phba->HAregaddr);
4769         readl(phba->HAregaddr); /* flush */
4770         return 0;
4771 }
4772
4773 /**
4774  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4775  *
4776  * This function calculates and returns the number of HBQs required to be
4777  * configured.
4778  **/
4779 int
4780 lpfc_sli_hbq_count(void)
4781 {
4782         return ARRAY_SIZE(lpfc_hbq_defs);
4783 }
4784
4785 /**
4786  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4787  *
4788  * This function adds the number of hbq entries in every HBQ to get
4789  * the total number of hbq entries required for the HBA and returns
4790  * the total count.
4791  **/
4792 static int
4793 lpfc_sli_hbq_entry_count(void)
4794 {
4795         int  hbq_count = lpfc_sli_hbq_count();
4796         int  count = 0;
4797         int  i;
4798
4799         for (i = 0; i < hbq_count; ++i)
4800                 count += lpfc_hbq_defs[i]->entry_count;
4801         return count;
4802 }
4803
4804 /**
4805  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4806  *
4807  * This function calculates amount of memory required for all hbq entries
4808  * to be configured and returns the total memory required.
4809  **/
4810 int
4811 lpfc_sli_hbq_size(void)
4812 {
4813         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4814 }
4815
4816 /**
4817  * lpfc_sli_hbq_setup - configure and initialize HBQs
4818  * @phba: Pointer to HBA context object.
4819  *
4820  * This function is called during the SLI initialization to configure
4821  * all the HBQs and post buffers to the HBQ. The caller is not
4822  * required to hold any locks. This function will return zero if successful
4823  * else it will return negative error code.
4824  **/
4825 static int
4826 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4827 {
4828         int  hbq_count = lpfc_sli_hbq_count();
4829         LPFC_MBOXQ_t *pmb;
4830         MAILBOX_t *pmbox;
4831         uint32_t hbqno;
4832         uint32_t hbq_entry_index;
4833
4834                                 /* Get a Mailbox buffer to setup mailbox
4835                                  * commands for HBA initialization
4836                                  */
4837         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4838
4839         if (!pmb)
4840                 return -ENOMEM;
4841
4842         pmbox = &pmb->u.mb;
4843
4844         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4845         phba->link_state = LPFC_INIT_MBX_CMDS;
4846         phba->hbq_in_use = 1;
4847
4848         hbq_entry_index = 0;
4849         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4850                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4851                 phba->hbqs[hbqno].hbqPutIdx      = 0;
4852                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4853                 phba->hbqs[hbqno].entry_count =
4854                         lpfc_hbq_defs[hbqno]->entry_count;
4855                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4856                         hbq_entry_index, pmb);
4857                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4858
4859                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4860                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4861                            mbxStatus <status>, ring <num> */
4862
4863                         lpfc_printf_log(phba, KERN_ERR,
4864                                         LOG_SLI | LOG_VPORT,
4865                                         "1805 Adapter failed to init. "
4866                                         "Data: x%x x%x x%x\n",
4867                                         pmbox->mbxCommand,
4868                                         pmbox->mbxStatus, hbqno);
4869
4870                         phba->link_state = LPFC_HBA_ERROR;
4871                         mempool_free(pmb, phba->mbox_mem_pool);
4872                         return -ENXIO;
4873                 }
4874         }
4875         phba->hbq_count = hbq_count;
4876
4877         mempool_free(pmb, phba->mbox_mem_pool);
4878
4879         /* Initially populate or replenish the HBQs */
4880         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4881                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4882         return 0;
4883 }
4884
4885 /**
4886  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4887  * @phba: Pointer to HBA context object.
4888  *
4889  * This function is called during the SLI initialization to configure
4890  * all the HBQs and post buffers to the HBQ. The caller is not
4891  * required to hold any locks. This function will return zero if successful
4892  * else it will return negative error code.
4893  **/
4894 static int
4895 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4896 {
4897         phba->hbq_in_use = 1;
4898         phba->hbqs[LPFC_ELS_HBQ].entry_count =
4899                 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4900         phba->hbq_count = 1;
4901         lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4902         /* Initially populate or replenish the HBQs */
4903         return 0;
4904 }
4905
4906 /**
4907  * lpfc_sli_config_port - Issue config port mailbox command
4908  * @phba: Pointer to HBA context object.
4909  * @sli_mode: sli mode - 2/3
4910  *
4911  * This function is called by the sli initialization code path
4912  * to issue config_port mailbox command. This function restarts the
4913  * HBA firmware and issues a config_port mailbox command to configure
4914  * the SLI interface in the sli mode specified by sli_mode
4915  * variable. The caller is not required to hold any locks.
4916  * The function returns 0 if successful, else returns negative error
4917  * code.
4918  **/
4919 int
4920 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4921 {
4922         LPFC_MBOXQ_t *pmb;
4923         uint32_t resetcount = 0, rc = 0, done = 0;
4924
4925         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4926         if (!pmb) {
4927                 phba->link_state = LPFC_HBA_ERROR;
4928                 return -ENOMEM;
4929         }
4930
4931         phba->sli_rev = sli_mode;
4932         while (resetcount < 2 && !done) {
4933                 spin_lock_irq(&phba->hbalock);
4934                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4935                 spin_unlock_irq(&phba->hbalock);
4936                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4937                 lpfc_sli_brdrestart(phba);
4938                 rc = lpfc_sli_chipset_init(phba);
4939                 if (rc)
4940                         break;
4941
4942                 spin_lock_irq(&phba->hbalock);
4943                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4944                 spin_unlock_irq(&phba->hbalock);
4945                 resetcount++;
4946
4947                 /* Call pre CONFIG_PORT mailbox command initialization.  A
4948                  * value of 0 means the call was successful.  Any other
4949                  * nonzero value is a failure, but if ERESTART is returned,
4950                  * the driver may reset the HBA and try again.
4951                  */
4952                 rc = lpfc_config_port_prep(phba);
4953                 if (rc == -ERESTART) {
4954                         phba->link_state = LPFC_LINK_UNKNOWN;
4955                         continue;
4956                 } else if (rc)
4957                         break;
4958
4959                 phba->link_state = LPFC_INIT_MBX_CMDS;
4960                 lpfc_config_port(phba, pmb);
4961                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4962                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4963                                         LPFC_SLI3_HBQ_ENABLED |
4964                                         LPFC_SLI3_CRP_ENABLED |
4965                                         LPFC_SLI3_BG_ENABLED |
4966                                         LPFC_SLI3_DSS_ENABLED);
4967                 if (rc != MBX_SUCCESS) {
4968                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4969                                 "0442 Adapter failed to init, mbxCmd x%x "
4970                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4971                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4972                         spin_lock_irq(&phba->hbalock);
4973                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4974                         spin_unlock_irq(&phba->hbalock);
4975                         rc = -ENXIO;
4976                 } else {
4977                         /* Allow asynchronous mailbox command to go through */
4978                         spin_lock_irq(&phba->hbalock);
4979                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4980                         spin_unlock_irq(&phba->hbalock);
4981                         done = 1;
4982
4983                         if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4984                             (pmb->u.mb.un.varCfgPort.gasabt == 0))
4985                                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4986                                         "3110 Port did not grant ASABT\n");
4987                 }
4988         }
4989         if (!done) {
4990                 rc = -EINVAL;
4991                 goto do_prep_failed;
4992         }
4993         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4994                 if (!pmb->u.mb.un.varCfgPort.cMA) {
4995                         rc = -ENXIO;
4996                         goto do_prep_failed;
4997                 }
4998                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4999                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
5000                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5001                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5002                                 phba->max_vpi : phba->max_vports;
5003
5004                 } else
5005                         phba->max_vpi = 0;
5006                 phba->fips_level = 0;
5007                 phba->fips_spec_rev = 0;
5008                 if (pmb->u.mb.un.varCfgPort.gdss) {
5009                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
5010                         phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
5011                         phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5012                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5013                                         "2850 Security Crypto Active. FIPS x%d "
5014                                         "(Spec Rev: x%d)",
5015                                         phba->fips_level, phba->fips_spec_rev);
5016                 }
5017                 if (pmb->u.mb.un.varCfgPort.sec_err) {
5018                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5019                                         "2856 Config Port Security Crypto "
5020                                         "Error: x%x ",
5021                                         pmb->u.mb.un.varCfgPort.sec_err);
5022                 }
5023                 if (pmb->u.mb.un.varCfgPort.gerbm)
5024                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
5025                 if (pmb->u.mb.un.varCfgPort.gcrp)
5026                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
5027
5028                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5029                 phba->port_gp = phba->mbox->us.s3_pgp.port;
5030
5031                 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5032                         if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5033                                 phba->cfg_enable_bg = 0;
5034                                 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5035                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5036                                                 "0443 Adapter did not grant "
5037                                                 "BlockGuard\n");
5038                         }
5039                 }
5040         } else {
5041                 phba->hbq_get = NULL;
5042                 phba->port_gp = phba->mbox->us.s2.port;
5043                 phba->max_vpi = 0;
5044         }
5045 do_prep_failed:
5046         mempool_free(pmb, phba->mbox_mem_pool);
5047         return rc;
5048 }
5049
5050
5051 /**
5052  * lpfc_sli_hba_setup - SLI initialization function
5053  * @phba: Pointer to HBA context object.
5054  *
5055  * This function is the main SLI initialization function. This function
5056  * is called by the HBA initialization code, HBA reset code and HBA
5057  * error attention handler code. Caller is not required to hold any
5058  * locks. This function issues config_port mailbox command to configure
5059  * the SLI, setup iocb rings and HBQ rings. In the end the function
5060  * calls the config_port_post function to issue init_link mailbox
5061  * command and to start the discovery. The function will return zero
5062  * if successful, else it will return negative error code.
5063  **/
5064 int
5065 lpfc_sli_hba_setup(struct lpfc_hba *phba)
5066 {
5067         uint32_t rc;
5068         int  mode = 3, i;
5069         int longs;
5070
5071         switch (phba->cfg_sli_mode) {
5072         case 2:
5073                 if (phba->cfg_enable_npiv) {
5074                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
5075                                 "1824 NPIV enabled: Override sli_mode "
5076                                 "parameter (%d) to auto (0).\n",
5077                                 phba->cfg_sli_mode);
5078                         break;
5079                 }
5080                 mode = 2;
5081                 break;
5082         case 0:
5083         case 3:
5084                 break;
5085         default:
5086                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
5087                                 "1819 Unrecognized sli_mode parameter: %d.\n",
5088                                 phba->cfg_sli_mode);
5089
5090                 break;
5091         }
5092         phba->fcp_embed_io = 0; /* SLI4 FC support only */
5093
5094         rc = lpfc_sli_config_port(phba, mode);
5095
5096         if (rc && phba->cfg_sli_mode == 3)
5097                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
5098                                 "1820 Unable to select SLI-3.  "
5099                                 "Not supported by adapter.\n");
5100         if (rc && mode != 2)
5101                 rc = lpfc_sli_config_port(phba, 2);
5102         else if (rc && mode == 2)
5103                 rc = lpfc_sli_config_port(phba, 3);
5104         if (rc)
5105                 goto lpfc_sli_hba_setup_error;
5106
5107         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5108         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5109                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5110                 if (!rc) {
5111                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5112                                         "2709 This device supports "
5113                                         "Advanced Error Reporting (AER)\n");
5114                         spin_lock_irq(&phba->hbalock);
5115                         phba->hba_flag |= HBA_AER_ENABLED;
5116                         spin_unlock_irq(&phba->hbalock);
5117                 } else {
5118                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5119                                         "2708 This device does not support "
5120                                         "Advanced Error Reporting (AER): %d\n",
5121                                         rc);
5122                         phba->cfg_aer_support = 0;
5123                 }
5124         }
5125
5126         if (phba->sli_rev == 3) {
5127                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5128                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
5129         } else {
5130                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5131                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
5132                 phba->sli3_options = 0;
5133         }
5134
5135         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5136                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5137                         phba->sli_rev, phba->max_vpi);
5138         rc = lpfc_sli_ring_map(phba);
5139
5140         if (rc)
5141                 goto lpfc_sli_hba_setup_error;
5142
5143         /* Initialize VPIs. */
5144         if (phba->sli_rev == LPFC_SLI_REV3) {
5145                 /*
5146                  * The VPI bitmask and physical ID array are allocated
5147                  * and initialized once only - at driver load.  A port
5148                  * reset doesn't need to reinitialize this memory.
5149                  */
5150                 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5151                         longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5152                         phba->vpi_bmask = kcalloc(longs,
5153                                                   sizeof(unsigned long),
5154                                                   GFP_KERNEL);
5155                         if (!phba->vpi_bmask) {
5156                                 rc = -ENOMEM;
5157                                 goto lpfc_sli_hba_setup_error;
5158                         }
5159
5160                         phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5161                                                 sizeof(uint16_t),
5162                                                 GFP_KERNEL);
5163                         if (!phba->vpi_ids) {
5164                                 kfree(phba->vpi_bmask);
5165                                 rc = -ENOMEM;
5166                                 goto lpfc_sli_hba_setup_error;
5167                         }
5168                         for (i = 0; i < phba->max_vpi; i++)
5169                                 phba->vpi_ids[i] = i;
5170                 }
5171         }
5172
5173         /* Init HBQs */
5174         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5175                 rc = lpfc_sli_hbq_setup(phba);
5176                 if (rc)
5177                         goto lpfc_sli_hba_setup_error;
5178         }
5179         spin_lock_irq(&phba->hbalock);
5180         phba->sli.sli_flag |= LPFC_PROCESS_LA;
5181         spin_unlock_irq(&phba->hbalock);
5182
5183         rc = lpfc_config_port_post(phba);
5184         if (rc)
5185                 goto lpfc_sli_hba_setup_error;
5186
5187         return rc;
5188
5189 lpfc_sli_hba_setup_error:
5190         phba->link_state = LPFC_HBA_ERROR;
5191         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5192                         "0445 Firmware initialization failed\n");
5193         return rc;
5194 }
5195
5196 /**
5197  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5198  * @phba: Pointer to HBA context object.
5199  * @mboxq: mailbox pointer.
5200  * This function issue a dump mailbox command to read config region
5201  * 23 and parse the records in the region and populate driver
5202  * data structure.
5203  **/
5204 static int
5205 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
5206 {
5207         LPFC_MBOXQ_t *mboxq;
5208         struct lpfc_dmabuf *mp;
5209         struct lpfc_mqe *mqe;
5210         uint32_t data_length;
5211         int rc;
5212
5213         /* Program the default value of vlan_id and fc_map */
5214         phba->valid_vlan = 0;
5215         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5216         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5217         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5218
5219         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5220         if (!mboxq)
5221                 return -ENOMEM;
5222
5223         mqe = &mboxq->u.mqe;
5224         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5225                 rc = -ENOMEM;
5226                 goto out_free_mboxq;
5227         }
5228
5229         mp = (struct lpfc_dmabuf *) mboxq->context1;
5230         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5231
5232         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5233                         "(%d):2571 Mailbox cmd x%x Status x%x "
5234                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5235                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5236                         "CQ: x%x x%x x%x x%x\n",
5237                         mboxq->vport ? mboxq->vport->vpi : 0,
5238                         bf_get(lpfc_mqe_command, mqe),
5239                         bf_get(lpfc_mqe_status, mqe),
5240                         mqe->un.mb_words[0], mqe->un.mb_words[1],
5241                         mqe->un.mb_words[2], mqe->un.mb_words[3],
5242                         mqe->un.mb_words[4], mqe->un.mb_words[5],
5243                         mqe->un.mb_words[6], mqe->un.mb_words[7],
5244                         mqe->un.mb_words[8], mqe->un.mb_words[9],
5245                         mqe->un.mb_words[10], mqe->un.mb_words[11],
5246                         mqe->un.mb_words[12], mqe->un.mb_words[13],
5247                         mqe->un.mb_words[14], mqe->un.mb_words[15],
5248                         mqe->un.mb_words[16], mqe->un.mb_words[50],
5249                         mboxq->mcqe.word0,
5250                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5251                         mboxq->mcqe.trailer);
5252
5253         if (rc) {
5254                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5255                 kfree(mp);
5256                 rc = -EIO;
5257                 goto out_free_mboxq;
5258         }
5259         data_length = mqe->un.mb_words[5];
5260         if (data_length > DMP_RGN23_SIZE) {
5261                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5262                 kfree(mp);
5263                 rc = -EIO;
5264                 goto out_free_mboxq;
5265         }
5266
5267         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5268         lpfc_mbuf_free(phba, mp->virt, mp->phys);
5269         kfree(mp);
5270         rc = 0;
5271
5272 out_free_mboxq:
5273         mempool_free(mboxq, phba->mbox_mem_pool);
5274         return rc;
5275 }
5276
5277 /**
5278  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5279  * @phba: pointer to lpfc hba data structure.
5280  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5281  * @vpd: pointer to the memory to hold resulting port vpd data.
5282  * @vpd_size: On input, the number of bytes allocated to @vpd.
5283  *            On output, the number of data bytes in @vpd.
5284  *
5285  * This routine executes a READ_REV SLI4 mailbox command.  In
5286  * addition, this routine gets the port vpd data.
5287  *
5288  * Return codes
5289  *      0 - successful
5290  *      -ENOMEM - could not allocated memory.
5291  **/
5292 static int
5293 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5294                     uint8_t *vpd, uint32_t *vpd_size)
5295 {
5296         int rc = 0;
5297         uint32_t dma_size;
5298         struct lpfc_dmabuf *dmabuf;
5299         struct lpfc_mqe *mqe;
5300
5301         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5302         if (!dmabuf)
5303                 return -ENOMEM;
5304
5305         /*
5306          * Get a DMA buffer for the vpd data resulting from the READ_REV
5307          * mailbox command.
5308          */
5309         dma_size = *vpd_size;
5310         dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5311                                            &dmabuf->phys, GFP_KERNEL);
5312         if (!dmabuf->virt) {
5313                 kfree(dmabuf);
5314                 return -ENOMEM;
5315         }
5316
5317         /*
5318          * The SLI4 implementation of READ_REV conflicts at word1,
5319          * bits 31:16 and SLI4 adds vpd functionality not present
5320          * in SLI3.  This code corrects the conflicts.
5321          */
5322         lpfc_read_rev(phba, mboxq);
5323         mqe = &mboxq->u.mqe;
5324         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5325         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5326         mqe->un.read_rev.word1 &= 0x0000FFFF;
5327         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5328         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5329
5330         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5331         if (rc) {
5332                 dma_free_coherent(&phba->pcidev->dev, dma_size,
5333                                   dmabuf->virt, dmabuf->phys);
5334                 kfree(dmabuf);
5335                 return -EIO;
5336         }
5337
5338         /*
5339          * The available vpd length cannot be bigger than the
5340          * DMA buffer passed to the port.  Catch the less than
5341          * case and update the caller's size.
5342          */
5343         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5344                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5345
5346         memcpy(vpd, dmabuf->virt, *vpd_size);
5347
5348         dma_free_coherent(&phba->pcidev->dev, dma_size,
5349                           dmabuf->virt, dmabuf->phys);
5350         kfree(dmabuf);
5351         return 0;
5352 }
5353
5354 /**
5355  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5356  * @phba: pointer to lpfc hba data structure.
5357  *
5358  * This routine retrieves SLI4 device physical port name this PCI function
5359  * is attached to.
5360  *
5361  * Return codes
5362  *      0 - successful
5363  *      otherwise - failed to retrieve physical port name
5364  **/
5365 static int
5366 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5367 {
5368         LPFC_MBOXQ_t *mboxq;
5369         struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5370         struct lpfc_controller_attribute *cntl_attr;
5371         struct lpfc_mbx_get_port_name *get_port_name;
5372         void *virtaddr = NULL;
5373         uint32_t alloclen, reqlen;
5374         uint32_t shdr_status, shdr_add_status;
5375         union lpfc_sli4_cfg_shdr *shdr;
5376         char cport_name = 0;
5377         int rc;
5378
5379         /* We assume nothing at this point */
5380         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5381         phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5382
5383         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5384         if (!mboxq)
5385                 return -ENOMEM;
5386         /* obtain link type and link number via READ_CONFIG */
5387         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5388         lpfc_sli4_read_config(phba);
5389         if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5390                 goto retrieve_ppname;
5391
5392         /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5393         reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5394         alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5395                         LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5396                         LPFC_SLI4_MBX_NEMBED);
5397         if (alloclen < reqlen) {
5398                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5399                                 "3084 Allocated DMA memory size (%d) is "
5400                                 "less than the requested DMA memory size "
5401                                 "(%d)\n", alloclen, reqlen);
5402                 rc = -ENOMEM;
5403                 goto out_free_mboxq;
5404         }
5405         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5406         virtaddr = mboxq->sge_array->addr[0];
5407         mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5408         shdr = &mbx_cntl_attr->cfg_shdr;
5409         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5410         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5411         if (shdr_status || shdr_add_status || rc) {
5412                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5413                                 "3085 Mailbox x%x (x%x/x%x) failed, "
5414                                 "rc:x%x, status:x%x, add_status:x%x\n",
5415                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5416                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5417                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5418                                 rc, shdr_status, shdr_add_status);
5419                 rc = -ENXIO;
5420                 goto out_free_mboxq;
5421         }
5422         cntl_attr = &mbx_cntl_attr->cntl_attr;
5423         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5424         phba->sli4_hba.lnk_info.lnk_tp =
5425                 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5426         phba->sli4_hba.lnk_info.lnk_no =
5427                 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5428         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5429                         "3086 lnk_type:%d, lnk_numb:%d\n",
5430                         phba->sli4_hba.lnk_info.lnk_tp,
5431                         phba->sli4_hba.lnk_info.lnk_no);
5432
5433 retrieve_ppname:
5434         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5435                 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5436                 sizeof(struct lpfc_mbx_get_port_name) -
5437                 sizeof(struct lpfc_sli4_cfg_mhdr),
5438                 LPFC_SLI4_MBX_EMBED);
5439         get_port_name = &mboxq->u.mqe.un.get_port_name;
5440         shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5441         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5442         bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5443                 phba->sli4_hba.lnk_info.lnk_tp);
5444         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5445         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5446         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5447         if (shdr_status || shdr_add_status || rc) {
5448                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5449                                 "3087 Mailbox x%x (x%x/x%x) failed: "
5450                                 "rc:x%x, status:x%x, add_status:x%x\n",
5451                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5452                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5453                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5454                                 rc, shdr_status, shdr_add_status);
5455                 rc = -ENXIO;
5456                 goto out_free_mboxq;
5457         }
5458         switch (phba->sli4_hba.lnk_info.lnk_no) {
5459         case LPFC_LINK_NUMBER_0:
5460                 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5461                                 &get_port_name->u.response);
5462                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5463                 break;
5464         case LPFC_LINK_NUMBER_1:
5465                 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5466                                 &get_port_name->u.response);
5467                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5468                 break;
5469         case LPFC_LINK_NUMBER_2:
5470                 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5471                                 &get_port_name->u.response);
5472                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5473                 break;
5474         case LPFC_LINK_NUMBER_3:
5475                 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5476                                 &get_port_name->u.response);
5477                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5478                 break;
5479         default:
5480                 break;
5481         }
5482
5483         if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5484                 phba->Port[0] = cport_name;
5485                 phba->Port[1] = '\0';
5486                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5487                                 "3091 SLI get port name: %s\n", phba->Port);
5488         }
5489
5490 out_free_mboxq:
5491         if (rc != MBX_TIMEOUT) {
5492                 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5493                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
5494                 else
5495                         mempool_free(mboxq, phba->mbox_mem_pool);
5496         }
5497         return rc;
5498 }
5499
5500 /**
5501  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5502  * @phba: pointer to lpfc hba data structure.
5503  *
5504  * This routine is called to explicitly arm the SLI4 device's completion and
5505  * event queues
5506  **/
5507 static void
5508 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5509 {
5510         int qidx;
5511         struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
5512
5513         sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5514         sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5515         if (sli4_hba->nvmels_cq)
5516                 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
5517                                                 LPFC_QUEUE_REARM);
5518
5519         if (sli4_hba->fcp_cq)
5520                 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5521                         sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
5522                                                 LPFC_QUEUE_REARM);
5523
5524         if (sli4_hba->nvme_cq)
5525                 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5526                         sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
5527                                                 LPFC_QUEUE_REARM);
5528
5529         if (phba->cfg_fof)
5530                 sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
5531
5532         if (sli4_hba->hba_eq)
5533                 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5534                         sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5535                                                         LPFC_QUEUE_REARM);
5536
5537         if (phba->nvmet_support) {
5538                 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5539                         sli4_hba->sli4_cq_release(
5540                                 sli4_hba->nvmet_cqset[qidx],
5541                                 LPFC_QUEUE_REARM);
5542                 }
5543         }
5544
5545         if (phba->cfg_fof)
5546                 sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
5547 }
5548
5549 /**
5550  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5551  * @phba: Pointer to HBA context object.
5552  * @type: The resource extent type.
5553  * @extnt_count: buffer to hold port available extent count.
5554  * @extnt_size: buffer to hold element count per extent.
5555  *
5556  * This function calls the port and retrievs the number of available
5557  * extents and their size for a particular extent type.
5558  *
5559  * Returns: 0 if successful.  Nonzero otherwise.
5560  **/
5561 int
5562 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5563                                uint16_t *extnt_count, uint16_t *extnt_size)
5564 {
5565         int rc = 0;
5566         uint32_t length;
5567         uint32_t mbox_tmo;
5568         struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5569         LPFC_MBOXQ_t *mbox;
5570
5571         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5572         if (!mbox)
5573                 return -ENOMEM;
5574
5575         /* Find out how many extents are available for this resource type */
5576         length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5577                   sizeof(struct lpfc_sli4_cfg_mhdr));
5578         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5579                          LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5580                          length, LPFC_SLI4_MBX_EMBED);
5581
5582         /* Send an extents count of 0 - the GET doesn't use it. */
5583         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5584                                         LPFC_SLI4_MBX_EMBED);
5585         if (unlikely(rc)) {
5586                 rc = -EIO;
5587                 goto err_exit;
5588         }
5589
5590         if (!phba->sli4_hba.intr_enable)
5591                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5592         else {
5593                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5594                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5595         }
5596         if (unlikely(rc)) {
5597                 rc = -EIO;
5598                 goto err_exit;
5599         }
5600
5601         rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5602         if (bf_get(lpfc_mbox_hdr_status,
5603                    &rsrc_info->header.cfg_shdr.response)) {
5604                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5605                                 "2930 Failed to get resource extents "
5606                                 "Status 0x%x Add'l Status 0x%x\n",
5607                                 bf_get(lpfc_mbox_hdr_status,
5608                                        &rsrc_info->header.cfg_shdr.response),
5609                                 bf_get(lpfc_mbox_hdr_add_status,
5610                                        &rsrc_info->header.cfg_shdr.response));
5611                 rc = -EIO;
5612                 goto err_exit;
5613         }
5614
5615         *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5616                               &rsrc_info->u.rsp);
5617         *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5618                              &rsrc_info->u.rsp);
5619
5620         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5621                         "3162 Retrieved extents type-%d from port: count:%d, "
5622                         "size:%d\n", type, *extnt_count, *extnt_size);
5623
5624 err_exit:
5625         mempool_free(mbox, phba->mbox_mem_pool);
5626         return rc;
5627 }
5628
5629 /**
5630  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5631  * @phba: Pointer to HBA context object.
5632  * @type: The extent type to check.
5633  *
5634  * This function reads the current available extents from the port and checks
5635  * if the extent count or extent size has changed since the last access.
5636  * Callers use this routine post port reset to understand if there is a
5637  * extent reprovisioning requirement.
5638  *
5639  * Returns:
5640  *   -Error: error indicates problem.
5641  *   1: Extent count or size has changed.
5642  *   0: No changes.
5643  **/
5644 static int
5645 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5646 {
5647         uint16_t curr_ext_cnt, rsrc_ext_cnt;
5648         uint16_t size_diff, rsrc_ext_size;
5649         int rc = 0;
5650         struct lpfc_rsrc_blks *rsrc_entry;
5651         struct list_head *rsrc_blk_list = NULL;
5652
5653         size_diff = 0;
5654         curr_ext_cnt = 0;
5655         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5656                                             &rsrc_ext_cnt,
5657                                             &rsrc_ext_size);
5658         if (unlikely(rc))
5659                 return -EIO;
5660
5661         switch (type) {
5662         case LPFC_RSC_TYPE_FCOE_RPI:
5663                 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5664                 break;
5665         case LPFC_RSC_TYPE_FCOE_VPI:
5666                 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5667                 break;
5668         case LPFC_RSC_TYPE_FCOE_XRI:
5669                 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5670                 break;
5671         case LPFC_RSC_TYPE_FCOE_VFI:
5672                 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5673                 break;
5674         default:
5675                 break;
5676         }
5677
5678         list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5679                 curr_ext_cnt++;
5680                 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5681                         size_diff++;
5682         }
5683
5684         if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5685                 rc = 1;
5686
5687         return rc;
5688 }
5689
5690 /**
5691  * lpfc_sli4_cfg_post_extnts -
5692  * @phba: Pointer to HBA context object.
5693  * @extnt_cnt - number of available extents.
5694  * @type - the extent type (rpi, xri, vfi, vpi).
5695  * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5696  * @mbox - pointer to the caller's allocated mailbox structure.
5697  *
5698  * This function executes the extents allocation request.  It also
5699  * takes care of the amount of memory needed to allocate or get the
5700  * allocated extents. It is the caller's responsibility to evaluate
5701  * the response.
5702  *
5703  * Returns:
5704  *   -Error:  Error value describes the condition found.
5705  *   0: if successful
5706  **/
5707 static int
5708 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5709                           uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5710 {
5711         int rc = 0;
5712         uint32_t req_len;
5713         uint32_t emb_len;
5714         uint32_t alloc_len, mbox_tmo;
5715
5716         /* Calculate the total requested length of the dma memory */
5717         req_len = extnt_cnt * sizeof(uint16_t);
5718
5719         /*
5720          * Calculate the size of an embedded mailbox.  The uint32_t
5721          * accounts for extents-specific word.
5722          */
5723         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5724                 sizeof(uint32_t);
5725
5726         /*
5727          * Presume the allocation and response will fit into an embedded
5728          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
5729          */
5730         *emb = LPFC_SLI4_MBX_EMBED;
5731         if (req_len > emb_len) {
5732                 req_len = extnt_cnt * sizeof(uint16_t) +
5733                         sizeof(union lpfc_sli4_cfg_shdr) +
5734                         sizeof(uint32_t);
5735                 *emb = LPFC_SLI4_MBX_NEMBED;
5736         }
5737
5738         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5739                                      LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5740                                      req_len, *emb);
5741         if (alloc_len < req_len) {
5742                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5743                         "2982 Allocated DMA memory size (x%x) is "
5744                         "less than the requested DMA memory "
5745                         "size (x%x)\n", alloc_len, req_len);
5746                 return -ENOMEM;
5747         }
5748         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5749         if (unlikely(rc))
5750                 return -EIO;
5751
5752         if (!phba->sli4_hba.intr_enable)
5753                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5754         else {
5755                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5756                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5757         }
5758
5759         if (unlikely(rc))
5760                 rc = -EIO;
5761         return rc;
5762 }
5763
5764 /**
5765  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5766  * @phba: Pointer to HBA context object.
5767  * @type:  The resource extent type to allocate.
5768  *
5769  * This function allocates the number of elements for the specified
5770  * resource type.
5771  **/
5772 static int
5773 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5774 {
5775         bool emb = false;
5776         uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5777         uint16_t rsrc_id, rsrc_start, j, k;
5778         uint16_t *ids;
5779         int i, rc;
5780         unsigned long longs;
5781         unsigned long *bmask;
5782         struct lpfc_rsrc_blks *rsrc_blks;
5783         LPFC_MBOXQ_t *mbox;
5784         uint32_t length;
5785         struct lpfc_id_range *id_array = NULL;
5786         void *virtaddr = NULL;
5787         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5788         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5789         struct list_head *ext_blk_list;
5790
5791         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5792                                             &rsrc_cnt,
5793                                             &rsrc_size);
5794         if (unlikely(rc))
5795                 return -EIO;
5796
5797         if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5798                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5799                         "3009 No available Resource Extents "
5800                         "for resource type 0x%x: Count: 0x%x, "
5801                         "Size 0x%x\n", type, rsrc_cnt,
5802                         rsrc_size);
5803                 return -ENOMEM;
5804         }
5805
5806         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5807                         "2903 Post resource extents type-0x%x: "
5808                         "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5809
5810         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5811         if (!mbox)
5812                 return -ENOMEM;
5813
5814         rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5815         if (unlikely(rc)) {
5816                 rc = -EIO;
5817                 goto err_exit;
5818         }
5819
5820         /*
5821          * Figure out where the response is located.  Then get local pointers
5822          * to the response data.  The port does not guarantee to respond to
5823          * all extents counts request so update the local variable with the
5824          * allocated count from the port.
5825          */
5826         if (emb == LPFC_SLI4_MBX_EMBED) {
5827                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5828                 id_array = &rsrc_ext->u.rsp.id[0];
5829                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5830         } else {
5831                 virtaddr = mbox->sge_array->addr[0];
5832                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5833                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5834                 id_array = &n_rsrc->id;
5835         }
5836
5837         longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5838         rsrc_id_cnt = rsrc_cnt * rsrc_size;
5839
5840         /*
5841          * Based on the resource size and count, correct the base and max
5842          * resource values.
5843          */
5844         length = sizeof(struct lpfc_rsrc_blks);
5845         switch (type) {
5846         case LPFC_RSC_TYPE_FCOE_RPI:
5847                 phba->sli4_hba.rpi_bmask = kcalloc(longs,
5848                                                    sizeof(unsigned long),
5849                                                    GFP_KERNEL);
5850                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5851                         rc = -ENOMEM;
5852                         goto err_exit;
5853                 }
5854                 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
5855                                                  sizeof(uint16_t),
5856                                                  GFP_KERNEL);
5857                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5858                         kfree(phba->sli4_hba.rpi_bmask);
5859                         rc = -ENOMEM;
5860                         goto err_exit;
5861                 }
5862
5863                 /*
5864                  * The next_rpi was initialized with the maximum available
5865                  * count but the port may allocate a smaller number.  Catch
5866                  * that case and update the next_rpi.
5867                  */
5868                 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5869
5870                 /* Initialize local ptrs for common extent processing later. */
5871                 bmask = phba->sli4_hba.rpi_bmask;
5872                 ids = phba->sli4_hba.rpi_ids;
5873                 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5874                 break;
5875         case LPFC_RSC_TYPE_FCOE_VPI:
5876                 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
5877                                           GFP_KERNEL);
5878                 if (unlikely(!phba->vpi_bmask)) {
5879                         rc = -ENOMEM;
5880                         goto err_exit;
5881                 }
5882                 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
5883                                          GFP_KERNEL);
5884                 if (unlikely(!phba->vpi_ids)) {
5885                         kfree(phba->vpi_bmask);
5886                         rc = -ENOMEM;
5887                         goto err_exit;
5888                 }
5889
5890                 /* Initialize local ptrs for common extent processing later. */
5891                 bmask = phba->vpi_bmask;
5892                 ids = phba->vpi_ids;
5893                 ext_blk_list = &phba->lpfc_vpi_blk_list;
5894                 break;
5895         case LPFC_RSC_TYPE_FCOE_XRI:
5896                 phba->sli4_hba.xri_bmask = kcalloc(longs,
5897                                                    sizeof(unsigned long),
5898                                                    GFP_KERNEL);
5899                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5900                         rc = -ENOMEM;
5901                         goto err_exit;
5902                 }
5903                 phba->sli4_hba.max_cfg_param.xri_used = 0;
5904                 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
5905                                                  sizeof(uint16_t),
5906                                                  GFP_KERNEL);
5907                 if (unlikely(!phba->sli4_hba.xri_ids)) {
5908                         kfree(phba->sli4_hba.xri_bmask);
5909                         rc = -ENOMEM;
5910                         goto err_exit;
5911                 }
5912
5913                 /* Initialize local ptrs for common extent processing later. */
5914                 bmask = phba->sli4_hba.xri_bmask;
5915                 ids = phba->sli4_hba.xri_ids;
5916                 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5917                 break;
5918         case LPFC_RSC_TYPE_FCOE_VFI:
5919                 phba->sli4_hba.vfi_bmask = kcalloc(longs,
5920                                                    sizeof(unsigned long),
5921                                                    GFP_KERNEL);
5922                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5923                         rc = -ENOMEM;
5924                         goto err_exit;
5925                 }
5926                 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
5927                                                  sizeof(uint16_t),
5928                                                  GFP_KERNEL);
5929                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5930                         kfree(phba->sli4_hba.vfi_bmask);
5931                         rc = -ENOMEM;
5932                         goto err_exit;
5933                 }
5934
5935                 /* Initialize local ptrs for common extent processing later. */
5936                 bmask = phba->sli4_hba.vfi_bmask;
5937                 ids = phba->sli4_hba.vfi_ids;
5938                 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5939                 break;
5940         default:
5941                 /* Unsupported Opcode.  Fail call. */
5942                 id_array = NULL;
5943                 bmask = NULL;
5944                 ids = NULL;
5945                 ext_blk_list = NULL;
5946                 goto err_exit;
5947         }
5948
5949         /*
5950          * Complete initializing the extent configuration with the
5951          * allocated ids assigned to this function.  The bitmask serves
5952          * as an index into the array and manages the available ids.  The
5953          * array just stores the ids communicated to the port via the wqes.
5954          */
5955         for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5956                 if ((i % 2) == 0)
5957                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5958                                          &id_array[k]);
5959                 else
5960                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5961                                          &id_array[k]);
5962
5963                 rsrc_blks = kzalloc(length, GFP_KERNEL);
5964                 if (unlikely(!rsrc_blks)) {
5965                         rc = -ENOMEM;
5966                         kfree(bmask);
5967                         kfree(ids);
5968                         goto err_exit;
5969                 }
5970                 rsrc_blks->rsrc_start = rsrc_id;
5971                 rsrc_blks->rsrc_size = rsrc_size;
5972                 list_add_tail(&rsrc_blks->list, ext_blk_list);
5973                 rsrc_start = rsrc_id;
5974                 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
5975                         phba->sli4_hba.scsi_xri_start = rsrc_start +
5976                                 lpfc_sli4_get_iocb_cnt(phba);
5977                         phba->sli4_hba.nvme_xri_start =
5978                                 phba->sli4_hba.scsi_xri_start +
5979                                 phba->sli4_hba.scsi_xri_max;
5980                 }
5981
5982                 while (rsrc_id < (rsrc_start + rsrc_size)) {
5983                         ids[j] = rsrc_id;
5984                         rsrc_id++;
5985                         j++;
5986                 }
5987                 /* Entire word processed.  Get next word.*/
5988                 if ((i % 2) == 1)
5989                         k++;
5990         }
5991  err_exit:
5992         lpfc_sli4_mbox_cmd_free(phba, mbox);
5993         return rc;
5994 }
5995
5996
5997
5998 /**
5999  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6000  * @phba: Pointer to HBA context object.
6001  * @type: the extent's type.
6002  *
6003  * This function deallocates all extents of a particular resource type.
6004  * SLI4 does not allow for deallocating a particular extent range.  It
6005  * is the caller's responsibility to release all kernel memory resources.
6006  **/
6007 static int
6008 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6009 {
6010         int rc;
6011         uint32_t length, mbox_tmo = 0;
6012         LPFC_MBOXQ_t *mbox;
6013         struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6014         struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6015
6016         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6017         if (!mbox)
6018                 return -ENOMEM;
6019
6020         /*
6021          * This function sends an embedded mailbox because it only sends the
6022          * the resource type.  All extents of this type are released by the
6023          * port.
6024          */
6025         length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6026                   sizeof(struct lpfc_sli4_cfg_mhdr));
6027         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6028                          LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6029                          length, LPFC_SLI4_MBX_EMBED);
6030
6031         /* Send an extents count of 0 - the dealloc doesn't use it. */
6032         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6033                                         LPFC_SLI4_MBX_EMBED);
6034         if (unlikely(rc)) {
6035                 rc = -EIO;
6036                 goto out_free_mbox;
6037         }
6038         if (!phba->sli4_hba.intr_enable)
6039                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6040         else {
6041                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6042                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6043         }
6044         if (unlikely(rc)) {
6045                 rc = -EIO;
6046                 goto out_free_mbox;
6047         }
6048
6049         dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6050         if (bf_get(lpfc_mbox_hdr_status,
6051                    &dealloc_rsrc->header.cfg_shdr.response)) {
6052                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6053                                 "2919 Failed to release resource extents "
6054                                 "for type %d - Status 0x%x Add'l Status 0x%x. "
6055                                 "Resource memory not released.\n",
6056                                 type,
6057                                 bf_get(lpfc_mbox_hdr_status,
6058                                     &dealloc_rsrc->header.cfg_shdr.response),
6059                                 bf_get(lpfc_mbox_hdr_add_status,
6060                                     &dealloc_rsrc->header.cfg_shdr.response));
6061                 rc = -EIO;
6062                 goto out_free_mbox;
6063         }
6064
6065         /* Release kernel memory resources for the specific type. */
6066         switch (type) {
6067         case LPFC_RSC_TYPE_FCOE_VPI:
6068                 kfree(phba->vpi_bmask);
6069                 kfree(phba->vpi_ids);
6070                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6071                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6072                                     &phba->lpfc_vpi_blk_list, list) {
6073                         list_del_init(&rsrc_blk->list);
6074                         kfree(rsrc_blk);
6075                 }
6076                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6077                 break;
6078         case LPFC_RSC_TYPE_FCOE_XRI:
6079                 kfree(phba->sli4_hba.xri_bmask);
6080                 kfree(phba->sli4_hba.xri_ids);
6081                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6082                                     &phba->sli4_hba.lpfc_xri_blk_list, list) {
6083                         list_del_init(&rsrc_blk->list);
6084                         kfree(rsrc_blk);
6085                 }
6086                 break;
6087         case LPFC_RSC_TYPE_FCOE_VFI:
6088                 kfree(phba->sli4_hba.vfi_bmask);
6089                 kfree(phba->sli4_hba.vfi_ids);
6090                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6091                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6092                                     &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6093                         list_del_init(&rsrc_blk->list);
6094                         kfree(rsrc_blk);
6095                 }
6096                 break;
6097         case LPFC_RSC_TYPE_FCOE_RPI:
6098                 /* RPI bitmask and physical id array are cleaned up earlier. */
6099                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6100                                     &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6101                         list_del_init(&rsrc_blk->list);
6102                         kfree(rsrc_blk);
6103                 }
6104                 break;
6105         default:
6106                 break;
6107         }
6108
6109         bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6110
6111  out_free_mbox:
6112         mempool_free(mbox, phba->mbox_mem_pool);
6113         return rc;
6114 }
6115
6116 static void
6117 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6118                   uint32_t feature)
6119 {
6120         uint32_t len;
6121
6122         len = sizeof(struct lpfc_mbx_set_feature) -
6123                 sizeof(struct lpfc_sli4_cfg_mhdr);
6124         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6125                          LPFC_MBOX_OPCODE_SET_FEATURES, len,
6126                          LPFC_SLI4_MBX_EMBED);
6127
6128         switch (feature) {
6129         case LPFC_SET_UE_RECOVERY:
6130                 bf_set(lpfc_mbx_set_feature_UER,
6131                        &mbox->u.mqe.un.set_feature, 1);
6132                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6133                 mbox->u.mqe.un.set_feature.param_len = 8;
6134                 break;
6135         case LPFC_SET_MDS_DIAGS:
6136                 bf_set(lpfc_mbx_set_feature_mds,
6137                        &mbox->u.mqe.un.set_feature, 1);
6138                 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
6139                        &mbox->u.mqe.un.set_feature, 1);
6140                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6141                 mbox->u.mqe.un.set_feature.param_len = 8;
6142                 break;
6143         }
6144
6145         return;
6146 }
6147
6148 /**
6149  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6150  * @phba: Pointer to HBA context object.
6151  *
6152  * This function allocates all SLI4 resource identifiers.
6153  **/
6154 int
6155 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6156 {
6157         int i, rc, error = 0;
6158         uint16_t count, base;
6159         unsigned long longs;
6160
6161         if (!phba->sli4_hba.rpi_hdrs_in_use)
6162                 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6163         if (phba->sli4_hba.extents_in_use) {
6164                 /*
6165                  * The port supports resource extents. The XRI, VPI, VFI, RPI
6166                  * resource extent count must be read and allocated before
6167                  * provisioning the resource id arrays.
6168                  */
6169                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6170                     LPFC_IDX_RSRC_RDY) {
6171                         /*
6172                          * Extent-based resources are set - the driver could
6173                          * be in a port reset. Figure out if any corrective
6174                          * actions need to be taken.
6175                          */
6176                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6177                                                  LPFC_RSC_TYPE_FCOE_VFI);
6178                         if (rc != 0)
6179                                 error++;
6180                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6181                                                  LPFC_RSC_TYPE_FCOE_VPI);
6182                         if (rc != 0)
6183                                 error++;
6184                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6185                                                  LPFC_RSC_TYPE_FCOE_XRI);
6186                         if (rc != 0)
6187                                 error++;
6188                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6189                                                  LPFC_RSC_TYPE_FCOE_RPI);
6190                         if (rc != 0)
6191                                 error++;
6192
6193                         /*
6194                          * It's possible that the number of resources
6195                          * provided to this port instance changed between
6196                          * resets.  Detect this condition and reallocate
6197                          * resources.  Otherwise, there is no action.
6198                          */
6199                         if (error) {
6200                                 lpfc_printf_log(phba, KERN_INFO,
6201                                                 LOG_MBOX | LOG_INIT,
6202                                                 "2931 Detected extent resource "
6203                                                 "change.  Reallocating all "
6204                                                 "extents.\n");
6205                                 rc = lpfc_sli4_dealloc_extent(phba,
6206                                                  LPFC_RSC_TYPE_FCOE_VFI);
6207                                 rc = lpfc_sli4_dealloc_extent(phba,
6208                                                  LPFC_RSC_TYPE_FCOE_VPI);
6209                                 rc = lpfc_sli4_dealloc_extent(phba,
6210                                                  LPFC_RSC_TYPE_FCOE_XRI);
6211                                 rc = lpfc_sli4_dealloc_extent(phba,
6212                                                  LPFC_RSC_TYPE_FCOE_RPI);
6213                         } else
6214                                 return 0;
6215                 }
6216
6217                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6218                 if (unlikely(rc))
6219                         goto err_exit;
6220
6221                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6222                 if (unlikely(rc))
6223                         goto err_exit;
6224
6225                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6226                 if (unlikely(rc))
6227                         goto err_exit;
6228
6229                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6230                 if (unlikely(rc))
6231                         goto err_exit;
6232                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6233                        LPFC_IDX_RSRC_RDY);
6234                 return rc;
6235         } else {
6236                 /*
6237                  * The port does not support resource extents.  The XRI, VPI,
6238                  * VFI, RPI resource ids were determined from READ_CONFIG.
6239                  * Just allocate the bitmasks and provision the resource id
6240                  * arrays.  If a port reset is active, the resources don't
6241                  * need any action - just exit.
6242                  */
6243                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6244                     LPFC_IDX_RSRC_RDY) {
6245                         lpfc_sli4_dealloc_resource_identifiers(phba);
6246                         lpfc_sli4_remove_rpis(phba);
6247                 }
6248                 /* RPIs. */
6249                 count = phba->sli4_hba.max_cfg_param.max_rpi;
6250                 if (count <= 0) {
6251                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6252                                         "3279 Invalid provisioning of "
6253                                         "rpi:%d\n", count);
6254                         rc = -EINVAL;
6255                         goto err_exit;
6256                 }
6257                 base = phba->sli4_hba.max_cfg_param.rpi_base;
6258                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6259                 phba->sli4_hba.rpi_bmask = kcalloc(longs,
6260                                                    sizeof(unsigned long),
6261                                                    GFP_KERNEL);
6262                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6263                         rc = -ENOMEM;
6264                         goto err_exit;
6265                 }
6266                 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
6267                                                  GFP_KERNEL);
6268                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6269                         rc = -ENOMEM;
6270                         goto free_rpi_bmask;
6271                 }
6272
6273                 for (i = 0; i < count; i++)
6274                         phba->sli4_hba.rpi_ids[i] = base + i;
6275
6276                 /* VPIs. */
6277                 count = phba->sli4_hba.max_cfg_param.max_vpi;
6278                 if (count <= 0) {
6279                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6280                                         "3280 Invalid provisioning of "
6281                                         "vpi:%d\n", count);
6282                         rc = -EINVAL;
6283                         goto free_rpi_ids;
6284                 }
6285                 base = phba->sli4_hba.max_cfg_param.vpi_base;
6286                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6287                 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6288                                           GFP_KERNEL);
6289                 if (unlikely(!phba->vpi_bmask)) {
6290                         rc = -ENOMEM;
6291                         goto free_rpi_ids;
6292                 }
6293                 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
6294                                         GFP_KERNEL);
6295                 if (unlikely(!phba->vpi_ids)) {
6296                         rc = -ENOMEM;
6297                         goto free_vpi_bmask;
6298                 }
6299
6300                 for (i = 0; i < count; i++)
6301                         phba->vpi_ids[i] = base + i;
6302
6303                 /* XRIs. */
6304                 count = phba->sli4_hba.max_cfg_param.max_xri;
6305                 if (count <= 0) {
6306                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6307                                         "3281 Invalid provisioning of "
6308                                         "xri:%d\n", count);
6309                         rc = -EINVAL;
6310                         goto free_vpi_ids;
6311                 }
6312                 base = phba->sli4_hba.max_cfg_param.xri_base;
6313                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6314                 phba->sli4_hba.xri_bmask = kcalloc(longs,
6315                                                    sizeof(unsigned long),
6316                                                    GFP_KERNEL);
6317                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6318                         rc = -ENOMEM;
6319                         goto free_vpi_ids;
6320                 }
6321                 phba->sli4_hba.max_cfg_param.xri_used = 0;
6322                 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
6323                                                  GFP_KERNEL);
6324                 if (unlikely(!phba->sli4_hba.xri_ids)) {
6325                         rc = -ENOMEM;
6326                         goto free_xri_bmask;
6327                 }
6328
6329                 for (i = 0; i < count; i++)
6330                         phba->sli4_hba.xri_ids[i] = base + i;
6331
6332                 /* VFIs. */
6333                 count = phba->sli4_hba.max_cfg_param.max_vfi;
6334                 if (count <= 0) {
6335                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6336                                         "3282 Invalid provisioning of "
6337                                         "vfi:%d\n", count);
6338                         rc = -EINVAL;
6339                         goto free_xri_ids;
6340                 }
6341                 base = phba->sli4_hba.max_cfg_param.vfi_base;
6342                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6343                 phba->sli4_hba.vfi_bmask = kcalloc(longs,
6344                                                    sizeof(unsigned long),
6345                                                    GFP_KERNEL);
6346                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6347                         rc = -ENOMEM;
6348                         goto free_xri_ids;
6349                 }
6350                 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
6351                                                  GFP_KERNEL);
6352                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6353                         rc = -ENOMEM;
6354                         goto free_vfi_bmask;
6355                 }
6356
6357                 for (i = 0; i < count; i++)
6358                         phba->sli4_hba.vfi_ids[i] = base + i;
6359
6360                 /*
6361                  * Mark all resources ready.  An HBA reset doesn't need
6362                  * to reset the initialization.
6363                  */
6364                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6365                        LPFC_IDX_RSRC_RDY);
6366                 return 0;
6367         }
6368
6369  free_vfi_bmask:
6370         kfree(phba->sli4_hba.vfi_bmask);
6371         phba->sli4_hba.vfi_bmask = NULL;
6372  free_xri_ids:
6373         kfree(phba->sli4_hba.xri_ids);
6374         phba->sli4_hba.xri_ids = NULL;
6375  free_xri_bmask:
6376         kfree(phba->sli4_hba.xri_bmask);
6377         phba->sli4_hba.xri_bmask = NULL;
6378  free_vpi_ids:
6379         kfree(phba->vpi_ids);
6380         phba->vpi_ids = NULL;
6381  free_vpi_bmask:
6382         kfree(phba->vpi_bmask);
6383         phba->vpi_bmask = NULL;
6384  free_rpi_ids:
6385         kfree(phba->sli4_hba.rpi_ids);
6386         phba->sli4_hba.rpi_ids = NULL;
6387  free_rpi_bmask:
6388         kfree(phba->sli4_hba.rpi_bmask);
6389         phba->sli4_hba.rpi_bmask = NULL;
6390  err_exit:
6391         return rc;
6392 }
6393
6394 /**
6395  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6396  * @phba: Pointer to HBA context object.
6397  *
6398  * This function allocates the number of elements for the specified
6399  * resource type.
6400  **/
6401 int
6402 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6403 {
6404         if (phba->sli4_hba.extents_in_use) {
6405                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6406                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6407                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6408                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6409         } else {
6410                 kfree(phba->vpi_bmask);
6411                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6412                 kfree(phba->vpi_ids);
6413                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6414                 kfree(phba->sli4_hba.xri_bmask);
6415                 kfree(phba->sli4_hba.xri_ids);
6416                 kfree(phba->sli4_hba.vfi_bmask);
6417                 kfree(phba->sli4_hba.vfi_ids);
6418                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6419                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6420         }
6421
6422         return 0;
6423 }
6424
6425 /**
6426  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6427  * @phba: Pointer to HBA context object.
6428  * @type: The resource extent type.
6429  * @extnt_count: buffer to hold port extent count response
6430  * @extnt_size: buffer to hold port extent size response.
6431  *
6432  * This function calls the port to read the host allocated extents
6433  * for a particular type.
6434  **/
6435 int
6436 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6437                                uint16_t *extnt_cnt, uint16_t *extnt_size)
6438 {
6439         bool emb;
6440         int rc = 0;
6441         uint16_t curr_blks = 0;
6442         uint32_t req_len, emb_len;
6443         uint32_t alloc_len, mbox_tmo;
6444         struct list_head *blk_list_head;
6445         struct lpfc_rsrc_blks *rsrc_blk;
6446         LPFC_MBOXQ_t *mbox;
6447         void *virtaddr = NULL;
6448         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6449         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6450         union  lpfc_sli4_cfg_shdr *shdr;
6451
6452         switch (type) {
6453         case LPFC_RSC_TYPE_FCOE_VPI:
6454                 blk_list_head = &phba->lpfc_vpi_blk_list;
6455                 break;
6456         case LPFC_RSC_TYPE_FCOE_XRI:
6457                 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6458                 break;
6459         case LPFC_RSC_TYPE_FCOE_VFI:
6460                 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6461                 break;
6462         case LPFC_RSC_TYPE_FCOE_RPI:
6463                 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6464                 break;
6465         default:
6466                 return -EIO;
6467         }
6468
6469         /* Count the number of extents currently allocatd for this type. */
6470         list_for_each_entry(rsrc_blk, blk_list_head, list) {
6471                 if (curr_blks == 0) {
6472                         /*
6473                          * The GET_ALLOCATED mailbox does not return the size,
6474                          * just the count.  The size should be just the size
6475                          * stored in the current allocated block and all sizes
6476                          * for an extent type are the same so set the return
6477                          * value now.
6478                          */
6479                         *extnt_size = rsrc_blk->rsrc_size;
6480                 }
6481                 curr_blks++;
6482         }
6483
6484         /*
6485          * Calculate the size of an embedded mailbox.  The uint32_t
6486          * accounts for extents-specific word.
6487          */
6488         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6489                 sizeof(uint32_t);
6490
6491         /*
6492          * Presume the allocation and response will fit into an embedded
6493          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6494          */
6495         emb = LPFC_SLI4_MBX_EMBED;
6496         req_len = emb_len;
6497         if (req_len > emb_len) {
6498                 req_len = curr_blks * sizeof(uint16_t) +
6499                         sizeof(union lpfc_sli4_cfg_shdr) +
6500                         sizeof(uint32_t);
6501                 emb = LPFC_SLI4_MBX_NEMBED;
6502         }
6503
6504         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6505         if (!mbox)
6506                 return -ENOMEM;
6507         memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6508
6509         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6510                                      LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6511                                      req_len, emb);
6512         if (alloc_len < req_len) {
6513                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6514                         "2983 Allocated DMA memory size (x%x) is "
6515                         "less than the requested DMA memory "
6516                         "size (x%x)\n", alloc_len, req_len);
6517                 rc = -ENOMEM;
6518                 goto err_exit;
6519         }
6520         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6521         if (unlikely(rc)) {
6522                 rc = -EIO;
6523                 goto err_exit;
6524         }
6525
6526         if (!phba->sli4_hba.intr_enable)
6527                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6528         else {
6529                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6530                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6531         }
6532
6533         if (unlikely(rc)) {
6534                 rc = -EIO;
6535                 goto err_exit;
6536         }
6537
6538         /*
6539          * Figure out where the response is located.  Then get local pointers
6540          * to the response data.  The port does not guarantee to respond to
6541          * all extents counts request so update the local variable with the
6542          * allocated count from the port.
6543          */
6544         if (emb == LPFC_SLI4_MBX_EMBED) {
6545                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6546                 shdr = &rsrc_ext->header.cfg_shdr;
6547                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6548         } else {
6549                 virtaddr = mbox->sge_array->addr[0];
6550                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6551                 shdr = &n_rsrc->cfg_shdr;
6552                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6553         }
6554
6555         if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6556                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6557                         "2984 Failed to read allocated resources "
6558                         "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6559                         type,
6560                         bf_get(lpfc_mbox_hdr_status, &shdr->response),
6561                         bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6562                 rc = -EIO;
6563                 goto err_exit;
6564         }
6565  err_exit:
6566         lpfc_sli4_mbox_cmd_free(phba, mbox);
6567         return rc;
6568 }
6569
6570 /**
6571  * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
6572  * @phba: pointer to lpfc hba data structure.
6573  * @pring: Pointer to driver SLI ring object.
6574  * @sgl_list: linked link of sgl buffers to post
6575  * @cnt: number of linked list buffers
6576  *
6577  * This routine walks the list of buffers that have been allocated and
6578  * repost them to the port by using SGL block post. This is needed after a
6579  * pci_function_reset/warm_start or start. It attempts to construct blocks
6580  * of buffer sgls which contains contiguous xris and uses the non-embedded
6581  * SGL block post mailbox commands to post them to the port. For single
6582  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6583  * mailbox command for posting.
6584  *
6585  * Returns: 0 = success, non-zero failure.
6586  **/
6587 static int
6588 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6589                           struct list_head *sgl_list, int cnt)
6590 {
6591         struct lpfc_sglq *sglq_entry = NULL;
6592         struct lpfc_sglq *sglq_entry_next = NULL;
6593         struct lpfc_sglq *sglq_entry_first = NULL;
6594         int status, total_cnt;
6595         int post_cnt = 0, num_posted = 0, block_cnt = 0;
6596         int last_xritag = NO_XRI;
6597         LIST_HEAD(prep_sgl_list);
6598         LIST_HEAD(blck_sgl_list);
6599         LIST_HEAD(allc_sgl_list);
6600         LIST_HEAD(post_sgl_list);
6601         LIST_HEAD(free_sgl_list);
6602
6603         spin_lock_irq(&phba->hbalock);
6604         spin_lock(&phba->sli4_hba.sgl_list_lock);
6605         list_splice_init(sgl_list, &allc_sgl_list);
6606         spin_unlock(&phba->sli4_hba.sgl_list_lock);
6607         spin_unlock_irq(&phba->hbalock);
6608
6609         total_cnt = cnt;
6610         list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6611                                  &allc_sgl_list, list) {
6612                 list_del_init(&sglq_entry->list);
6613                 block_cnt++;
6614                 if ((last_xritag != NO_XRI) &&
6615                     (sglq_entry->sli4_xritag != last_xritag + 1)) {
6616                         /* a hole in xri block, form a sgl posting block */
6617                         list_splice_init(&prep_sgl_list, &blck_sgl_list);
6618                         post_cnt = block_cnt - 1;
6619                         /* prepare list for next posting block */
6620                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
6621                         block_cnt = 1;
6622                 } else {
6623                         /* prepare list for next posting block */
6624                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
6625                         /* enough sgls for non-embed sgl mbox command */
6626                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6627                                 list_splice_init(&prep_sgl_list,
6628                                                  &blck_sgl_list);
6629                                 post_cnt = block_cnt;
6630                                 block_cnt = 0;
6631                         }
6632                 }
6633                 num_posted++;
6634
6635                 /* keep track of last sgl's xritag */
6636                 last_xritag = sglq_entry->sli4_xritag;
6637
6638                 /* end of repost sgl list condition for buffers */
6639                 if (num_posted == total_cnt) {
6640                         if (post_cnt == 0) {
6641                                 list_splice_init(&prep_sgl_list,
6642                                                  &blck_sgl_list);
6643                                 post_cnt = block_cnt;
6644                         } else if (block_cnt == 1) {
6645                                 status = lpfc_sli4_post_sgl(phba,
6646                                                 sglq_entry->phys, 0,
6647                                                 sglq_entry->sli4_xritag);
6648                                 if (!status) {
6649                                         /* successful, put sgl to posted list */
6650                                         list_add_tail(&sglq_entry->list,
6651                                                       &post_sgl_list);
6652                                 } else {
6653                                         /* Failure, put sgl to free list */
6654                                         lpfc_printf_log(phba, KERN_WARNING,
6655                                                 LOG_SLI,
6656                                                 "3159 Failed to post "
6657                                                 "sgl, xritag:x%x\n",
6658                                                 sglq_entry->sli4_xritag);
6659                                         list_add_tail(&sglq_entry->list,
6660                                                       &free_sgl_list);
6661                                         total_cnt--;
6662                                 }
6663                         }
6664                 }
6665
6666                 /* continue until a nembed page worth of sgls */
6667                 if (post_cnt == 0)
6668                         continue;
6669
6670                 /* post the buffer list sgls as a block */
6671                 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6672                                                  post_cnt);
6673
6674                 if (!status) {
6675                         /* success, put sgl list to posted sgl list */
6676                         list_splice_init(&blck_sgl_list, &post_sgl_list);
6677                 } else {
6678                         /* Failure, put sgl list to free sgl list */
6679                         sglq_entry_first = list_first_entry(&blck_sgl_list,
6680                                                             struct lpfc_sglq,
6681                                                             list);
6682                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6683                                         "3160 Failed to post sgl-list, "
6684                                         "xritag:x%x-x%x\n",
6685                                         sglq_entry_first->sli4_xritag,
6686                                         (sglq_entry_first->sli4_xritag +
6687                                          post_cnt - 1));
6688                         list_splice_init(&blck_sgl_list, &free_sgl_list);
6689                         total_cnt -= post_cnt;
6690                 }
6691
6692                 /* don't reset xirtag due to hole in xri block */
6693                 if (block_cnt == 0)
6694                         last_xritag = NO_XRI;
6695
6696                 /* reset sgl post count for next round of posting */
6697                 post_cnt = 0;
6698         }
6699
6700         /* free the sgls failed to post */
6701         lpfc_free_sgl_list(phba, &free_sgl_list);
6702
6703         /* push sgls posted to the available list */
6704         if (!list_empty(&post_sgl_list)) {
6705                 spin_lock_irq(&phba->hbalock);
6706                 spin_lock(&phba->sli4_hba.sgl_list_lock);
6707                 list_splice_init(&post_sgl_list, sgl_list);
6708                 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6709                 spin_unlock_irq(&phba->hbalock);
6710         } else {
6711                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6712                                 "3161 Failure to post sgl to port.\n");
6713                 return -EIO;
6714         }
6715
6716         /* return the number of XRIs actually posted */
6717         return total_cnt;
6718 }
6719
6720 void
6721 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6722 {
6723         uint32_t len;
6724
6725         len = sizeof(struct lpfc_mbx_set_host_data) -
6726                 sizeof(struct lpfc_sli4_cfg_mhdr);
6727         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6728                          LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6729                          LPFC_SLI4_MBX_EMBED);
6730
6731         mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
6732         mbox->u.mqe.un.set_host_data.param_len =
6733                                         LPFC_HOST_OS_DRIVER_VERSION_SIZE;
6734         snprintf(mbox->u.mqe.un.set_host_data.data,
6735                  LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6736                  "Linux %s v"LPFC_DRIVER_VERSION,
6737                  (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6738 }
6739
6740 int
6741 lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
6742                     struct lpfc_queue *drq, int count, int idx)
6743 {
6744         int rc, i;
6745         struct lpfc_rqe hrqe;
6746         struct lpfc_rqe drqe;
6747         struct lpfc_rqb *rqbp;
6748         unsigned long flags;
6749         struct rqb_dmabuf *rqb_buffer;
6750         LIST_HEAD(rqb_buf_list);
6751
6752         spin_lock_irqsave(&phba->hbalock, flags);
6753         rqbp = hrq->rqbp;
6754         for (i = 0; i < count; i++) {
6755                 /* IF RQ is already full, don't bother */
6756                 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6757                         break;
6758                 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6759                 if (!rqb_buffer)
6760                         break;
6761                 rqb_buffer->hrq = hrq;
6762                 rqb_buffer->drq = drq;
6763                 rqb_buffer->idx = idx;
6764                 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6765         }
6766         while (!list_empty(&rqb_buf_list)) {
6767                 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6768                                  hbuf.list);
6769
6770                 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6771                 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6772                 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6773                 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6774                 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6775                 if (rc < 0) {
6776                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6777                                         "6421 Cannot post to HRQ %d: %x %x %x "
6778                                         "DRQ %x %x\n",
6779                                         hrq->queue_id,
6780                                         hrq->host_index,
6781                                         hrq->hba_index,
6782                                         hrq->entry_count,
6783                                         drq->host_index,
6784                                         drq->hba_index);
6785                         rqbp->rqb_free_buffer(phba, rqb_buffer);
6786                 } else {
6787                         list_add_tail(&rqb_buffer->hbuf.list,
6788                                       &rqbp->rqb_buffer_list);
6789                         rqbp->buffer_count++;
6790                 }
6791         }
6792         spin_unlock_irqrestore(&phba->hbalock, flags);
6793         return 1;
6794 }
6795
6796 /**
6797  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
6798  * @phba: Pointer to HBA context object.
6799  *
6800  * This function is the main SLI4 device initialization PCI function. This
6801  * function is called by the HBA initialization code, HBA reset code and
6802  * HBA error attention handler code. Caller is not required to hold any
6803  * locks.
6804  **/
6805 int
6806 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6807 {
6808         int rc, i, cnt;
6809         LPFC_MBOXQ_t *mboxq;
6810         struct lpfc_mqe *mqe;
6811         uint8_t *vpd;
6812         uint32_t vpd_size;
6813         uint32_t ftr_rsp = 0;
6814         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6815         struct lpfc_vport *vport = phba->pport;
6816         struct lpfc_dmabuf *mp;
6817         struct lpfc_rqb *rqbp;
6818
6819         /* Perform a PCI function reset to start from clean */
6820         rc = lpfc_pci_function_reset(phba);
6821         if (unlikely(rc))
6822                 return -ENODEV;
6823
6824         /* Check the HBA Host Status Register for readyness */
6825         rc = lpfc_sli4_post_status_check(phba);
6826         if (unlikely(rc))
6827                 return -ENODEV;
6828         else {
6829                 spin_lock_irq(&phba->hbalock);
6830                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6831                 spin_unlock_irq(&phba->hbalock);
6832         }
6833
6834         /*
6835          * Allocate a single mailbox container for initializing the
6836          * port.
6837          */
6838         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6839         if (!mboxq)
6840                 return -ENOMEM;
6841
6842         /* Issue READ_REV to collect vpd and FW information. */
6843         vpd_size = SLI4_PAGE_SIZE;
6844         vpd = kzalloc(vpd_size, GFP_KERNEL);
6845         if (!vpd) {
6846                 rc = -ENOMEM;
6847                 goto out_free_mbox;
6848         }
6849
6850         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
6851         if (unlikely(rc)) {
6852                 kfree(vpd);
6853                 goto out_free_mbox;
6854         }
6855
6856         mqe = &mboxq->u.mqe;
6857         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6858         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
6859                 phba->hba_flag |= HBA_FCOE_MODE;
6860                 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6861         } else {
6862                 phba->hba_flag &= ~HBA_FCOE_MODE;
6863         }
6864
6865         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6866                 LPFC_DCBX_CEE_MODE)
6867                 phba->hba_flag |= HBA_FIP_SUPPORT;
6868         else
6869                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6870
6871         phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6872
6873         if (phba->sli_rev != LPFC_SLI_REV4) {
6874                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6875                         "0376 READ_REV Error. SLI Level %d "
6876                         "FCoE enabled %d\n",
6877                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
6878                 rc = -EIO;
6879                 kfree(vpd);
6880                 goto out_free_mbox;
6881         }
6882
6883         /*
6884          * Continue initialization with default values even if driver failed
6885          * to read FCoE param config regions, only read parameters if the
6886          * board is FCoE
6887          */
6888         if (phba->hba_flag & HBA_FCOE_MODE &&
6889             lpfc_sli4_read_fcoe_params(phba))
6890                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6891                         "2570 Failed to read FCoE parameters\n");
6892
6893         /*
6894          * Retrieve sli4 device physical port name, failure of doing it
6895          * is considered as non-fatal.
6896          */
6897         rc = lpfc_sli4_retrieve_pport_name(phba);
6898         if (!rc)
6899                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6900                                 "3080 Successful retrieving SLI4 device "
6901                                 "physical port name: %s.\n", phba->Port);
6902
6903         /*
6904          * Evaluate the read rev and vpd data. Populate the driver
6905          * state with the results. If this routine fails, the failure
6906          * is not fatal as the driver will use generic values.
6907          */
6908         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6909         if (unlikely(!rc)) {
6910                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6911                                 "0377 Error %d parsing vpd. "
6912                                 "Using defaults.\n", rc);
6913                 rc = 0;
6914         }
6915         kfree(vpd);
6916
6917         /* Save information as VPD data */
6918         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6919         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6920
6921         /*
6922          * This is because first G7 ASIC doesn't support the standard
6923          * 0x5a NVME cmd descriptor type/subtype
6924          */
6925         if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6926                         LPFC_SLI_INTF_IF_TYPE_6) &&
6927             (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
6928             (phba->vpd.rev.smRev == 0) &&
6929             (phba->cfg_nvme_embed_cmd == 1))
6930                 phba->cfg_nvme_embed_cmd = 0;
6931
6932         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6933         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6934                                          &mqe->un.read_rev);
6935         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6936                                        &mqe->un.read_rev);
6937         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6938                                             &mqe->un.read_rev);
6939         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6940                                            &mqe->un.read_rev);
6941         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6942         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6943         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6944         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6945         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6946         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6947         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6948                         "(%d):0380 READ_REV Status x%x "
6949                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6950                         mboxq->vport ? mboxq->vport->vpi : 0,
6951                         bf_get(lpfc_mqe_status, mqe),
6952                         phba->vpd.rev.opFwName,
6953                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6954                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
6955
6956         /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3)  */
6957         rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6958         if (phba->pport->cfg_lun_queue_depth > rc) {
6959                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6960                                 "3362 LUN queue depth changed from %d to %d\n",
6961                                 phba->pport->cfg_lun_queue_depth, rc);
6962                 phba->pport->cfg_lun_queue_depth = rc;
6963         }
6964
6965         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6966             LPFC_SLI_INTF_IF_TYPE_0) {
6967                 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6968                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6969                 if (rc == MBX_SUCCESS) {
6970                         phba->hba_flag |= HBA_RECOVERABLE_UE;
6971                         /* Set 1Sec interval to detect UE */
6972                         phba->eratt_poll_interval = 1;
6973                         phba->sli4_hba.ue_to_sr = bf_get(
6974                                         lpfc_mbx_set_feature_UESR,
6975                                         &mboxq->u.mqe.un.set_feature);
6976                         phba->sli4_hba.ue_to_rp = bf_get(
6977                                         lpfc_mbx_set_feature_UERP,
6978                                         &mboxq->u.mqe.un.set_feature);
6979                 }
6980         }
6981
6982         if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6983                 /* Enable MDS Diagnostics only if the SLI Port supports it */
6984                 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6985                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6986                 if (rc != MBX_SUCCESS)
6987                         phba->mds_diags_support = 0;
6988         }
6989
6990         /*
6991          * Discover the port's supported feature set and match it against the
6992          * hosts requests.
6993          */
6994         lpfc_request_features(phba, mboxq);
6995         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6996         if (unlikely(rc)) {
6997                 rc = -EIO;
6998                 goto out_free_mbox;
6999         }
7000
7001         /*
7002          * The port must support FCP initiator mode as this is the
7003          * only mode running in the host.
7004          */
7005         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7006                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7007                                 "0378 No support for fcpi mode.\n");
7008                 ftr_rsp++;
7009         }
7010
7011         /* Performance Hints are ONLY for FCoE */
7012         if (phba->hba_flag & HBA_FCOE_MODE) {
7013                 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7014                         phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7015                 else
7016                         phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7017         }
7018
7019         /*
7020          * If the port cannot support the host's requested features
7021          * then turn off the global config parameters to disable the
7022          * feature in the driver.  This is not a fatal error.
7023          */
7024         if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7025                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7026                         phba->cfg_enable_bg = 0;
7027                         phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
7028                         ftr_rsp++;
7029                 }
7030         }
7031
7032         if (phba->max_vpi && phba->cfg_enable_npiv &&
7033             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7034                 ftr_rsp++;
7035
7036         if (ftr_rsp) {
7037                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7038                                 "0379 Feature Mismatch Data: x%08x %08x "
7039                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7040                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7041                                 phba->cfg_enable_npiv, phba->max_vpi);
7042                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7043                         phba->cfg_enable_bg = 0;
7044                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7045                         phba->cfg_enable_npiv = 0;
7046         }
7047
7048         /* These SLI3 features are assumed in SLI4 */
7049         spin_lock_irq(&phba->hbalock);
7050         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7051         spin_unlock_irq(&phba->hbalock);
7052
7053         /*
7054          * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
7055          * calls depends on these resources to complete port setup.
7056          */
7057         rc = lpfc_sli4_alloc_resource_identifiers(phba);
7058         if (rc) {
7059                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7060                                 "2920 Failed to alloc Resource IDs "
7061                                 "rc = x%x\n", rc);
7062                 goto out_free_mbox;
7063         }
7064
7065         lpfc_set_host_data(phba, mboxq);
7066
7067         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7068         if (rc) {
7069                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7070                                 "2134 Failed to set host os driver version %x",
7071                                 rc);
7072         }
7073
7074         /* Read the port's service parameters. */
7075         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7076         if (rc) {
7077                 phba->link_state = LPFC_HBA_ERROR;
7078                 rc = -ENOMEM;
7079                 goto out_free_mbox;
7080         }
7081
7082         mboxq->vport = vport;
7083         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7084         mp = (struct lpfc_dmabuf *) mboxq->context1;
7085         if (rc == MBX_SUCCESS) {
7086                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7087                 rc = 0;
7088         }
7089
7090         /*
7091          * This memory was allocated by the lpfc_read_sparam routine. Release
7092          * it to the mbuf pool.
7093          */
7094         lpfc_mbuf_free(phba, mp->virt, mp->phys);
7095         kfree(mp);
7096         mboxq->context1 = NULL;
7097         if (unlikely(rc)) {
7098                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7099                                 "0382 READ_SPARAM command failed "
7100                                 "status %d, mbxStatus x%x\n",
7101                                 rc, bf_get(lpfc_mqe_status, mqe));
7102                 phba->link_state = LPFC_HBA_ERROR;
7103                 rc = -EIO;
7104                 goto out_free_mbox;
7105         }
7106
7107         lpfc_update_vport_wwn(vport);
7108
7109         /* Update the fc_host data structures with new wwn. */
7110         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7111         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7112
7113         /* Create all the SLI4 queues */
7114         rc = lpfc_sli4_queue_create(phba);
7115         if (rc) {
7116                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7117                                 "3089 Failed to allocate queues\n");
7118                 rc = -ENODEV;
7119                 goto out_free_mbox;
7120         }
7121         /* Set up all the queues to the device */
7122         rc = lpfc_sli4_queue_setup(phba);
7123         if (unlikely(rc)) {
7124                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7125                                 "0381 Error %d during queue setup.\n ", rc);
7126                 goto out_stop_timers;
7127         }
7128         /* Initialize the driver internal SLI layer lists. */
7129         lpfc_sli4_setup(phba);
7130         lpfc_sli4_queue_init(phba);
7131
7132         /* update host els xri-sgl sizes and mappings */
7133         rc = lpfc_sli4_els_sgl_update(phba);
7134         if (unlikely(rc)) {
7135                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7136                                 "1400 Failed to update xri-sgl size and "
7137                                 "mapping: %d\n", rc);
7138                 goto out_destroy_queue;
7139         }
7140
7141         /* register the els sgl pool to the port */
7142         rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7143                                        phba->sli4_hba.els_xri_cnt);
7144         if (unlikely(rc < 0)) {
7145                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7146                                 "0582 Error %d during els sgl post "
7147                                 "operation\n", rc);
7148                 rc = -ENODEV;
7149                 goto out_destroy_queue;
7150         }
7151         phba->sli4_hba.els_xri_cnt = rc;
7152
7153         if (phba->nvmet_support) {
7154                 /* update host nvmet xri-sgl sizes and mappings */
7155                 rc = lpfc_sli4_nvmet_sgl_update(phba);
7156                 if (unlikely(rc)) {
7157                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7158                                         "6308 Failed to update nvmet-sgl size "
7159                                         "and mapping: %d\n", rc);
7160                         goto out_destroy_queue;
7161                 }
7162
7163                 /* register the nvmet sgl pool to the port */
7164                 rc = lpfc_sli4_repost_sgl_list(
7165                         phba,
7166                         &phba->sli4_hba.lpfc_nvmet_sgl_list,
7167                         phba->sli4_hba.nvmet_xri_cnt);
7168                 if (unlikely(rc < 0)) {
7169                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7170                                         "3117 Error %d during nvmet "
7171                                         "sgl post\n", rc);
7172                         rc = -ENODEV;
7173                         goto out_destroy_queue;
7174                 }
7175                 phba->sli4_hba.nvmet_xri_cnt = rc;
7176
7177                 cnt = phba->cfg_iocb_cnt * 1024;
7178                 /* We need 1 iocbq for every SGL, for IO processing */
7179                 cnt += phba->sli4_hba.nvmet_xri_cnt;
7180         } else {
7181                 /* update host scsi xri-sgl sizes and mappings */
7182                 rc = lpfc_sli4_scsi_sgl_update(phba);
7183                 if (unlikely(rc)) {
7184                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7185                                         "6309 Failed to update scsi-sgl size "
7186                                         "and mapping: %d\n", rc);
7187                         goto out_destroy_queue;
7188                 }
7189
7190                 /* update host nvme xri-sgl sizes and mappings */
7191                 rc = lpfc_sli4_nvme_sgl_update(phba);
7192                 if (unlikely(rc)) {
7193                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7194                                         "6082 Failed to update nvme-sgl size "
7195                                         "and mapping: %d\n", rc);
7196                         goto out_destroy_queue;
7197                 }
7198
7199                 cnt = phba->cfg_iocb_cnt * 1024;
7200         }
7201
7202         if (!phba->sli.iocbq_lookup) {
7203                 /* Initialize and populate the iocb list per host */
7204                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7205                                 "2821 initialize iocb list %d total %d\n",
7206                                 phba->cfg_iocb_cnt, cnt);
7207                 rc = lpfc_init_iocb_list(phba, cnt);
7208                 if (rc) {
7209                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7210                                         "1413 Failed to init iocb list.\n");
7211                         goto out_destroy_queue;
7212                 }
7213         }
7214
7215         if (phba->nvmet_support)
7216                 lpfc_nvmet_create_targetport(phba);
7217
7218         if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
7219                 /* Post initial buffers to all RQs created */
7220                 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7221                         rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7222                         INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7223                         rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7224                         rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
7225                         rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
7226                         rqbp->buffer_count = 0;
7227
7228                         lpfc_post_rq_buffer(
7229                                 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7230                                 phba->sli4_hba.nvmet_mrq_data[i],
7231                                 phba->cfg_nvmet_mrq_post, i);
7232                 }
7233         }
7234
7235         if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7236                 /* register the allocated scsi sgl pool to the port */
7237                 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7238                 if (unlikely(rc)) {
7239                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7240                                         "0383 Error %d during scsi sgl post "
7241                                         "operation\n", rc);
7242                         /* Some Scsi buffers were moved to abort scsi list */
7243                         /* A pci function reset will repost them */
7244                         rc = -ENODEV;
7245                         goto out_destroy_queue;
7246                 }
7247         }
7248
7249         if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7250             (phba->nvmet_support == 0)) {
7251
7252                 /* register the allocated nvme sgl pool to the port */
7253                 rc = lpfc_repost_nvme_sgl_list(phba);
7254                 if (unlikely(rc)) {
7255                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7256                                         "6116 Error %d during nvme sgl post "
7257                                         "operation\n", rc);
7258                         /* Some NVME buffers were moved to abort nvme list */
7259                         /* A pci function reset will repost them */
7260                         rc = -ENODEV;
7261                         goto out_destroy_queue;
7262                 }
7263         }
7264
7265         /* Post the rpi header region to the device. */
7266         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7267         if (unlikely(rc)) {
7268                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7269                                 "0393 Error %d during rpi post operation\n",
7270                                 rc);
7271                 rc = -ENODEV;
7272                 goto out_destroy_queue;
7273         }
7274         lpfc_sli4_node_prep(phba);
7275
7276         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
7277                 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
7278                         /*
7279                          * The FC Port needs to register FCFI (index 0)
7280                          */
7281                         lpfc_reg_fcfi(phba, mboxq);
7282                         mboxq->vport = phba->pport;
7283                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7284                         if (rc != MBX_SUCCESS)
7285                                 goto out_unset_queue;
7286                         rc = 0;
7287                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7288                                                 &mboxq->u.mqe.un.reg_fcfi);
7289                 } else {
7290                         /* We are a NVME Target mode with MRQ > 1 */
7291
7292                         /* First register the FCFI */
7293                         lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7294                         mboxq->vport = phba->pport;
7295                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7296                         if (rc != MBX_SUCCESS)
7297                                 goto out_unset_queue;
7298                         rc = 0;
7299                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7300                                                 &mboxq->u.mqe.un.reg_fcfi_mrq);
7301
7302                         /* Next register the MRQs */
7303                         lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7304                         mboxq->vport = phba->pport;
7305                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7306                         if (rc != MBX_SUCCESS)
7307                                 goto out_unset_queue;
7308                         rc = 0;
7309                 }
7310                 /* Check if the port is configured to be disabled */
7311                 lpfc_sli_read_link_ste(phba);
7312         }
7313
7314         /* Arm the CQs and then EQs on device */
7315         lpfc_sli4_arm_cqeq_intr(phba);
7316
7317         /* Indicate device interrupt mode */
7318         phba->sli4_hba.intr_enable = 1;
7319
7320         /* Allow asynchronous mailbox command to go through */
7321         spin_lock_irq(&phba->hbalock);
7322         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7323         spin_unlock_irq(&phba->hbalock);
7324
7325         /* Post receive buffers to the device */
7326         lpfc_sli4_rb_setup(phba);
7327
7328         /* Reset HBA FCF states after HBA reset */
7329         phba->fcf.fcf_flag = 0;
7330         phba->fcf.current_rec.flag = 0;
7331
7332         /* Start the ELS watchdog timer */
7333         mod_timer(&vport->els_tmofunc,
7334                   jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
7335
7336         /* Start heart beat timer */
7337         mod_timer(&phba->hb_tmofunc,
7338                   jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
7339         phba->hb_outstanding = 0;
7340         phba->last_completion_time = jiffies;
7341
7342         /* Start error attention (ERATT) polling timer */
7343         mod_timer(&phba->eratt_poll,
7344                   jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
7345
7346         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7347         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7348                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7349                 if (!rc) {
7350                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7351                                         "2829 This device supports "
7352                                         "Advanced Error Reporting (AER)\n");
7353                         spin_lock_irq(&phba->hbalock);
7354                         phba->hba_flag |= HBA_AER_ENABLED;
7355                         spin_unlock_irq(&phba->hbalock);
7356                 } else {
7357                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7358                                         "2830 This device does not support "
7359                                         "Advanced Error Reporting (AER)\n");
7360                         phba->cfg_aer_support = 0;
7361                 }
7362                 rc = 0;
7363         }
7364
7365         /*
7366          * The port is ready, set the host's link state to LINK_DOWN
7367          * in preparation for link interrupts.
7368          */
7369         spin_lock_irq(&phba->hbalock);
7370         phba->link_state = LPFC_LINK_DOWN;
7371         spin_unlock_irq(&phba->hbalock);
7372         if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7373             (phba->hba_flag & LINK_DISABLED)) {
7374                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7375                                 "3103 Adapter Link is disabled.\n");
7376                 lpfc_down_link(phba, mboxq);
7377                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7378                 if (rc != MBX_SUCCESS) {
7379                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7380                                         "3104 Adapter failed to issue "
7381                                         "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7382                         goto out_unset_queue;
7383                 }
7384         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
7385                 /* don't perform init_link on SLI4 FC port loopback test */
7386                 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7387                         rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7388                         if (rc)
7389                                 goto out_unset_queue;
7390                 }
7391         }
7392         mempool_free(mboxq, phba->mbox_mem_pool);
7393         return rc;
7394 out_unset_queue:
7395         /* Unset all the queues set up in this routine when error out */
7396         lpfc_sli4_queue_unset(phba);
7397 out_destroy_queue:
7398         lpfc_free_iocb_list(phba);
7399         lpfc_sli4_queue_destroy(phba);
7400 out_stop_timers:
7401         lpfc_stop_hba_timers(phba);
7402 out_free_mbox:
7403         mempool_free(mboxq, phba->mbox_mem_pool);
7404         return rc;
7405 }
7406
7407 /**
7408  * lpfc_mbox_timeout - Timeout call back function for mbox timer
7409  * @ptr: context object - pointer to hba structure.
7410  *
7411  * This is the callback function for mailbox timer. The mailbox
7412  * timer is armed when a new mailbox command is issued and the timer
7413  * is deleted when the mailbox complete. The function is called by
7414  * the kernel timer code when a mailbox does not complete within
7415  * expected time. This function wakes up the worker thread to
7416  * process the mailbox timeout and returns. All the processing is
7417  * done by the worker thread function lpfc_mbox_timeout_handler.
7418  **/
7419 void
7420 lpfc_mbox_timeout(struct timer_list *t)
7421 {
7422         struct lpfc_hba  *phba = from_timer(phba, t, sli.mbox_tmo);
7423         unsigned long iflag;
7424         uint32_t tmo_posted;
7425
7426         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7427         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7428         if (!tmo_posted)
7429                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7430         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7431
7432         if (!tmo_posted)
7433                 lpfc_worker_wake_up(phba);
7434         return;
7435 }
7436
7437 /**
7438  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7439  *                                    are pending
7440  * @phba: Pointer to HBA context object.
7441  *
7442  * This function checks if any mailbox completions are present on the mailbox
7443  * completion queue.
7444  **/
7445 static bool
7446 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7447 {
7448
7449         uint32_t idx;
7450         struct lpfc_queue *mcq;
7451         struct lpfc_mcqe *mcqe;
7452         bool pending_completions = false;
7453         uint8_t qe_valid;
7454
7455         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7456                 return false;
7457
7458         /* Check for completions on mailbox completion queue */
7459
7460         mcq = phba->sli4_hba.mbx_cq;
7461         idx = mcq->hba_index;
7462         qe_valid = mcq->qe_valid;
7463         while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
7464                 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7465                 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7466                     (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7467                         pending_completions = true;
7468                         break;
7469                 }
7470                 idx = (idx + 1) % mcq->entry_count;
7471                 if (mcq->hba_index == idx)
7472                         break;
7473
7474                 /* if the index wrapped around, toggle the valid bit */
7475                 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7476                         qe_valid = (qe_valid) ? 0 : 1;
7477         }
7478         return pending_completions;
7479
7480 }
7481
7482 /**
7483  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7484  *                                            that were missed.
7485  * @phba: Pointer to HBA context object.
7486  *
7487  * For sli4, it is possible to miss an interrupt. As such mbox completions
7488  * maybe missed causing erroneous mailbox timeouts to occur. This function
7489  * checks to see if mbox completions are on the mailbox completion queue
7490  * and will process all the completions associated with the eq for the
7491  * mailbox completion queue.
7492  **/
7493 bool
7494 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7495 {
7496         struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
7497         uint32_t eqidx;
7498         struct lpfc_queue *fpeq = NULL;
7499         struct lpfc_eqe *eqe;
7500         bool mbox_pending;
7501
7502         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7503                 return false;
7504
7505         /* Find the eq associated with the mcq */
7506
7507         if (sli4_hba->hba_eq)
7508                 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
7509                         if (sli4_hba->hba_eq[eqidx]->queue_id ==
7510                             sli4_hba->mbx_cq->assoc_qid) {
7511                                 fpeq = sli4_hba->hba_eq[eqidx];
7512                                 break;
7513                         }
7514         if (!fpeq)
7515                 return false;
7516
7517         /* Turn off interrupts from this EQ */
7518
7519         sli4_hba->sli4_eq_clr_intr(fpeq);
7520
7521         /* Check to see if a mbox completion is pending */
7522
7523         mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7524
7525         /*
7526          * If a mbox completion is pending, process all the events on EQ
7527          * associated with the mbox completion queue (this could include
7528          * mailbox commands, async events, els commands, receive queue data
7529          * and fcp commands)
7530          */
7531
7532         if (mbox_pending)
7533                 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7534                         lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7535                         fpeq->EQ_processed++;
7536                 }
7537
7538         /* Always clear and re-arm the EQ */
7539
7540         sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7541
7542         return mbox_pending;
7543
7544 }
7545
7546 /**
7547  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7548  * @phba: Pointer to HBA context object.
7549  *
7550  * This function is called from worker thread when a mailbox command times out.
7551  * The caller is not required to hold any locks. This function will reset the
7552  * HBA and recover all the pending commands.
7553  **/
7554 void
7555 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7556 {
7557         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
7558         MAILBOX_t *mb = NULL;
7559
7560         struct lpfc_sli *psli = &phba->sli;
7561
7562         /* If the mailbox completed, process the completion and return */
7563         if (lpfc_sli4_process_missed_mbox_completions(phba))
7564                 return;
7565
7566         if (pmbox != NULL)
7567                 mb = &pmbox->u.mb;
7568         /* Check the pmbox pointer first.  There is a race condition
7569          * between the mbox timeout handler getting executed in the
7570          * worklist and the mailbox actually completing. When this
7571          * race condition occurs, the mbox_active will be NULL.
7572          */
7573         spin_lock_irq(&phba->hbalock);
7574         if (pmbox == NULL) {
7575                 lpfc_printf_log(phba, KERN_WARNING,
7576                                 LOG_MBOX | LOG_SLI,
7577                                 "0353 Active Mailbox cleared - mailbox timeout "
7578                                 "exiting\n");
7579                 spin_unlock_irq(&phba->hbalock);
7580                 return;
7581         }
7582
7583         /* Mbox cmd <mbxCommand> timeout */
7584         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7585                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7586                         mb->mbxCommand,
7587                         phba->pport->port_state,
7588                         phba->sli.sli_flag,
7589                         phba->sli.mbox_active);
7590         spin_unlock_irq(&phba->hbalock);
7591
7592         /* Setting state unknown so lpfc_sli_abort_iocb_ring
7593          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
7594          * it to fail all outstanding SCSI IO.
7595          */
7596         spin_lock_irq(&phba->pport->work_port_lock);
7597         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7598         spin_unlock_irq(&phba->pport->work_port_lock);
7599         spin_lock_irq(&phba->hbalock);
7600         phba->link_state = LPFC_LINK_UNKNOWN;
7601         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
7602         spin_unlock_irq(&phba->hbalock);
7603
7604         lpfc_sli_abort_fcp_rings(phba);
7605
7606         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7607                         "0345 Resetting board due to mailbox timeout\n");
7608
7609         /* Reset the HBA device */
7610         lpfc_reset_hba(phba);
7611 }
7612
7613 /**
7614  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7615  * @phba: Pointer to HBA context object.
7616  * @pmbox: Pointer to mailbox object.
7617  * @flag: Flag indicating how the mailbox need to be processed.
7618  *
7619  * This function is called by discovery code and HBA management code
7620  * to submit a mailbox command to firmware with SLI-3 interface spec. This
7621  * function gets the hbalock to protect the data structures.
7622  * The mailbox command can be submitted in polling mode, in which case
7623  * this function will wait in a polling loop for the completion of the
7624  * mailbox.
7625  * If the mailbox is submitted in no_wait mode (not polling) the
7626  * function will submit the command and returns immediately without waiting
7627  * for the mailbox completion. The no_wait is supported only when HBA
7628  * is in SLI2/SLI3 mode - interrupts are enabled.
7629  * The SLI interface allows only one mailbox pending at a time. If the
7630  * mailbox is issued in polling mode and there is already a mailbox
7631  * pending, then the function will return an error. If the mailbox is issued
7632  * in NO_WAIT mode and there is a mailbox pending already, the function
7633  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7634  * The sli layer owns the mailbox object until the completion of mailbox
7635  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7636  * return codes the caller owns the mailbox command after the return of
7637  * the function.
7638  **/
7639 static int
7640 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7641                        uint32_t flag)
7642 {
7643         MAILBOX_t *mbx;
7644         struct lpfc_sli *psli = &phba->sli;
7645         uint32_t status, evtctr;
7646         uint32_t ha_copy, hc_copy;
7647         int i;
7648         unsigned long timeout;
7649         unsigned long drvr_flag = 0;
7650         uint32_t word0, ldata;
7651         void __iomem *to_slim;
7652         int processing_queue = 0;
7653
7654         spin_lock_irqsave(&phba->hbalock, drvr_flag);
7655         if (!pmbox) {
7656                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7657                 /* processing mbox queue from intr_handler */
7658                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7659                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7660                         return MBX_SUCCESS;
7661                 }
7662                 processing_queue = 1;
7663                 pmbox = lpfc_mbox_get(phba);
7664                 if (!pmbox) {
7665                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7666                         return MBX_SUCCESS;
7667                 }
7668         }
7669
7670         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
7671                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
7672                 if(!pmbox->vport) {
7673                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7674                         lpfc_printf_log(phba, KERN_ERR,
7675                                         LOG_MBOX | LOG_VPORT,
7676                                         "1806 Mbox x%x failed. No vport\n",
7677                                         pmbox->u.mb.mbxCommand);
7678                         dump_stack();
7679                         goto out_not_finished;
7680                 }
7681         }
7682
7683         /* If the PCI channel is in offline state, do not post mbox. */
7684         if (unlikely(pci_channel_offline(phba->pcidev))) {
7685                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7686                 goto out_not_finished;
7687         }
7688
7689         /* If HBA has a deferred error attention, fail the iocb. */
7690         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7691                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7692                 goto out_not_finished;
7693         }
7694
7695         psli = &phba->sli;
7696
7697         mbx = &pmbox->u.mb;
7698         status = MBX_SUCCESS;
7699
7700         if (phba->link_state == LPFC_HBA_ERROR) {
7701                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7702
7703                 /* Mbox command <mbxCommand> cannot issue */
7704                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7705                                 "(%d):0311 Mailbox command x%x cannot "
7706                                 "issue Data: x%x x%x\n",
7707                                 pmbox->vport ? pmbox->vport->vpi : 0,
7708                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7709                 goto out_not_finished;
7710         }
7711
7712         if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
7713                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7714                         !(hc_copy & HC_MBINT_ENA)) {
7715                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7716                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7717                                 "(%d):2528 Mailbox command x%x cannot "
7718                                 "issue Data: x%x x%x\n",
7719                                 pmbox->vport ? pmbox->vport->vpi : 0,
7720                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7721                         goto out_not_finished;
7722                 }
7723         }
7724
7725         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7726                 /* Polling for a mbox command when another one is already active
7727                  * is not allowed in SLI. Also, the driver must have established
7728                  * SLI2 mode to queue and process multiple mbox commands.
7729                  */
7730
7731                 if (flag & MBX_POLL) {
7732                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7733
7734                         /* Mbox command <mbxCommand> cannot issue */
7735                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7736                                         "(%d):2529 Mailbox command x%x "
7737                                         "cannot issue Data: x%x x%x\n",
7738                                         pmbox->vport ? pmbox->vport->vpi : 0,
7739                                         pmbox->u.mb.mbxCommand,
7740                                         psli->sli_flag, flag);
7741                         goto out_not_finished;
7742                 }
7743
7744                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
7745                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7746                         /* Mbox command <mbxCommand> cannot issue */
7747                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7748                                         "(%d):2530 Mailbox command x%x "
7749                                         "cannot issue Data: x%x x%x\n",
7750                                         pmbox->vport ? pmbox->vport->vpi : 0,
7751                                         pmbox->u.mb.mbxCommand,
7752                                         psli->sli_flag, flag);
7753                         goto out_not_finished;
7754                 }
7755
7756                 /* Another mailbox command is still being processed, queue this
7757                  * command to be processed later.
7758                  */
7759                 lpfc_mbox_put(phba, pmbox);
7760
7761                 /* Mbox cmd issue - BUSY */
7762                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7763                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
7764                                 "x%x x%x x%x x%x\n",
7765                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
7766                                 mbx->mbxCommand,
7767                                 phba->pport ? phba->pport->port_state : 0xff,
7768                                 psli->sli_flag, flag);
7769
7770                 psli->slistat.mbox_busy++;
7771                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7772
7773                 if (pmbox->vport) {
7774                         lpfc_debugfs_disc_trc(pmbox->vport,
7775                                 LPFC_DISC_TRC_MBOX_VPORT,
7776                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
7777                                 (uint32_t)mbx->mbxCommand,
7778                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7779                 }
7780                 else {
7781                         lpfc_debugfs_disc_trc(phba->pport,
7782                                 LPFC_DISC_TRC_MBOX,
7783                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
7784                                 (uint32_t)mbx->mbxCommand,
7785                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7786                 }
7787
7788                 return MBX_BUSY;
7789         }
7790
7791         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7792
7793         /* If we are not polling, we MUST be in SLI2 mode */
7794         if (flag != MBX_POLL) {
7795                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
7796                     (mbx->mbxCommand != MBX_KILL_BOARD)) {
7797                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7798                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7799                         /* Mbox command <mbxCommand> cannot issue */
7800                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7801                                         "(%d):2531 Mailbox command x%x "
7802                                         "cannot issue Data: x%x x%x\n",
7803                                         pmbox->vport ? pmbox->vport->vpi : 0,
7804                                         pmbox->u.mb.mbxCommand,
7805                                         psli->sli_flag, flag);
7806                         goto out_not_finished;
7807                 }
7808                 /* timeout active mbox command */
7809                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7810                                            1000);
7811                 mod_timer(&psli->mbox_tmo, jiffies + timeout);
7812         }
7813
7814         /* Mailbox cmd <cmd> issue */
7815         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7816                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
7817                         "x%x\n",
7818                         pmbox->vport ? pmbox->vport->vpi : 0,
7819                         mbx->mbxCommand,
7820                         phba->pport ? phba->pport->port_state : 0xff,
7821                         psli->sli_flag, flag);
7822
7823         if (mbx->mbxCommand != MBX_HEARTBEAT) {
7824                 if (pmbox->vport) {
7825                         lpfc_debugfs_disc_trc(pmbox->vport,
7826                                 LPFC_DISC_TRC_MBOX_VPORT,
7827                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7828                                 (uint32_t)mbx->mbxCommand,
7829                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7830                 }
7831                 else {
7832                         lpfc_debugfs_disc_trc(phba->pport,
7833                                 LPFC_DISC_TRC_MBOX,
7834                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
7835                                 (uint32_t)mbx->mbxCommand,
7836                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7837                 }
7838         }
7839
7840         psli->slistat.mbox_cmd++;
7841         evtctr = psli->slistat.mbox_event;
7842
7843         /* next set own bit for the adapter and copy over command word */
7844         mbx->mbxOwner = OWN_CHIP;
7845
7846         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7847                 /* Populate mbox extension offset word. */
7848                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
7849                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7850                                 = (uint8_t *)phba->mbox_ext
7851                                   - (uint8_t *)phba->mbox;
7852                 }
7853
7854                 /* Copy the mailbox extension data */
7855                 if (pmbox->in_ext_byte_len && pmbox->context2) {
7856                         lpfc_sli_pcimem_bcopy(pmbox->context2,
7857                                 (uint8_t *)phba->mbox_ext,
7858                                 pmbox->in_ext_byte_len);
7859                 }
7860                 /* Copy command data to host SLIM area */
7861                 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
7862         } else {
7863                 /* Populate mbox extension offset word. */
7864                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
7865                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7866                                 = MAILBOX_HBA_EXT_OFFSET;
7867
7868                 /* Copy the mailbox extension data */
7869                 if (pmbox->in_ext_byte_len && pmbox->context2)
7870                         lpfc_memcpy_to_slim(phba->MBslimaddr +
7871                                 MAILBOX_HBA_EXT_OFFSET,
7872                                 pmbox->context2, pmbox->in_ext_byte_len);
7873
7874                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
7875                         /* copy command data into host mbox for cmpl */
7876                         lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7877                                               MAILBOX_CMD_SIZE);
7878
7879                 /* First copy mbox command data to HBA SLIM, skip past first
7880                    word */
7881                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
7882                 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
7883                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
7884
7885                 /* Next copy over first word, with mbxOwner set */
7886                 ldata = *((uint32_t *)mbx);
7887                 to_slim = phba->MBslimaddr;
7888                 writel(ldata, to_slim);
7889                 readl(to_slim); /* flush */
7890
7891                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
7892                         /* switch over to host mailbox */
7893                         psli->sli_flag |= LPFC_SLI_ACTIVE;
7894         }
7895
7896         wmb();
7897
7898         switch (flag) {
7899         case MBX_NOWAIT:
7900                 /* Set up reference to mailbox command */
7901                 psli->mbox_active = pmbox;
7902                 /* Interrupt board to do it */
7903                 writel(CA_MBATT, phba->CAregaddr);
7904                 readl(phba->CAregaddr); /* flush */
7905                 /* Don't wait for it to finish, just return */
7906                 break;
7907
7908         case MBX_POLL:
7909                 /* Set up null reference to mailbox command */
7910                 psli->mbox_active = NULL;
7911                 /* Interrupt board to do it */
7912                 writel(CA_MBATT, phba->CAregaddr);
7913                 readl(phba->CAregaddr); /* flush */
7914
7915                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7916                         /* First read mbox status word */
7917                         word0 = *((uint32_t *)phba->mbox);
7918                         word0 = le32_to_cpu(word0);
7919                 } else {
7920                         /* First read mbox status word */
7921                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
7922                                 spin_unlock_irqrestore(&phba->hbalock,
7923                                                        drvr_flag);
7924                                 goto out_not_finished;
7925                         }
7926                 }
7927
7928                 /* Read the HBA Host Attention Register */
7929                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7930                         spin_unlock_irqrestore(&phba->hbalock,
7931                                                        drvr_flag);
7932                         goto out_not_finished;
7933                 }
7934                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7935                                                         1000) + jiffies;
7936                 i = 0;
7937                 /* Wait for command to complete */
7938                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7939                        (!(ha_copy & HA_MBATT) &&
7940                         (phba->link_state > LPFC_WARM_START))) {
7941                         if (time_after(jiffies, timeout)) {
7942                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7943                                 spin_unlock_irqrestore(&phba->hbalock,
7944                                                        drvr_flag);
7945                                 goto out_not_finished;
7946                         }
7947
7948                         /* Check if we took a mbox interrupt while we were
7949                            polling */
7950                         if (((word0 & OWN_CHIP) != OWN_CHIP)
7951                             && (evtctr != psli->slistat.mbox_event))
7952                                 break;
7953
7954                         if (i++ > 10) {
7955                                 spin_unlock_irqrestore(&phba->hbalock,
7956                                                        drvr_flag);
7957                                 msleep(1);
7958                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7959                         }
7960
7961                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7962                                 /* First copy command data */
7963                                 word0 = *((uint32_t *)phba->mbox);
7964                                 word0 = le32_to_cpu(word0);
7965                                 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7966                                         MAILBOX_t *slimmb;
7967                                         uint32_t slimword0;
7968                                         /* Check real SLIM for any errors */
7969                                         slimword0 = readl(phba->MBslimaddr);
7970                                         slimmb = (MAILBOX_t *) & slimword0;
7971                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7972                                             && slimmb->mbxStatus) {
7973                                                 psli->sli_flag &=
7974                                                     ~LPFC_SLI_ACTIVE;
7975                                                 word0 = slimword0;
7976                                         }
7977                                 }
7978                         } else {
7979                                 /* First copy command data */
7980                                 word0 = readl(phba->MBslimaddr);
7981                         }
7982                         /* Read the HBA Host Attention Register */
7983                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7984                                 spin_unlock_irqrestore(&phba->hbalock,
7985                                                        drvr_flag);
7986                                 goto out_not_finished;
7987                         }
7988                 }
7989
7990                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7991                         /* copy results back to user */
7992                         lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7993                                                 MAILBOX_CMD_SIZE);
7994                         /* Copy the mailbox extension data */
7995                         if (pmbox->out_ext_byte_len && pmbox->context2) {
7996                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7997                                                       pmbox->context2,
7998                                                       pmbox->out_ext_byte_len);
7999                         }
8000                 } else {
8001                         /* First copy command data */
8002                         lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
8003                                                 MAILBOX_CMD_SIZE);
8004                         /* Copy the mailbox extension data */
8005                         if (pmbox->out_ext_byte_len && pmbox->context2) {
8006                                 lpfc_memcpy_from_slim(pmbox->context2,
8007                                         phba->MBslimaddr +
8008                                         MAILBOX_HBA_EXT_OFFSET,
8009                                         pmbox->out_ext_byte_len);
8010                         }
8011                 }
8012
8013                 writel(HA_MBATT, phba->HAregaddr);
8014                 readl(phba->HAregaddr); /* flush */
8015
8016                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8017                 status = mbx->mbxStatus;
8018         }
8019
8020         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8021         return status;
8022
8023 out_not_finished:
8024         if (processing_queue) {
8025                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
8026                 lpfc_mbox_cmpl_put(phba, pmbox);
8027         }
8028         return MBX_NOT_FINISHED;
8029 }
8030
8031 /**
8032  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8033  * @phba: Pointer to HBA context object.
8034  *
8035  * The function blocks the posting of SLI4 asynchronous mailbox commands from
8036  * the driver internal pending mailbox queue. It will then try to wait out the
8037  * possible outstanding mailbox command before return.
8038  *
8039  * Returns:
8040  *      0 - the outstanding mailbox command completed; otherwise, the wait for
8041  *      the outstanding mailbox command timed out.
8042  **/
8043 static int
8044 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8045 {
8046         struct lpfc_sli *psli = &phba->sli;
8047         int rc = 0;
8048         unsigned long timeout = 0;
8049
8050         /* Mark the asynchronous mailbox command posting as blocked */
8051         spin_lock_irq(&phba->hbalock);
8052         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8053         /* Determine how long we might wait for the active mailbox
8054          * command to be gracefully completed by firmware.
8055          */
8056         if (phba->sli.mbox_active)
8057                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8058                                                 phba->sli.mbox_active) *
8059                                                 1000) + jiffies;
8060         spin_unlock_irq(&phba->hbalock);
8061
8062         /* Make sure the mailbox is really active */
8063         if (timeout)
8064                 lpfc_sli4_process_missed_mbox_completions(phba);
8065
8066         /* Wait for the outstnading mailbox command to complete */
8067         while (phba->sli.mbox_active) {
8068                 /* Check active mailbox complete status every 2ms */
8069                 msleep(2);
8070                 if (time_after(jiffies, timeout)) {
8071                         /* Timeout, marked the outstanding cmd not complete */
8072                         rc = 1;
8073                         break;
8074                 }
8075         }
8076
8077         /* Can not cleanly block async mailbox command, fails it */
8078         if (rc) {
8079                 spin_lock_irq(&phba->hbalock);
8080                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8081                 spin_unlock_irq(&phba->hbalock);
8082         }
8083         return rc;
8084 }
8085
8086 /**
8087  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8088  * @phba: Pointer to HBA context object.
8089  *
8090  * The function unblocks and resume posting of SLI4 asynchronous mailbox
8091  * commands from the driver internal pending mailbox queue. It makes sure
8092  * that there is no outstanding mailbox command before resuming posting
8093  * asynchronous mailbox commands. If, for any reason, there is outstanding
8094  * mailbox command, it will try to wait it out before resuming asynchronous
8095  * mailbox command posting.
8096  **/
8097 static void
8098 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8099 {
8100         struct lpfc_sli *psli = &phba->sli;
8101
8102         spin_lock_irq(&phba->hbalock);
8103         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8104                 /* Asynchronous mailbox posting is not blocked, do nothing */
8105                 spin_unlock_irq(&phba->hbalock);
8106                 return;
8107         }
8108
8109         /* Outstanding synchronous mailbox command is guaranteed to be done,
8110          * successful or timeout, after timing-out the outstanding mailbox
8111          * command shall always be removed, so just unblock posting async
8112          * mailbox command and resume
8113          */
8114         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8115         spin_unlock_irq(&phba->hbalock);
8116
8117         /* wake up worker thread to post asynchronlous mailbox command */
8118         lpfc_worker_wake_up(phba);
8119 }
8120
8121 /**
8122  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8123  * @phba: Pointer to HBA context object.
8124  * @mboxq: Pointer to mailbox object.
8125  *
8126  * The function waits for the bootstrap mailbox register ready bit from
8127  * port for twice the regular mailbox command timeout value.
8128  *
8129  *      0 - no timeout on waiting for bootstrap mailbox register ready.
8130  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8131  **/
8132 static int
8133 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8134 {
8135         uint32_t db_ready;
8136         unsigned long timeout;
8137         struct lpfc_register bmbx_reg;
8138
8139         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8140                                    * 1000) + jiffies;
8141
8142         do {
8143                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8144                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8145                 if (!db_ready)
8146                         msleep(2);
8147
8148                 if (time_after(jiffies, timeout))
8149                         return MBXERR_ERROR;
8150         } while (!db_ready);
8151
8152         return 0;
8153 }
8154
8155 /**
8156  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8157  * @phba: Pointer to HBA context object.
8158  * @mboxq: Pointer to mailbox object.
8159  *
8160  * The function posts a mailbox to the port.  The mailbox is expected
8161  * to be comletely filled in and ready for the port to operate on it.
8162  * This routine executes a synchronous completion operation on the
8163  * mailbox by polling for its completion.
8164  *
8165  * The caller must not be holding any locks when calling this routine.
8166  *
8167  * Returns:
8168  *      MBX_SUCCESS - mailbox posted successfully
8169  *      Any of the MBX error values.
8170  **/
8171 static int
8172 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8173 {
8174         int rc = MBX_SUCCESS;
8175         unsigned long iflag;
8176         uint32_t mcqe_status;
8177         uint32_t mbx_cmnd;
8178         struct lpfc_sli *psli = &phba->sli;
8179         struct lpfc_mqe *mb = &mboxq->u.mqe;
8180         struct lpfc_bmbx_create *mbox_rgn;
8181         struct dma_address *dma_address;
8182
8183         /*
8184          * Only one mailbox can be active to the bootstrap mailbox region
8185          * at a time and there is no queueing provided.
8186          */
8187         spin_lock_irqsave(&phba->hbalock, iflag);
8188         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8189                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8190                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8191                                 "(%d):2532 Mailbox command x%x (x%x/x%x) "
8192                                 "cannot issue Data: x%x x%x\n",
8193                                 mboxq->vport ? mboxq->vport->vpi : 0,
8194                                 mboxq->u.mb.mbxCommand,
8195                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8196                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8197                                 psli->sli_flag, MBX_POLL);
8198                 return MBXERR_ERROR;
8199         }
8200         /* The server grabs the token and owns it until release */
8201         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8202         phba->sli.mbox_active = mboxq;
8203         spin_unlock_irqrestore(&phba->hbalock, iflag);
8204
8205         /* wait for bootstrap mbox register for readyness */
8206         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8207         if (rc)
8208                 goto exit;
8209
8210         /*
8211          * Initialize the bootstrap memory region to avoid stale data areas
8212          * in the mailbox post.  Then copy the caller's mailbox contents to
8213          * the bmbx mailbox region.
8214          */
8215         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8216         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
8217         lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8218                                sizeof(struct lpfc_mqe));
8219
8220         /* Post the high mailbox dma address to the port and wait for ready. */
8221         dma_address = &phba->sli4_hba.bmbx.dma_address;
8222         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8223
8224         /* wait for bootstrap mbox register for hi-address write done */
8225         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8226         if (rc)
8227                 goto exit;
8228
8229         /* Post the low mailbox dma address to the port. */
8230         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
8231
8232         /* wait for bootstrap mbox register for low address write done */
8233         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8234         if (rc)
8235                 goto exit;
8236
8237         /*
8238          * Read the CQ to ensure the mailbox has completed.
8239          * If so, update the mailbox status so that the upper layers
8240          * can complete the request normally.
8241          */
8242         lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8243                                sizeof(struct lpfc_mqe));
8244         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8245         lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8246                                sizeof(struct lpfc_mcqe));
8247         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
8248         /*
8249          * When the CQE status indicates a failure and the mailbox status
8250          * indicates success then copy the CQE status into the mailbox status
8251          * (and prefix it with x4000).
8252          */
8253         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
8254                 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8255                         bf_set(lpfc_mqe_status, mb,
8256                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
8257                 rc = MBXERR_ERROR;
8258         } else
8259                 lpfc_sli4_swap_str(phba, mboxq);
8260
8261         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8262                         "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
8263                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8264                         " x%x x%x CQ: x%x x%x x%x x%x\n",
8265                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8266                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8267                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8268                         bf_get(lpfc_mqe_status, mb),
8269                         mb->un.mb_words[0], mb->un.mb_words[1],
8270                         mb->un.mb_words[2], mb->un.mb_words[3],
8271                         mb->un.mb_words[4], mb->un.mb_words[5],
8272                         mb->un.mb_words[6], mb->un.mb_words[7],
8273                         mb->un.mb_words[8], mb->un.mb_words[9],
8274                         mb->un.mb_words[10], mb->un.mb_words[11],
8275                         mb->un.mb_words[12], mboxq->mcqe.word0,
8276                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
8277                         mboxq->mcqe.trailer);
8278 exit:
8279         /* We are holding the token, no needed for lock when release */
8280         spin_lock_irqsave(&phba->hbalock, iflag);
8281         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8282         phba->sli.mbox_active = NULL;
8283         spin_unlock_irqrestore(&phba->hbalock, iflag);
8284         return rc;
8285 }
8286
8287 /**
8288  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8289  * @phba: Pointer to HBA context object.
8290  * @pmbox: Pointer to mailbox object.
8291  * @flag: Flag indicating how the mailbox need to be processed.
8292  *
8293  * This function is called by discovery code and HBA management code to submit
8294  * a mailbox command to firmware with SLI-4 interface spec.
8295  *
8296  * Return codes the caller owns the mailbox command after the return of the
8297  * function.
8298  **/
8299 static int
8300 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8301                        uint32_t flag)
8302 {
8303         struct lpfc_sli *psli = &phba->sli;
8304         unsigned long iflags;
8305         int rc;
8306
8307         /* dump from issue mailbox command if setup */
8308         lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8309
8310         rc = lpfc_mbox_dev_check(phba);
8311         if (unlikely(rc)) {
8312                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8313                                 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8314                                 "cannot issue Data: x%x x%x\n",
8315                                 mboxq->vport ? mboxq->vport->vpi : 0,
8316                                 mboxq->u.mb.mbxCommand,
8317                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8318                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8319                                 psli->sli_flag, flag);
8320                 goto out_not_finished;
8321         }
8322
8323         /* Detect polling mode and jump to a handler */
8324         if (!phba->sli4_hba.intr_enable) {
8325                 if (flag == MBX_POLL)
8326                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8327                 else
8328                         rc = -EIO;
8329                 if (rc != MBX_SUCCESS)
8330                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8331                                         "(%d):2541 Mailbox command x%x "
8332                                         "(x%x/x%x) failure: "
8333                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
8334                                         "Data: x%x x%x\n,",
8335                                         mboxq->vport ? mboxq->vport->vpi : 0,
8336                                         mboxq->u.mb.mbxCommand,
8337                                         lpfc_sli_config_mbox_subsys_get(phba,
8338                                                                         mboxq),
8339                                         lpfc_sli_config_mbox_opcode_get(phba,
8340                                                                         mboxq),
8341                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8342                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8343                                         bf_get(lpfc_mcqe_ext_status,
8344                                                &mboxq->mcqe),
8345                                         psli->sli_flag, flag);
8346                 return rc;
8347         } else if (flag == MBX_POLL) {
8348                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8349                                 "(%d):2542 Try to issue mailbox command "
8350                                 "x%x (x%x/x%x) synchronously ahead of async "
8351                                 "mailbox command queue: x%x x%x\n",
8352                                 mboxq->vport ? mboxq->vport->vpi : 0,
8353                                 mboxq->u.mb.mbxCommand,
8354                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8355                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8356                                 psli->sli_flag, flag);
8357                 /* Try to block the asynchronous mailbox posting */
8358                 rc = lpfc_sli4_async_mbox_block(phba);
8359                 if (!rc) {
8360                         /* Successfully blocked, now issue sync mbox cmd */
8361                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8362                         if (rc != MBX_SUCCESS)
8363                                 lpfc_printf_log(phba, KERN_WARNING,
8364                                         LOG_MBOX | LOG_SLI,
8365                                         "(%d):2597 Sync Mailbox command "
8366                                         "x%x (x%x/x%x) failure: "
8367                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
8368                                         "Data: x%x x%x\n,",
8369                                         mboxq->vport ? mboxq->vport->vpi : 0,
8370                                         mboxq->u.mb.mbxCommand,
8371                                         lpfc_sli_config_mbox_subsys_get(phba,
8372                                                                         mboxq),
8373                                         lpfc_sli_config_mbox_opcode_get(phba,
8374                                                                         mboxq),
8375                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8376                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8377                                         bf_get(lpfc_mcqe_ext_status,
8378                                                &mboxq->mcqe),
8379                                         psli->sli_flag, flag);
8380                         /* Unblock the async mailbox posting afterward */
8381                         lpfc_sli4_async_mbox_unblock(phba);
8382                 }
8383                 return rc;
8384         }
8385
8386         /* Now, interrupt mode asynchrous mailbox command */
8387         rc = lpfc_mbox_cmd_check(phba, mboxq);
8388         if (rc) {
8389                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8390                                 "(%d):2543 Mailbox command x%x (x%x/x%x) "
8391                                 "cannot issue Data: x%x x%x\n",
8392                                 mboxq->vport ? mboxq->vport->vpi : 0,
8393                                 mboxq->u.mb.mbxCommand,
8394                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8395                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8396                                 psli->sli_flag, flag);
8397                 goto out_not_finished;
8398         }
8399
8400         /* Put the mailbox command to the driver internal FIFO */
8401         psli->slistat.mbox_busy++;
8402         spin_lock_irqsave(&phba->hbalock, iflags);
8403         lpfc_mbox_put(phba, mboxq);
8404         spin_unlock_irqrestore(&phba->hbalock, iflags);
8405         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8406                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
8407                         "x%x (x%x/x%x) x%x x%x x%x\n",
8408                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8409                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8410                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8411                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8412                         phba->pport->port_state,
8413                         psli->sli_flag, MBX_NOWAIT);
8414         /* Wake up worker thread to transport mailbox command from head */
8415         lpfc_worker_wake_up(phba);
8416
8417         return MBX_BUSY;
8418
8419 out_not_finished:
8420         return MBX_NOT_FINISHED;
8421 }
8422
8423 /**
8424  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8425  * @phba: Pointer to HBA context object.
8426  *
8427  * This function is called by worker thread to send a mailbox command to
8428  * SLI4 HBA firmware.
8429  *
8430  **/
8431 int
8432 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8433 {
8434         struct lpfc_sli *psli = &phba->sli;
8435         LPFC_MBOXQ_t *mboxq;
8436         int rc = MBX_SUCCESS;
8437         unsigned long iflags;
8438         struct lpfc_mqe *mqe;
8439         uint32_t mbx_cmnd;
8440
8441         /* Check interrupt mode before post async mailbox command */
8442         if (unlikely(!phba->sli4_hba.intr_enable))
8443                 return MBX_NOT_FINISHED;
8444
8445         /* Check for mailbox command service token */
8446         spin_lock_irqsave(&phba->hbalock, iflags);
8447         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8448                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8449                 return MBX_NOT_FINISHED;
8450         }
8451         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8452                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8453                 return MBX_NOT_FINISHED;
8454         }
8455         if (unlikely(phba->sli.mbox_active)) {
8456                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8457                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8458                                 "0384 There is pending active mailbox cmd\n");
8459                 return MBX_NOT_FINISHED;
8460         }
8461         /* Take the mailbox command service token */
8462         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8463
8464         /* Get the next mailbox command from head of queue */
8465         mboxq = lpfc_mbox_get(phba);
8466
8467         /* If no more mailbox command waiting for post, we're done */
8468         if (!mboxq) {
8469                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8470                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8471                 return MBX_SUCCESS;
8472         }
8473         phba->sli.mbox_active = mboxq;
8474         spin_unlock_irqrestore(&phba->hbalock, iflags);
8475
8476         /* Check device readiness for posting mailbox command */
8477         rc = lpfc_mbox_dev_check(phba);
8478         if (unlikely(rc))
8479                 /* Driver clean routine will clean up pending mailbox */
8480                 goto out_not_finished;
8481
8482         /* Prepare the mbox command to be posted */
8483         mqe = &mboxq->u.mqe;
8484         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8485
8486         /* Start timer for the mbox_tmo and log some mailbox post messages */
8487         mod_timer(&psli->mbox_tmo, (jiffies +
8488                   msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
8489
8490         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8491                         "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
8492                         "x%x x%x\n",
8493                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8494                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8495                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8496                         phba->pport->port_state, psli->sli_flag);
8497
8498         if (mbx_cmnd != MBX_HEARTBEAT) {
8499                 if (mboxq->vport) {
8500                         lpfc_debugfs_disc_trc(mboxq->vport,
8501                                 LPFC_DISC_TRC_MBOX_VPORT,
8502                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8503                                 mbx_cmnd, mqe->un.mb_words[0],
8504                                 mqe->un.mb_words[1]);
8505                 } else {
8506                         lpfc_debugfs_disc_trc(phba->pport,
8507                                 LPFC_DISC_TRC_MBOX,
8508                                 "MBOX Send: cmd:x%x mb:x%x x%x",
8509                                 mbx_cmnd, mqe->un.mb_words[0],
8510                                 mqe->un.mb_words[1]);
8511                 }
8512         }
8513         psli->slistat.mbox_cmd++;
8514
8515         /* Post the mailbox command to the port */
8516         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8517         if (rc != MBX_SUCCESS) {
8518                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8519                                 "(%d):2533 Mailbox command x%x (x%x/x%x) "
8520                                 "cannot issue Data: x%x x%x\n",
8521                                 mboxq->vport ? mboxq->vport->vpi : 0,
8522                                 mboxq->u.mb.mbxCommand,
8523                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8524                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8525                                 psli->sli_flag, MBX_NOWAIT);
8526                 goto out_not_finished;
8527         }
8528
8529         return rc;
8530
8531 out_not_finished:
8532         spin_lock_irqsave(&phba->hbalock, iflags);
8533         if (phba->sli.mbox_active) {
8534                 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8535                 __lpfc_mbox_cmpl_put(phba, mboxq);
8536                 /* Release the token */
8537                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8538                 phba->sli.mbox_active = NULL;
8539         }
8540         spin_unlock_irqrestore(&phba->hbalock, iflags);
8541
8542         return MBX_NOT_FINISHED;
8543 }
8544
8545 /**
8546  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8547  * @phba: Pointer to HBA context object.
8548  * @pmbox: Pointer to mailbox object.
8549  * @flag: Flag indicating how the mailbox need to be processed.
8550  *
8551  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8552  * the API jump table function pointer from the lpfc_hba struct.
8553  *
8554  * Return codes the caller owns the mailbox command after the return of the
8555  * function.
8556  **/
8557 int
8558 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8559 {
8560         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8561 }
8562
8563 /**
8564  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
8565  * @phba: The hba struct for which this call is being executed.
8566  * @dev_grp: The HBA PCI-Device group number.
8567  *
8568  * This routine sets up the mbox interface API function jump table in @phba
8569  * struct.
8570  * Returns: 0 - success, -ENODEV - failure.
8571  **/
8572 int
8573 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8574 {
8575
8576         switch (dev_grp) {
8577         case LPFC_PCI_DEV_LP:
8578                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8579                 phba->lpfc_sli_handle_slow_ring_event =
8580                                 lpfc_sli_handle_slow_ring_event_s3;
8581                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8582                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8583                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8584                 break;
8585         case LPFC_PCI_DEV_OC:
8586                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8587                 phba->lpfc_sli_handle_slow_ring_event =
8588                                 lpfc_sli_handle_slow_ring_event_s4;
8589                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8590                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8591                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8592                 break;
8593         default:
8594                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8595                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
8596                                 dev_grp);
8597                 return -ENODEV;
8598                 break;
8599         }
8600         return 0;
8601 }
8602
8603 /**
8604  * __lpfc_sli_ringtx_put - Add an iocb to the txq
8605  * @phba: Pointer to HBA context object.
8606  * @pring: Pointer to driver SLI ring object.
8607  * @piocb: Pointer to address of newly added command iocb.
8608  *
8609  * This function is called with hbalock held to add a command
8610  * iocb to the txq when SLI layer cannot submit the command iocb
8611  * to the ring.
8612  **/
8613 void
8614 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8615                     struct lpfc_iocbq *piocb)
8616 {
8617         lockdep_assert_held(&phba->hbalock);
8618         /* Insert the caller's iocb in the txq tail for later processing. */
8619         list_add_tail(&piocb->list, &pring->txq);
8620 }
8621
8622 /**
8623  * lpfc_sli_next_iocb - Get the next iocb in the txq
8624  * @phba: Pointer to HBA context object.
8625  * @pring: Pointer to driver SLI ring object.
8626  * @piocb: Pointer to address of newly added command iocb.
8627  *
8628  * This function is called with hbalock held before a new
8629  * iocb is submitted to the firmware. This function checks
8630  * txq to flush the iocbs in txq to Firmware before
8631  * submitting new iocbs to the Firmware.
8632  * If there are iocbs in the txq which need to be submitted
8633  * to firmware, lpfc_sli_next_iocb returns the first element
8634  * of the txq after dequeuing it from txq.
8635  * If there is no iocb in the txq then the function will return
8636  * *piocb and *piocb is set to NULL. Caller needs to check
8637  * *piocb to find if there are more commands in the txq.
8638  **/
8639 static struct lpfc_iocbq *
8640 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8641                    struct lpfc_iocbq **piocb)
8642 {
8643         struct lpfc_iocbq * nextiocb;
8644
8645         lockdep_assert_held(&phba->hbalock);
8646
8647         nextiocb = lpfc_sli_ringtx_get(phba, pring);
8648         if (!nextiocb) {
8649                 nextiocb = *piocb;
8650                 *piocb = NULL;
8651         }
8652
8653         return nextiocb;
8654 }
8655
8656 /**
8657  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
8658  * @phba: Pointer to HBA context object.
8659  * @ring_number: SLI ring number to issue iocb on.
8660  * @piocb: Pointer to command iocb.
8661  * @flag: Flag indicating if this command can be put into txq.
8662  *
8663  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8664  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8665  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8666  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8667  * this function allows only iocbs for posting buffers. This function finds
8668  * next available slot in the command ring and posts the command to the
8669  * available slot and writes the port attention register to request HBA start
8670  * processing new iocb. If there is no slot available in the ring and
8671  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8672  * the function returns IOCB_BUSY.
8673  *
8674  * This function is called with hbalock held. The function will return success
8675  * after it successfully submit the iocb to firmware or after adding to the
8676  * txq.
8677  **/
8678 static int
8679 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
8680                     struct lpfc_iocbq *piocb, uint32_t flag)
8681 {
8682         struct lpfc_iocbq *nextiocb;
8683         IOCB_t *iocb;
8684         struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
8685
8686         lockdep_assert_held(&phba->hbalock);
8687
8688         if (piocb->iocb_cmpl && (!piocb->vport) &&
8689            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8690            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8691                 lpfc_printf_log(phba, KERN_ERR,
8692                                 LOG_SLI | LOG_VPORT,
8693                                 "1807 IOCB x%x failed. No vport\n",
8694                                 piocb->iocb.ulpCommand);
8695                 dump_stack();
8696                 return IOCB_ERROR;
8697         }
8698
8699
8700         /* If the PCI channel is in offline state, do not post iocbs. */
8701         if (unlikely(pci_channel_offline(phba->pcidev)))
8702                 return IOCB_ERROR;
8703
8704         /* If HBA has a deferred error attention, fail the iocb. */
8705         if (unlikely(phba->hba_flag & DEFER_ERATT))
8706                 return IOCB_ERROR;
8707
8708         /*
8709          * We should never get an IOCB if we are in a < LINK_DOWN state
8710          */
8711         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8712                 return IOCB_ERROR;
8713
8714         /*
8715          * Check to see if we are blocking IOCB processing because of a
8716          * outstanding event.
8717          */
8718         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
8719                 goto iocb_busy;
8720
8721         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
8722                 /*
8723                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
8724                  * can be issued if the link is not up.
8725                  */
8726                 switch (piocb->iocb.ulpCommand) {
8727                 case CMD_GEN_REQUEST64_CR:
8728                 case CMD_GEN_REQUEST64_CX:
8729                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8730                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
8731                                         FC_RCTL_DD_UNSOL_CMD) ||
8732                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8733                                         MENLO_TRANSPORT_TYPE))
8734
8735                                 goto iocb_busy;
8736                         break;
8737                 case CMD_QUE_RING_BUF_CN:
8738                 case CMD_QUE_RING_BUF64_CN:
8739                         /*
8740                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8741                          * completion, iocb_cmpl MUST be 0.
8742                          */
8743                         if (piocb->iocb_cmpl)
8744                                 piocb->iocb_cmpl = NULL;
8745                         /*FALLTHROUGH*/
8746                 case CMD_CREATE_XRI_CR:
8747                 case CMD_CLOSE_XRI_CN:
8748                 case CMD_CLOSE_XRI_CX:
8749                         break;
8750                 default:
8751                         goto iocb_busy;
8752                 }
8753
8754         /*
8755          * For FCP commands, we must be in a state where we can process link
8756          * attention events.
8757          */
8758         } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
8759                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
8760                 goto iocb_busy;
8761         }
8762
8763         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8764                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8765                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8766
8767         if (iocb)
8768                 lpfc_sli_update_ring(phba, pring);
8769         else
8770                 lpfc_sli_update_full_ring(phba, pring);
8771
8772         if (!piocb)
8773                 return IOCB_SUCCESS;
8774
8775         goto out_busy;
8776
8777  iocb_busy:
8778         pring->stats.iocb_cmd_delay++;
8779
8780  out_busy:
8781
8782         if (!(flag & SLI_IOCB_RET_IOCB)) {
8783                 __lpfc_sli_ringtx_put(phba, pring, piocb);
8784                 return IOCB_SUCCESS;
8785         }
8786
8787         return IOCB_BUSY;
8788 }
8789
8790 /**
8791  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8792  * @phba: Pointer to HBA context object.
8793  * @piocb: Pointer to command iocb.
8794  * @sglq: Pointer to the scatter gather queue object.
8795  *
8796  * This routine converts the bpl or bde that is in the IOCB
8797  * to a sgl list for the sli4 hardware. The physical address
8798  * of the bpl/bde is converted back to a virtual address.
8799  * If the IOCB contains a BPL then the list of BDE's is
8800  * converted to sli4_sge's. If the IOCB contains a single
8801  * BDE then it is converted to a single sli_sge.
8802  * The IOCB is still in cpu endianess so the contents of
8803  * the bpl can be used without byte swapping.
8804  *
8805  * Returns valid XRI = Success, NO_XRI = Failure.
8806 **/
8807 static uint16_t
8808 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8809                 struct lpfc_sglq *sglq)
8810 {
8811         uint16_t xritag = NO_XRI;
8812         struct ulp_bde64 *bpl = NULL;
8813         struct ulp_bde64 bde;
8814         struct sli4_sge *sgl  = NULL;
8815         struct lpfc_dmabuf *dmabuf;
8816         IOCB_t *icmd;
8817         int numBdes = 0;
8818         int i = 0;
8819         uint32_t offset = 0; /* accumulated offset in the sg request list */
8820         int inbound = 0; /* number of sg reply entries inbound from firmware */
8821
8822         if (!piocbq || !sglq)
8823                 return xritag;
8824
8825         sgl  = (struct sli4_sge *)sglq->sgl;
8826         icmd = &piocbq->iocb;
8827         if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8828                 return sglq->sli4_xritag;
8829         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8830                 numBdes = icmd->un.genreq64.bdl.bdeSize /
8831                                 sizeof(struct ulp_bde64);
8832                 /* The addrHigh and addrLow fields within the IOCB
8833                  * have not been byteswapped yet so there is no
8834                  * need to swap them back.
8835                  */
8836                 if (piocbq->context3)
8837                         dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8838                 else
8839                         return xritag;
8840
8841                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
8842                 if (!bpl)
8843                         return xritag;
8844
8845                 for (i = 0; i < numBdes; i++) {
8846                         /* Should already be byte swapped. */
8847                         sgl->addr_hi = bpl->addrHigh;
8848                         sgl->addr_lo = bpl->addrLow;
8849
8850                         sgl->word2 = le32_to_cpu(sgl->word2);
8851                         if ((i+1) == numBdes)
8852                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
8853                         else
8854                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
8855                         /* swap the size field back to the cpu so we
8856                          * can assign it to the sgl.
8857                          */
8858                         bde.tus.w = le32_to_cpu(bpl->tus.w);
8859                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
8860                         /* The offsets in the sgl need to be accumulated
8861                          * separately for the request and reply lists.
8862                          * The request is always first, the reply follows.
8863                          */
8864                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8865                                 /* add up the reply sg entries */
8866                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8867                                         inbound++;
8868                                 /* first inbound? reset the offset */
8869                                 if (inbound == 1)
8870                                         offset = 0;
8871                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
8872                                 bf_set(lpfc_sli4_sge_type, sgl,
8873                                         LPFC_SGE_TYPE_DATA);
8874                                 offset += bde.tus.f.bdeSize;
8875                         }
8876                         sgl->word2 = cpu_to_le32(sgl->word2);
8877                         bpl++;
8878                         sgl++;
8879                 }
8880         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8881                         /* The addrHigh and addrLow fields of the BDE have not
8882                          * been byteswapped yet so they need to be swapped
8883                          * before putting them in the sgl.
8884                          */
8885                         sgl->addr_hi =
8886                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8887                         sgl->addr_lo =
8888                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
8889                         sgl->word2 = le32_to_cpu(sgl->word2);
8890                         bf_set(lpfc_sli4_sge_last, sgl, 1);
8891                         sgl->word2 = cpu_to_le32(sgl->word2);
8892                         sgl->sge_len =
8893                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
8894         }
8895         return sglq->sli4_xritag;
8896 }
8897
8898 /**
8899  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8900  * @phba: Pointer to HBA context object.
8901  * @piocb: Pointer to command iocb.
8902  * @wqe: Pointer to the work queue entry.
8903  *
8904  * This routine converts the iocb command to its Work Queue Entry
8905  * equivalent. The wqe pointer should not have any fields set when
8906  * this routine is called because it will memcpy over them.
8907  * This routine does not set the CQ_ID or the WQEC bits in the
8908  * wqe.
8909  *
8910  * Returns: 0 = Success, IOCB_ERROR = Failure.
8911  **/
8912 static int
8913 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8914                 union lpfc_wqe128 *wqe)
8915 {
8916         uint32_t xmit_len = 0, total_len = 0;
8917         uint8_t ct = 0;
8918         uint32_t fip;
8919         uint32_t abort_tag;
8920         uint8_t command_type = ELS_COMMAND_NON_FIP;
8921         uint8_t cmnd;
8922         uint16_t xritag;
8923         uint16_t abrt_iotag;
8924         struct lpfc_iocbq *abrtiocbq;
8925         struct ulp_bde64 *bpl = NULL;
8926         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
8927         int numBdes, i;
8928         struct ulp_bde64 bde;
8929         struct lpfc_nodelist *ndlp;
8930         uint32_t *pcmd;
8931         uint32_t if_type;
8932
8933         fip = phba->hba_flag & HBA_FIP_SUPPORT;
8934         /* The fcp commands will set command type */
8935         if (iocbq->iocb_flag &  LPFC_IO_FCP)
8936                 command_type = FCP_COMMAND;
8937         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
8938                 command_type = ELS_COMMAND_FIP;
8939         else
8940                 command_type = ELS_COMMAND_NON_FIP;
8941
8942         if (phba->fcp_embed_io)
8943                 memset(wqe, 0, sizeof(union lpfc_wqe128));
8944         /* Some of the fields are in the right position already */
8945         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8946         if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8947                 /* The ct field has moved so reset */
8948                 wqe->generic.wqe_com.word7 = 0;
8949                 wqe->generic.wqe_com.word10 = 0;
8950         }
8951
8952         abort_tag = (uint32_t) iocbq->iotag;
8953         xritag = iocbq->sli4_xritag;
8954         /* words0-2 bpl convert bde */
8955         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8956                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8957                                 sizeof(struct ulp_bde64);
8958                 bpl  = (struct ulp_bde64 *)
8959                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8960                 if (!bpl)
8961                         return IOCB_ERROR;
8962
8963                 /* Should already be byte swapped. */
8964                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
8965                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
8966                 /* swap the size field back to the cpu so we
8967                  * can assign it to the sgl.
8968                  */
8969                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
8970                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8971                 total_len = 0;
8972                 for (i = 0; i < numBdes; i++) {
8973                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
8974                         total_len += bde.tus.f.bdeSize;
8975                 }
8976         } else
8977                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
8978
8979         iocbq->iocb.ulpIoTag = iocbq->iotag;
8980         cmnd = iocbq->iocb.ulpCommand;
8981
8982         switch (iocbq->iocb.ulpCommand) {
8983         case CMD_ELS_REQUEST64_CR:
8984                 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8985                         ndlp = iocbq->context_un.ndlp;
8986                 else
8987                         ndlp = (struct lpfc_nodelist *)iocbq->context1;
8988                 if (!iocbq->iocb.ulpLe) {
8989                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8990                                 "2007 Only Limited Edition cmd Format"
8991                                 " supported 0x%x\n",
8992                                 iocbq->iocb.ulpCommand);
8993                         return IOCB_ERROR;
8994                 }
8995
8996                 wqe->els_req.payload_len = xmit_len;
8997                 /* Els_reguest64 has a TMO */
8998                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8999                         iocbq->iocb.ulpTimeout);
9000                 /* Need a VF for word 4 set the vf bit*/
9001                 bf_set(els_req64_vf, &wqe->els_req, 0);
9002                 /* And a VFID for word 12 */
9003                 bf_set(els_req64_vfid, &wqe->els_req, 0);
9004                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9005                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9006                        iocbq->iocb.ulpContext);
9007                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9008                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
9009                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
9010                 if (command_type == ELS_COMMAND_FIP)
9011                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9012                                         >> LPFC_FIP_ELS_ID_SHIFT);
9013                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9014                                         iocbq->context2)->virt);
9015                 if_type = bf_get(lpfc_sli_intf_if_type,
9016                                         &phba->sli4_hba.sli_intf);
9017                 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
9018                         if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
9019                                 *pcmd == ELS_CMD_SCR ||
9020                                 *pcmd == ELS_CMD_FDISC ||
9021                                 *pcmd == ELS_CMD_LOGO ||
9022                                 *pcmd == ELS_CMD_PLOGI)) {
9023                                 bf_set(els_req64_sp, &wqe->els_req, 1);
9024                                 bf_set(els_req64_sid, &wqe->els_req,
9025                                         iocbq->vport->fc_myDID);
9026                                 if ((*pcmd == ELS_CMD_FLOGI) &&
9027                                         !(phba->fc_topology ==
9028                                                 LPFC_TOPOLOGY_LOOP))
9029                                         bf_set(els_req64_sid, &wqe->els_req, 0);
9030                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9031                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9032                                         phba->vpi_ids[iocbq->vport->vpi]);
9033                         } else if (pcmd && iocbq->context1) {
9034                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9035                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9036                                         phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9037                         }
9038                 }
9039                 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9040                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9041                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9042                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9043                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9044                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9045                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9046                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
9047                 wqe->els_req.max_response_payload_len = total_len - xmit_len;
9048                 break;
9049         case CMD_XMIT_SEQUENCE64_CX:
9050                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9051                        iocbq->iocb.un.ulpWord[3]);
9052                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
9053                        iocbq->iocb.unsli3.rcvsli3.ox_id);
9054                 /* The entire sequence is transmitted for this IOCB */
9055                 xmit_len = total_len;
9056                 cmnd = CMD_XMIT_SEQUENCE64_CR;
9057                 if (phba->link_flag & LS_LOOPBACK_MODE)
9058                         bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
9059         case CMD_XMIT_SEQUENCE64_CR:
9060                 /* word3 iocb=io_tag32 wqe=reserved */
9061                 wqe->xmit_sequence.rsvd3 = 0;
9062                 /* word4 relative_offset memcpy */
9063                 /* word5 r_ctl/df_ctl memcpy */
9064                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9065                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9066                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9067                        LPFC_WQE_IOD_WRITE);
9068                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9069                        LPFC_WQE_LENLOC_WORD12);
9070                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
9071                 wqe->xmit_sequence.xmit_len = xmit_len;
9072                 command_type = OTHER_COMMAND;
9073                 break;
9074         case CMD_XMIT_BCAST64_CN:
9075                 /* word3 iocb=iotag32 wqe=seq_payload_len */
9076                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
9077                 /* word4 iocb=rsvd wqe=rsvd */
9078                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9079                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
9080                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
9081                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9082                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9083                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9084                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9085                        LPFC_WQE_LENLOC_WORD3);
9086                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
9087                 break;
9088         case CMD_FCP_IWRITE64_CR:
9089                 command_type = FCP_COMMAND_DATA_OUT;
9090                 /* word3 iocb=iotag wqe=payload_offset_len */
9091                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9092                 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9093                        xmit_len + sizeof(struct fcp_rsp));
9094                 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9095                        0);
9096                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9097                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9098                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9099                        iocbq->iocb.ulpFCP2Rcvy);
9100                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9101                 /* Always open the exchange */
9102                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9103                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9104                        LPFC_WQE_LENLOC_WORD4);
9105                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
9106                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
9107                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9108                         bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
9109                         bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9110                         if (iocbq->priority) {
9111                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9112                                        (iocbq->priority << 1));
9113                         } else {
9114                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9115                                        (phba->cfg_XLanePriority << 1));
9116                         }
9117                 }
9118                 /* Note, word 10 is already initialized to 0 */
9119
9120                 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9121                 if (phba->cfg_enable_pbde)
9122                         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9123                 else
9124                         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9125
9126                 if (phba->fcp_embed_io) {
9127                         struct lpfc_scsi_buf *lpfc_cmd;
9128                         struct sli4_sge *sgl;
9129                         struct fcp_cmnd *fcp_cmnd;
9130                         uint32_t *ptr;
9131
9132                         /* 128 byte wqe support here */
9133
9134                         lpfc_cmd = iocbq->context1;
9135                         sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9136                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9137
9138                         /* Word 0-2 - FCP_CMND */
9139                         wqe->generic.bde.tus.f.bdeFlags =
9140                                 BUFF_TYPE_BDE_IMMED;
9141                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9142                         wqe->generic.bde.addrHigh = 0;
9143                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9144
9145                         bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9146                         bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
9147
9148                         /* Word 22-29  FCP CMND Payload */
9149                         ptr = &wqe->words[22];
9150                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9151                 }
9152                 break;
9153         case CMD_FCP_IREAD64_CR:
9154                 /* word3 iocb=iotag wqe=payload_offset_len */
9155                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9156                 bf_set(payload_offset_len, &wqe->fcp_iread,
9157                        xmit_len + sizeof(struct fcp_rsp));
9158                 bf_set(cmd_buff_len, &wqe->fcp_iread,
9159                        0);
9160                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9161                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9162                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9163                        iocbq->iocb.ulpFCP2Rcvy);
9164                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
9165                 /* Always open the exchange */
9166                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9167                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9168                        LPFC_WQE_LENLOC_WORD4);
9169                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
9170                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
9171                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9172                         bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
9173                         bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9174                         if (iocbq->priority) {
9175                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9176                                        (iocbq->priority << 1));
9177                         } else {
9178                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9179                                        (phba->cfg_XLanePriority << 1));
9180                         }
9181                 }
9182                 /* Note, word 10 is already initialized to 0 */
9183
9184                 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9185                 if (phba->cfg_enable_pbde)
9186                         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9187                 else
9188                         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9189
9190                 if (phba->fcp_embed_io) {
9191                         struct lpfc_scsi_buf *lpfc_cmd;
9192                         struct sli4_sge *sgl;
9193                         struct fcp_cmnd *fcp_cmnd;
9194                         uint32_t *ptr;
9195
9196                         /* 128 byte wqe support here */
9197
9198                         lpfc_cmd = iocbq->context1;
9199                         sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9200                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9201
9202                         /* Word 0-2 - FCP_CMND */
9203                         wqe->generic.bde.tus.f.bdeFlags =
9204                                 BUFF_TYPE_BDE_IMMED;
9205                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9206                         wqe->generic.bde.addrHigh = 0;
9207                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9208
9209                         bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9210                         bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
9211
9212                         /* Word 22-29  FCP CMND Payload */
9213                         ptr = &wqe->words[22];
9214                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9215                 }
9216                 break;
9217         case CMD_FCP_ICMND64_CR:
9218                 /* word3 iocb=iotag wqe=payload_offset_len */
9219                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9220                 bf_set(payload_offset_len, &wqe->fcp_icmd,
9221                        xmit_len + sizeof(struct fcp_rsp));
9222                 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9223                        0);
9224                 /* word3 iocb=IO_TAG wqe=reserved */
9225                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
9226                 /* Always open the exchange */
9227                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9228                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9229                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9230                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9231                        LPFC_WQE_LENLOC_NONE);
9232                 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9233                        iocbq->iocb.ulpFCP2Rcvy);
9234                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9235                         bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
9236                         bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9237                         if (iocbq->priority) {
9238                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9239                                        (iocbq->priority << 1));
9240                         } else {
9241                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9242                                        (phba->cfg_XLanePriority << 1));
9243                         }
9244                 }
9245                 /* Note, word 10 is already initialized to 0 */
9246
9247                 if (phba->fcp_embed_io) {
9248                         struct lpfc_scsi_buf *lpfc_cmd;
9249                         struct sli4_sge *sgl;
9250                         struct fcp_cmnd *fcp_cmnd;
9251                         uint32_t *ptr;
9252
9253                         /* 128 byte wqe support here */
9254
9255                         lpfc_cmd = iocbq->context1;
9256                         sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9257                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9258
9259                         /* Word 0-2 - FCP_CMND */
9260                         wqe->generic.bde.tus.f.bdeFlags =
9261                                 BUFF_TYPE_BDE_IMMED;
9262                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9263                         wqe->generic.bde.addrHigh = 0;
9264                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9265
9266                         bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9267                         bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
9268
9269                         /* Word 22-29  FCP CMND Payload */
9270                         ptr = &wqe->words[22];
9271                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9272                 }
9273                 break;
9274         case CMD_GEN_REQUEST64_CR:
9275                 /* For this command calculate the xmit length of the
9276                  * request bde.
9277                  */
9278                 xmit_len = 0;
9279                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9280                         sizeof(struct ulp_bde64);
9281                 for (i = 0; i < numBdes; i++) {
9282                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9283                         if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9284                                 break;
9285                         xmit_len += bde.tus.f.bdeSize;
9286                 }
9287                 /* word3 iocb=IO_TAG wqe=request_payload_len */
9288                 wqe->gen_req.request_payload_len = xmit_len;
9289                 /* word4 iocb=parameter wqe=relative_offset memcpy */
9290                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
9291                 /* word6 context tag copied in memcpy */
9292                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
9293                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9294                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9295                                 "2015 Invalid CT %x command 0x%x\n",
9296                                 ct, iocbq->iocb.ulpCommand);
9297                         return IOCB_ERROR;
9298                 }
9299                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9300                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9301                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9302                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9303                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9304                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9305                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9306                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
9307                 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
9308                 command_type = OTHER_COMMAND;
9309                 break;
9310         case CMD_XMIT_ELS_RSP64_CX:
9311                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
9312                 /* words0-2 BDE memcpy */
9313                 /* word3 iocb=iotag32 wqe=response_payload_len */
9314                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
9315                 /* word4 */
9316                 wqe->xmit_els_rsp.word4 = 0;
9317                 /* word5 iocb=rsvd wge=did */
9318                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
9319                          iocbq->iocb.un.xseq64.xmit_els_remoteID);
9320
9321                 if_type = bf_get(lpfc_sli_intf_if_type,
9322                                         &phba->sli4_hba.sli_intf);
9323                 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
9324                         if (iocbq->vport->fc_flag & FC_PT2PT) {
9325                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9326                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9327                                         iocbq->vport->fc_myDID);
9328                                 if (iocbq->vport->fc_myDID == Fabric_DID) {
9329                                         bf_set(wqe_els_did,
9330                                                 &wqe->xmit_els_rsp.wqe_dest, 0);
9331                                 }
9332                         }
9333                 }
9334                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9335                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9336                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9337                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
9338                        iocbq->iocb.unsli3.rcvsli3.ox_id);
9339                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
9340                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9341                                phba->vpi_ids[iocbq->vport->vpi]);
9342                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9343                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9344                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9345                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9346                        LPFC_WQE_LENLOC_WORD3);
9347                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
9348                 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9349                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9350                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9351                                         iocbq->context2)->virt);
9352                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
9353                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9354                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9355                                         iocbq->vport->fc_myDID);
9356                                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9357                                 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9358                                         phba->vpi_ids[phba->pport->vpi]);
9359                 }
9360                 command_type = OTHER_COMMAND;
9361                 break;
9362         case CMD_CLOSE_XRI_CN:
9363         case CMD_ABORT_XRI_CN:
9364         case CMD_ABORT_XRI_CX:
9365                 /* words 0-2 memcpy should be 0 rserved */
9366                 /* port will send abts */
9367                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9368                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9369                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9370                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9371                 } else
9372                         fip = 0;
9373
9374                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
9375                         /*
9376                          * The link is down, or the command was ELS_FIP
9377                          * so the fw does not need to send abts
9378                          * on the wire.
9379                          */
9380                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9381                 else
9382                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9383                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
9384                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9385                 wqe->abort_cmd.rsrvd5 = 0;
9386                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
9387                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9388                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
9389                 /*
9390                  * The abort handler will send us CMD_ABORT_XRI_CN or
9391                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9392                  */
9393                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9394                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9395                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9396                        LPFC_WQE_LENLOC_NONE);
9397                 cmnd = CMD_ABORT_XRI_CX;
9398                 command_type = OTHER_COMMAND;
9399                 xritag = 0;
9400                 break;
9401         case CMD_XMIT_BLS_RSP64_CX:
9402                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
9403                 /* As BLS ABTS RSP WQE is very different from other WQEs,
9404                  * we re-construct this WQE here based on information in
9405                  * iocbq from scratch.
9406                  */
9407                 memset(wqe, 0, sizeof(union lpfc_wqe));
9408                 /* OX_ID is invariable to who sent ABTS to CT exchange */
9409                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
9410                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9411                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
9412                     LPFC_ABTS_UNSOL_INT) {
9413                         /* ABTS sent by initiator to CT exchange, the
9414                          * RX_ID field will be filled with the newly
9415                          * allocated responder XRI.
9416                          */
9417                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9418                                iocbq->sli4_xritag);
9419                 } else {
9420                         /* ABTS sent by responder to CT exchange, the
9421                          * RX_ID field will be filled with the responder
9422                          * RX_ID from ABTS.
9423                          */
9424                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9425                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
9426                 }
9427                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9428                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
9429
9430                 /* Use CT=VPI */
9431                 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9432                         ndlp->nlp_DID);
9433                 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9434                         iocbq->iocb.ulpContext);
9435                 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
9436                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
9437                         phba->vpi_ids[phba->pport->vpi]);
9438                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9439                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9440                        LPFC_WQE_LENLOC_NONE);
9441                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9442                 command_type = OTHER_COMMAND;
9443                 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9444                         bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9445                                bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9446                         bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9447                                bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9448                         bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9449                                bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9450                 }
9451
9452                 break;
9453         case CMD_SEND_FRAME:
9454                 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9455                 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9456                 return 0;
9457         case CMD_XRI_ABORTED_CX:
9458         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
9459         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9460         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9461         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9462         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9463         default:
9464                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9465                                 "2014 Invalid command 0x%x\n",
9466                                 iocbq->iocb.ulpCommand);
9467                 return IOCB_ERROR;
9468                 break;
9469         }
9470
9471         if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9472                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9473         else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9474                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9475         else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9476                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9477         iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9478                               LPFC_IO_DIF_INSERT);
9479         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9480         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9481         wqe->generic.wqe_com.abort_tag = abort_tag;
9482         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9483         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9484         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9485         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
9486         return 0;
9487 }
9488
9489 /**
9490  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9491  * @phba: Pointer to HBA context object.
9492  * @ring_number: SLI ring number to issue iocb on.
9493  * @piocb: Pointer to command iocb.
9494  * @flag: Flag indicating if this command can be put into txq.
9495  *
9496  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9497  * an iocb command to an HBA with SLI-4 interface spec.
9498  *
9499  * This function is called with hbalock held. The function will return success
9500  * after it successfully submit the iocb to firmware or after adding to the
9501  * txq.
9502  **/
9503 static int
9504 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9505                          struct lpfc_iocbq *piocb, uint32_t flag)
9506 {
9507         struct lpfc_sglq *sglq;
9508         union lpfc_wqe128 wqe;
9509         struct lpfc_queue *wq;
9510         struct lpfc_sli_ring *pring;
9511
9512         /* Get the WQ */
9513         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9514             (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9515                 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9516                         wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9517                 else
9518                         wq = phba->sli4_hba.oas_wq;
9519         } else {
9520                 wq = phba->sli4_hba.els_wq;
9521         }
9522
9523         /* Get corresponding ring */
9524         pring = wq->pring;
9525
9526         /*
9527          * The WQE can be either 64 or 128 bytes,
9528          */
9529
9530         lockdep_assert_held(&phba->hbalock);
9531
9532         if (piocb->sli4_xritag == NO_XRI) {
9533                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
9534                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
9535                         sglq = NULL;
9536                 else {
9537                         if (!list_empty(&pring->txq)) {
9538                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
9539                                         __lpfc_sli_ringtx_put(phba,
9540                                                 pring, piocb);
9541                                         return IOCB_SUCCESS;
9542                                 } else {
9543                                         return IOCB_BUSY;
9544                                 }
9545                         } else {
9546                                 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
9547                                 if (!sglq) {
9548                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
9549                                                 __lpfc_sli_ringtx_put(phba,
9550                                                                 pring,
9551                                                                 piocb);
9552                                                 return IOCB_SUCCESS;
9553                                         } else
9554                                                 return IOCB_BUSY;
9555                                 }
9556                         }
9557                 }
9558         } else if (piocb->iocb_flag &  LPFC_IO_FCP)
9559                 /* These IO's already have an XRI and a mapped sgl. */
9560                 sglq = NULL;
9561         else {
9562                 /*
9563                  * This is a continuation of a commandi,(CX) so this
9564                  * sglq is on the active list
9565                  */
9566                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
9567                 if (!sglq)
9568                         return IOCB_ERROR;
9569         }
9570
9571         if (sglq) {
9572                 piocb->sli4_lxritag = sglq->sli4_lxritag;
9573                 piocb->sli4_xritag = sglq->sli4_xritag;
9574                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
9575                         return IOCB_ERROR;
9576         }
9577
9578         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
9579                 return IOCB_ERROR;
9580
9581         if (lpfc_sli4_wq_put(wq, &wqe))
9582                 return IOCB_ERROR;
9583         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9584
9585         return 0;
9586 }
9587
9588 /**
9589  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9590  *
9591  * This routine wraps the actual lockless version for issusing IOCB function
9592  * pointer from the lpfc_hba struct.
9593  *
9594  * Return codes:
9595  * IOCB_ERROR - Error
9596  * IOCB_SUCCESS - Success
9597  * IOCB_BUSY - Busy
9598  **/
9599 int
9600 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9601                 struct lpfc_iocbq *piocb, uint32_t flag)
9602 {
9603         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9604 }
9605
9606 /**
9607  * lpfc_sli_api_table_setup - Set up sli api function jump table
9608  * @phba: The hba struct for which this call is being executed.
9609  * @dev_grp: The HBA PCI-Device group number.
9610  *
9611  * This routine sets up the SLI interface API function jump table in @phba
9612  * struct.
9613  * Returns: 0 - success, -ENODEV - failure.
9614  **/
9615 int
9616 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9617 {
9618
9619         switch (dev_grp) {
9620         case LPFC_PCI_DEV_LP:
9621                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9622                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9623                 break;
9624         case LPFC_PCI_DEV_OC:
9625                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9626                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9627                 break;
9628         default:
9629                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9630                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
9631                                 dev_grp);
9632                 return -ENODEV;
9633                 break;
9634         }
9635         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9636         return 0;
9637 }
9638
9639 /**
9640  * lpfc_sli4_calc_ring - Calculates which ring to use
9641  * @phba: Pointer to HBA context object.
9642  * @piocb: Pointer to command iocb.
9643  *
9644  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9645  * hba_wqidx, thus we need to calculate the corresponding ring.
9646  * Since ABORTS must go on the same WQ of the command they are
9647  * aborting, we use command's hba_wqidx.
9648  */
9649 struct lpfc_sli_ring *
9650 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9651 {
9652         if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
9653                 if (!(phba->cfg_fof) ||
9654                     (!(piocb->iocb_flag & LPFC_IO_FOF))) {
9655                         if (unlikely(!phba->sli4_hba.fcp_wq))
9656                                 return NULL;
9657                         /*
9658                          * for abort iocb hba_wqidx should already
9659                          * be setup based on what work queue we used.
9660                          */
9661                         if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9662                                 piocb->hba_wqidx =
9663                                         lpfc_sli4_scmd_to_wqidx_distr(phba,
9664                                                               piocb->context1);
9665                                 piocb->hba_wqidx = piocb->hba_wqidx %
9666                                         phba->cfg_fcp_io_channel;
9667                         }
9668                         return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
9669                 } else {
9670                         if (unlikely(!phba->sli4_hba.oas_wq))
9671                                 return NULL;
9672                         piocb->hba_wqidx = 0;
9673                         return phba->sli4_hba.oas_wq->pring;
9674                 }
9675         } else {
9676                 if (unlikely(!phba->sli4_hba.els_wq))
9677                         return NULL;
9678                 piocb->hba_wqidx = 0;
9679                 return phba->sli4_hba.els_wq->pring;
9680         }
9681 }
9682
9683 /**
9684  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9685  * @phba: Pointer to HBA context object.
9686  * @pring: Pointer to driver SLI ring object.
9687  * @piocb: Pointer to command iocb.
9688  * @flag: Flag indicating if this command can be put into txq.
9689  *
9690  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9691  * function. This function gets the hbalock and calls
9692  * __lpfc_sli_issue_iocb function and will return the error returned
9693  * by __lpfc_sli_issue_iocb function. This wrapper is used by
9694  * functions which do not hold hbalock.
9695  **/
9696 int
9697 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9698                     struct lpfc_iocbq *piocb, uint32_t flag)
9699 {
9700         struct lpfc_hba_eq_hdl *hba_eq_hdl;
9701         struct lpfc_sli_ring *pring;
9702         struct lpfc_queue *fpeq;
9703         struct lpfc_eqe *eqe;
9704         unsigned long iflags;
9705         int rc, idx;
9706
9707         if (phba->sli_rev == LPFC_SLI_REV4) {
9708                 pring = lpfc_sli4_calc_ring(phba, piocb);
9709                 if (unlikely(pring == NULL))
9710                         return IOCB_ERROR;
9711
9712                 spin_lock_irqsave(&pring->ring_lock, iflags);
9713                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9714                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9715
9716                 if (lpfc_fcp_look_ahead && (piocb->iocb_flag &  LPFC_IO_FCP)) {
9717                         idx = piocb->hba_wqidx;
9718                         hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
9719
9720                         if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
9721
9722                                 /* Get associated EQ with this index */
9723                                 fpeq = phba->sli4_hba.hba_eq[idx];
9724
9725                                 /* Turn off interrupts from this EQ */
9726                                 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
9727
9728                                 /*
9729                                  * Process all the events on FCP EQ
9730                                  */
9731                                 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9732                                         lpfc_sli4_hba_handle_eqe(phba,
9733                                                 eqe, idx);
9734                                         fpeq->EQ_processed++;
9735                                 }
9736
9737                                 /* Always clear and re-arm the EQ */
9738                                 phba->sli4_hba.sli4_eq_release(fpeq,
9739                                         LPFC_QUEUE_REARM);
9740                         }
9741                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
9742                 }
9743         } else {
9744                 /* For now, SLI2/3 will still use hbalock */
9745                 spin_lock_irqsave(&phba->hbalock, iflags);
9746                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9747                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9748         }
9749         return rc;
9750 }
9751
9752 /**
9753  * lpfc_extra_ring_setup - Extra ring setup function
9754  * @phba: Pointer to HBA context object.
9755  *
9756  * This function is called while driver attaches with the
9757  * HBA to setup the extra ring. The extra ring is used
9758  * only when driver needs to support target mode functionality
9759  * or IP over FC functionalities.
9760  *
9761  * This function is called with no lock held. SLI3 only.
9762  **/
9763 static int
9764 lpfc_extra_ring_setup( struct lpfc_hba *phba)
9765 {
9766         struct lpfc_sli *psli;
9767         struct lpfc_sli_ring *pring;
9768
9769         psli = &phba->sli;
9770
9771         /* Adjust cmd/rsp ring iocb entries more evenly */
9772
9773         /* Take some away from the FCP ring */
9774         pring = &psli->sli3_ring[LPFC_FCP_RING];
9775         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9776         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9777         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9778         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9779
9780         /* and give them to the extra ring */
9781         pring = &psli->sli3_ring[LPFC_EXTRA_RING];
9782
9783         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9784         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9785         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9786         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9787
9788         /* Setup default profile for this ring */
9789         pring->iotag_max = 4096;
9790         pring->num_mask = 1;
9791         pring->prt[0].profile = 0;      /* Mask 0 */
9792         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9793         pring->prt[0].type = phba->cfg_multi_ring_type;
9794         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9795         return 0;
9796 }
9797
9798 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9799  * @phba: Pointer to HBA context object.
9800  * @iocbq: Pointer to iocb object.
9801  *
9802  * The async_event handler calls this routine when it receives
9803  * an ASYNC_STATUS_CN event from the port.  The port generates
9804  * this event when an Abort Sequence request to an rport fails
9805  * twice in succession.  The abort could be originated by the
9806  * driver or by the port.  The ABTS could have been for an ELS
9807  * or FCP IO.  The port only generates this event when an ABTS
9808  * fails to complete after one retry.
9809  */
9810 static void
9811 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9812                           struct lpfc_iocbq *iocbq)
9813 {
9814         struct lpfc_nodelist *ndlp = NULL;
9815         uint16_t rpi = 0, vpi = 0;
9816         struct lpfc_vport *vport = NULL;
9817
9818         /* The rpi in the ulpContext is vport-sensitive. */
9819         vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9820         rpi = iocbq->iocb.ulpContext;
9821
9822         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9823                         "3092 Port generated ABTS async event "
9824                         "on vpi %d rpi %d status 0x%x\n",
9825                         vpi, rpi, iocbq->iocb.ulpStatus);
9826
9827         vport = lpfc_find_vport_by_vpid(phba, vpi);
9828         if (!vport)
9829                 goto err_exit;
9830         ndlp = lpfc_findnode_rpi(vport, rpi);
9831         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9832                 goto err_exit;
9833
9834         if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9835                 lpfc_sli_abts_recover_port(vport, ndlp);
9836         return;
9837
9838  err_exit:
9839         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9840                         "3095 Event Context not found, no "
9841                         "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9842                         iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9843                         vpi, rpi);
9844 }
9845
9846 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9847  * @phba: pointer to HBA context object.
9848  * @ndlp: nodelist pointer for the impacted rport.
9849  * @axri: pointer to the wcqe containing the failed exchange.
9850  *
9851  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9852  * port.  The port generates this event when an abort exchange request to an
9853  * rport fails twice in succession with no reply.  The abort could be originated
9854  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
9855  */
9856 void
9857 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9858                            struct lpfc_nodelist *ndlp,
9859                            struct sli4_wcqe_xri_aborted *axri)
9860 {
9861         struct lpfc_vport *vport;
9862         uint32_t ext_status = 0;
9863
9864         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
9865                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9866                                 "3115 Node Context not found, driver "
9867                                 "ignoring abts err event\n");
9868                 return;
9869         }
9870
9871         vport = ndlp->vport;
9872         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9873                         "3116 Port generated FCP XRI ABORT event on "
9874                         "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
9875                         ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
9876                         bf_get(lpfc_wcqe_xa_xri, axri),
9877                         bf_get(lpfc_wcqe_xa_status, axri),
9878                         axri->parameter);
9879
9880         /*
9881          * Catch the ABTS protocol failure case.  Older OCe FW releases returned
9882          * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9883          * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9884          */
9885         ext_status = axri->parameter & IOERR_PARAM_MASK;
9886         if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9887             ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
9888                 lpfc_sli_abts_recover_port(vport, ndlp);
9889 }
9890
9891 /**
9892  * lpfc_sli_async_event_handler - ASYNC iocb handler function
9893  * @phba: Pointer to HBA context object.
9894  * @pring: Pointer to driver SLI ring object.
9895  * @iocbq: Pointer to iocb object.
9896  *
9897  * This function is called by the slow ring event handler
9898  * function when there is an ASYNC event iocb in the ring.
9899  * This function is called with no lock held.
9900  * Currently this function handles only temperature related
9901  * ASYNC events. The function decodes the temperature sensor
9902  * event message and posts events for the management applications.
9903  **/
9904 static void
9905 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9906         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9907 {
9908         IOCB_t *icmd;
9909         uint16_t evt_code;
9910         struct temp_event temp_event_data;
9911         struct Scsi_Host *shost;
9912         uint32_t *iocb_w;
9913
9914         icmd = &iocbq->iocb;
9915         evt_code = icmd->un.asyncstat.evt_code;
9916
9917         switch (evt_code) {
9918         case ASYNC_TEMP_WARN:
9919         case ASYNC_TEMP_SAFE:
9920                 temp_event_data.data = (uint32_t) icmd->ulpContext;
9921                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9922                 if (evt_code == ASYNC_TEMP_WARN) {
9923                         temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9924                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9925                                 "0347 Adapter is very hot, please take "
9926                                 "corrective action. temperature : %d Celsius\n",
9927                                 (uint32_t) icmd->ulpContext);
9928                 } else {
9929                         temp_event_data.event_code = LPFC_NORMAL_TEMP;
9930                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9931                                 "0340 Adapter temperature is OK now. "
9932                                 "temperature : %d Celsius\n",
9933                                 (uint32_t) icmd->ulpContext);
9934                 }
9935
9936                 /* Send temperature change event to applications */
9937                 shost = lpfc_shost_from_vport(phba->pport);
9938                 fc_host_post_vendor_event(shost, fc_get_event_number(),
9939                         sizeof(temp_event_data), (char *) &temp_event_data,
9940                         LPFC_NL_VENDOR_ID);
9941                 break;
9942         case ASYNC_STATUS_CN:
9943                 lpfc_sli_abts_err_handler(phba, iocbq);
9944                 break;
9945         default:
9946                 iocb_w = (uint32_t *) icmd;
9947                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9948                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
9949                         " evt_code 0x%x\n"
9950                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
9951                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
9952                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
9953                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
9954                         pring->ringno, icmd->un.asyncstat.evt_code,
9955                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9956                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9957                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9958                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9959
9960                 break;
9961         }
9962 }
9963
9964
9965 /**
9966  * lpfc_sli4_setup - SLI ring setup function
9967  * @phba: Pointer to HBA context object.
9968  *
9969  * lpfc_sli_setup sets up rings of the SLI interface with
9970  * number of iocbs per ring and iotags. This function is
9971  * called while driver attach to the HBA and before the
9972  * interrupts are enabled. So there is no need for locking.
9973  *
9974  * This function always returns 0.
9975  **/
9976 int
9977 lpfc_sli4_setup(struct lpfc_hba *phba)
9978 {
9979         struct lpfc_sli_ring *pring;
9980
9981         pring = phba->sli4_hba.els_wq->pring;
9982         pring->num_mask = LPFC_MAX_RING_MASK;
9983         pring->prt[0].profile = 0;      /* Mask 0 */
9984         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9985         pring->prt[0].type = FC_TYPE_ELS;
9986         pring->prt[0].lpfc_sli_rcv_unsol_event =
9987             lpfc_els_unsol_event;
9988         pring->prt[1].profile = 0;      /* Mask 1 */
9989         pring->prt[1].rctl = FC_RCTL_ELS_REP;
9990         pring->prt[1].type = FC_TYPE_ELS;
9991         pring->prt[1].lpfc_sli_rcv_unsol_event =
9992             lpfc_els_unsol_event;
9993         pring->prt[2].profile = 0;      /* Mask 2 */
9994         /* NameServer Inquiry */
9995         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9996         /* NameServer */
9997         pring->prt[2].type = FC_TYPE_CT;
9998         pring->prt[2].lpfc_sli_rcv_unsol_event =
9999             lpfc_ct_unsol_event;
10000         pring->prt[3].profile = 0;      /* Mask 3 */
10001         /* NameServer response */
10002         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10003         /* NameServer */
10004         pring->prt[3].type = FC_TYPE_CT;
10005         pring->prt[3].lpfc_sli_rcv_unsol_event =
10006             lpfc_ct_unsol_event;
10007         return 0;
10008 }
10009
10010 /**
10011  * lpfc_sli_setup - SLI ring setup function
10012  * @phba: Pointer to HBA context object.
10013  *
10014  * lpfc_sli_setup sets up rings of the SLI interface with
10015  * number of iocbs per ring and iotags. This function is
10016  * called while driver attach to the HBA and before the
10017  * interrupts are enabled. So there is no need for locking.
10018  *
10019  * This function always returns 0. SLI3 only.
10020  **/
10021 int
10022 lpfc_sli_setup(struct lpfc_hba *phba)
10023 {
10024         int i, totiocbsize = 0;
10025         struct lpfc_sli *psli = &phba->sli;
10026         struct lpfc_sli_ring *pring;
10027
10028         psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
10029         psli->sli_flag = 0;
10030
10031         psli->iocbq_lookup = NULL;
10032         psli->iocbq_lookup_len = 0;
10033         psli->last_iotag = 0;
10034
10035         for (i = 0; i < psli->num_rings; i++) {
10036                 pring = &psli->sli3_ring[i];
10037                 switch (i) {
10038                 case LPFC_FCP_RING:     /* ring 0 - FCP */
10039                         /* numCiocb and numRiocb are used in config_port */
10040                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10041                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10042                         pring->sli.sli3.numCiocb +=
10043                                 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10044                         pring->sli.sli3.numRiocb +=
10045                                 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10046                         pring->sli.sli3.numCiocb +=
10047                                 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10048                         pring->sli.sli3.numRiocb +=
10049                                 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10050                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10051                                                         SLI3_IOCB_CMD_SIZE :
10052                                                         SLI2_IOCB_CMD_SIZE;
10053                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10054                                                         SLI3_IOCB_RSP_SIZE :
10055                                                         SLI2_IOCB_RSP_SIZE;
10056                         pring->iotag_ctr = 0;
10057                         pring->iotag_max =
10058                             (phba->cfg_hba_queue_depth * 2);
10059                         pring->fast_iotag = pring->iotag_max;
10060                         pring->num_mask = 0;
10061                         break;
10062                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
10063                         /* numCiocb and numRiocb are used in config_port */
10064                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10065                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10066                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10067                                                         SLI3_IOCB_CMD_SIZE :
10068                                                         SLI2_IOCB_CMD_SIZE;
10069                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10070                                                         SLI3_IOCB_RSP_SIZE :
10071                                                         SLI2_IOCB_RSP_SIZE;
10072                         pring->iotag_max = phba->cfg_hba_queue_depth;
10073                         pring->num_mask = 0;
10074                         break;
10075                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
10076                         /* numCiocb and numRiocb are used in config_port */
10077                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10078                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10079                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10080                                                         SLI3_IOCB_CMD_SIZE :
10081                                                         SLI2_IOCB_CMD_SIZE;
10082                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10083                                                         SLI3_IOCB_RSP_SIZE :
10084                                                         SLI2_IOCB_RSP_SIZE;
10085                         pring->fast_iotag = 0;
10086                         pring->iotag_ctr = 0;
10087                         pring->iotag_max = 4096;
10088                         pring->lpfc_sli_rcv_async_status =
10089                                 lpfc_sli_async_event_handler;
10090                         pring->num_mask = LPFC_MAX_RING_MASK;
10091                         pring->prt[0].profile = 0;      /* Mask 0 */
10092                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10093                         pring->prt[0].type = FC_TYPE_ELS;
10094                         pring->prt[0].lpfc_sli_rcv_unsol_event =
10095                             lpfc_els_unsol_event;
10096                         pring->prt[1].profile = 0;      /* Mask 1 */
10097                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
10098                         pring->prt[1].type = FC_TYPE_ELS;
10099                         pring->prt[1].lpfc_sli_rcv_unsol_event =
10100                             lpfc_els_unsol_event;
10101                         pring->prt[2].profile = 0;      /* Mask 2 */
10102                         /* NameServer Inquiry */
10103                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10104                         /* NameServer */
10105                         pring->prt[2].type = FC_TYPE_CT;
10106                         pring->prt[2].lpfc_sli_rcv_unsol_event =
10107                             lpfc_ct_unsol_event;
10108                         pring->prt[3].profile = 0;      /* Mask 3 */
10109                         /* NameServer response */
10110                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10111                         /* NameServer */
10112                         pring->prt[3].type = FC_TYPE_CT;
10113                         pring->prt[3].lpfc_sli_rcv_unsol_event =
10114                             lpfc_ct_unsol_event;
10115                         break;
10116                 }
10117                 totiocbsize += (pring->sli.sli3.numCiocb *
10118                         pring->sli.sli3.sizeCiocb) +
10119                         (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
10120         }
10121         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
10122                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
10123                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10124                        "SLI2 SLIM Data: x%x x%lx\n",
10125                        phba->brd_no, totiocbsize,
10126                        (unsigned long) MAX_SLIM_IOCB_SIZE);
10127         }
10128         if (phba->cfg_multi_ring_support == 2)
10129                 lpfc_extra_ring_setup(phba);
10130
10131         return 0;
10132 }
10133
10134 /**
10135  * lpfc_sli4_queue_init - Queue initialization function
10136  * @phba: Pointer to HBA context object.
10137  *
10138  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
10139  * ring. This function also initializes ring indices of each ring.
10140  * This function is called during the initialization of the SLI
10141  * interface of an HBA.
10142  * This function is called with no lock held and always returns
10143  * 1.
10144  **/
10145 void
10146 lpfc_sli4_queue_init(struct lpfc_hba *phba)
10147 {
10148         struct lpfc_sli *psli;
10149         struct lpfc_sli_ring *pring;
10150         int i;
10151
10152         psli = &phba->sli;
10153         spin_lock_irq(&phba->hbalock);
10154         INIT_LIST_HEAD(&psli->mboxq);
10155         INIT_LIST_HEAD(&psli->mboxq_cmpl);
10156         /* Initialize list headers for txq and txcmplq as double linked lists */
10157         for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10158                 pring = phba->sli4_hba.fcp_wq[i]->pring;
10159                 pring->flag = 0;
10160                 pring->ringno = LPFC_FCP_RING;
10161                 INIT_LIST_HEAD(&pring->txq);
10162                 INIT_LIST_HEAD(&pring->txcmplq);
10163                 INIT_LIST_HEAD(&pring->iocb_continueq);
10164                 spin_lock_init(&pring->ring_lock);
10165         }
10166         for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10167                 pring = phba->sli4_hba.nvme_wq[i]->pring;
10168                 pring->flag = 0;
10169                 pring->ringno = LPFC_FCP_RING;
10170                 INIT_LIST_HEAD(&pring->txq);
10171                 INIT_LIST_HEAD(&pring->txcmplq);
10172                 INIT_LIST_HEAD(&pring->iocb_continueq);
10173                 spin_lock_init(&pring->ring_lock);
10174         }
10175         pring = phba->sli4_hba.els_wq->pring;
10176         pring->flag = 0;
10177         pring->ringno = LPFC_ELS_RING;
10178         INIT_LIST_HEAD(&pring->txq);
10179         INIT_LIST_HEAD(&pring->txcmplq);
10180         INIT_LIST_HEAD(&pring->iocb_continueq);
10181         spin_lock_init(&pring->ring_lock);
10182
10183         if (phba->cfg_nvme_io_channel) {
10184                 pring = phba->sli4_hba.nvmels_wq->pring;
10185                 pring->flag = 0;
10186                 pring->ringno = LPFC_ELS_RING;
10187                 INIT_LIST_HEAD(&pring->txq);
10188                 INIT_LIST_HEAD(&pring->txcmplq);
10189                 INIT_LIST_HEAD(&pring->iocb_continueq);
10190                 spin_lock_init(&pring->ring_lock);
10191         }
10192
10193         if (phba->cfg_fof) {
10194                 pring = phba->sli4_hba.oas_wq->pring;
10195                 pring->flag = 0;
10196                 pring->ringno = LPFC_FCP_RING;
10197                 INIT_LIST_HEAD(&pring->txq);
10198                 INIT_LIST_HEAD(&pring->txcmplq);
10199                 INIT_LIST_HEAD(&pring->iocb_continueq);
10200                 spin_lock_init(&pring->ring_lock);
10201         }
10202
10203         spin_unlock_irq(&phba->hbalock);
10204 }
10205
10206 /**
10207  * lpfc_sli_queue_init - Queue initialization function
10208  * @phba: Pointer to HBA context object.
10209  *
10210  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10211  * ring. This function also initializes ring indices of each ring.
10212  * This function is called during the initialization of the SLI
10213  * interface of an HBA.
10214  * This function is called with no lock held and always returns
10215  * 1.
10216  **/
10217 void
10218 lpfc_sli_queue_init(struct lpfc_hba *phba)
10219 {
10220         struct lpfc_sli *psli;
10221         struct lpfc_sli_ring *pring;
10222         int i;
10223
10224         psli = &phba->sli;
10225         spin_lock_irq(&phba->hbalock);
10226         INIT_LIST_HEAD(&psli->mboxq);
10227         INIT_LIST_HEAD(&psli->mboxq_cmpl);
10228         /* Initialize list headers for txq and txcmplq as double linked lists */
10229         for (i = 0; i < psli->num_rings; i++) {
10230                 pring = &psli->sli3_ring[i];
10231                 pring->ringno = i;
10232                 pring->sli.sli3.next_cmdidx  = 0;
10233                 pring->sli.sli3.local_getidx = 0;
10234                 pring->sli.sli3.cmdidx = 0;
10235                 INIT_LIST_HEAD(&pring->iocb_continueq);
10236                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
10237                 INIT_LIST_HEAD(&pring->postbufq);
10238                 pring->flag = 0;
10239                 INIT_LIST_HEAD(&pring->txq);
10240                 INIT_LIST_HEAD(&pring->txcmplq);
10241                 spin_lock_init(&pring->ring_lock);
10242         }
10243         spin_unlock_irq(&phba->hbalock);
10244 }
10245
10246 /**
10247  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10248  * @phba: Pointer to HBA context object.
10249  *
10250  * This routine flushes the mailbox command subsystem. It will unconditionally
10251  * flush all the mailbox commands in the three possible stages in the mailbox
10252  * command sub-system: pending mailbox command queue; the outstanding mailbox
10253  * command; and completed mailbox command queue. It is caller's responsibility
10254  * to make sure that the driver is in the proper state to flush the mailbox
10255  * command sub-system. Namely, the posting of mailbox commands into the
10256  * pending mailbox command queue from the various clients must be stopped;
10257  * either the HBA is in a state that it will never works on the outstanding
10258  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10259  * mailbox command has been completed.
10260  **/
10261 static void
10262 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10263 {
10264         LIST_HEAD(completions);
10265         struct lpfc_sli *psli = &phba->sli;
10266         LPFC_MBOXQ_t *pmb;
10267         unsigned long iflag;
10268
10269         /* Flush all the mailbox commands in the mbox system */
10270         spin_lock_irqsave(&phba->hbalock, iflag);
10271         /* The pending mailbox command queue */
10272         list_splice_init(&phba->sli.mboxq, &completions);
10273         /* The outstanding active mailbox command */
10274         if (psli->mbox_active) {
10275                 list_add_tail(&psli->mbox_active->list, &completions);
10276                 psli->mbox_active = NULL;
10277                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10278         }
10279         /* The completed mailbox command queue */
10280         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10281         spin_unlock_irqrestore(&phba->hbalock, iflag);
10282
10283         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10284         while (!list_empty(&completions)) {
10285                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10286                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10287                 if (pmb->mbox_cmpl)
10288                         pmb->mbox_cmpl(phba, pmb);
10289         }
10290 }
10291
10292 /**
10293  * lpfc_sli_host_down - Vport cleanup function
10294  * @vport: Pointer to virtual port object.
10295  *
10296  * lpfc_sli_host_down is called to clean up the resources
10297  * associated with a vport before destroying virtual
10298  * port data structures.
10299  * This function does following operations:
10300  * - Free discovery resources associated with this virtual
10301  *   port.
10302  * - Free iocbs associated with this virtual port in
10303  *   the txq.
10304  * - Send abort for all iocb commands associated with this
10305  *   vport in txcmplq.
10306  *
10307  * This function is called with no lock held and always returns 1.
10308  **/
10309 int
10310 lpfc_sli_host_down(struct lpfc_vport *vport)
10311 {
10312         LIST_HEAD(completions);
10313         struct lpfc_hba *phba = vport->phba;
10314         struct lpfc_sli *psli = &phba->sli;
10315         struct lpfc_queue *qp = NULL;
10316         struct lpfc_sli_ring *pring;
10317         struct lpfc_iocbq *iocb, *next_iocb;
10318         int i;
10319         unsigned long flags = 0;
10320         uint16_t prev_pring_flag;
10321
10322         lpfc_cleanup_discovery_resources(vport);
10323
10324         spin_lock_irqsave(&phba->hbalock, flags);
10325
10326         /*
10327          * Error everything on the txq since these iocbs
10328          * have not been given to the FW yet.
10329          * Also issue ABTS for everything on the txcmplq
10330          */
10331         if (phba->sli_rev != LPFC_SLI_REV4) {
10332                 for (i = 0; i < psli->num_rings; i++) {
10333                         pring = &psli->sli3_ring[i];
10334                         prev_pring_flag = pring->flag;
10335                         /* Only slow rings */
10336                         if (pring->ringno == LPFC_ELS_RING) {
10337                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10338                                 /* Set the lpfc data pending flag */
10339                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10340                         }
10341                         list_for_each_entry_safe(iocb, next_iocb,
10342                                                  &pring->txq, list) {
10343                                 if (iocb->vport != vport)
10344                                         continue;
10345                                 list_move_tail(&iocb->list, &completions);
10346                         }
10347                         list_for_each_entry_safe(iocb, next_iocb,
10348                                                  &pring->txcmplq, list) {
10349                                 if (iocb->vport != vport)
10350                                         continue;
10351                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10352                         }
10353                         pring->flag = prev_pring_flag;
10354                 }
10355         } else {
10356                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10357                         pring = qp->pring;
10358                         if (!pring)
10359                                 continue;
10360                         if (pring == phba->sli4_hba.els_wq->pring) {
10361                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10362                                 /* Set the lpfc data pending flag */
10363                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10364                         }
10365                         prev_pring_flag = pring->flag;
10366                         spin_lock_irq(&pring->ring_lock);
10367                         list_for_each_entry_safe(iocb, next_iocb,
10368                                                  &pring->txq, list) {
10369                                 if (iocb->vport != vport)
10370                                         continue;
10371                                 list_move_tail(&iocb->list, &completions);
10372                         }
10373                         spin_unlock_irq(&pring->ring_lock);
10374                         list_for_each_entry_safe(iocb, next_iocb,
10375                                                  &pring->txcmplq, list) {
10376                                 if (iocb->vport != vport)
10377                                         continue;
10378                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10379                         }
10380                         pring->flag = prev_pring_flag;
10381                 }
10382         }
10383         spin_unlock_irqrestore(&phba->hbalock, flags);
10384
10385         /* Cancel all the IOCBs from the completions list */
10386         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10387                               IOERR_SLI_DOWN);
10388         return 1;
10389 }
10390
10391 /**
10392  * lpfc_sli_hba_down - Resource cleanup function for the HBA
10393  * @phba: Pointer to HBA context object.
10394  *
10395  * This function cleans up all iocb, buffers, mailbox commands
10396  * while shutting down the HBA. This function is called with no
10397  * lock held and always returns 1.
10398  * This function does the following to cleanup driver resources:
10399  * - Free discovery resources for each virtual port
10400  * - Cleanup any pending fabric iocbs
10401  * - Iterate through the iocb txq and free each entry
10402  *   in the list.
10403  * - Free up any buffer posted to the HBA
10404  * - Free mailbox commands in the mailbox queue.
10405  **/
10406 int
10407 lpfc_sli_hba_down(struct lpfc_hba *phba)
10408 {
10409         LIST_HEAD(completions);
10410         struct lpfc_sli *psli = &phba->sli;
10411         struct lpfc_queue *qp = NULL;
10412         struct lpfc_sli_ring *pring;
10413         struct lpfc_dmabuf *buf_ptr;
10414         unsigned long flags = 0;
10415         int i;
10416
10417         /* Shutdown the mailbox command sub-system */
10418         lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
10419
10420         lpfc_hba_down_prep(phba);
10421
10422         lpfc_fabric_abort_hba(phba);
10423
10424         spin_lock_irqsave(&phba->hbalock, flags);
10425
10426         /*
10427          * Error everything on the txq since these iocbs
10428          * have not been given to the FW yet.
10429          */
10430         if (phba->sli_rev != LPFC_SLI_REV4) {
10431                 for (i = 0; i < psli->num_rings; i++) {
10432                         pring = &psli->sli3_ring[i];
10433                         /* Only slow rings */
10434                         if (pring->ringno == LPFC_ELS_RING) {
10435                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10436                                 /* Set the lpfc data pending flag */
10437                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10438                         }
10439                         list_splice_init(&pring->txq, &completions);
10440                 }
10441         } else {
10442                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10443                         pring = qp->pring;
10444                         if (!pring)
10445                                 continue;
10446                         spin_lock_irq(&pring->ring_lock);
10447                         list_splice_init(&pring->txq, &completions);
10448                         spin_unlock_irq(&pring->ring_lock);
10449                         if (pring == phba->sli4_hba.els_wq->pring) {
10450                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10451                                 /* Set the lpfc data pending flag */
10452                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10453                         }
10454                 }
10455         }
10456         spin_unlock_irqrestore(&phba->hbalock, flags);
10457
10458         /* Cancel all the IOCBs from the completions list */
10459         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10460                               IOERR_SLI_DOWN);
10461
10462         spin_lock_irqsave(&phba->hbalock, flags);
10463         list_splice_init(&phba->elsbuf, &completions);
10464         phba->elsbuf_cnt = 0;
10465         phba->elsbuf_prev_cnt = 0;
10466         spin_unlock_irqrestore(&phba->hbalock, flags);
10467
10468         while (!list_empty(&completions)) {
10469                 list_remove_head(&completions, buf_ptr,
10470                         struct lpfc_dmabuf, list);
10471                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10472                 kfree(buf_ptr);
10473         }
10474
10475         /* Return any active mbox cmds */
10476         del_timer_sync(&psli->mbox_tmo);
10477
10478         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
10479         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10480         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
10481
10482         return 1;
10483 }
10484
10485 /**
10486  * lpfc_sli_pcimem_bcopy - SLI memory copy function
10487  * @srcp: Source memory pointer.
10488  * @destp: Destination memory pointer.
10489  * @cnt: Number of words required to be copied.
10490  *
10491  * This function is used for copying data between driver memory
10492  * and the SLI memory. This function also changes the endianness
10493  * of each word if native endianness is different from SLI
10494  * endianness. This function can be called with or without
10495  * lock.
10496  **/
10497 void
10498 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10499 {
10500         uint32_t *src = srcp;
10501         uint32_t *dest = destp;
10502         uint32_t ldata;
10503         int i;
10504
10505         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10506                 ldata = *src;
10507                 ldata = le32_to_cpu(ldata);
10508                 *dest = ldata;
10509                 src++;
10510                 dest++;
10511         }
10512 }
10513
10514
10515 /**
10516  * lpfc_sli_bemem_bcopy - SLI memory copy function
10517  * @srcp: Source memory pointer.
10518  * @destp: Destination memory pointer.
10519  * @cnt: Number of words required to be copied.
10520  *
10521  * This function is used for copying data between a data structure
10522  * with big endian representation to local endianness.
10523  * This function can be called with or without lock.
10524  **/
10525 void
10526 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10527 {
10528         uint32_t *src = srcp;
10529         uint32_t *dest = destp;
10530         uint32_t ldata;
10531         int i;
10532
10533         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10534                 ldata = *src;
10535                 ldata = be32_to_cpu(ldata);
10536                 *dest = ldata;
10537                 src++;
10538                 dest++;
10539         }
10540 }
10541
10542 /**
10543  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
10544  * @phba: Pointer to HBA context object.
10545  * @pring: Pointer to driver SLI ring object.
10546  * @mp: Pointer to driver buffer object.
10547  *
10548  * This function is called with no lock held.
10549  * It always return zero after adding the buffer to the postbufq
10550  * buffer list.
10551  **/
10552 int
10553 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10554                          struct lpfc_dmabuf *mp)
10555 {
10556         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10557            later */
10558         spin_lock_irq(&phba->hbalock);
10559         list_add_tail(&mp->list, &pring->postbufq);
10560         pring->postbufq_cnt++;
10561         spin_unlock_irq(&phba->hbalock);
10562         return 0;
10563 }
10564
10565 /**
10566  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
10567  * @phba: Pointer to HBA context object.
10568  *
10569  * When HBQ is enabled, buffers are searched based on tags. This function
10570  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10571  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10572  * does not conflict with tags of buffer posted for unsolicited events.
10573  * The function returns the allocated tag. The function is called with
10574  * no locks held.
10575  **/
10576 uint32_t
10577 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10578 {
10579         spin_lock_irq(&phba->hbalock);
10580         phba->buffer_tag_count++;
10581         /*
10582          * Always set the QUE_BUFTAG_BIT to distiguish between
10583          * a tag assigned by HBQ.
10584          */
10585         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10586         spin_unlock_irq(&phba->hbalock);
10587         return phba->buffer_tag_count;
10588 }
10589
10590 /**
10591  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
10592  * @phba: Pointer to HBA context object.
10593  * @pring: Pointer to driver SLI ring object.
10594  * @tag: Buffer tag.
10595  *
10596  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10597  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10598  * iocb is posted to the response ring with the tag of the buffer.
10599  * This function searches the pring->postbufq list using the tag
10600  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10601  * iocb. If the buffer is found then lpfc_dmabuf object of the
10602  * buffer is returned to the caller else NULL is returned.
10603  * This function is called with no lock held.
10604  **/
10605 struct lpfc_dmabuf *
10606 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10607                         uint32_t tag)
10608 {
10609         struct lpfc_dmabuf *mp, *next_mp;
10610         struct list_head *slp = &pring->postbufq;
10611
10612         /* Search postbufq, from the beginning, looking for a match on tag */
10613         spin_lock_irq(&phba->hbalock);
10614         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10615                 if (mp->buffer_tag == tag) {
10616                         list_del_init(&mp->list);
10617                         pring->postbufq_cnt--;
10618                         spin_unlock_irq(&phba->hbalock);
10619                         return mp;
10620                 }
10621         }
10622
10623         spin_unlock_irq(&phba->hbalock);
10624         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10625                         "0402 Cannot find virtual addr for buffer tag on "
10626                         "ring %d Data x%lx x%p x%p x%x\n",
10627                         pring->ringno, (unsigned long) tag,
10628                         slp->next, slp->prev, pring->postbufq_cnt);
10629
10630         return NULL;
10631 }
10632
10633 /**
10634  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
10635  * @phba: Pointer to HBA context object.
10636  * @pring: Pointer to driver SLI ring object.
10637  * @phys: DMA address of the buffer.
10638  *
10639  * This function searches the buffer list using the dma_address
10640  * of unsolicited event to find the driver's lpfc_dmabuf object
10641  * corresponding to the dma_address. The function returns the
10642  * lpfc_dmabuf object if a buffer is found else it returns NULL.
10643  * This function is called by the ct and els unsolicited event
10644  * handlers to get the buffer associated with the unsolicited
10645  * event.
10646  *
10647  * This function is called with no lock held.
10648  **/
10649 struct lpfc_dmabuf *
10650 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10651                          dma_addr_t phys)
10652 {
10653         struct lpfc_dmabuf *mp, *next_mp;
10654         struct list_head *slp = &pring->postbufq;
10655
10656         /* Search postbufq, from the beginning, looking for a match on phys */
10657         spin_lock_irq(&phba->hbalock);
10658         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10659                 if (mp->phys == phys) {
10660                         list_del_init(&mp->list);
10661                         pring->postbufq_cnt--;
10662                         spin_unlock_irq(&phba->hbalock);
10663                         return mp;
10664                 }
10665         }
10666
10667         spin_unlock_irq(&phba->hbalock);
10668         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10669                         "0410 Cannot find virtual addr for mapped buf on "
10670                         "ring %d Data x%llx x%p x%p x%x\n",
10671                         pring->ringno, (unsigned long long)phys,
10672                         slp->next, slp->prev, pring->postbufq_cnt);
10673         return NULL;
10674 }
10675
10676 /**
10677  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
10678  * @phba: Pointer to HBA context object.
10679  * @cmdiocb: Pointer to driver command iocb object.
10680  * @rspiocb: Pointer to driver response iocb object.
10681  *
10682  * This function is the completion handler for the abort iocbs for
10683  * ELS commands. This function is called from the ELS ring event
10684  * handler with no lock held. This function frees memory resources
10685  * associated with the abort iocb.
10686  **/
10687 static void
10688 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10689                         struct lpfc_iocbq *rspiocb)
10690 {
10691         IOCB_t *irsp = &rspiocb->iocb;
10692         uint16_t abort_iotag, abort_context;
10693         struct lpfc_iocbq *abort_iocb = NULL;
10694
10695         if (irsp->ulpStatus) {
10696
10697                 /*
10698                  * Assume that the port already completed and returned, or
10699                  * will return the iocb. Just Log the message.
10700                  */
10701                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10702                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10703
10704                 spin_lock_irq(&phba->hbalock);
10705                 if (phba->sli_rev < LPFC_SLI_REV4) {
10706                         if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
10707                             irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
10708                             irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
10709                                 spin_unlock_irq(&phba->hbalock);
10710                                 goto release_iocb;
10711                         }
10712                         if (abort_iotag != 0 &&
10713                                 abort_iotag <= phba->sli.last_iotag)
10714                                 abort_iocb =
10715                                         phba->sli.iocbq_lookup[abort_iotag];
10716                 } else
10717                         /* For sli4 the abort_tag is the XRI,
10718                          * so the abort routine puts the iotag  of the iocb
10719                          * being aborted in the context field of the abort
10720                          * IOCB.
10721                          */
10722                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
10723
10724                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10725                                 "0327 Cannot abort els iocb %p "
10726                                 "with tag %x context %x, abort status %x, "
10727                                 "abort code %x\n",
10728                                 abort_iocb, abort_iotag, abort_context,
10729                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
10730
10731                 spin_unlock_irq(&phba->hbalock);
10732         }
10733 release_iocb:
10734         lpfc_sli_release_iocbq(phba, cmdiocb);
10735         return;
10736 }
10737
10738 /**
10739  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
10740  * @phba: Pointer to HBA context object.
10741  * @cmdiocb: Pointer to driver command iocb object.
10742  * @rspiocb: Pointer to driver response iocb object.
10743  *
10744  * The function is called from SLI ring event handler with no
10745  * lock held. This function is the completion handler for ELS commands
10746  * which are aborted. The function frees memory resources used for
10747  * the aborted ELS commands.
10748  **/
10749 static void
10750 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10751                      struct lpfc_iocbq *rspiocb)
10752 {
10753         IOCB_t *irsp = &rspiocb->iocb;
10754
10755         /* ELS cmd tag <ulpIoTag> completes */
10756         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10757                         "0139 Ignoring ELS cmd tag x%x completion Data: "
10758                         "x%x x%x x%x\n",
10759                         irsp->ulpIoTag, irsp->ulpStatus,
10760                         irsp->un.ulpWord[4], irsp->ulpTimeout);
10761         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10762                 lpfc_ct_free_iocb(phba, cmdiocb);
10763         else
10764                 lpfc_els_free_iocb(phba, cmdiocb);
10765         return;
10766 }
10767
10768 /**
10769  * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
10770  * @phba: Pointer to HBA context object.
10771  * @pring: Pointer to driver SLI ring object.
10772  * @cmdiocb: Pointer to driver command iocb object.
10773  *
10774  * This function issues an abort iocb for the provided command iocb down to
10775  * the port. Other than the case the outstanding command iocb is an abort
10776  * request, this function issues abort out unconditionally. This function is
10777  * called with hbalock held. The function returns 0 when it fails due to
10778  * memory allocation failure or when the command iocb is an abort request.
10779  **/
10780 static int
10781 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10782                            struct lpfc_iocbq *cmdiocb)
10783 {
10784         struct lpfc_vport *vport = cmdiocb->vport;
10785         struct lpfc_iocbq *abtsiocbp;
10786         IOCB_t *icmd = NULL;
10787         IOCB_t *iabt = NULL;
10788         int retval;
10789         unsigned long iflags;
10790         struct lpfc_nodelist *ndlp;
10791
10792         lockdep_assert_held(&phba->hbalock);
10793
10794         /*
10795          * There are certain command types we don't want to abort.  And we
10796          * don't want to abort commands that are already in the process of
10797          * being aborted.
10798          */
10799         icmd = &cmdiocb->iocb;
10800         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10801             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10802             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10803                 return 0;
10804
10805         /* issue ABTS for this IOCB based on iotag */
10806         abtsiocbp = __lpfc_sli_get_iocbq(phba);
10807         if (abtsiocbp == NULL)
10808                 return 0;
10809
10810         /* This signals the response to set the correct status
10811          * before calling the completion handler
10812          */
10813         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10814
10815         iabt = &abtsiocbp->iocb;
10816         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10817         iabt->un.acxri.abortContextTag = icmd->ulpContext;
10818         if (phba->sli_rev == LPFC_SLI_REV4) {
10819                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
10820                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10821         } else {
10822                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
10823                 if (pring->ringno == LPFC_ELS_RING) {
10824                         ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
10825                         iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
10826                 }
10827         }
10828         iabt->ulpLe = 1;
10829         iabt->ulpClass = icmd->ulpClass;
10830
10831         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10832         abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
10833         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10834                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
10835         if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10836                 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
10837
10838         if (phba->link_state >= LPFC_LINK_UP)
10839                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10840         else
10841                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10842
10843         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
10844         abtsiocbp->vport = vport;
10845
10846         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10847                          "0339 Abort xri x%x, original iotag x%x, "
10848                          "abort cmd iotag x%x\n",
10849                          iabt->un.acxri.abortIoTag,
10850                          iabt->un.acxri.abortContextTag,
10851                          abtsiocbp->iotag);
10852
10853         if (phba->sli_rev == LPFC_SLI_REV4) {
10854                 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10855                 if (unlikely(pring == NULL))
10856                         return 0;
10857                 /* Note: both hbalock and ring_lock need to be set here */
10858                 spin_lock_irqsave(&pring->ring_lock, iflags);
10859                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10860                         abtsiocbp, 0);
10861                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10862         } else {
10863                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10864                         abtsiocbp, 0);
10865         }
10866
10867         if (retval)
10868                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
10869
10870         /*
10871          * Caller to this routine should check for IOCB_ERROR
10872          * and handle it properly.  This routine no longer removes
10873          * iocb off txcmplq and call compl in case of IOCB_ERROR.
10874          */
10875         return retval;
10876 }
10877
10878 /**
10879  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10880  * @phba: Pointer to HBA context object.
10881  * @pring: Pointer to driver SLI ring object.
10882  * @cmdiocb: Pointer to driver command iocb object.
10883  *
10884  * This function issues an abort iocb for the provided command iocb. In case
10885  * of unloading, the abort iocb will not be issued to commands on the ELS
10886  * ring. Instead, the callback function shall be changed to those commands
10887  * so that nothing happens when them finishes. This function is called with
10888  * hbalock held. The function returns 0 when the command iocb is an abort
10889  * request.
10890  **/
10891 int
10892 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10893                            struct lpfc_iocbq *cmdiocb)
10894 {
10895         struct lpfc_vport *vport = cmdiocb->vport;
10896         int retval = IOCB_ERROR;
10897         IOCB_t *icmd = NULL;
10898
10899         lockdep_assert_held(&phba->hbalock);
10900
10901         /*
10902          * There are certain command types we don't want to abort.  And we
10903          * don't want to abort commands that are already in the process of
10904          * being aborted.
10905          */
10906         icmd = &cmdiocb->iocb;
10907         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10908             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10909             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10910                 return 0;
10911
10912         if (!pring) {
10913                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10914                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10915                 else
10916                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10917                 goto abort_iotag_exit;
10918         }
10919
10920         /*
10921          * If we're unloading, don't abort iocb on the ELS ring, but change
10922          * the callback so that nothing happens when it finishes.
10923          */
10924         if ((vport->load_flag & FC_UNLOADING) &&
10925             (pring->ringno == LPFC_ELS_RING)) {
10926                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10927                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10928                 else
10929                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10930                 goto abort_iotag_exit;
10931         }
10932
10933         /* Now, we try to issue the abort to the cmdiocb out */
10934         retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10935
10936 abort_iotag_exit:
10937         /*
10938          * Caller to this routine should check for IOCB_ERROR
10939          * and handle it properly.  This routine no longer removes
10940          * iocb off txcmplq and call compl in case of IOCB_ERROR.
10941          */
10942         return retval;
10943 }
10944
10945 /**
10946  * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10947  * @phba: Pointer to HBA context object.
10948  * @pring: Pointer to driver SLI ring object.
10949  * @cmdiocb: Pointer to driver command iocb object.
10950  *
10951  * This function issues an abort iocb for the provided command iocb down to
10952  * the port. Other than the case the outstanding command iocb is an abort
10953  * request, this function issues abort out unconditionally. This function is
10954  * called with hbalock held. The function returns 0 when it fails due to
10955  * memory allocation failure or when the command iocb is an abort request.
10956  **/
10957 static int
10958 lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10959                         struct lpfc_iocbq *cmdiocb)
10960 {
10961         struct lpfc_vport *vport = cmdiocb->vport;
10962         struct lpfc_iocbq *abtsiocbp;
10963         union lpfc_wqe128 *abts_wqe;
10964         int retval;
10965
10966         /*
10967          * There are certain command types we don't want to abort.  And we
10968          * don't want to abort commands that are already in the process of
10969          * being aborted.
10970          */
10971         if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10972             cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10973             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10974                 return 0;
10975
10976         /* issue ABTS for this io based on iotag */
10977         abtsiocbp = __lpfc_sli_get_iocbq(phba);
10978         if (abtsiocbp == NULL)
10979                 return 0;
10980
10981         /* This signals the response to set the correct status
10982          * before calling the completion handler
10983          */
10984         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10985
10986         /* Complete prepping the abort wqe and issue to the FW. */
10987         abts_wqe = &abtsiocbp->wqe;
10988         bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10989         bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10990
10991         /* Explicitly set reserved fields to zero.*/
10992         abts_wqe->abort_cmd.rsrvd4 = 0;
10993         abts_wqe->abort_cmd.rsrvd5 = 0;
10994
10995         /* WQE Common - word 6.  Context is XRI tag.  Set 0. */
10996         bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10997         bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10998
10999         /* word 7 */
11000         bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
11001         bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
11002         bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
11003                cmdiocb->iocb.ulpClass);
11004
11005         /* word 8 - tell the FW to abort the IO associated with this
11006          * outstanding exchange ID.
11007          */
11008         abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
11009
11010         /* word 9 - this is the iotag for the abts_wqe completion. */
11011         bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
11012                abtsiocbp->iotag);
11013
11014         /* word 10 */
11015         bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
11016         bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
11017         bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
11018
11019         /* word 11 */
11020         bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11021         bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
11022         bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
11023
11024         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11025         abtsiocbp->iocb_flag |= LPFC_IO_NVME;
11026         abtsiocbp->vport = vport;
11027         abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
11028         retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
11029         if (retval) {
11030                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11031                                  "6147 Failed abts issue_wqe with status x%x "
11032                                  "for oxid x%x\n",
11033                                  retval, cmdiocb->sli4_xritag);
11034                 lpfc_sli_release_iocbq(phba, abtsiocbp);
11035                 return retval;
11036         }
11037
11038         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11039                          "6148 Drv Abort NVME Request Issued for "
11040                          "ox_id x%x on reqtag x%x\n",
11041                          cmdiocb->sli4_xritag,
11042                          abtsiocbp->iotag);
11043
11044         return retval;
11045 }
11046
11047 /**
11048  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11049  * @phba: pointer to lpfc HBA data structure.
11050  *
11051  * This routine will abort all pending and outstanding iocbs to an HBA.
11052  **/
11053 void
11054 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11055 {
11056         struct lpfc_sli *psli = &phba->sli;
11057         struct lpfc_sli_ring *pring;
11058         struct lpfc_queue *qp = NULL;
11059         int i;
11060
11061         if (phba->sli_rev != LPFC_SLI_REV4) {
11062                 for (i = 0; i < psli->num_rings; i++) {
11063                         pring = &psli->sli3_ring[i];
11064                         lpfc_sli_abort_iocb_ring(phba, pring);
11065                 }
11066                 return;
11067         }
11068         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11069                 pring = qp->pring;
11070                 if (!pring)
11071                         continue;
11072                 lpfc_sli_abort_iocb_ring(phba, pring);
11073         }
11074 }
11075
11076 /**
11077  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
11078  * @iocbq: Pointer to driver iocb object.
11079  * @vport: Pointer to driver virtual port object.
11080  * @tgt_id: SCSI ID of the target.
11081  * @lun_id: LUN ID of the scsi device.
11082  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11083  *
11084  * This function acts as an iocb filter for functions which abort or count
11085  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11086  * 0 if the filtering criteria is met for the given iocb and will return
11087  * 1 if the filtering criteria is not met.
11088  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11089  * given iocb is for the SCSI device specified by vport, tgt_id and
11090  * lun_id parameter.
11091  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
11092  * given iocb is for the SCSI target specified by vport and tgt_id
11093  * parameters.
11094  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11095  * given iocb is for the SCSI host associated with the given vport.
11096  * This function is called with no locks held.
11097  **/
11098 static int
11099 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11100                            uint16_t tgt_id, uint64_t lun_id,
11101                            lpfc_ctx_cmd ctx_cmd)
11102 {
11103         struct lpfc_scsi_buf *lpfc_cmd;
11104         int rc = 1;
11105
11106         if (iocbq->vport != vport)
11107                 return rc;
11108
11109         if (!(iocbq->iocb_flag &  LPFC_IO_FCP) ||
11110             !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
11111                 return rc;
11112
11113         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11114
11115         if (lpfc_cmd->pCmd == NULL)
11116                 return rc;
11117
11118         switch (ctx_cmd) {
11119         case LPFC_CTX_LUN:
11120                 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
11121                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11122                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
11123                         rc = 0;
11124                 break;
11125         case LPFC_CTX_TGT:
11126                 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
11127                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
11128                         rc = 0;
11129                 break;
11130         case LPFC_CTX_HOST:
11131                 rc = 0;
11132                 break;
11133         default:
11134                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
11135                         __func__, ctx_cmd);
11136                 break;
11137         }
11138
11139         return rc;
11140 }
11141
11142 /**
11143  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
11144  * @vport: Pointer to virtual port.
11145  * @tgt_id: SCSI ID of the target.
11146  * @lun_id: LUN ID of the scsi device.
11147  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11148  *
11149  * This function returns number of FCP commands pending for the vport.
11150  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11151  * commands pending on the vport associated with SCSI device specified
11152  * by tgt_id and lun_id parameters.
11153  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11154  * commands pending on the vport associated with SCSI target specified
11155  * by tgt_id parameter.
11156  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11157  * commands pending on the vport.
11158  * This function returns the number of iocbs which satisfy the filter.
11159  * This function is called without any lock held.
11160  **/
11161 int
11162 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11163                   lpfc_ctx_cmd ctx_cmd)
11164 {
11165         struct lpfc_hba *phba = vport->phba;
11166         struct lpfc_iocbq *iocbq;
11167         int sum, i;
11168
11169         spin_lock_irq(&phba->hbalock);
11170         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11171                 iocbq = phba->sli.iocbq_lookup[i];
11172
11173                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11174                                                 ctx_cmd) == 0)
11175                         sum++;
11176         }
11177         spin_unlock_irq(&phba->hbalock);
11178
11179         return sum;
11180 }
11181
11182 /**
11183  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
11184  * @phba: Pointer to HBA context object
11185  * @cmdiocb: Pointer to command iocb object.
11186  * @rspiocb: Pointer to response iocb object.
11187  *
11188  * This function is called when an aborted FCP iocb completes. This
11189  * function is called by the ring event handler with no lock held.
11190  * This function frees the iocb.
11191  **/
11192 void
11193 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11194                         struct lpfc_iocbq *rspiocb)
11195 {
11196         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11197                         "3096 ABORT_XRI_CN completing on rpi x%x "
11198                         "original iotag x%x, abort cmd iotag x%x "
11199                         "status 0x%x, reason 0x%x\n",
11200                         cmdiocb->iocb.un.acxri.abortContextTag,
11201                         cmdiocb->iocb.un.acxri.abortIoTag,
11202                         cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11203                         rspiocb->iocb.un.ulpWord[4]);
11204         lpfc_sli_release_iocbq(phba, cmdiocb);
11205         return;
11206 }
11207
11208 /**
11209  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
11210  * @vport: Pointer to virtual port.
11211  * @pring: Pointer to driver SLI ring object.
11212  * @tgt_id: SCSI ID of the target.
11213  * @lun_id: LUN ID of the scsi device.
11214  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11215  *
11216  * This function sends an abort command for every SCSI command
11217  * associated with the given virtual port pending on the ring
11218  * filtered by lpfc_sli_validate_fcp_iocb function.
11219  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11220  * FCP iocbs associated with lun specified by tgt_id and lun_id
11221  * parameters
11222  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11223  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11224  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11225  * FCP iocbs associated with virtual port.
11226  * This function returns number of iocbs it failed to abort.
11227  * This function is called with no locks held.
11228  **/
11229 int
11230 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11231                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
11232 {
11233         struct lpfc_hba *phba = vport->phba;
11234         struct lpfc_iocbq *iocbq;
11235         struct lpfc_iocbq *abtsiocb;
11236         struct lpfc_sli_ring *pring_s4;
11237         IOCB_t *cmd = NULL;
11238         int errcnt = 0, ret_val = 0;
11239         int i;
11240
11241         /* all I/Os are in process of being flushed */
11242         if (phba->hba_flag & HBA_FCP_IOQ_FLUSH)
11243                 return errcnt;
11244
11245         for (i = 1; i <= phba->sli.last_iotag; i++) {
11246                 iocbq = phba->sli.iocbq_lookup[i];
11247
11248                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11249                                                abort_cmd) != 0)
11250                         continue;
11251
11252                 /*
11253                  * If the iocbq is already being aborted, don't take a second
11254                  * action, but do count it.
11255                  */
11256                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11257                         continue;
11258
11259                 /* issue ABTS for this IOCB based on iotag */
11260                 abtsiocb = lpfc_sli_get_iocbq(phba);
11261                 if (abtsiocb == NULL) {
11262                         errcnt++;
11263                         continue;
11264                 }
11265
11266                 /* indicate the IO is being aborted by the driver. */
11267                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11268
11269                 cmd = &iocbq->iocb;
11270                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11271                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
11272                 if (phba->sli_rev == LPFC_SLI_REV4)
11273                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11274                 else
11275                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
11276                 abtsiocb->iocb.ulpLe = 1;
11277                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
11278                 abtsiocb->vport = vport;
11279
11280                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11281                 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
11282                 if (iocbq->iocb_flag & LPFC_IO_FCP)
11283                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
11284                 if (iocbq->iocb_flag & LPFC_IO_FOF)
11285                         abtsiocb->iocb_flag |= LPFC_IO_FOF;
11286
11287                 if (lpfc_is_link_up(phba))
11288                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11289                 else
11290                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11291
11292                 /* Setup callback routine and issue the command. */
11293                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11294                 if (phba->sli_rev == LPFC_SLI_REV4) {
11295                         pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11296                         if (!pring_s4)
11297                                 continue;
11298                         ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11299                                                       abtsiocb, 0);
11300                 } else
11301                         ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11302                                                       abtsiocb, 0);
11303                 if (ret_val == IOCB_ERROR) {
11304                         lpfc_sli_release_iocbq(phba, abtsiocb);
11305                         errcnt++;
11306                         continue;
11307                 }
11308         }
11309
11310         return errcnt;
11311 }
11312
11313 /**
11314  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11315  * @vport: Pointer to virtual port.
11316  * @pring: Pointer to driver SLI ring object.
11317  * @tgt_id: SCSI ID of the target.
11318  * @lun_id: LUN ID of the scsi device.
11319  * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11320  *
11321  * This function sends an abort command for every SCSI command
11322  * associated with the given virtual port pending on the ring
11323  * filtered by lpfc_sli_validate_fcp_iocb function.
11324  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11325  * FCP iocbs associated with lun specified by tgt_id and lun_id
11326  * parameters
11327  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11328  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11329  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11330  * FCP iocbs associated with virtual port.
11331  * This function returns number of iocbs it aborted .
11332  * This function is called with no locks held right after a taskmgmt
11333  * command is sent.
11334  **/
11335 int
11336 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11337                         uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11338 {
11339         struct lpfc_hba *phba = vport->phba;
11340         struct lpfc_scsi_buf *lpfc_cmd;
11341         struct lpfc_iocbq *abtsiocbq;
11342         struct lpfc_nodelist *ndlp;
11343         struct lpfc_iocbq *iocbq;
11344         IOCB_t *icmd;
11345         int sum, i, ret_val;
11346         unsigned long iflags;
11347         struct lpfc_sli_ring *pring_s4;
11348
11349         spin_lock_irqsave(&phba->hbalock, iflags);
11350
11351         /* all I/Os are in process of being flushed */
11352         if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11353                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11354                 return 0;
11355         }
11356         sum = 0;
11357
11358         for (i = 1; i <= phba->sli.last_iotag; i++) {
11359                 iocbq = phba->sli.iocbq_lookup[i];
11360
11361                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11362                                                cmd) != 0)
11363                         continue;
11364
11365                 /*
11366                  * If the iocbq is already being aborted, don't take a second
11367                  * action, but do count it.
11368                  */
11369                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11370                         continue;
11371
11372                 /* issue ABTS for this IOCB based on iotag */
11373                 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11374                 if (abtsiocbq == NULL)
11375                         continue;
11376
11377                 icmd = &iocbq->iocb;
11378                 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11379                 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11380                 if (phba->sli_rev == LPFC_SLI_REV4)
11381                         abtsiocbq->iocb.un.acxri.abortIoTag =
11382                                                          iocbq->sli4_xritag;
11383                 else
11384                         abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11385                 abtsiocbq->iocb.ulpLe = 1;
11386                 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11387                 abtsiocbq->vport = vport;
11388
11389                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11390                 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
11391                 if (iocbq->iocb_flag & LPFC_IO_FCP)
11392                         abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
11393                 if (iocbq->iocb_flag & LPFC_IO_FOF)
11394                         abtsiocbq->iocb_flag |= LPFC_IO_FOF;
11395
11396                 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11397                 ndlp = lpfc_cmd->rdata->pnode;
11398
11399                 if (lpfc_is_link_up(phba) &&
11400                     (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
11401                         abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11402                 else
11403                         abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11404
11405                 /* Setup callback routine and issue the command. */
11406                 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11407
11408                 /*
11409                  * Indicate the IO is being aborted by the driver and set
11410                  * the caller's flag into the aborted IO.
11411                  */
11412                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11413
11414                 if (phba->sli_rev == LPFC_SLI_REV4) {
11415                         pring_s4 = lpfc_sli4_calc_ring(phba, abtsiocbq);
11416                         if (!pring_s4)
11417                                 continue;
11418                         /* Note: both hbalock and ring_lock must be set here */
11419                         spin_lock(&pring_s4->ring_lock);
11420                         ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11421                                                         abtsiocbq, 0);
11422                         spin_unlock(&pring_s4->ring_lock);
11423                 } else {
11424                         ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11425                                                         abtsiocbq, 0);
11426                 }
11427
11428
11429                 if (ret_val == IOCB_ERROR)
11430                         __lpfc_sli_release_iocbq(phba, abtsiocbq);
11431                 else
11432                         sum++;
11433         }
11434         spin_unlock_irqrestore(&phba->hbalock, iflags);
11435         return sum;
11436 }
11437
11438 /**
11439  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
11440  * @phba: Pointer to HBA context object.
11441  * @cmdiocbq: Pointer to command iocb.
11442  * @rspiocbq: Pointer to response iocb.
11443  *
11444  * This function is the completion handler for iocbs issued using
11445  * lpfc_sli_issue_iocb_wait function. This function is called by the
11446  * ring event handler function without any lock held. This function
11447  * can be called from both worker thread context and interrupt
11448  * context. This function also can be called from other thread which
11449  * cleans up the SLI layer objects.
11450  * This function copy the contents of the response iocb to the
11451  * response iocb memory object provided by the caller of
11452  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11453  * sleeps for the iocb completion.
11454  **/
11455 static void
11456 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11457                         struct lpfc_iocbq *cmdiocbq,
11458                         struct lpfc_iocbq *rspiocbq)
11459 {
11460         wait_queue_head_t *pdone_q;
11461         unsigned long iflags;
11462         struct lpfc_scsi_buf *lpfc_cmd;
11463
11464         spin_lock_irqsave(&phba->hbalock, iflags);
11465         if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11466
11467                 /*
11468                  * A time out has occurred for the iocb.  If a time out
11469                  * completion handler has been supplied, call it.  Otherwise,
11470                  * just free the iocbq.
11471                  */
11472
11473                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11474                 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11475                 cmdiocbq->wait_iocb_cmpl = NULL;
11476                 if (cmdiocbq->iocb_cmpl)
11477                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11478                 else
11479                         lpfc_sli_release_iocbq(phba, cmdiocbq);
11480                 return;
11481         }
11482
11483         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11484         if (cmdiocbq->context2 && rspiocbq)
11485                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11486                        &rspiocbq->iocb, sizeof(IOCB_t));
11487
11488         /* Set the exchange busy flag for task management commands */
11489         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11490                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11491                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11492                         cur_iocbq);
11493                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11494         }
11495
11496         pdone_q = cmdiocbq->context_un.wait_queue;
11497         if (pdone_q)
11498                 wake_up(pdone_q);
11499         spin_unlock_irqrestore(&phba->hbalock, iflags);
11500         return;
11501 }
11502
11503 /**
11504  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11505  * @phba: Pointer to HBA context object..
11506  * @piocbq: Pointer to command iocb.
11507  * @flag: Flag to test.
11508  *
11509  * This routine grabs the hbalock and then test the iocb_flag to
11510  * see if the passed in flag is set.
11511  * Returns:
11512  * 1 if flag is set.
11513  * 0 if flag is not set.
11514  **/
11515 static int
11516 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11517                  struct lpfc_iocbq *piocbq, uint32_t flag)
11518 {
11519         unsigned long iflags;
11520         int ret;
11521
11522         spin_lock_irqsave(&phba->hbalock, iflags);
11523         ret = piocbq->iocb_flag & flag;
11524         spin_unlock_irqrestore(&phba->hbalock, iflags);
11525         return ret;
11526
11527 }
11528
11529 /**
11530  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
11531  * @phba: Pointer to HBA context object..
11532  * @pring: Pointer to sli ring.
11533  * @piocb: Pointer to command iocb.
11534  * @prspiocbq: Pointer to response iocb.
11535  * @timeout: Timeout in number of seconds.
11536  *
11537  * This function issues the iocb to firmware and waits for the
11538  * iocb to complete. The iocb_cmpl field of the shall be used
11539  * to handle iocbs which time out. If the field is NULL, the
11540  * function shall free the iocbq structure.  If more clean up is
11541  * needed, the caller is expected to provide a completion function
11542  * that will provide the needed clean up.  If the iocb command is
11543  * not completed within timeout seconds, the function will either
11544  * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11545  * completion function set in the iocb_cmpl field and then return
11546  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
11547  * resources if this function returns IOCB_TIMEDOUT.
11548  * The function waits for the iocb completion using an
11549  * non-interruptible wait.
11550  * This function will sleep while waiting for iocb completion.
11551  * So, this function should not be called from any context which
11552  * does not allow sleeping. Due to the same reason, this function
11553  * cannot be called with interrupt disabled.
11554  * This function assumes that the iocb completions occur while
11555  * this function sleep. So, this function cannot be called from
11556  * the thread which process iocb completion for this ring.
11557  * This function clears the iocb_flag of the iocb object before
11558  * issuing the iocb and the iocb completion handler sets this
11559  * flag and wakes this thread when the iocb completes.
11560  * The contents of the response iocb will be copied to prspiocbq
11561  * by the completion handler when the command completes.
11562  * This function returns IOCB_SUCCESS when success.
11563  * This function is called with no lock held.
11564  **/
11565 int
11566 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
11567                          uint32_t ring_number,
11568                          struct lpfc_iocbq *piocb,
11569                          struct lpfc_iocbq *prspiocbq,
11570                          uint32_t timeout)
11571 {
11572         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
11573         long timeleft, timeout_req = 0;
11574         int retval = IOCB_SUCCESS;
11575         uint32_t creg_val;
11576         struct lpfc_iocbq *iocb;
11577         int txq_cnt = 0;
11578         int txcmplq_cnt = 0;
11579         struct lpfc_sli_ring *pring;
11580         unsigned long iflags;
11581         bool iocb_completed = true;
11582
11583         if (phba->sli_rev >= LPFC_SLI_REV4)
11584                 pring = lpfc_sli4_calc_ring(phba, piocb);
11585         else
11586                 pring = &phba->sli.sli3_ring[ring_number];
11587         /*
11588          * If the caller has provided a response iocbq buffer, then context2
11589          * is NULL or its an error.
11590          */
11591         if (prspiocbq) {
11592                 if (piocb->context2)
11593                         return IOCB_ERROR;
11594                 piocb->context2 = prspiocbq;
11595         }
11596
11597         piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
11598         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11599         piocb->context_un.wait_queue = &done_q;
11600         piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
11601
11602         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11603                 if (lpfc_readl(phba->HCregaddr, &creg_val))
11604                         return IOCB_ERROR;
11605                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11606                 writel(creg_val, phba->HCregaddr);
11607                 readl(phba->HCregaddr); /* flush */
11608         }
11609
11610         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11611                                      SLI_IOCB_RET_IOCB);
11612         if (retval == IOCB_SUCCESS) {
11613                 timeout_req = msecs_to_jiffies(timeout * 1000);
11614                 timeleft = wait_event_timeout(done_q,
11615                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
11616                                 timeout_req);
11617                 spin_lock_irqsave(&phba->hbalock, iflags);
11618                 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11619
11620                         /*
11621                          * IOCB timed out.  Inform the wake iocb wait
11622                          * completion function and set local status
11623                          */
11624
11625                         iocb_completed = false;
11626                         piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11627                 }
11628                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11629                 if (iocb_completed) {
11630                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11631                                         "0331 IOCB wake signaled\n");
11632                         /* Note: we are not indicating if the IOCB has a success
11633                          * status or not - that's for the caller to check.
11634                          * IOCB_SUCCESS means just that the command was sent and
11635                          * completed. Not that it completed successfully.
11636                          * */
11637                 } else if (timeleft == 0) {
11638                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11639                                         "0338 IOCB wait timeout error - no "
11640                                         "wake response Data x%x\n", timeout);
11641                         retval = IOCB_TIMEDOUT;
11642                 } else {
11643                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11644                                         "0330 IOCB wake NOT set, "
11645                                         "Data x%x x%lx\n",
11646                                         timeout, (timeleft / jiffies));
11647                         retval = IOCB_TIMEDOUT;
11648                 }
11649         } else if (retval == IOCB_BUSY) {
11650                 if (phba->cfg_log_verbose & LOG_SLI) {
11651                         list_for_each_entry(iocb, &pring->txq, list) {
11652                                 txq_cnt++;
11653                         }
11654                         list_for_each_entry(iocb, &pring->txcmplq, list) {
11655                                 txcmplq_cnt++;
11656                         }
11657                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11658                                 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11659                                 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11660                 }
11661                 return retval;
11662         } else {
11663                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11664                                 "0332 IOCB wait issue failed, Data x%x\n",
11665                                 retval);
11666                 retval = IOCB_ERROR;
11667         }
11668
11669         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11670                 if (lpfc_readl(phba->HCregaddr, &creg_val))
11671                         return IOCB_ERROR;
11672                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11673                 writel(creg_val, phba->HCregaddr);
11674                 readl(phba->HCregaddr); /* flush */
11675         }
11676
11677         if (prspiocbq)
11678                 piocb->context2 = NULL;
11679
11680         piocb->context_un.wait_queue = NULL;
11681         piocb->iocb_cmpl = NULL;
11682         return retval;
11683 }
11684
11685 /**
11686  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
11687  * @phba: Pointer to HBA context object.
11688  * @pmboxq: Pointer to driver mailbox object.
11689  * @timeout: Timeout in number of seconds.
11690  *
11691  * This function issues the mailbox to firmware and waits for the
11692  * mailbox command to complete. If the mailbox command is not
11693  * completed within timeout seconds, it returns MBX_TIMEOUT.
11694  * The function waits for the mailbox completion using an
11695  * interruptible wait. If the thread is woken up due to a
11696  * signal, MBX_TIMEOUT error is returned to the caller. Caller
11697  * should not free the mailbox resources, if this function returns
11698  * MBX_TIMEOUT.
11699  * This function will sleep while waiting for mailbox completion.
11700  * So, this function should not be called from any context which
11701  * does not allow sleeping. Due to the same reason, this function
11702  * cannot be called with interrupt disabled.
11703  * This function assumes that the mailbox completion occurs while
11704  * this function sleep. So, this function cannot be called from
11705  * the worker thread which processes mailbox completion.
11706  * This function is called in the context of HBA management
11707  * applications.
11708  * This function returns MBX_SUCCESS when successful.
11709  * This function is called with no lock held.
11710  **/
11711 int
11712 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
11713                          uint32_t timeout)
11714 {
11715         struct completion mbox_done;
11716         int retval;
11717         unsigned long flag;
11718
11719         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
11720         /* setup wake call as IOCB callback */
11721         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11722
11723         /* setup context3 field to pass wait_queue pointer to wake function  */
11724         init_completion(&mbox_done);
11725         pmboxq->context3 = &mbox_done;
11726         /* now issue the command */
11727         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
11728         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
11729                 wait_for_completion_timeout(&mbox_done,
11730                                             msecs_to_jiffies(timeout * 1000));
11731
11732                 spin_lock_irqsave(&phba->hbalock, flag);
11733                 pmboxq->context3 = NULL;
11734                 /*
11735                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
11736                  * else do not free the resources.
11737                  */
11738                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
11739                         retval = MBX_SUCCESS;
11740                 } else {
11741                         retval = MBX_TIMEOUT;
11742                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11743                 }
11744                 spin_unlock_irqrestore(&phba->hbalock, flag);
11745         }
11746         return retval;
11747 }
11748
11749 /**
11750  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
11751  * @phba: Pointer to HBA context.
11752  *
11753  * This function is called to shutdown the driver's mailbox sub-system.
11754  * It first marks the mailbox sub-system is in a block state to prevent
11755  * the asynchronous mailbox command from issued off the pending mailbox
11756  * command queue. If the mailbox command sub-system shutdown is due to
11757  * HBA error conditions such as EEH or ERATT, this routine shall invoke
11758  * the mailbox sub-system flush routine to forcefully bring down the
11759  * mailbox sub-system. Otherwise, if it is due to normal condition (such
11760  * as with offline or HBA function reset), this routine will wait for the
11761  * outstanding mailbox command to complete before invoking the mailbox
11762  * sub-system flush routine to gracefully bring down mailbox sub-system.
11763  **/
11764 void
11765 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
11766 {
11767         struct lpfc_sli *psli = &phba->sli;
11768         unsigned long timeout;
11769
11770         if (mbx_action == LPFC_MBX_NO_WAIT) {
11771                 /* delay 100ms for port state */
11772                 msleep(100);
11773                 lpfc_sli_mbox_sys_flush(phba);
11774                 return;
11775         }
11776         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
11777
11778         spin_lock_irq(&phba->hbalock);
11779         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
11780
11781         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
11782                 /* Determine how long we might wait for the active mailbox
11783                  * command to be gracefully completed by firmware.
11784                  */
11785                 if (phba->sli.mbox_active)
11786                         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11787                                                 phba->sli.mbox_active) *
11788                                                 1000) + jiffies;
11789                 spin_unlock_irq(&phba->hbalock);
11790
11791                 while (phba->sli.mbox_active) {
11792                         /* Check active mailbox complete status every 2ms */
11793                         msleep(2);
11794                         if (time_after(jiffies, timeout))
11795                                 /* Timeout, let the mailbox flush routine to
11796                                  * forcefully release active mailbox command
11797                                  */
11798                                 break;
11799                 }
11800         } else
11801                 spin_unlock_irq(&phba->hbalock);
11802
11803         lpfc_sli_mbox_sys_flush(phba);
11804 }
11805
11806 /**
11807  * lpfc_sli_eratt_read - read sli-3 error attention events
11808  * @phba: Pointer to HBA context.
11809  *
11810  * This function is called to read the SLI3 device error attention registers
11811  * for possible error attention events. The caller must hold the hostlock
11812  * with spin_lock_irq().
11813  *
11814  * This function returns 1 when there is Error Attention in the Host Attention
11815  * Register and returns 0 otherwise.
11816  **/
11817 static int
11818 lpfc_sli_eratt_read(struct lpfc_hba *phba)
11819 {
11820         uint32_t ha_copy;
11821
11822         /* Read chip Host Attention (HA) register */
11823         if (lpfc_readl(phba->HAregaddr, &ha_copy))
11824                 goto unplug_err;
11825
11826         if (ha_copy & HA_ERATT) {
11827                 /* Read host status register to retrieve error event */
11828                 if (lpfc_sli_read_hs(phba))
11829                         goto unplug_err;
11830
11831                 /* Check if there is a deferred error condition is active */
11832                 if ((HS_FFER1 & phba->work_hs) &&
11833                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
11834                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
11835                         phba->hba_flag |= DEFER_ERATT;
11836                         /* Clear all interrupt enable conditions */
11837                         writel(0, phba->HCregaddr);
11838                         readl(phba->HCregaddr);
11839                 }
11840
11841                 /* Set the driver HA work bitmap */
11842                 phba->work_ha |= HA_ERATT;
11843                 /* Indicate polling handles this ERATT */
11844                 phba->hba_flag |= HBA_ERATT_HANDLED;
11845                 return 1;
11846         }
11847         return 0;
11848
11849 unplug_err:
11850         /* Set the driver HS work bitmap */
11851         phba->work_hs |= UNPLUG_ERR;
11852         /* Set the driver HA work bitmap */
11853         phba->work_ha |= HA_ERATT;
11854         /* Indicate polling handles this ERATT */
11855         phba->hba_flag |= HBA_ERATT_HANDLED;
11856         return 1;
11857 }
11858
11859 /**
11860  * lpfc_sli4_eratt_read - read sli-4 error attention events
11861  * @phba: Pointer to HBA context.
11862  *
11863  * This function is called to read the SLI4 device error attention registers
11864  * for possible error attention events. The caller must hold the hostlock
11865  * with spin_lock_irq().
11866  *
11867  * This function returns 1 when there is Error Attention in the Host Attention
11868  * Register and returns 0 otherwise.
11869  **/
11870 static int
11871 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11872 {
11873         uint32_t uerr_sta_hi, uerr_sta_lo;
11874         uint32_t if_type, portsmphr;
11875         struct lpfc_register portstat_reg;
11876
11877         /*
11878          * For now, use the SLI4 device internal unrecoverable error
11879          * registers for error attention. This can be changed later.
11880          */
11881         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11882         switch (if_type) {
11883         case LPFC_SLI_INTF_IF_TYPE_0:
11884                 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11885                         &uerr_sta_lo) ||
11886                         lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11887                         &uerr_sta_hi)) {
11888                         phba->work_hs |= UNPLUG_ERR;
11889                         phba->work_ha |= HA_ERATT;
11890                         phba->hba_flag |= HBA_ERATT_HANDLED;
11891                         return 1;
11892                 }
11893                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11894                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11895                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11896                                         "1423 HBA Unrecoverable error: "
11897                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11898                                         "ue_mask_lo_reg=0x%x, "
11899                                         "ue_mask_hi_reg=0x%x\n",
11900                                         uerr_sta_lo, uerr_sta_hi,
11901                                         phba->sli4_hba.ue_mask_lo,
11902                                         phba->sli4_hba.ue_mask_hi);
11903                         phba->work_status[0] = uerr_sta_lo;
11904                         phba->work_status[1] = uerr_sta_hi;
11905                         phba->work_ha |= HA_ERATT;
11906                         phba->hba_flag |= HBA_ERATT_HANDLED;
11907                         return 1;
11908                 }
11909                 break;
11910         case LPFC_SLI_INTF_IF_TYPE_2:
11911         case LPFC_SLI_INTF_IF_TYPE_6:
11912                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11913                         &portstat_reg.word0) ||
11914                         lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11915                         &portsmphr)){
11916                         phba->work_hs |= UNPLUG_ERR;
11917                         phba->work_ha |= HA_ERATT;
11918                         phba->hba_flag |= HBA_ERATT_HANDLED;
11919                         return 1;
11920                 }
11921                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11922                         phba->work_status[0] =
11923                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11924                         phba->work_status[1] =
11925                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11926                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11927                                         "2885 Port Status Event: "
11928                                         "port status reg 0x%x, "
11929                                         "port smphr reg 0x%x, "
11930                                         "error 1=0x%x, error 2=0x%x\n",
11931                                         portstat_reg.word0,
11932                                         portsmphr,
11933                                         phba->work_status[0],
11934                                         phba->work_status[1]);
11935                         phba->work_ha |= HA_ERATT;
11936                         phba->hba_flag |= HBA_ERATT_HANDLED;
11937                         return 1;
11938                 }
11939                 break;
11940         case LPFC_SLI_INTF_IF_TYPE_1:
11941         default:
11942                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11943                                 "2886 HBA Error Attention on unsupported "
11944                                 "if type %d.", if_type);
11945                 return 1;
11946         }
11947
11948         return 0;
11949 }
11950
11951 /**
11952  * lpfc_sli_check_eratt - check error attention events
11953  * @phba: Pointer to HBA context.
11954  *
11955  * This function is called from timer soft interrupt context to check HBA's
11956  * error attention register bit for error attention events.
11957  *
11958  * This function returns 1 when there is Error Attention in the Host Attention
11959  * Register and returns 0 otherwise.
11960  **/
11961 int
11962 lpfc_sli_check_eratt(struct lpfc_hba *phba)
11963 {
11964         uint32_t ha_copy;
11965
11966         /* If somebody is waiting to handle an eratt, don't process it
11967          * here. The brdkill function will do this.
11968          */
11969         if (phba->link_flag & LS_IGNORE_ERATT)
11970                 return 0;
11971
11972         /* Check if interrupt handler handles this ERATT */
11973         spin_lock_irq(&phba->hbalock);
11974         if (phba->hba_flag & HBA_ERATT_HANDLED) {
11975                 /* Interrupt handler has handled ERATT */
11976                 spin_unlock_irq(&phba->hbalock);
11977                 return 0;
11978         }
11979
11980         /*
11981          * If there is deferred error attention, do not check for error
11982          * attention
11983          */
11984         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11985                 spin_unlock_irq(&phba->hbalock);
11986                 return 0;
11987         }
11988
11989         /* If PCI channel is offline, don't process it */
11990         if (unlikely(pci_channel_offline(phba->pcidev))) {
11991                 spin_unlock_irq(&phba->hbalock);
11992                 return 0;
11993         }
11994
11995         switch (phba->sli_rev) {
11996         case LPFC_SLI_REV2:
11997         case LPFC_SLI_REV3:
11998                 /* Read chip Host Attention (HA) register */
11999                 ha_copy = lpfc_sli_eratt_read(phba);
12000                 break;
12001         case LPFC_SLI_REV4:
12002                 /* Read device Uncoverable Error (UERR) registers */
12003                 ha_copy = lpfc_sli4_eratt_read(phba);
12004                 break;
12005         default:
12006                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12007                                 "0299 Invalid SLI revision (%d)\n",
12008                                 phba->sli_rev);
12009                 ha_copy = 0;
12010                 break;
12011         }
12012         spin_unlock_irq(&phba->hbalock);
12013
12014         return ha_copy;
12015 }
12016
12017 /**
12018  * lpfc_intr_state_check - Check device state for interrupt handling
12019  * @phba: Pointer to HBA context.
12020  *
12021  * This inline routine checks whether a device or its PCI slot is in a state
12022  * that the interrupt should be handled.
12023  *
12024  * This function returns 0 if the device or the PCI slot is in a state that
12025  * interrupt should be handled, otherwise -EIO.
12026  */
12027 static inline int
12028 lpfc_intr_state_check(struct lpfc_hba *phba)
12029 {
12030         /* If the pci channel is offline, ignore all the interrupts */
12031         if (unlikely(pci_channel_offline(phba->pcidev)))
12032                 return -EIO;
12033
12034         /* Update device level interrupt statistics */
12035         phba->sli.slistat.sli_intr++;
12036
12037         /* Ignore all interrupts during initialization. */
12038         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12039                 return -EIO;
12040
12041         return 0;
12042 }
12043
12044 /**
12045  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
12046  * @irq: Interrupt number.
12047  * @dev_id: The device context pointer.
12048  *
12049  * This function is directly called from the PCI layer as an interrupt
12050  * service routine when device with SLI-3 interface spec is enabled with
12051  * MSI-X multi-message interrupt mode and there are slow-path events in
12052  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12053  * interrupt mode, this function is called as part of the device-level
12054  * interrupt handler. When the PCI slot is in error recovery or the HBA
12055  * is undergoing initialization, the interrupt handler will not process
12056  * the interrupt. The link attention and ELS ring attention events are
12057  * handled by the worker thread. The interrupt handler signals the worker
12058  * thread and returns for these events. This function is called without
12059  * any lock held. It gets the hbalock to access and update SLI data
12060  * structures.
12061  *
12062  * This function returns IRQ_HANDLED when interrupt is handled else it
12063  * returns IRQ_NONE.
12064  **/
12065 irqreturn_t
12066 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
12067 {
12068         struct lpfc_hba  *phba;
12069         uint32_t ha_copy, hc_copy;
12070         uint32_t work_ha_copy;
12071         unsigned long status;
12072         unsigned long iflag;
12073         uint32_t control;
12074
12075         MAILBOX_t *mbox, *pmbox;
12076         struct lpfc_vport *vport;
12077         struct lpfc_nodelist *ndlp;
12078         struct lpfc_dmabuf *mp;
12079         LPFC_MBOXQ_t *pmb;
12080         int rc;
12081
12082         /*
12083          * Get the driver's phba structure from the dev_id and
12084          * assume the HBA is not interrupting.
12085          */
12086         phba = (struct lpfc_hba *)dev_id;
12087
12088         if (unlikely(!phba))
12089                 return IRQ_NONE;
12090
12091         /*
12092          * Stuff needs to be attented to when this function is invoked as an
12093          * individual interrupt handler in MSI-X multi-message interrupt mode
12094          */
12095         if (phba->intr_type == MSIX) {
12096                 /* Check device state for handling interrupt */
12097                 if (lpfc_intr_state_check(phba))
12098                         return IRQ_NONE;
12099                 /* Need to read HA REG for slow-path events */
12100                 spin_lock_irqsave(&phba->hbalock, iflag);
12101                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12102                         goto unplug_error;
12103                 /* If somebody is waiting to handle an eratt don't process it
12104                  * here. The brdkill function will do this.
12105                  */
12106                 if (phba->link_flag & LS_IGNORE_ERATT)
12107                         ha_copy &= ~HA_ERATT;
12108                 /* Check the need for handling ERATT in interrupt handler */
12109                 if (ha_copy & HA_ERATT) {
12110                         if (phba->hba_flag & HBA_ERATT_HANDLED)
12111                                 /* ERATT polling has handled ERATT */
12112                                 ha_copy &= ~HA_ERATT;
12113                         else
12114                                 /* Indicate interrupt handler handles ERATT */
12115                                 phba->hba_flag |= HBA_ERATT_HANDLED;
12116                 }
12117
12118                 /*
12119                  * If there is deferred error attention, do not check for any
12120                  * interrupt.
12121                  */
12122                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12123                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12124                         return IRQ_NONE;
12125                 }
12126
12127                 /* Clear up only attention source related to slow-path */
12128                 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12129                         goto unplug_error;
12130
12131                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12132                         HC_LAINT_ENA | HC_ERINT_ENA),
12133                         phba->HCregaddr);
12134                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12135                         phba->HAregaddr);
12136                 writel(hc_copy, phba->HCregaddr);
12137                 readl(phba->HAregaddr); /* flush */
12138                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12139         } else
12140                 ha_copy = phba->ha_copy;
12141
12142         work_ha_copy = ha_copy & phba->work_ha_mask;
12143
12144         if (work_ha_copy) {
12145                 if (work_ha_copy & HA_LATT) {
12146                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12147                                 /*
12148                                  * Turn off Link Attention interrupts
12149                                  * until CLEAR_LA done
12150                                  */
12151                                 spin_lock_irqsave(&phba->hbalock, iflag);
12152                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
12153                                 if (lpfc_readl(phba->HCregaddr, &control))
12154                                         goto unplug_error;
12155                                 control &= ~HC_LAINT_ENA;
12156                                 writel(control, phba->HCregaddr);
12157                                 readl(phba->HCregaddr); /* flush */
12158                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12159                         }
12160                         else
12161                                 work_ha_copy &= ~HA_LATT;
12162                 }
12163
12164                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
12165                         /*
12166                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12167                          * the only slow ring.
12168                          */
12169                         status = (work_ha_copy &
12170                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
12171                         status >>= (4*LPFC_ELS_RING);
12172                         if (status & HA_RXMASK) {
12173                                 spin_lock_irqsave(&phba->hbalock, iflag);
12174                                 if (lpfc_readl(phba->HCregaddr, &control))
12175                                         goto unplug_error;
12176
12177                                 lpfc_debugfs_slow_ring_trc(phba,
12178                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
12179                                 control, status,
12180                                 (uint32_t)phba->sli.slistat.sli_intr);
12181
12182                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
12183                                         lpfc_debugfs_slow_ring_trc(phba,
12184                                                 "ISR Disable ring:"
12185                                                 "pwork:x%x hawork:x%x wait:x%x",
12186                                                 phba->work_ha, work_ha_copy,
12187                                                 (uint32_t)((unsigned long)
12188                                                 &phba->work_waitq));
12189
12190                                         control &=
12191                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
12192                                         writel(control, phba->HCregaddr);
12193                                         readl(phba->HCregaddr); /* flush */
12194                                 }
12195                                 else {
12196                                         lpfc_debugfs_slow_ring_trc(phba,
12197                                                 "ISR slow ring:   pwork:"
12198                                                 "x%x hawork:x%x wait:x%x",
12199                                                 phba->work_ha, work_ha_copy,
12200                                                 (uint32_t)((unsigned long)
12201                                                 &phba->work_waitq));
12202                                 }
12203                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12204                         }
12205                 }
12206                 spin_lock_irqsave(&phba->hbalock, iflag);
12207                 if (work_ha_copy & HA_ERATT) {
12208                         if (lpfc_sli_read_hs(phba))
12209                                 goto unplug_error;
12210                         /*
12211                          * Check if there is a deferred error condition
12212                          * is active
12213                          */
12214                         if ((HS_FFER1 & phba->work_hs) &&
12215                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
12216                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
12217                                   phba->work_hs)) {
12218                                 phba->hba_flag |= DEFER_ERATT;
12219                                 /* Clear all interrupt enable conditions */
12220                                 writel(0, phba->HCregaddr);
12221                                 readl(phba->HCregaddr);
12222                         }
12223                 }
12224
12225                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
12226                         pmb = phba->sli.mbox_active;
12227                         pmbox = &pmb->u.mb;
12228                         mbox = phba->mbox;
12229                         vport = pmb->vport;
12230
12231                         /* First check out the status word */
12232                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12233                         if (pmbox->mbxOwner != OWN_HOST) {
12234                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12235                                 /*
12236                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
12237                                  * mbxStatus <status>
12238                                  */
12239                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12240                                                 LOG_SLI,
12241                                                 "(%d):0304 Stray Mailbox "
12242                                                 "Interrupt mbxCommand x%x "
12243                                                 "mbxStatus x%x\n",
12244                                                 (vport ? vport->vpi : 0),
12245                                                 pmbox->mbxCommand,
12246                                                 pmbox->mbxStatus);
12247                                 /* clear mailbox attention bit */
12248                                 work_ha_copy &= ~HA_MBATT;
12249                         } else {
12250                                 phba->sli.mbox_active = NULL;
12251                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12252                                 phba->last_completion_time = jiffies;
12253                                 del_timer(&phba->sli.mbox_tmo);
12254                                 if (pmb->mbox_cmpl) {
12255                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
12256                                                         MAILBOX_CMD_SIZE);
12257                                         if (pmb->out_ext_byte_len &&
12258                                                 pmb->context2)
12259                                                 lpfc_sli_pcimem_bcopy(
12260                                                 phba->mbox_ext,
12261                                                 pmb->context2,
12262                                                 pmb->out_ext_byte_len);
12263                                 }
12264                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12265                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12266
12267                                         lpfc_debugfs_disc_trc(vport,
12268                                                 LPFC_DISC_TRC_MBOX_VPORT,
12269                                                 "MBOX dflt rpi: : "
12270                                                 "status:x%x rpi:x%x",
12271                                                 (uint32_t)pmbox->mbxStatus,
12272                                                 pmbox->un.varWords[0], 0);
12273
12274                                         if (!pmbox->mbxStatus) {
12275                                                 mp = (struct lpfc_dmabuf *)
12276                                                         (pmb->context1);
12277                                                 ndlp = (struct lpfc_nodelist *)
12278                                                         pmb->context2;
12279
12280                                                 /* Reg_LOGIN of dflt RPI was
12281                                                  * successful. new lets get
12282                                                  * rid of the RPI using the
12283                                                  * same mbox buffer.
12284                                                  */
12285                                                 lpfc_unreg_login(phba,
12286                                                         vport->vpi,
12287                                                         pmbox->un.varWords[0],
12288                                                         pmb);
12289                                                 pmb->mbox_cmpl =
12290                                                         lpfc_mbx_cmpl_dflt_rpi;
12291                                                 pmb->context1 = mp;
12292                                                 pmb->context2 = ndlp;
12293                                                 pmb->vport = vport;
12294                                                 rc = lpfc_sli_issue_mbox(phba,
12295                                                                 pmb,
12296                                                                 MBX_NOWAIT);
12297                                                 if (rc != MBX_BUSY)
12298                                                         lpfc_printf_log(phba,
12299                                                         KERN_ERR,
12300                                                         LOG_MBOX | LOG_SLI,
12301                                                         "0350 rc should have"
12302                                                         "been MBX_BUSY\n");
12303                                                 if (rc != MBX_NOT_FINISHED)
12304                                                         goto send_current_mbox;
12305                                         }
12306                                 }
12307                                 spin_lock_irqsave(
12308                                                 &phba->pport->work_port_lock,
12309                                                 iflag);
12310                                 phba->pport->work_port_events &=
12311                                         ~WORKER_MBOX_TMO;
12312                                 spin_unlock_irqrestore(
12313                                                 &phba->pport->work_port_lock,
12314                                                 iflag);
12315                                 lpfc_mbox_cmpl_put(phba, pmb);
12316                         }
12317                 } else
12318                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12319
12320                 if ((work_ha_copy & HA_MBATT) &&
12321                     (phba->sli.mbox_active == NULL)) {
12322 send_current_mbox:
12323                         /* Process next mailbox command if there is one */
12324                         do {
12325                                 rc = lpfc_sli_issue_mbox(phba, NULL,
12326                                                          MBX_NOWAIT);
12327                         } while (rc == MBX_NOT_FINISHED);
12328                         if (rc != MBX_SUCCESS)
12329                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12330                                                 LOG_SLI, "0349 rc should be "
12331                                                 "MBX_SUCCESS\n");
12332                 }
12333
12334                 spin_lock_irqsave(&phba->hbalock, iflag);
12335                 phba->work_ha |= work_ha_copy;
12336                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12337                 lpfc_worker_wake_up(phba);
12338         }
12339         return IRQ_HANDLED;
12340 unplug_error:
12341         spin_unlock_irqrestore(&phba->hbalock, iflag);
12342         return IRQ_HANDLED;
12343
12344 } /* lpfc_sli_sp_intr_handler */
12345
12346 /**
12347  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
12348  * @irq: Interrupt number.
12349  * @dev_id: The device context pointer.
12350  *
12351  * This function is directly called from the PCI layer as an interrupt
12352  * service routine when device with SLI-3 interface spec is enabled with
12353  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12354  * ring event in the HBA. However, when the device is enabled with either
12355  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12356  * device-level interrupt handler. When the PCI slot is in error recovery
12357  * or the HBA is undergoing initialization, the interrupt handler will not
12358  * process the interrupt. The SCSI FCP fast-path ring event are handled in
12359  * the intrrupt context. This function is called without any lock held.
12360  * It gets the hbalock to access and update SLI data structures.
12361  *
12362  * This function returns IRQ_HANDLED when interrupt is handled else it
12363  * returns IRQ_NONE.
12364  **/
12365 irqreturn_t
12366 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
12367 {
12368         struct lpfc_hba  *phba;
12369         uint32_t ha_copy;
12370         unsigned long status;
12371         unsigned long iflag;
12372         struct lpfc_sli_ring *pring;
12373
12374         /* Get the driver's phba structure from the dev_id and
12375          * assume the HBA is not interrupting.
12376          */
12377         phba = (struct lpfc_hba *) dev_id;
12378
12379         if (unlikely(!phba))
12380                 return IRQ_NONE;
12381
12382         /*
12383          * Stuff needs to be attented to when this function is invoked as an
12384          * individual interrupt handler in MSI-X multi-message interrupt mode
12385          */
12386         if (phba->intr_type == MSIX) {
12387                 /* Check device state for handling interrupt */
12388                 if (lpfc_intr_state_check(phba))
12389                         return IRQ_NONE;
12390                 /* Need to read HA REG for FCP ring and other ring events */
12391                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12392                         return IRQ_HANDLED;
12393                 /* Clear up only attention source related to fast-path */
12394                 spin_lock_irqsave(&phba->hbalock, iflag);
12395                 /*
12396                  * If there is deferred error attention, do not check for
12397                  * any interrupt.
12398                  */
12399                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12400                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12401                         return IRQ_NONE;
12402                 }
12403                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12404                         phba->HAregaddr);
12405                 readl(phba->HAregaddr); /* flush */
12406                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12407         } else
12408                 ha_copy = phba->ha_copy;
12409
12410         /*
12411          * Process all events on FCP ring. Take the optimized path for FCP IO.
12412          */
12413         ha_copy &= ~(phba->work_ha_mask);
12414
12415         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12416         status >>= (4*LPFC_FCP_RING);
12417         pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12418         if (status & HA_RXMASK)
12419                 lpfc_sli_handle_fast_ring_event(phba, pring, status);
12420
12421         if (phba->cfg_multi_ring_support == 2) {
12422                 /*
12423                  * Process all events on extra ring. Take the optimized path
12424                  * for extra ring IO.
12425                  */
12426                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12427                 status >>= (4*LPFC_EXTRA_RING);
12428                 if (status & HA_RXMASK) {
12429                         lpfc_sli_handle_fast_ring_event(phba,
12430                                         &phba->sli.sli3_ring[LPFC_EXTRA_RING],
12431                                         status);
12432                 }
12433         }
12434         return IRQ_HANDLED;
12435 }  /* lpfc_sli_fp_intr_handler */
12436
12437 /**
12438  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
12439  * @irq: Interrupt number.
12440  * @dev_id: The device context pointer.
12441  *
12442  * This function is the HBA device-level interrupt handler to device with
12443  * SLI-3 interface spec, called from the PCI layer when either MSI or
12444  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12445  * requires driver attention. This function invokes the slow-path interrupt
12446  * attention handling function and fast-path interrupt attention handling
12447  * function in turn to process the relevant HBA attention events. This
12448  * function is called without any lock held. It gets the hbalock to access
12449  * and update SLI data structures.
12450  *
12451  * This function returns IRQ_HANDLED when interrupt is handled, else it
12452  * returns IRQ_NONE.
12453  **/
12454 irqreturn_t
12455 lpfc_sli_intr_handler(int irq, void *dev_id)
12456 {
12457         struct lpfc_hba  *phba;
12458         irqreturn_t sp_irq_rc, fp_irq_rc;
12459         unsigned long status1, status2;
12460         uint32_t hc_copy;
12461
12462         /*
12463          * Get the driver's phba structure from the dev_id and
12464          * assume the HBA is not interrupting.
12465          */
12466         phba = (struct lpfc_hba *) dev_id;
12467
12468         if (unlikely(!phba))
12469                 return IRQ_NONE;
12470
12471         /* Check device state for handling interrupt */
12472         if (lpfc_intr_state_check(phba))
12473                 return IRQ_NONE;
12474
12475         spin_lock(&phba->hbalock);
12476         if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12477                 spin_unlock(&phba->hbalock);
12478                 return IRQ_HANDLED;
12479         }
12480
12481         if (unlikely(!phba->ha_copy)) {
12482                 spin_unlock(&phba->hbalock);
12483                 return IRQ_NONE;
12484         } else if (phba->ha_copy & HA_ERATT) {
12485                 if (phba->hba_flag & HBA_ERATT_HANDLED)
12486                         /* ERATT polling has handled ERATT */
12487                         phba->ha_copy &= ~HA_ERATT;
12488                 else
12489                         /* Indicate interrupt handler handles ERATT */
12490                         phba->hba_flag |= HBA_ERATT_HANDLED;
12491         }
12492
12493         /*
12494          * If there is deferred error attention, do not check for any interrupt.
12495          */
12496         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12497                 spin_unlock(&phba->hbalock);
12498                 return IRQ_NONE;
12499         }
12500
12501         /* Clear attention sources except link and error attentions */
12502         if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12503                 spin_unlock(&phba->hbalock);
12504                 return IRQ_HANDLED;
12505         }
12506         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12507                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12508                 phba->HCregaddr);
12509         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
12510         writel(hc_copy, phba->HCregaddr);
12511         readl(phba->HAregaddr); /* flush */
12512         spin_unlock(&phba->hbalock);
12513
12514         /*
12515          * Invokes slow-path host attention interrupt handling as appropriate.
12516          */
12517
12518         /* status of events with mailbox and link attention */
12519         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12520
12521         /* status of events with ELS ring */
12522         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
12523         status2 >>= (4*LPFC_ELS_RING);
12524
12525         if (status1 || (status2 & HA_RXMASK))
12526                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
12527         else
12528                 sp_irq_rc = IRQ_NONE;
12529
12530         /*
12531          * Invoke fast-path host attention interrupt handling as appropriate.
12532          */
12533
12534         /* status of events with FCP ring */
12535         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12536         status1 >>= (4*LPFC_FCP_RING);
12537
12538         /* status of events with extra ring */
12539         if (phba->cfg_multi_ring_support == 2) {
12540                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12541                 status2 >>= (4*LPFC_EXTRA_RING);
12542         } else
12543                 status2 = 0;
12544
12545         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
12546                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
12547         else
12548                 fp_irq_rc = IRQ_NONE;
12549
12550         /* Return device-level interrupt handling status */
12551         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
12552 }  /* lpfc_sli_intr_handler */
12553
12554 /**
12555  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12556  * @phba: pointer to lpfc hba data structure.
12557  *
12558  * This routine is invoked by the worker thread to process all the pending
12559  * SLI4 FCP abort XRI events.
12560  **/
12561 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12562 {
12563         struct lpfc_cq_event *cq_event;
12564
12565         /* First, declare the fcp xri abort event has been handled */
12566         spin_lock_irq(&phba->hbalock);
12567         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12568         spin_unlock_irq(&phba->hbalock);
12569         /* Now, handle all the fcp xri abort events */
12570         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12571                 /* Get the first event from the head of the event queue */
12572                 spin_lock_irq(&phba->hbalock);
12573                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12574                                  cq_event, struct lpfc_cq_event, list);
12575                 spin_unlock_irq(&phba->hbalock);
12576                 /* Notify aborted XRI for FCP work queue */
12577                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12578                 /* Free the event processed back to the free pool */
12579                 lpfc_sli4_cq_event_release(phba, cq_event);
12580         }
12581 }
12582
12583 /**
12584  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12585  * @phba: pointer to lpfc hba data structure.
12586  *
12587  * This routine is invoked by the worker thread to process all the pending
12588  * SLI4 els abort xri events.
12589  **/
12590 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12591 {
12592         struct lpfc_cq_event *cq_event;
12593
12594         /* First, declare the els xri abort event has been handled */
12595         spin_lock_irq(&phba->hbalock);
12596         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12597         spin_unlock_irq(&phba->hbalock);
12598         /* Now, handle all the els xri abort events */
12599         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12600                 /* Get the first event from the head of the event queue */
12601                 spin_lock_irq(&phba->hbalock);
12602                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12603                                  cq_event, struct lpfc_cq_event, list);
12604                 spin_unlock_irq(&phba->hbalock);
12605                 /* Notify aborted XRI for ELS work queue */
12606                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12607                 /* Free the event processed back to the free pool */
12608                 lpfc_sli4_cq_event_release(phba, cq_event);
12609         }
12610 }
12611
12612 /**
12613  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12614  * @phba: pointer to lpfc hba data structure
12615  * @pIocbIn: pointer to the rspiocbq
12616  * @pIocbOut: pointer to the cmdiocbq
12617  * @wcqe: pointer to the complete wcqe
12618  *
12619  * This routine transfers the fields of a command iocbq to a response iocbq
12620  * by copying all the IOCB fields from command iocbq and transferring the
12621  * completion status information from the complete wcqe.
12622  **/
12623 static void
12624 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12625                               struct lpfc_iocbq *pIocbIn,
12626                               struct lpfc_iocbq *pIocbOut,
12627                               struct lpfc_wcqe_complete *wcqe)
12628 {
12629         int numBdes, i;
12630         unsigned long iflags;
12631         uint32_t status, max_response;
12632         struct lpfc_dmabuf *dmabuf;
12633         struct ulp_bde64 *bpl, bde;
12634         size_t offset = offsetof(struct lpfc_iocbq, iocb);
12635
12636         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12637                sizeof(struct lpfc_iocbq) - offset);
12638         /* Map WCQE parameters into irspiocb parameters */
12639         status = bf_get(lpfc_wcqe_c_status, wcqe);
12640         pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
12641         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12642                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12643                         pIocbIn->iocb.un.fcpi.fcpi_parm =
12644                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
12645                                         wcqe->total_data_placed;
12646                 else
12647                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12648         else {
12649                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12650                 switch (pIocbOut->iocb.ulpCommand) {
12651                 case CMD_ELS_REQUEST64_CR:
12652                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12653                         bpl  = (struct ulp_bde64 *)dmabuf->virt;
12654                         bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12655                         max_response = bde.tus.f.bdeSize;
12656                         break;
12657                 case CMD_GEN_REQUEST64_CR:
12658                         max_response = 0;
12659                         if (!pIocbOut->context3)
12660                                 break;
12661                         numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12662                                         sizeof(struct ulp_bde64);
12663                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12664                         bpl = (struct ulp_bde64 *)dmabuf->virt;
12665                         for (i = 0; i < numBdes; i++) {
12666                                 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12667                                 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12668                                         max_response += bde.tus.f.bdeSize;
12669                         }
12670                         break;
12671                 default:
12672                         max_response = wcqe->total_data_placed;
12673                         break;
12674                 }
12675                 if (max_response < wcqe->total_data_placed)
12676                         pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12677                 else
12678                         pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12679                                 wcqe->total_data_placed;
12680         }
12681
12682         /* Convert BG errors for completion status */
12683         if (status == CQE_STATUS_DI_ERROR) {
12684                 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12685
12686                 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12687                         pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12688                 else
12689                         pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12690
12691                 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12692                 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12693                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12694                                 BGS_GUARD_ERR_MASK;
12695                 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12696                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12697                                 BGS_APPTAG_ERR_MASK;
12698                 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12699                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12700                                 BGS_REFTAG_ERR_MASK;
12701
12702                 /* Check to see if there was any good data before the error */
12703                 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12704                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12705                                 BGS_HI_WATER_MARK_PRESENT_MASK;
12706                         pIocbIn->iocb.unsli3.sli3_bg.bghm =
12707                                 wcqe->total_data_placed;
12708                 }
12709
12710                 /*
12711                 * Set ALL the error bits to indicate we don't know what
12712                 * type of error it is.
12713                 */
12714                 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12715                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12716                                 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12717                                 BGS_GUARD_ERR_MASK);
12718         }
12719
12720         /* Pick up HBA exchange busy condition */
12721         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12722                 spin_lock_irqsave(&phba->hbalock, iflags);
12723                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12724                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12725         }
12726 }
12727
12728 /**
12729  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12730  * @phba: Pointer to HBA context object.
12731  * @wcqe: Pointer to work-queue completion queue entry.
12732  *
12733  * This routine handles an ELS work-queue completion event and construct
12734  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12735  * discovery engine to handle.
12736  *
12737  * Return: Pointer to the receive IOCBQ, NULL otherwise.
12738  **/
12739 static struct lpfc_iocbq *
12740 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12741                                struct lpfc_iocbq *irspiocbq)
12742 {
12743         struct lpfc_sli_ring *pring;
12744         struct lpfc_iocbq *cmdiocbq;
12745         struct lpfc_wcqe_complete *wcqe;
12746         unsigned long iflags;
12747
12748         pring = lpfc_phba_elsring(phba);
12749         if (unlikely(!pring))
12750                 return NULL;
12751
12752         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
12753         spin_lock_irqsave(&pring->ring_lock, iflags);
12754         pring->stats.iocb_event++;
12755         /* Look up the ELS command IOCB and create pseudo response IOCB */
12756         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12757                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12758         if (unlikely(!cmdiocbq)) {
12759                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12760                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12761                                 "0386 ELS complete with no corresponding "
12762                                 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12763                                 wcqe->word0, wcqe->total_data_placed,
12764                                 wcqe->parameter, wcqe->word3);
12765                 lpfc_sli_release_iocbq(phba, irspiocbq);
12766                 return NULL;
12767         }
12768
12769         /* Put the iocb back on the txcmplq */
12770         lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12771         spin_unlock_irqrestore(&pring->ring_lock, iflags);
12772
12773         /* Fake the irspiocbq and copy necessary response information */
12774         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
12775
12776         return irspiocbq;
12777 }
12778
12779 inline struct lpfc_cq_event *
12780 lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12781 {
12782         struct lpfc_cq_event *cq_event;
12783
12784         /* Allocate a new internal CQ_EVENT entry */
12785         cq_event = lpfc_sli4_cq_event_alloc(phba);
12786         if (!cq_event) {
12787                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12788                                 "0602 Failed to alloc CQ_EVENT entry\n");
12789                 return NULL;
12790         }
12791
12792         /* Move the CQE into the event */
12793         memcpy(&cq_event->cqe, entry, size);
12794         return cq_event;
12795 }
12796
12797 /**
12798  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12799  * @phba: Pointer to HBA context object.
12800  * @cqe: Pointer to mailbox completion queue entry.
12801  *
12802  * This routine process a mailbox completion queue entry with asynchrous
12803  * event.
12804  *
12805  * Return: true if work posted to worker thread, otherwise false.
12806  **/
12807 static bool
12808 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12809 {
12810         struct lpfc_cq_event *cq_event;
12811         unsigned long iflags;
12812
12813         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12814                         "0392 Async Event: word0:x%x, word1:x%x, "
12815                         "word2:x%x, word3:x%x\n", mcqe->word0,
12816                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12817
12818         cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12819         if (!cq_event)
12820                 return false;
12821         spin_lock_irqsave(&phba->hbalock, iflags);
12822         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12823         /* Set the async event flag */
12824         phba->hba_flag |= ASYNC_EVENT;
12825         spin_unlock_irqrestore(&phba->hbalock, iflags);
12826
12827         return true;
12828 }
12829
12830 /**
12831  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12832  * @phba: Pointer to HBA context object.
12833  * @cqe: Pointer to mailbox completion queue entry.
12834  *
12835  * This routine process a mailbox completion queue entry with mailbox
12836  * completion event.
12837  *
12838  * Return: true if work posted to worker thread, otherwise false.
12839  **/
12840 static bool
12841 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12842 {
12843         uint32_t mcqe_status;
12844         MAILBOX_t *mbox, *pmbox;
12845         struct lpfc_mqe *mqe;
12846         struct lpfc_vport *vport;
12847         struct lpfc_nodelist *ndlp;
12848         struct lpfc_dmabuf *mp;
12849         unsigned long iflags;
12850         LPFC_MBOXQ_t *pmb;
12851         bool workposted = false;
12852         int rc;
12853
12854         /* If not a mailbox complete MCQE, out by checking mailbox consume */
12855         if (!bf_get(lpfc_trailer_completed, mcqe))
12856                 goto out_no_mqe_complete;
12857
12858         /* Get the reference to the active mbox command */
12859         spin_lock_irqsave(&phba->hbalock, iflags);
12860         pmb = phba->sli.mbox_active;
12861         if (unlikely(!pmb)) {
12862                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12863                                 "1832 No pending MBOX command to handle\n");
12864                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12865                 goto out_no_mqe_complete;
12866         }
12867         spin_unlock_irqrestore(&phba->hbalock, iflags);
12868         mqe = &pmb->u.mqe;
12869         pmbox = (MAILBOX_t *)&pmb->u.mqe;
12870         mbox = phba->mbox;
12871         vport = pmb->vport;
12872
12873         /* Reset heartbeat timer */
12874         phba->last_completion_time = jiffies;
12875         del_timer(&phba->sli.mbox_tmo);
12876
12877         /* Move mbox data to caller's mailbox region, do endian swapping */
12878         if (pmb->mbox_cmpl && mbox)
12879                 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
12880
12881         /*
12882          * For mcqe errors, conditionally move a modified error code to
12883          * the mbox so that the error will not be missed.
12884          */
12885         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12886         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12887                 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12888                         bf_set(lpfc_mqe_status, mqe,
12889                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
12890         }
12891         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12892                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12893                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12894                                       "MBOX dflt rpi: status:x%x rpi:x%x",
12895                                       mcqe_status,
12896                                       pmbox->un.varWords[0], 0);
12897                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12898                         mp = (struct lpfc_dmabuf *)(pmb->context1);
12899                         ndlp = (struct lpfc_nodelist *)pmb->context2;
12900                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
12901                          * RID of the PPI using the same mbox buffer.
12902                          */
12903                         lpfc_unreg_login(phba, vport->vpi,
12904                                          pmbox->un.varWords[0], pmb);
12905                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12906                         pmb->context1 = mp;
12907                         pmb->context2 = ndlp;
12908                         pmb->vport = vport;
12909                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12910                         if (rc != MBX_BUSY)
12911                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12912                                                 LOG_SLI, "0385 rc should "
12913                                                 "have been MBX_BUSY\n");
12914                         if (rc != MBX_NOT_FINISHED)
12915                                 goto send_current_mbox;
12916                 }
12917         }
12918         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12919         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12920         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12921
12922         /* There is mailbox completion work to do */
12923         spin_lock_irqsave(&phba->hbalock, iflags);
12924         __lpfc_mbox_cmpl_put(phba, pmb);
12925         phba->work_ha |= HA_MBATT;
12926         spin_unlock_irqrestore(&phba->hbalock, iflags);
12927         workposted = true;
12928
12929 send_current_mbox:
12930         spin_lock_irqsave(&phba->hbalock, iflags);
12931         /* Release the mailbox command posting token */
12932         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12933         /* Setting active mailbox pointer need to be in sync to flag clear */
12934         phba->sli.mbox_active = NULL;
12935         spin_unlock_irqrestore(&phba->hbalock, iflags);
12936         /* Wake up worker thread to post the next pending mailbox command */
12937         lpfc_worker_wake_up(phba);
12938 out_no_mqe_complete:
12939         if (bf_get(lpfc_trailer_consumed, mcqe))
12940                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12941         return workposted;
12942 }
12943
12944 /**
12945  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12946  * @phba: Pointer to HBA context object.
12947  * @cqe: Pointer to mailbox completion queue entry.
12948  *
12949  * This routine process a mailbox completion queue entry, it invokes the
12950  * proper mailbox complete handling or asynchrous event handling routine
12951  * according to the MCQE's async bit.
12952  *
12953  * Return: true if work posted to worker thread, otherwise false.
12954  **/
12955 static bool
12956 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12957 {
12958         struct lpfc_mcqe mcqe;
12959         bool workposted;
12960
12961         /* Copy the mailbox MCQE and convert endian order as needed */
12962         lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12963
12964         /* Invoke the proper event handling routine */
12965         if (!bf_get(lpfc_trailer_async, &mcqe))
12966                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12967         else
12968                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12969         return workposted;
12970 }
12971
12972 /**
12973  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12974  * @phba: Pointer to HBA context object.
12975  * @cq: Pointer to associated CQ
12976  * @wcqe: Pointer to work-queue completion queue entry.
12977  *
12978  * This routine handles an ELS work-queue completion event.
12979  *
12980  * Return: true if work posted to worker thread, otherwise false.
12981  **/
12982 static bool
12983 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12984                              struct lpfc_wcqe_complete *wcqe)
12985 {
12986         struct lpfc_iocbq *irspiocbq;
12987         unsigned long iflags;
12988         struct lpfc_sli_ring *pring = cq->pring;
12989         int txq_cnt = 0;
12990         int txcmplq_cnt = 0;
12991         int fcp_txcmplq_cnt = 0;
12992
12993         /* Check for response status */
12994         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12995                 /* Log the error status */
12996                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12997                                 "0357 ELS CQE error: status=x%x: "
12998                                 "CQE: %08x %08x %08x %08x\n",
12999                                 bf_get(lpfc_wcqe_c_status, wcqe),
13000                                 wcqe->word0, wcqe->total_data_placed,
13001                                 wcqe->parameter, wcqe->word3);
13002         }
13003
13004         /* Get an irspiocbq for later ELS response processing use */
13005         irspiocbq = lpfc_sli_get_iocbq(phba);
13006         if (!irspiocbq) {
13007                 if (!list_empty(&pring->txq))
13008                         txq_cnt++;
13009                 if (!list_empty(&pring->txcmplq))
13010                         txcmplq_cnt++;
13011                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13012                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13013                         "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
13014                         txq_cnt, phba->iocb_cnt,
13015                         fcp_txcmplq_cnt,
13016                         txcmplq_cnt);
13017                 return false;
13018         }
13019
13020         /* Save off the slow-path queue event for work thread to process */
13021         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
13022         spin_lock_irqsave(&phba->hbalock, iflags);
13023         list_add_tail(&irspiocbq->cq_event.list,
13024                       &phba->sli4_hba.sp_queue_event);
13025         phba->hba_flag |= HBA_SP_QUEUE_EVT;
13026         spin_unlock_irqrestore(&phba->hbalock, iflags);
13027
13028         return true;
13029 }
13030
13031 /**
13032  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13033  * @phba: Pointer to HBA context object.
13034  * @wcqe: Pointer to work-queue completion queue entry.
13035  *
13036  * This routine handles slow-path WQ entry consumed event by invoking the
13037  * proper WQ release routine to the slow-path WQ.
13038  **/
13039 static void
13040 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13041                              struct lpfc_wcqe_release *wcqe)
13042 {
13043         /* sanity check on queue memory */
13044         if (unlikely(!phba->sli4_hba.els_wq))
13045                 return;
13046         /* Check for the slow-path ELS work queue */
13047         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13048                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13049                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13050         else
13051                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13052                                 "2579 Slow-path wqe consume event carries "
13053                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13054                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13055                                 phba->sli4_hba.els_wq->queue_id);
13056 }
13057
13058 /**
13059  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13060  * @phba: Pointer to HBA context object.
13061  * @cq: Pointer to a WQ completion queue.
13062  * @wcqe: Pointer to work-queue completion queue entry.
13063  *
13064  * This routine handles an XRI abort event.
13065  *
13066  * Return: true if work posted to worker thread, otherwise false.
13067  **/
13068 static bool
13069 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13070                                    struct lpfc_queue *cq,
13071                                    struct sli4_wcqe_xri_aborted *wcqe)
13072 {
13073         bool workposted = false;
13074         struct lpfc_cq_event *cq_event;
13075         unsigned long iflags;
13076
13077         switch (cq->subtype) {
13078         case LPFC_FCP:
13079                 cq_event = lpfc_cq_event_setup(
13080                         phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13081                 if (!cq_event)
13082                         return false;
13083                 spin_lock_irqsave(&phba->hbalock, iflags);
13084                 list_add_tail(&cq_event->list,
13085                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13086                 /* Set the fcp xri abort event flag */
13087                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13088                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13089                 workposted = true;
13090                 break;
13091         case LPFC_NVME_LS: /* NVME LS uses ELS resources */
13092         case LPFC_ELS:
13093                 cq_event = lpfc_cq_event_setup(
13094                         phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13095                 if (!cq_event)
13096                         return false;
13097                 spin_lock_irqsave(&phba->hbalock, iflags);
13098                 list_add_tail(&cq_event->list,
13099                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13100                 /* Set the els xri abort event flag */
13101                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13102                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13103                 workposted = true;
13104                 break;
13105         case LPFC_NVME:
13106                 /* Notify aborted XRI for NVME work queue */
13107                 if (phba->nvmet_support)
13108                         lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13109                 else
13110                         lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13111
13112                 workposted = false;
13113                 break;
13114         default:
13115                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13116                                 "0603 Invalid CQ subtype %d: "
13117                                 "%08x %08x %08x %08x\n",
13118                                 cq->subtype, wcqe->word0, wcqe->parameter,
13119                                 wcqe->word2, wcqe->word3);
13120                 workposted = false;
13121                 break;
13122         }
13123         return workposted;
13124 }
13125
13126 /**
13127  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
13128  * @phba: Pointer to HBA context object.
13129  * @rcqe: Pointer to receive-queue completion queue entry.
13130  *
13131  * This routine process a receive-queue completion queue entry.
13132  *
13133  * Return: true if work posted to worker thread, otherwise false.
13134  **/
13135 static bool
13136 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13137 {
13138         bool workposted = false;
13139         struct fc_frame_header *fc_hdr;
13140         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13141         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
13142         struct lpfc_nvmet_tgtport *tgtp;
13143         struct hbq_dmabuf *dma_buf;
13144         uint32_t status, rq_id;
13145         unsigned long iflags;
13146
13147         /* sanity check on queue memory */
13148         if (unlikely(!hrq) || unlikely(!drq))
13149                 return workposted;
13150
13151         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13152                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13153         else
13154                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13155         if (rq_id != hrq->queue_id)
13156                 goto out;
13157
13158         status = bf_get(lpfc_rcqe_status, rcqe);
13159         switch (status) {
13160         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13161                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13162                                 "2537 Receive Frame Truncated!!\n");
13163         case FC_STATUS_RQ_SUCCESS:
13164                 spin_lock_irqsave(&phba->hbalock, iflags);
13165                 lpfc_sli4_rq_release(hrq, drq);
13166                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13167                 if (!dma_buf) {
13168                         hrq->RQ_no_buf_found++;
13169                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13170                         goto out;
13171                 }
13172                 hrq->RQ_rcv_buf++;
13173                 hrq->RQ_buf_posted--;
13174                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
13175
13176                 /* If a NVME LS event (type 0x28), treat it as Fast path */
13177                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13178
13179                 /* save off the frame for the word thread to process */
13180                 list_add_tail(&dma_buf->cq_event.list,
13181                               &phba->sli4_hba.sp_queue_event);
13182                 /* Frame received */
13183                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
13184                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13185                 workposted = true;
13186                 break;
13187         case FC_STATUS_INSUFF_BUF_FRM_DISC:
13188                 if (phba->nvmet_support) {
13189                         tgtp = phba->targetport->private;
13190                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13191                                         "6402 RQE Error x%x, posted %d err_cnt "
13192                                         "%d: %x %x %x\n",
13193                                         status, hrq->RQ_buf_posted,
13194                                         hrq->RQ_no_posted_buf,
13195                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
13196                                         atomic_read(&tgtp->rcv_fcp_cmd_out),
13197                                         atomic_read(&tgtp->xmt_fcp_release));
13198                 }
13199                 /* fallthrough */
13200
13201         case FC_STATUS_INSUFF_BUF_NEED_BUF:
13202                 hrq->RQ_no_posted_buf++;
13203                 /* Post more buffers if possible */
13204                 spin_lock_irqsave(&phba->hbalock, iflags);
13205                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13206                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13207                 workposted = true;
13208                 break;
13209         }
13210 out:
13211         return workposted;
13212 }
13213
13214 /**
13215  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13216  * @phba: Pointer to HBA context object.
13217  * @cq: Pointer to the completion queue.
13218  * @wcqe: Pointer to a completion queue entry.
13219  *
13220  * This routine process a slow-path work-queue or receive queue completion queue
13221  * entry.
13222  *
13223  * Return: true if work posted to worker thread, otherwise false.
13224  **/
13225 static bool
13226 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13227                          struct lpfc_cqe *cqe)
13228 {
13229         struct lpfc_cqe cqevt;
13230         bool workposted = false;
13231
13232         /* Copy the work queue CQE and convert endian order if needed */
13233         lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
13234
13235         /* Check and process for different type of WCQE and dispatch */
13236         switch (bf_get(lpfc_cqe_code, &cqevt)) {
13237         case CQE_CODE_COMPL_WQE:
13238                 /* Process the WQ/RQ complete event */
13239                 phba->last_completion_time = jiffies;
13240                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
13241                                 (struct lpfc_wcqe_complete *)&cqevt);
13242                 break;
13243         case CQE_CODE_RELEASE_WQE:
13244                 /* Process the WQ release event */
13245                 lpfc_sli4_sp_handle_rel_wcqe(phba,
13246                                 (struct lpfc_wcqe_release *)&cqevt);
13247                 break;
13248         case CQE_CODE_XRI_ABORTED:
13249                 /* Process the WQ XRI abort event */
13250                 phba->last_completion_time = jiffies;
13251                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13252                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
13253                 break;
13254         case CQE_CODE_RECEIVE:
13255         case CQE_CODE_RECEIVE_V1:
13256                 /* Process the RQ event */
13257                 phba->last_completion_time = jiffies;
13258                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
13259                                 (struct lpfc_rcqe *)&cqevt);
13260                 break;
13261         default:
13262                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13263                                 "0388 Not a valid WCQE code: x%x\n",
13264                                 bf_get(lpfc_cqe_code, &cqevt));
13265                 break;
13266         }
13267         return workposted;
13268 }
13269
13270 /**
13271  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13272  * @phba: Pointer to HBA context object.
13273  * @eqe: Pointer to fast-path event queue entry.
13274  *
13275  * This routine process a event queue entry from the slow-path event queue.
13276  * It will check the MajorCode and MinorCode to determine this is for a
13277  * completion event on a completion queue, if not, an error shall be logged
13278  * and just return. Otherwise, it will get to the corresponding completion
13279  * queue and process all the entries on that completion queue, rearm the
13280  * completion queue, and then return.
13281  *
13282  **/
13283 static void
13284 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13285         struct lpfc_queue *speq)
13286 {
13287         struct lpfc_queue *cq = NULL, *childq;
13288         uint16_t cqid;
13289
13290         /* Get the reference to the corresponding CQ */
13291         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13292
13293         list_for_each_entry(childq, &speq->child_list, list) {
13294                 if (childq->queue_id == cqid) {
13295                         cq = childq;
13296                         break;
13297                 }
13298         }
13299         if (unlikely(!cq)) {
13300                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13301                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13302                                         "0365 Slow-path CQ identifier "
13303                                         "(%d) does not exist\n", cqid);
13304                 return;
13305         }
13306
13307         /* Save EQ associated with this CQ */
13308         cq->assoc_qp = speq;
13309
13310         if (!queue_work(phba->wq, &cq->spwork))
13311                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13312                                 "0390 Cannot schedule soft IRQ "
13313                                 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13314                                 cqid, cq->queue_id, smp_processor_id());
13315 }
13316
13317 /**
13318  * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13319  * @phba: Pointer to HBA context object.
13320  *
13321  * This routine process a event queue entry from the slow-path event queue.
13322  * It will check the MajorCode and MinorCode to determine this is for a
13323  * completion event on a completion queue, if not, an error shall be logged
13324  * and just return. Otherwise, it will get to the corresponding completion
13325  * queue and process all the entries on that completion queue, rearm the
13326  * completion queue, and then return.
13327  *
13328  **/
13329 static void
13330 lpfc_sli4_sp_process_cq(struct work_struct *work)
13331 {
13332         struct lpfc_queue *cq =
13333                 container_of(work, struct lpfc_queue, spwork);
13334         struct lpfc_hba *phba = cq->phba;
13335         struct lpfc_cqe *cqe;
13336         bool workposted = false;
13337         int ccount = 0;
13338
13339         /* Process all the entries to the CQ */
13340         switch (cq->type) {
13341         case LPFC_MCQ:
13342                 while ((cqe = lpfc_sli4_cq_get(cq))) {
13343                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
13344                         if (!(++ccount % cq->entry_repost))
13345                                 break;
13346                         cq->CQ_mbox++;
13347                 }
13348                 break;
13349         case LPFC_WCQ:
13350                 while ((cqe = lpfc_sli4_cq_get(cq))) {
13351                         if (cq->subtype == LPFC_FCP ||
13352                             cq->subtype == LPFC_NVME) {
13353 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13354                                 if (phba->ktime_on)
13355                                         cq->isr_timestamp = ktime_get_ns();
13356                                 else
13357                                         cq->isr_timestamp = 0;
13358 #endif
13359                                 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
13360                                                                        cqe);
13361                         } else {
13362                                 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13363                                                                       cqe);
13364                         }
13365                         if (!(++ccount % cq->entry_repost))
13366                                 break;
13367                 }
13368
13369                 /* Track the max number of CQEs processed in 1 EQ */
13370                 if (ccount > cq->CQ_max_cqe)
13371                         cq->CQ_max_cqe = ccount;
13372                 break;
13373         default:
13374                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13375                                 "0370 Invalid completion queue type (%d)\n",
13376                                 cq->type);
13377                 return;
13378         }
13379
13380         /* Catch the no cq entry condition, log an error */
13381         if (unlikely(ccount == 0))
13382                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13383                                 "0371 No entry from the CQ: identifier "
13384                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13385
13386         /* In any case, flash and re-arm the RCQ */
13387         phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
13388
13389         /* wake up worker thread if there are works to be done */
13390         if (workposted)
13391                 lpfc_worker_wake_up(phba);
13392 }
13393
13394 /**
13395  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
13396  * @phba: Pointer to HBA context object.
13397  * @cq: Pointer to associated CQ
13398  * @wcqe: Pointer to work-queue completion queue entry.
13399  *
13400  * This routine process a fast-path work queue completion entry from fast-path
13401  * event queue for FCP command response completion.
13402  **/
13403 static void
13404 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13405                              struct lpfc_wcqe_complete *wcqe)
13406 {
13407         struct lpfc_sli_ring *pring = cq->pring;
13408         struct lpfc_iocbq *cmdiocbq;
13409         struct lpfc_iocbq irspiocbq;
13410         unsigned long iflags;
13411
13412         /* Check for response status */
13413         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13414                 /* If resource errors reported from HBA, reduce queue
13415                  * depth of the SCSI device.
13416                  */
13417                 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13418                      IOSTAT_LOCAL_REJECT)) &&
13419                     ((wcqe->parameter & IOERR_PARAM_MASK) ==
13420                      IOERR_NO_RESOURCES))
13421                         phba->lpfc_rampdown_queue_depth(phba);
13422
13423                 /* Log the error status */
13424                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13425                                 "0373 FCP CQE error: status=x%x: "
13426                                 "CQE: %08x %08x %08x %08x\n",
13427                                 bf_get(lpfc_wcqe_c_status, wcqe),
13428                                 wcqe->word0, wcqe->total_data_placed,
13429                                 wcqe->parameter, wcqe->word3);
13430         }
13431
13432         /* Look up the FCP command IOCB and create pseudo response IOCB */
13433         spin_lock_irqsave(&pring->ring_lock, iflags);
13434         pring->stats.iocb_event++;
13435         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13436                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13437         spin_unlock_irqrestore(&pring->ring_lock, iflags);
13438         if (unlikely(!cmdiocbq)) {
13439                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13440                                 "0374 FCP complete with no corresponding "
13441                                 "cmdiocb: iotag (%d)\n",
13442                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13443                 return;
13444         }
13445 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13446         cmdiocbq->isr_timestamp = cq->isr_timestamp;
13447 #endif
13448         if (cmdiocbq->iocb_cmpl == NULL) {
13449                 if (cmdiocbq->wqe_cmpl) {
13450                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13451                                 spin_lock_irqsave(&phba->hbalock, iflags);
13452                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13453                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13454                         }
13455
13456                         /* Pass the cmd_iocb and the wcqe to the upper layer */
13457                         (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13458                         return;
13459                 }
13460                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13461                                 "0375 FCP cmdiocb not callback function "
13462                                 "iotag: (%d)\n",
13463                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13464                 return;
13465         }
13466
13467         /* Fake the irspiocb and copy necessary response information */
13468         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
13469
13470         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13471                 spin_lock_irqsave(&phba->hbalock, iflags);
13472                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13473                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13474         }
13475
13476         /* Pass the cmd_iocb and the rsp state to the upper layer */
13477         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13478 }
13479
13480 /**
13481  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13482  * @phba: Pointer to HBA context object.
13483  * @cq: Pointer to completion queue.
13484  * @wcqe: Pointer to work-queue completion queue entry.
13485  *
13486  * This routine handles an fast-path WQ entry consumed event by invoking the
13487  * proper WQ release routine to the slow-path WQ.
13488  **/
13489 static void
13490 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13491                              struct lpfc_wcqe_release *wcqe)
13492 {
13493         struct lpfc_queue *childwq;
13494         bool wqid_matched = false;
13495         uint16_t hba_wqid;
13496
13497         /* Check for fast-path FCP work queue release */
13498         hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
13499         list_for_each_entry(childwq, &cq->child_list, list) {
13500                 if (childwq->queue_id == hba_wqid) {
13501                         lpfc_sli4_wq_release(childwq,
13502                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13503                         if (childwq->q_flag & HBA_NVMET_WQFULL)
13504                                 lpfc_nvmet_wqfull_process(phba, childwq);
13505                         wqid_matched = true;
13506                         break;
13507                 }
13508         }
13509         /* Report warning log message if no match found */
13510         if (wqid_matched != true)
13511                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13512                                 "2580 Fast-path wqe consume event carries "
13513                                 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
13514 }
13515
13516 /**
13517  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13518  * @phba: Pointer to HBA context object.
13519  * @rcqe: Pointer to receive-queue completion queue entry.
13520  *
13521  * This routine process a receive-queue completion queue entry.
13522  *
13523  * Return: true if work posted to worker thread, otherwise false.
13524  **/
13525 static bool
13526 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13527                             struct lpfc_rcqe *rcqe)
13528 {
13529         bool workposted = false;
13530         struct lpfc_queue *hrq;
13531         struct lpfc_queue *drq;
13532         struct rqb_dmabuf *dma_buf;
13533         struct fc_frame_header *fc_hdr;
13534         struct lpfc_nvmet_tgtport *tgtp;
13535         uint32_t status, rq_id;
13536         unsigned long iflags;
13537         uint32_t fctl, idx;
13538
13539         if ((phba->nvmet_support == 0) ||
13540             (phba->sli4_hba.nvmet_cqset == NULL))
13541                 return workposted;
13542
13543         idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13544         hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13545         drq = phba->sli4_hba.nvmet_mrq_data[idx];
13546
13547         /* sanity check on queue memory */
13548         if (unlikely(!hrq) || unlikely(!drq))
13549                 return workposted;
13550
13551         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13552                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13553         else
13554                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13555
13556         if ((phba->nvmet_support == 0) ||
13557             (rq_id != hrq->queue_id))
13558                 return workposted;
13559
13560         status = bf_get(lpfc_rcqe_status, rcqe);
13561         switch (status) {
13562         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13563                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13564                                 "6126 Receive Frame Truncated!!\n");
13565                 /* Drop thru */
13566         case FC_STATUS_RQ_SUCCESS:
13567                 spin_lock_irqsave(&phba->hbalock, iflags);
13568                 lpfc_sli4_rq_release(hrq, drq);
13569                 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13570                 if (!dma_buf) {
13571                         hrq->RQ_no_buf_found++;
13572                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13573                         goto out;
13574                 }
13575                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13576                 hrq->RQ_rcv_buf++;
13577                 hrq->RQ_buf_posted--;
13578                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13579
13580                 /* Just some basic sanity checks on FCP Command frame */
13581                 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13582                 fc_hdr->fh_f_ctl[1] << 8 |
13583                 fc_hdr->fh_f_ctl[2]);
13584                 if (((fctl &
13585                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13586                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13587                     (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13588                         goto drop;
13589
13590                 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13591                         dma_buf->bytes_recv = bf_get(lpfc_rcqe_length,  rcqe);
13592                         lpfc_nvmet_unsol_fcp_event(
13593                                 phba, idx, dma_buf,
13594                                 cq->isr_timestamp);
13595                         return false;
13596                 }
13597 drop:
13598                 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13599                 break;
13600         case FC_STATUS_INSUFF_BUF_FRM_DISC:
13601                 if (phba->nvmet_support) {
13602                         tgtp = phba->targetport->private;
13603                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13604                                         "6401 RQE Error x%x, posted %d err_cnt "
13605                                         "%d: %x %x %x\n",
13606                                         status, hrq->RQ_buf_posted,
13607                                         hrq->RQ_no_posted_buf,
13608                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
13609                                         atomic_read(&tgtp->rcv_fcp_cmd_out),
13610                                         atomic_read(&tgtp->xmt_fcp_release));
13611                 }
13612                 /* fallthrough */
13613
13614         case FC_STATUS_INSUFF_BUF_NEED_BUF:
13615                 hrq->RQ_no_posted_buf++;
13616                 /* Post more buffers if possible */
13617                 break;
13618         }
13619 out:
13620         return workposted;
13621 }
13622
13623 /**
13624  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
13625  * @cq: Pointer to the completion queue.
13626  * @eqe: Pointer to fast-path completion queue entry.
13627  *
13628  * This routine process a fast-path work queue completion entry from fast-path
13629  * event queue for FCP command response completion.
13630  **/
13631 static int
13632 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13633                          struct lpfc_cqe *cqe)
13634 {
13635         struct lpfc_wcqe_release wcqe;
13636         bool workposted = false;
13637
13638         /* Copy the work queue CQE and convert endian order if needed */
13639         lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13640
13641         /* Check and process for different type of WCQE and dispatch */
13642         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13643         case CQE_CODE_COMPL_WQE:
13644         case CQE_CODE_NVME_ERSP:
13645                 cq->CQ_wq++;
13646                 /* Process the WQ complete event */
13647                 phba->last_completion_time = jiffies;
13648                 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13649                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13650                                 (struct lpfc_wcqe_complete *)&wcqe);
13651                 if (cq->subtype == LPFC_NVME_LS)
13652                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13653                                 (struct lpfc_wcqe_complete *)&wcqe);
13654                 break;
13655         case CQE_CODE_RELEASE_WQE:
13656                 cq->CQ_release_wqe++;
13657                 /* Process the WQ release event */
13658                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13659                                 (struct lpfc_wcqe_release *)&wcqe);
13660                 break;
13661         case CQE_CODE_XRI_ABORTED:
13662                 cq->CQ_xri_aborted++;
13663                 /* Process the WQ XRI abort event */
13664                 phba->last_completion_time = jiffies;
13665                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13666                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
13667                 break;
13668         case CQE_CODE_RECEIVE_V1:
13669         case CQE_CODE_RECEIVE:
13670                 phba->last_completion_time = jiffies;
13671                 if (cq->subtype == LPFC_NVMET) {
13672                         workposted = lpfc_sli4_nvmet_handle_rcqe(
13673                                 phba, cq, (struct lpfc_rcqe *)&wcqe);
13674                 }
13675                 break;
13676         default:
13677                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13678                                 "0144 Not a valid CQE code: x%x\n",
13679                                 bf_get(lpfc_wcqe_c_code, &wcqe));
13680                 break;
13681         }
13682         return workposted;
13683 }
13684
13685 /**
13686  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
13687  * @phba: Pointer to HBA context object.
13688  * @eqe: Pointer to fast-path event queue entry.
13689  *
13690  * This routine process a event queue entry from the fast-path event queue.
13691  * It will check the MajorCode and MinorCode to determine this is for a
13692  * completion event on a completion queue, if not, an error shall be logged
13693  * and just return. Otherwise, it will get to the corresponding completion
13694  * queue and process all the entries on the completion queue, rearm the
13695  * completion queue, and then return.
13696  **/
13697 static void
13698 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13699                         uint32_t qidx)
13700 {
13701         struct lpfc_queue *cq = NULL;
13702         uint16_t cqid, id;
13703
13704         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13705                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13706                                 "0366 Not a valid completion "
13707                                 "event: majorcode=x%x, minorcode=x%x\n",
13708                                 bf_get_le32(lpfc_eqe_major_code, eqe),
13709                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
13710                 return;
13711         }
13712
13713         /* Get the reference to the corresponding CQ */
13714         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13715
13716         if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13717                 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13718                 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13719                         /* Process NVMET unsol rcv */
13720                         cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13721                         goto  process_cq;
13722                 }
13723         }
13724
13725         if (phba->sli4_hba.nvme_cq_map &&
13726             (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
13727                 /* Process NVME / NVMET command completion */
13728                 cq = phba->sli4_hba.nvme_cq[qidx];
13729                 goto  process_cq;
13730         }
13731
13732         if (phba->sli4_hba.fcp_cq_map &&
13733             (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13734                 /* Process FCP command completion */
13735                 cq = phba->sli4_hba.fcp_cq[qidx];
13736                 goto  process_cq;
13737         }
13738
13739         if (phba->sli4_hba.nvmels_cq &&
13740             (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13741                 /* Process NVME unsol rcv */
13742                 cq = phba->sli4_hba.nvmels_cq;
13743         }
13744
13745         /* Otherwise this is a Slow path event */
13746         if (cq == NULL) {
13747                 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13748                 return;
13749         }
13750
13751 process_cq:
13752         if (unlikely(cqid != cq->queue_id)) {
13753                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13754                                 "0368 Miss-matched fast-path completion "
13755                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13756                                 cqid, cq->queue_id);
13757                 return;
13758         }
13759
13760         /* Save EQ associated with this CQ */
13761         cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13762
13763         if (!queue_work(phba->wq, &cq->irqwork))
13764                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13765                                 "0363 Cannot schedule soft IRQ "
13766                                 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13767                                 cqid, cq->queue_id, smp_processor_id());
13768 }
13769
13770 /**
13771  * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13772  * @phba: Pointer to HBA context object.
13773  * @eqe: Pointer to fast-path event queue entry.
13774  *
13775  * This routine process a event queue entry from the fast-path event queue.
13776  * It will check the MajorCode and MinorCode to determine this is for a
13777  * completion event on a completion queue, if not, an error shall be logged
13778  * and just return. Otherwise, it will get to the corresponding completion
13779  * queue and process all the entries on the completion queue, rearm the
13780  * completion queue, and then return.
13781  **/
13782 static void
13783 lpfc_sli4_hba_process_cq(struct work_struct *work)
13784 {
13785         struct lpfc_queue *cq =
13786                 container_of(work, struct lpfc_queue, irqwork);
13787         struct lpfc_hba *phba = cq->phba;
13788         struct lpfc_cqe *cqe;
13789         bool workposted = false;
13790         int ccount = 0;
13791
13792         /* Process all the entries to the CQ */
13793         while ((cqe = lpfc_sli4_cq_get(cq))) {
13794 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13795                 if (phba->ktime_on)
13796                         cq->isr_timestamp = ktime_get_ns();
13797                 else
13798                         cq->isr_timestamp = 0;
13799 #endif
13800                 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
13801                 if (!(++ccount % cq->entry_repost))
13802                         break;
13803         }
13804
13805         /* Track the max number of CQEs processed in 1 EQ */
13806         if (ccount > cq->CQ_max_cqe)
13807                 cq->CQ_max_cqe = ccount;
13808         cq->assoc_qp->EQ_cqe_cnt += ccount;
13809
13810         /* Catch the no cq entry condition */
13811         if (unlikely(ccount == 0))
13812                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13813                                 "0369 No entry from fast-path completion "
13814                                 "queue fcpcqid=%d\n", cq->queue_id);
13815
13816         /* In any case, flash and re-arm the CQ */
13817         phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
13818
13819         /* wake up worker thread if there are works to be done */
13820         if (workposted)
13821                 lpfc_worker_wake_up(phba);
13822 }
13823
13824 static void
13825 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13826 {
13827         struct lpfc_eqe *eqe;
13828
13829         /* walk all the EQ entries and drop on the floor */
13830         while ((eqe = lpfc_sli4_eq_get(eq)))
13831                 ;
13832
13833         /* Clear and re-arm the EQ */
13834         phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
13835 }
13836
13837
13838 /**
13839  * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13840  *                           entry
13841  * @phba: Pointer to HBA context object.
13842  * @eqe: Pointer to fast-path event queue entry.
13843  *
13844  * This routine process a event queue entry from the Flash Optimized Fabric
13845  * event queue.  It will check the MajorCode and MinorCode to determine this
13846  * is for a completion event on a completion queue, if not, an error shall be
13847  * logged and just return. Otherwise, it will get to the corresponding
13848  * completion queue and process all the entries on the completion queue, rearm
13849  * the completion queue, and then return.
13850  **/
13851 static void
13852 lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13853 {
13854         struct lpfc_queue *cq;
13855         uint16_t cqid;
13856
13857         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13858                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13859                                 "9147 Not a valid completion "
13860                                 "event: majorcode=x%x, minorcode=x%x\n",
13861                                 bf_get_le32(lpfc_eqe_major_code, eqe),
13862                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
13863                 return;
13864         }
13865
13866         /* Get the reference to the corresponding CQ */
13867         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13868
13869         /* Next check for OAS */
13870         cq = phba->sli4_hba.oas_cq;
13871         if (unlikely(!cq)) {
13872                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13873                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13874                                         "9148 OAS completion queue "
13875                                         "does not exist\n");
13876                 return;
13877         }
13878
13879         if (unlikely(cqid != cq->queue_id)) {
13880                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13881                                 "9149 Miss-matched fast-path compl "
13882                                 "queue id: eqcqid=%d, fcpcqid=%d\n",
13883                                 cqid, cq->queue_id);
13884                 return;
13885         }
13886
13887         /* Save EQ associated with this CQ */
13888         cq->assoc_qp = phba->sli4_hba.fof_eq;
13889
13890         /* CQ work will be processed on CPU affinitized to this IRQ */
13891         if (!queue_work(phba->wq, &cq->irqwork))
13892                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13893                                 "0367 Cannot schedule soft IRQ "
13894                                 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13895                                 cqid, cq->queue_id, smp_processor_id());
13896 }
13897
13898 /**
13899  * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13900  * @irq: Interrupt number.
13901  * @dev_id: The device context pointer.
13902  *
13903  * This function is directly called from the PCI layer as an interrupt
13904  * service routine when device with SLI-4 interface spec is enabled with
13905  * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13906  * IOCB ring event in the HBA. However, when the device is enabled with either
13907  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13908  * device-level interrupt handler. When the PCI slot is in error recovery
13909  * or the HBA is undergoing initialization, the interrupt handler will not
13910  * process the interrupt. The Flash Optimized Fabric ring event are handled in
13911  * the intrrupt context. This function is called without any lock held.
13912  * It gets the hbalock to access and update SLI data structures. Note that,
13913  * the EQ to CQ are one-to-one map such that the EQ index is
13914  * equal to that of CQ index.
13915  *
13916  * This function returns IRQ_HANDLED when interrupt is handled else it
13917  * returns IRQ_NONE.
13918  **/
13919 irqreturn_t
13920 lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13921 {
13922         struct lpfc_hba *phba;
13923         struct lpfc_hba_eq_hdl *hba_eq_hdl;
13924         struct lpfc_queue *eq;
13925         struct lpfc_eqe *eqe;
13926         unsigned long iflag;
13927         int ecount = 0;
13928
13929         /* Get the driver's phba structure from the dev_id */
13930         hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13931         phba = hba_eq_hdl->phba;
13932
13933         if (unlikely(!phba))
13934                 return IRQ_NONE;
13935
13936         /* Get to the EQ struct associated with this vector */
13937         eq = phba->sli4_hba.fof_eq;
13938         if (unlikely(!eq))
13939                 return IRQ_NONE;
13940
13941         /* Check device state for handling interrupt */
13942         if (unlikely(lpfc_intr_state_check(phba))) {
13943                 /* Check again for link_state with lock held */
13944                 spin_lock_irqsave(&phba->hbalock, iflag);
13945                 if (phba->link_state < LPFC_LINK_DOWN)
13946                         /* Flush, clear interrupt, and rearm the EQ */
13947                         lpfc_sli4_eq_flush(phba, eq);
13948                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13949                 return IRQ_NONE;
13950         }
13951
13952         /*
13953          * Process all the event on FCP fast-path EQ
13954          */
13955         while ((eqe = lpfc_sli4_eq_get(eq))) {
13956                 lpfc_sli4_fof_handle_eqe(phba, eqe);
13957                 if (!(++ecount % eq->entry_repost))
13958                         break;
13959                 eq->EQ_processed++;
13960         }
13961
13962         /* Track the max number of EQEs processed in 1 intr */
13963         if (ecount > eq->EQ_max_eqe)
13964                 eq->EQ_max_eqe = ecount;
13965
13966
13967         if (unlikely(ecount == 0)) {
13968                 eq->EQ_no_entry++;
13969
13970                 if (phba->intr_type == MSIX)
13971                         /* MSI-X treated interrupt served as no EQ share INT */
13972                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13973                                         "9145 MSI-X interrupt with no EQE\n");
13974                 else {
13975                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13976                                         "9146 ISR interrupt with no EQE\n");
13977                         /* Non MSI-X treated on interrupt as EQ share INT */
13978                         return IRQ_NONE;
13979                 }
13980         }
13981         /* Always clear and re-arm the fast-path EQ */
13982         phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
13983         return IRQ_HANDLED;
13984 }
13985
13986 /**
13987  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
13988  * @irq: Interrupt number.
13989  * @dev_id: The device context pointer.
13990  *
13991  * This function is directly called from the PCI layer as an interrupt
13992  * service routine when device with SLI-4 interface spec is enabled with
13993  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13994  * ring event in the HBA. However, when the device is enabled with either
13995  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13996  * device-level interrupt handler. When the PCI slot is in error recovery
13997  * or the HBA is undergoing initialization, the interrupt handler will not
13998  * process the interrupt. The SCSI FCP fast-path ring event are handled in
13999  * the intrrupt context. This function is called without any lock held.
14000  * It gets the hbalock to access and update SLI data structures. Note that,
14001  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14002  * equal to that of FCP CQ index.
14003  *
14004  * The link attention and ELS ring attention events are handled
14005  * by the worker thread. The interrupt handler signals the worker thread
14006  * and returns for these events. This function is called without any lock
14007  * held. It gets the hbalock to access and update SLI data structures.
14008  *
14009  * This function returns IRQ_HANDLED when interrupt is handled else it
14010  * returns IRQ_NONE.
14011  **/
14012 irqreturn_t
14013 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
14014 {
14015         struct lpfc_hba *phba;
14016         struct lpfc_hba_eq_hdl *hba_eq_hdl;
14017         struct lpfc_queue *fpeq;
14018         struct lpfc_eqe *eqe;
14019         unsigned long iflag;
14020         int ecount = 0;
14021         int hba_eqidx;
14022
14023         /* Get the driver's phba structure from the dev_id */
14024         hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14025         phba = hba_eq_hdl->phba;
14026         hba_eqidx = hba_eq_hdl->idx;
14027
14028         if (unlikely(!phba))
14029                 return IRQ_NONE;
14030         if (unlikely(!phba->sli4_hba.hba_eq))
14031                 return IRQ_NONE;
14032
14033         /* Get to the EQ struct associated with this vector */
14034         fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
14035         if (unlikely(!fpeq))
14036                 return IRQ_NONE;
14037
14038         if (lpfc_fcp_look_ahead) {
14039                 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
14040                         phba->sli4_hba.sli4_eq_clr_intr(fpeq);
14041                 else {
14042                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14043                         return IRQ_NONE;
14044                 }
14045         }
14046
14047         /* Check device state for handling interrupt */
14048         if (unlikely(lpfc_intr_state_check(phba))) {
14049                 /* Check again for link_state with lock held */
14050                 spin_lock_irqsave(&phba->hbalock, iflag);
14051                 if (phba->link_state < LPFC_LINK_DOWN)
14052                         /* Flush, clear interrupt, and rearm the EQ */
14053                         lpfc_sli4_eq_flush(phba, fpeq);
14054                 spin_unlock_irqrestore(&phba->hbalock, iflag);
14055                 if (lpfc_fcp_look_ahead)
14056                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14057                 return IRQ_NONE;
14058         }
14059
14060         /*
14061          * Process all the event on FCP fast-path EQ
14062          */
14063         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
14064                 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14065                 if (!(++ecount % fpeq->entry_repost))
14066                         break;
14067                 fpeq->EQ_processed++;
14068         }
14069
14070         /* Track the max number of EQEs processed in 1 intr */
14071         if (ecount > fpeq->EQ_max_eqe)
14072                 fpeq->EQ_max_eqe = ecount;
14073
14074         /* Always clear and re-arm the fast-path EQ */
14075         phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
14076
14077         if (unlikely(ecount == 0)) {
14078                 fpeq->EQ_no_entry++;
14079
14080                 if (lpfc_fcp_look_ahead) {
14081                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14082                         return IRQ_NONE;
14083                 }
14084
14085                 if (phba->intr_type == MSIX)
14086                         /* MSI-X treated interrupt served as no EQ share INT */
14087                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14088                                         "0358 MSI-X interrupt with no EQE\n");
14089                 else
14090                         /* Non MSI-X treated on interrupt as EQ share INT */
14091                         return IRQ_NONE;
14092         }
14093
14094         if (lpfc_fcp_look_ahead)
14095                 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14096
14097         return IRQ_HANDLED;
14098 } /* lpfc_sli4_fp_intr_handler */
14099
14100 /**
14101  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14102  * @irq: Interrupt number.
14103  * @dev_id: The device context pointer.
14104  *
14105  * This function is the device-level interrupt handler to device with SLI-4
14106  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14107  * interrupt mode is enabled and there is an event in the HBA which requires
14108  * driver attention. This function invokes the slow-path interrupt attention
14109  * handling function and fast-path interrupt attention handling function in
14110  * turn to process the relevant HBA attention events. This function is called
14111  * without any lock held. It gets the hbalock to access and update SLI data
14112  * structures.
14113  *
14114  * This function returns IRQ_HANDLED when interrupt is handled, else it
14115  * returns IRQ_NONE.
14116  **/
14117 irqreturn_t
14118 lpfc_sli4_intr_handler(int irq, void *dev_id)
14119 {
14120         struct lpfc_hba  *phba;
14121         irqreturn_t hba_irq_rc;
14122         bool hba_handled = false;
14123         int qidx;
14124
14125         /* Get the driver's phba structure from the dev_id */
14126         phba = (struct lpfc_hba *)dev_id;
14127
14128         if (unlikely(!phba))
14129                 return IRQ_NONE;
14130
14131         /*
14132          * Invoke fast-path host attention interrupt handling as appropriate.
14133          */
14134         for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
14135                 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
14136                                         &phba->sli4_hba.hba_eq_hdl[qidx]);
14137                 if (hba_irq_rc == IRQ_HANDLED)
14138                         hba_handled |= true;
14139         }
14140
14141         if (phba->cfg_fof) {
14142                 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
14143                                         &phba->sli4_hba.hba_eq_hdl[qidx]);
14144                 if (hba_irq_rc == IRQ_HANDLED)
14145                         hba_handled |= true;
14146         }
14147
14148         return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
14149 } /* lpfc_sli4_intr_handler */
14150
14151 /**
14152  * lpfc_sli4_queue_free - free a queue structure and associated memory
14153  * @queue: The queue structure to free.
14154  *
14155  * This function frees a queue structure and the DMAable memory used for
14156  * the host resident queue. This function must be called after destroying the
14157  * queue on the HBA.
14158  **/
14159 void
14160 lpfc_sli4_queue_free(struct lpfc_queue *queue)
14161 {
14162         struct lpfc_dmabuf *dmabuf;
14163
14164         if (!queue)
14165                 return;
14166
14167         while (!list_empty(&queue->page_list)) {
14168                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14169                                  list);
14170                 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
14171                                   dmabuf->virt, dmabuf->phys);
14172                 kfree(dmabuf);
14173         }
14174         if (queue->rqbp) {
14175                 lpfc_free_rq_buffer(queue->phba, queue);
14176                 kfree(queue->rqbp);
14177         }
14178
14179         if (!list_empty(&queue->wq_list))
14180                 list_del(&queue->wq_list);
14181
14182         kfree(queue);
14183         return;
14184 }
14185
14186 /**
14187  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14188  * @phba: The HBA that this queue is being created on.
14189  * @page_size: The size of a queue page
14190  * @entry_size: The size of each queue entry for this queue.
14191  * @entry count: The number of entries that this queue will handle.
14192  *
14193  * This function allocates a queue structure and the DMAable memory used for
14194  * the host resident queue. This function must be called before creating the
14195  * queue on the HBA.
14196  **/
14197 struct lpfc_queue *
14198 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14199                       uint32_t entry_size, uint32_t entry_count)
14200 {
14201         struct lpfc_queue *queue;
14202         struct lpfc_dmabuf *dmabuf;
14203         int x, total_qe_count;
14204         void *dma_pointer;
14205         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14206
14207         if (!phba->sli4_hba.pc_sli4_params.supported)
14208                 hw_page_size = page_size;
14209
14210         queue = kzalloc(sizeof(struct lpfc_queue) +
14211                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14212         if (!queue)
14213                 return NULL;
14214         queue->page_count = (ALIGN(entry_size * entry_count,
14215                         hw_page_size))/hw_page_size;
14216
14217         /* If needed, Adjust page count to match the max the adapter supports */
14218         if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14219                 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14220
14221         INIT_LIST_HEAD(&queue->list);
14222         INIT_LIST_HEAD(&queue->wq_list);
14223         INIT_LIST_HEAD(&queue->wqfull_list);
14224         INIT_LIST_HEAD(&queue->page_list);
14225         INIT_LIST_HEAD(&queue->child_list);
14226
14227         /* Set queue parameters now.  If the system cannot provide memory
14228          * resources, the free routine needs to know what was allocated.
14229          */
14230         queue->entry_size = entry_size;
14231         queue->entry_count = entry_count;
14232         queue->page_size = hw_page_size;
14233         queue->phba = phba;
14234
14235         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14236                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14237                 if (!dmabuf)
14238                         goto out_fail;
14239                 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14240                                                    hw_page_size, &dmabuf->phys,
14241                                                    GFP_KERNEL);
14242                 if (!dmabuf->virt) {
14243                         kfree(dmabuf);
14244                         goto out_fail;
14245                 }
14246                 dmabuf->buffer_tag = x;
14247                 list_add_tail(&dmabuf->list, &queue->page_list);
14248                 /* initialize queue's entry array */
14249                 dma_pointer = dmabuf->virt;
14250                 for (; total_qe_count < entry_count &&
14251                      dma_pointer < (hw_page_size + dmabuf->virt);
14252                      total_qe_count++, dma_pointer += entry_size) {
14253                         queue->qe[total_qe_count].address = dma_pointer;
14254                 }
14255         }
14256         INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14257         INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
14258
14259         /* entry_repost will be set during q creation */
14260
14261         return queue;
14262 out_fail:
14263         lpfc_sli4_queue_free(queue);
14264         return NULL;
14265 }
14266
14267 /**
14268  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14269  * @phba: HBA structure that indicates port to create a queue on.
14270  * @pci_barset: PCI BAR set flag.
14271  *
14272  * This function shall perform iomap of the specified PCI BAR address to host
14273  * memory address if not already done so and return it. The returned host
14274  * memory address can be NULL.
14275  */
14276 static void __iomem *
14277 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14278 {
14279         if (!phba->pcidev)
14280                 return NULL;
14281
14282         switch (pci_barset) {
14283         case WQ_PCI_BAR_0_AND_1:
14284                 return phba->pci_bar0_memmap_p;
14285         case WQ_PCI_BAR_2_AND_3:
14286                 return phba->pci_bar2_memmap_p;
14287         case WQ_PCI_BAR_4_AND_5:
14288                 return phba->pci_bar4_memmap_p;
14289         default:
14290                 break;
14291         }
14292         return NULL;
14293 }
14294
14295 /**
14296  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
14297  * @phba: HBA structure that indicates port to create a queue on.
14298  * @startq: The starting FCP EQ to modify
14299  *
14300  * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
14301  * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14302  * updated in one mailbox command.
14303  *
14304  * The @phba struct is used to send mailbox command to HBA. The @startq
14305  * is used to get the starting FCP EQ to change.
14306  * This function is asynchronous and will wait for the mailbox
14307  * command to finish before continuing.
14308  *
14309  * On success this function will return a zero. If unable to allocate enough
14310  * memory this function will return -ENOMEM. If the queue create mailbox command
14311  * fails this function will return -ENXIO.
14312  **/
14313 int
14314 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14315                          uint32_t numq, uint32_t imax)
14316 {
14317         struct lpfc_mbx_modify_eq_delay *eq_delay;
14318         LPFC_MBOXQ_t *mbox;
14319         struct lpfc_queue *eq;
14320         int cnt, rc, length, status = 0;
14321         uint32_t shdr_status, shdr_add_status;
14322         uint32_t result, val;
14323         int qidx;
14324         union lpfc_sli4_cfg_shdr *shdr;
14325         uint16_t dmult;
14326
14327         if (startq >= phba->io_channel_irqs)
14328                 return 0;
14329
14330         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14331         if (!mbox)
14332                 return -ENOMEM;
14333         length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14334                   sizeof(struct lpfc_sli4_cfg_mhdr));
14335         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14336                          LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14337                          length, LPFC_SLI4_MBX_EMBED);
14338         eq_delay = &mbox->u.mqe.un.eq_delay;
14339
14340         /* Calculate delay multiper from maximum interrupt per second */
14341         result = imax / phba->io_channel_irqs;
14342         if (result > LPFC_DMULT_CONST || result == 0)
14343                 dmult = 0;
14344         else
14345                 dmult = LPFC_DMULT_CONST/result - 1;
14346         if (dmult > LPFC_DMULT_MAX)
14347                 dmult = LPFC_DMULT_MAX;
14348
14349         cnt = 0;
14350         for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14351                 eq = phba->sli4_hba.hba_eq[qidx];
14352                 if (!eq)
14353                         continue;
14354                 eq->q_mode = imax;
14355                 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14356                 eq_delay->u.request.eq[cnt].phase = 0;
14357                 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14358                 cnt++;
14359
14360                 /* q_mode is only used for auto_imax */
14361                 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14362                         /* Use EQ Delay Register method for q_mode */
14363
14364                         /* Convert for EQ Delay register */
14365                         val =  phba->cfg_fcp_imax;
14366                         if (val) {
14367                                 /* First, interrupts per sec per EQ */
14368                                 val = phba->cfg_fcp_imax /
14369                                         phba->io_channel_irqs;
14370
14371                                 /* us delay between each interrupt */
14372                                 val = LPFC_SEC_TO_USEC / val;
14373                         }
14374                         eq->q_mode = val;
14375                 } else {
14376                         eq->q_mode = imax;
14377                 }
14378
14379                 if (cnt >= numq)
14380                         break;
14381         }
14382         eq_delay->u.request.num_eq = cnt;
14383
14384         mbox->vport = phba->pport;
14385         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14386         mbox->context1 = NULL;
14387         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14388         shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14389         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14390         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14391         if (shdr_status || shdr_add_status || rc) {
14392                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14393                                 "2512 MODIFY_EQ_DELAY mailbox failed with "
14394                                 "status x%x add_status x%x, mbx status x%x\n",
14395                                 shdr_status, shdr_add_status, rc);
14396                 status = -ENXIO;
14397         }
14398         mempool_free(mbox, phba->mbox_mem_pool);
14399         return status;
14400 }
14401
14402 /**
14403  * lpfc_eq_create - Create an Event Queue on the HBA
14404  * @phba: HBA structure that indicates port to create a queue on.
14405  * @eq: The queue structure to use to create the event queue.
14406  * @imax: The maximum interrupt per second limit.
14407  *
14408  * This function creates an event queue, as detailed in @eq, on a port,
14409  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14410  *
14411  * The @phba struct is used to send mailbox command to HBA. The @eq struct
14412  * is used to get the entry count and entry size that are necessary to
14413  * determine the number of pages to allocate and use for this queue. This
14414  * function will send the EQ_CREATE mailbox command to the HBA to setup the
14415  * event queue. This function is asynchronous and will wait for the mailbox
14416  * command to finish before continuing.
14417  *
14418  * On success this function will return a zero. If unable to allocate enough
14419  * memory this function will return -ENOMEM. If the queue create mailbox command
14420  * fails this function will return -ENXIO.
14421  **/
14422 int
14423 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
14424 {
14425         struct lpfc_mbx_eq_create *eq_create;
14426         LPFC_MBOXQ_t *mbox;
14427         int rc, length, status = 0;
14428         struct lpfc_dmabuf *dmabuf;
14429         uint32_t shdr_status, shdr_add_status;
14430         union lpfc_sli4_cfg_shdr *shdr;
14431         uint16_t dmult;
14432         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14433
14434         /* sanity check on queue memory */
14435         if (!eq)
14436                 return -ENODEV;
14437         if (!phba->sli4_hba.pc_sli4_params.supported)
14438                 hw_page_size = SLI4_PAGE_SIZE;
14439
14440         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14441         if (!mbox)
14442                 return -ENOMEM;
14443         length = (sizeof(struct lpfc_mbx_eq_create) -
14444                   sizeof(struct lpfc_sli4_cfg_mhdr));
14445         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14446                          LPFC_MBOX_OPCODE_EQ_CREATE,
14447                          length, LPFC_SLI4_MBX_EMBED);
14448         eq_create = &mbox->u.mqe.un.eq_create;
14449         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14450         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14451                eq->page_count);
14452         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14453                LPFC_EQE_SIZE);
14454         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
14455
14456         /* Use version 2 of CREATE_EQ if eqav is set */
14457         if (phba->sli4_hba.pc_sli4_params.eqav) {
14458                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14459                        LPFC_Q_CREATE_VERSION_2);
14460                 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14461                        phba->sli4_hba.pc_sli4_params.eqav);
14462         }
14463
14464         /* don't setup delay multiplier using EQ_CREATE */
14465         dmult = 0;
14466         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14467                dmult);
14468         switch (eq->entry_count) {
14469         default:
14470                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14471                                 "0360 Unsupported EQ count. (%d)\n",
14472                                 eq->entry_count);
14473                 if (eq->entry_count < 256)
14474                         return -EINVAL;
14475                 /* otherwise default to smallest count (drop through) */
14476         case 256:
14477                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14478                        LPFC_EQ_CNT_256);
14479                 break;
14480         case 512:
14481                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14482                        LPFC_EQ_CNT_512);
14483                 break;
14484         case 1024:
14485                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14486                        LPFC_EQ_CNT_1024);
14487                 break;
14488         case 2048:
14489                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14490                        LPFC_EQ_CNT_2048);
14491                 break;
14492         case 4096:
14493                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14494                        LPFC_EQ_CNT_4096);
14495                 break;
14496         }
14497         list_for_each_entry(dmabuf, &eq->page_list, list) {
14498                 memset(dmabuf->virt, 0, hw_page_size);
14499                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14500                                         putPaddrLow(dmabuf->phys);
14501                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14502                                         putPaddrHigh(dmabuf->phys);
14503         }
14504         mbox->vport = phba->pport;
14505         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14506         mbox->context1 = NULL;
14507         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14508         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14509         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14510         if (shdr_status || shdr_add_status || rc) {
14511                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14512                                 "2500 EQ_CREATE mailbox failed with "
14513                                 "status x%x add_status x%x, mbx status x%x\n",
14514                                 shdr_status, shdr_add_status, rc);
14515                 status = -ENXIO;
14516         }
14517         eq->type = LPFC_EQ;
14518         eq->subtype = LPFC_NONE;
14519         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14520         if (eq->queue_id == 0xFFFF)
14521                 status = -ENXIO;
14522         eq->host_index = 0;
14523         eq->hba_index = 0;
14524         eq->entry_repost = LPFC_EQ_REPOST;
14525
14526         mempool_free(mbox, phba->mbox_mem_pool);
14527         return status;
14528 }
14529
14530 /**
14531  * lpfc_cq_create - Create a Completion Queue on the HBA
14532  * @phba: HBA structure that indicates port to create a queue on.
14533  * @cq: The queue structure to use to create the completion queue.
14534  * @eq: The event queue to bind this completion queue to.
14535  *
14536  * This function creates a completion queue, as detailed in @wq, on a port,
14537  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14538  *
14539  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14540  * is used to get the entry count and entry size that are necessary to
14541  * determine the number of pages to allocate and use for this queue. The @eq
14542  * is used to indicate which event queue to bind this completion queue to. This
14543  * function will send the CQ_CREATE mailbox command to the HBA to setup the
14544  * completion queue. This function is asynchronous and will wait for the mailbox
14545  * command to finish before continuing.
14546  *
14547  * On success this function will return a zero. If unable to allocate enough
14548  * memory this function will return -ENOMEM. If the queue create mailbox command
14549  * fails this function will return -ENXIO.
14550  **/
14551 int
14552 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14553                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14554 {
14555         struct lpfc_mbx_cq_create *cq_create;
14556         struct lpfc_dmabuf *dmabuf;
14557         LPFC_MBOXQ_t *mbox;
14558         int rc, length, status = 0;
14559         uint32_t shdr_status, shdr_add_status;
14560         union lpfc_sli4_cfg_shdr *shdr;
14561         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14562
14563         /* sanity check on queue memory */
14564         if (!cq || !eq)
14565                 return -ENODEV;
14566         if (!phba->sli4_hba.pc_sli4_params.supported)
14567                 hw_page_size = cq->page_size;
14568
14569         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14570         if (!mbox)
14571                 return -ENOMEM;
14572         length = (sizeof(struct lpfc_mbx_cq_create) -
14573                   sizeof(struct lpfc_sli4_cfg_mhdr));
14574         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14575                          LPFC_MBOX_OPCODE_CQ_CREATE,
14576                          length, LPFC_SLI4_MBX_EMBED);
14577         cq_create = &mbox->u.mqe.un.cq_create;
14578         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
14579         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14580                     cq->page_count);
14581         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14582         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
14583         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14584                phba->sli4_hba.pc_sli4_params.cqv);
14585         if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
14586                 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14587                        (cq->page_size / SLI4_PAGE_SIZE));
14588                 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14589                        eq->queue_id);
14590                 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14591                        phba->sli4_hba.pc_sli4_params.cqav);
14592         } else {
14593                 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14594                        eq->queue_id);
14595         }
14596         switch (cq->entry_count) {
14597         case 2048:
14598         case 4096:
14599                 if (phba->sli4_hba.pc_sli4_params.cqv ==
14600                     LPFC_Q_CREATE_VERSION_2) {
14601                         cq_create->u.request.context.lpfc_cq_context_count =
14602                                 cq->entry_count;
14603                         bf_set(lpfc_cq_context_count,
14604                                &cq_create->u.request.context,
14605                                LPFC_CQ_CNT_WORD7);
14606                         break;
14607                 }
14608                 /* Fall Thru */
14609         default:
14610                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14611                                 "0361 Unsupported CQ count: "
14612                                 "entry cnt %d sz %d pg cnt %d\n",
14613                                 cq->entry_count, cq->entry_size,
14614                                 cq->page_count);
14615                 if (cq->entry_count < 256) {
14616                         status = -EINVAL;
14617                         goto out;
14618                 }
14619                 /* otherwise default to smallest count (drop through) */
14620         case 256:
14621                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14622                        LPFC_CQ_CNT_256);
14623                 break;
14624         case 512:
14625                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14626                        LPFC_CQ_CNT_512);
14627                 break;
14628         case 1024:
14629                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14630                        LPFC_CQ_CNT_1024);
14631                 break;
14632         }
14633         list_for_each_entry(dmabuf, &cq->page_list, list) {
14634                 memset(dmabuf->virt, 0, cq->page_size);
14635                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14636                                         putPaddrLow(dmabuf->phys);
14637                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14638                                         putPaddrHigh(dmabuf->phys);
14639         }
14640         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14641
14642         /* The IOCTL status is embedded in the mailbox subheader. */
14643         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14644         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14645         if (shdr_status || shdr_add_status || rc) {
14646                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14647                                 "2501 CQ_CREATE mailbox failed with "
14648                                 "status x%x add_status x%x, mbx status x%x\n",
14649                                 shdr_status, shdr_add_status, rc);
14650                 status = -ENXIO;
14651                 goto out;
14652         }
14653         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14654         if (cq->queue_id == 0xFFFF) {
14655                 status = -ENXIO;
14656                 goto out;
14657         }
14658         /* link the cq onto the parent eq child list */
14659         list_add_tail(&cq->list, &eq->child_list);
14660         /* Set up completion queue's type and subtype */
14661         cq->type = type;
14662         cq->subtype = subtype;
14663         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14664         cq->assoc_qid = eq->queue_id;
14665         cq->host_index = 0;
14666         cq->hba_index = 0;
14667         cq->entry_repost = LPFC_CQ_REPOST;
14668
14669 out:
14670         mempool_free(mbox, phba->mbox_mem_pool);
14671         return status;
14672 }
14673
14674 /**
14675  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14676  * @phba: HBA structure that indicates port to create a queue on.
14677  * @cqp: The queue structure array to use to create the completion queues.
14678  * @eqp: The event queue array to bind these completion queues to.
14679  *
14680  * This function creates a set of  completion queue, s to support MRQ
14681  * as detailed in @cqp, on a port,
14682  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14683  *
14684  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14685  * is used to get the entry count and entry size that are necessary to
14686  * determine the number of pages to allocate and use for this queue. The @eq
14687  * is used to indicate which event queue to bind this completion queue to. This
14688  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14689  * completion queue. This function is asynchronous and will wait for the mailbox
14690  * command to finish before continuing.
14691  *
14692  * On success this function will return a zero. If unable to allocate enough
14693  * memory this function will return -ENOMEM. If the queue create mailbox command
14694  * fails this function will return -ENXIO.
14695  **/
14696 int
14697 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14698                    struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14699 {
14700         struct lpfc_queue *cq;
14701         struct lpfc_queue *eq;
14702         struct lpfc_mbx_cq_create_set *cq_set;
14703         struct lpfc_dmabuf *dmabuf;
14704         LPFC_MBOXQ_t *mbox;
14705         int rc, length, alloclen, status = 0;
14706         int cnt, idx, numcq, page_idx = 0;
14707         uint32_t shdr_status, shdr_add_status;
14708         union lpfc_sli4_cfg_shdr *shdr;
14709         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14710
14711         /* sanity check on queue memory */
14712         numcq = phba->cfg_nvmet_mrq;
14713         if (!cqp || !eqp || !numcq)
14714                 return -ENODEV;
14715
14716         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14717         if (!mbox)
14718                 return -ENOMEM;
14719
14720         length = sizeof(struct lpfc_mbx_cq_create_set);
14721         length += ((numcq * cqp[0]->page_count) *
14722                    sizeof(struct dma_address));
14723         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14724                         LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14725                         LPFC_SLI4_MBX_NEMBED);
14726         if (alloclen < length) {
14727                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14728                                 "3098 Allocated DMA memory size (%d) is "
14729                                 "less than the requested DMA memory size "
14730                                 "(%d)\n", alloclen, length);
14731                 status = -ENOMEM;
14732                 goto out;
14733         }
14734         cq_set = mbox->sge_array->addr[0];
14735         shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14736         bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14737
14738         for (idx = 0; idx < numcq; idx++) {
14739                 cq = cqp[idx];
14740                 eq = eqp[idx];
14741                 if (!cq || !eq) {
14742                         status = -ENOMEM;
14743                         goto out;
14744                 }
14745                 if (!phba->sli4_hba.pc_sli4_params.supported)
14746                         hw_page_size = cq->page_size;
14747
14748                 switch (idx) {
14749                 case 0:
14750                         bf_set(lpfc_mbx_cq_create_set_page_size,
14751                                &cq_set->u.request,
14752                                (hw_page_size / SLI4_PAGE_SIZE));
14753                         bf_set(lpfc_mbx_cq_create_set_num_pages,
14754                                &cq_set->u.request, cq->page_count);
14755                         bf_set(lpfc_mbx_cq_create_set_evt,
14756                                &cq_set->u.request, 1);
14757                         bf_set(lpfc_mbx_cq_create_set_valid,
14758                                &cq_set->u.request, 1);
14759                         bf_set(lpfc_mbx_cq_create_set_cqe_size,
14760                                &cq_set->u.request, 0);
14761                         bf_set(lpfc_mbx_cq_create_set_num_cq,
14762                                &cq_set->u.request, numcq);
14763                         bf_set(lpfc_mbx_cq_create_set_autovalid,
14764                                &cq_set->u.request,
14765                                phba->sli4_hba.pc_sli4_params.cqav);
14766                         switch (cq->entry_count) {
14767                         case 2048:
14768                         case 4096:
14769                                 if (phba->sli4_hba.pc_sli4_params.cqv ==
14770                                     LPFC_Q_CREATE_VERSION_2) {
14771                                         bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14772                                                &cq_set->u.request,
14773                                                 cq->entry_count);
14774                                         bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14775                                                &cq_set->u.request,
14776                                                LPFC_CQ_CNT_WORD7);
14777                                         break;
14778                                 }
14779                                 /* Fall Thru */
14780                         default:
14781                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14782                                                 "3118 Bad CQ count. (%d)\n",
14783                                                 cq->entry_count);
14784                                 if (cq->entry_count < 256) {
14785                                         status = -EINVAL;
14786                                         goto out;
14787                                 }
14788                                 /* otherwise default to smallest (drop thru) */
14789                         case 256:
14790                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14791                                        &cq_set->u.request, LPFC_CQ_CNT_256);
14792                                 break;
14793                         case 512:
14794                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14795                                        &cq_set->u.request, LPFC_CQ_CNT_512);
14796                                 break;
14797                         case 1024:
14798                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14799                                        &cq_set->u.request, LPFC_CQ_CNT_1024);
14800                                 break;
14801                         }
14802                         bf_set(lpfc_mbx_cq_create_set_eq_id0,
14803                                &cq_set->u.request, eq->queue_id);
14804                         break;
14805                 case 1:
14806                         bf_set(lpfc_mbx_cq_create_set_eq_id1,
14807                                &cq_set->u.request, eq->queue_id);
14808                         break;
14809                 case 2:
14810                         bf_set(lpfc_mbx_cq_create_set_eq_id2,
14811                                &cq_set->u.request, eq->queue_id);
14812                         break;
14813                 case 3:
14814                         bf_set(lpfc_mbx_cq_create_set_eq_id3,
14815                                &cq_set->u.request, eq->queue_id);
14816                         break;
14817                 case 4:
14818                         bf_set(lpfc_mbx_cq_create_set_eq_id4,
14819                                &cq_set->u.request, eq->queue_id);
14820                         break;
14821                 case 5:
14822                         bf_set(lpfc_mbx_cq_create_set_eq_id5,
14823                                &cq_set->u.request, eq->queue_id);
14824                         break;
14825                 case 6:
14826                         bf_set(lpfc_mbx_cq_create_set_eq_id6,
14827                                &cq_set->u.request, eq->queue_id);
14828                         break;
14829                 case 7:
14830                         bf_set(lpfc_mbx_cq_create_set_eq_id7,
14831                                &cq_set->u.request, eq->queue_id);
14832                         break;
14833                 case 8:
14834                         bf_set(lpfc_mbx_cq_create_set_eq_id8,
14835                                &cq_set->u.request, eq->queue_id);
14836                         break;
14837                 case 9:
14838                         bf_set(lpfc_mbx_cq_create_set_eq_id9,
14839                                &cq_set->u.request, eq->queue_id);
14840                         break;
14841                 case 10:
14842                         bf_set(lpfc_mbx_cq_create_set_eq_id10,
14843                                &cq_set->u.request, eq->queue_id);
14844                         break;
14845                 case 11:
14846                         bf_set(lpfc_mbx_cq_create_set_eq_id11,
14847                                &cq_set->u.request, eq->queue_id);
14848                         break;
14849                 case 12:
14850                         bf_set(lpfc_mbx_cq_create_set_eq_id12,
14851                                &cq_set->u.request, eq->queue_id);
14852                         break;
14853                 case 13:
14854                         bf_set(lpfc_mbx_cq_create_set_eq_id13,
14855                                &cq_set->u.request, eq->queue_id);
14856                         break;
14857                 case 14:
14858                         bf_set(lpfc_mbx_cq_create_set_eq_id14,
14859                                &cq_set->u.request, eq->queue_id);
14860                         break;
14861                 case 15:
14862                         bf_set(lpfc_mbx_cq_create_set_eq_id15,
14863                                &cq_set->u.request, eq->queue_id);
14864                         break;
14865                 }
14866
14867                 /* link the cq onto the parent eq child list */
14868                 list_add_tail(&cq->list, &eq->child_list);
14869                 /* Set up completion queue's type and subtype */
14870                 cq->type = type;
14871                 cq->subtype = subtype;
14872                 cq->assoc_qid = eq->queue_id;
14873                 cq->host_index = 0;
14874                 cq->hba_index = 0;
14875                 cq->entry_repost = LPFC_CQ_REPOST;
14876                 cq->chann = idx;
14877
14878                 rc = 0;
14879                 list_for_each_entry(dmabuf, &cq->page_list, list) {
14880                         memset(dmabuf->virt, 0, hw_page_size);
14881                         cnt = page_idx + dmabuf->buffer_tag;
14882                         cq_set->u.request.page[cnt].addr_lo =
14883                                         putPaddrLow(dmabuf->phys);
14884                         cq_set->u.request.page[cnt].addr_hi =
14885                                         putPaddrHigh(dmabuf->phys);
14886                         rc++;
14887                 }
14888                 page_idx += rc;
14889         }
14890
14891         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14892
14893         /* The IOCTL status is embedded in the mailbox subheader. */
14894         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14895         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14896         if (shdr_status || shdr_add_status || rc) {
14897                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14898                                 "3119 CQ_CREATE_SET mailbox failed with "
14899                                 "status x%x add_status x%x, mbx status x%x\n",
14900                                 shdr_status, shdr_add_status, rc);
14901                 status = -ENXIO;
14902                 goto out;
14903         }
14904         rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14905         if (rc == 0xFFFF) {
14906                 status = -ENXIO;
14907                 goto out;
14908         }
14909
14910         for (idx = 0; idx < numcq; idx++) {
14911                 cq = cqp[idx];
14912                 cq->queue_id = rc + idx;
14913         }
14914
14915 out:
14916         lpfc_sli4_mbox_cmd_free(phba, mbox);
14917         return status;
14918 }
14919
14920 /**
14921  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14922  * @phba: HBA structure that indicates port to create a queue on.
14923  * @mq: The queue structure to use to create the mailbox queue.
14924  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14925  * @cq: The completion queue to associate with this cq.
14926  *
14927  * This function provides failback (fb) functionality when the
14928  * mq_create_ext fails on older FW generations.  It's purpose is identical
14929  * to mq_create_ext otherwise.
14930  *
14931  * This routine cannot fail as all attributes were previously accessed and
14932  * initialized in mq_create_ext.
14933  **/
14934 static void
14935 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14936                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14937 {
14938         struct lpfc_mbx_mq_create *mq_create;
14939         struct lpfc_dmabuf *dmabuf;
14940         int length;
14941
14942         length = (sizeof(struct lpfc_mbx_mq_create) -
14943                   sizeof(struct lpfc_sli4_cfg_mhdr));
14944         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14945                          LPFC_MBOX_OPCODE_MQ_CREATE,
14946                          length, LPFC_SLI4_MBX_EMBED);
14947         mq_create = &mbox->u.mqe.un.mq_create;
14948         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14949                mq->page_count);
14950         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14951                cq->queue_id);
14952         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14953         switch (mq->entry_count) {
14954         case 16:
14955                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14956                        LPFC_MQ_RING_SIZE_16);
14957                 break;
14958         case 32:
14959                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14960                        LPFC_MQ_RING_SIZE_32);
14961                 break;
14962         case 64:
14963                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14964                        LPFC_MQ_RING_SIZE_64);
14965                 break;
14966         case 128:
14967                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14968                        LPFC_MQ_RING_SIZE_128);
14969                 break;
14970         }
14971         list_for_each_entry(dmabuf, &mq->page_list, list) {
14972                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14973                         putPaddrLow(dmabuf->phys);
14974                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14975                         putPaddrHigh(dmabuf->phys);
14976         }
14977 }
14978
14979 /**
14980  * lpfc_mq_create - Create a mailbox Queue on the HBA
14981  * @phba: HBA structure that indicates port to create a queue on.
14982  * @mq: The queue structure to use to create the mailbox queue.
14983  * @cq: The completion queue to associate with this cq.
14984  * @subtype: The queue's subtype.
14985  *
14986  * This function creates a mailbox queue, as detailed in @mq, on a port,
14987  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14988  *
14989  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14990  * is used to get the entry count and entry size that are necessary to
14991  * determine the number of pages to allocate and use for this queue. This
14992  * function will send the MQ_CREATE mailbox command to the HBA to setup the
14993  * mailbox queue. This function is asynchronous and will wait for the mailbox
14994  * command to finish before continuing.
14995  *
14996  * On success this function will return a zero. If unable to allocate enough
14997  * memory this function will return -ENOMEM. If the queue create mailbox command
14998  * fails this function will return -ENXIO.
14999  **/
15000 int32_t
15001 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15002                struct lpfc_queue *cq, uint32_t subtype)
15003 {
15004         struct lpfc_mbx_mq_create *mq_create;
15005         struct lpfc_mbx_mq_create_ext *mq_create_ext;
15006         struct lpfc_dmabuf *dmabuf;
15007         LPFC_MBOXQ_t *mbox;
15008         int rc, length, status = 0;
15009         uint32_t shdr_status, shdr_add_status;
15010         union lpfc_sli4_cfg_shdr *shdr;
15011         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15012
15013         /* sanity check on queue memory */
15014         if (!mq || !cq)
15015                 return -ENODEV;
15016         if (!phba->sli4_hba.pc_sli4_params.supported)
15017                 hw_page_size = SLI4_PAGE_SIZE;
15018
15019         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15020         if (!mbox)
15021                 return -ENOMEM;
15022         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15023                   sizeof(struct lpfc_sli4_cfg_mhdr));
15024         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15025                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15026                          length, LPFC_SLI4_MBX_EMBED);
15027
15028         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
15029         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
15030         bf_set(lpfc_mbx_mq_create_ext_num_pages,
15031                &mq_create_ext->u.request, mq->page_count);
15032         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15033                &mq_create_ext->u.request, 1);
15034         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
15035                &mq_create_ext->u.request, 1);
15036         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15037                &mq_create_ext->u.request, 1);
15038         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15039                &mq_create_ext->u.request, 1);
15040         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15041                &mq_create_ext->u.request, 1);
15042         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
15043         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15044                phba->sli4_hba.pc_sli4_params.mqv);
15045         if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15046                 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15047                        cq->queue_id);
15048         else
15049                 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15050                        cq->queue_id);
15051         switch (mq->entry_count) {
15052         default:
15053                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15054                                 "0362 Unsupported MQ count. (%d)\n",
15055                                 mq->entry_count);
15056                 if (mq->entry_count < 16) {
15057                         status = -EINVAL;
15058                         goto out;
15059                 }
15060                 /* otherwise default to smallest count (drop through) */
15061         case 16:
15062                 bf_set(lpfc_mq_context_ring_size,
15063                        &mq_create_ext->u.request.context,
15064                        LPFC_MQ_RING_SIZE_16);
15065                 break;
15066         case 32:
15067                 bf_set(lpfc_mq_context_ring_size,
15068                        &mq_create_ext->u.request.context,
15069                        LPFC_MQ_RING_SIZE_32);
15070                 break;
15071         case 64:
15072                 bf_set(lpfc_mq_context_ring_size,
15073                        &mq_create_ext->u.request.context,
15074                        LPFC_MQ_RING_SIZE_64);
15075                 break;
15076         case 128:
15077                 bf_set(lpfc_mq_context_ring_size,
15078                        &mq_create_ext->u.request.context,
15079                        LPFC_MQ_RING_SIZE_128);
15080                 break;
15081         }
15082         list_for_each_entry(dmabuf, &mq->page_list, list) {
15083                 memset(dmabuf->virt, 0, hw_page_size);
15084                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15085                                         putPaddrLow(dmabuf->phys);
15086                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
15087                                         putPaddrHigh(dmabuf->phys);
15088         }
15089         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15090         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15091                               &mq_create_ext->u.response);
15092         if (rc != MBX_SUCCESS) {
15093                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15094                                 "2795 MQ_CREATE_EXT failed with "
15095                                 "status x%x. Failback to MQ_CREATE.\n",
15096                                 rc);
15097                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15098                 mq_create = &mbox->u.mqe.un.mq_create;
15099                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15100                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15101                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15102                                       &mq_create->u.response);
15103         }
15104
15105         /* The IOCTL status is embedded in the mailbox subheader. */
15106         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15107         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15108         if (shdr_status || shdr_add_status || rc) {
15109                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15110                                 "2502 MQ_CREATE mailbox failed with "
15111                                 "status x%x add_status x%x, mbx status x%x\n",
15112                                 shdr_status, shdr_add_status, rc);
15113                 status = -ENXIO;
15114                 goto out;
15115         }
15116         if (mq->queue_id == 0xFFFF) {
15117                 status = -ENXIO;
15118                 goto out;
15119         }
15120         mq->type = LPFC_MQ;
15121         mq->assoc_qid = cq->queue_id;
15122         mq->subtype = subtype;
15123         mq->host_index = 0;
15124         mq->hba_index = 0;
15125         mq->entry_repost = LPFC_MQ_REPOST;
15126
15127         /* link the mq onto the parent cq child list */
15128         list_add_tail(&mq->list, &cq->child_list);
15129 out:
15130         mempool_free(mbox, phba->mbox_mem_pool);
15131         return status;
15132 }
15133
15134 /**
15135  * lpfc_wq_create - Create a Work Queue on the HBA
15136  * @phba: HBA structure that indicates port to create a queue on.
15137  * @wq: The queue structure to use to create the work queue.
15138  * @cq: The completion queue to bind this work queue to.
15139  * @subtype: The subtype of the work queue indicating its functionality.
15140  *
15141  * This function creates a work queue, as detailed in @wq, on a port, described
15142  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15143  *
15144  * The @phba struct is used to send mailbox command to HBA. The @wq struct
15145  * is used to get the entry count and entry size that are necessary to
15146  * determine the number of pages to allocate and use for this queue. The @cq
15147  * is used to indicate which completion queue to bind this work queue to. This
15148  * function will send the WQ_CREATE mailbox command to the HBA to setup the
15149  * work queue. This function is asynchronous and will wait for the mailbox
15150  * command to finish before continuing.
15151  *
15152  * On success this function will return a zero. If unable to allocate enough
15153  * memory this function will return -ENOMEM. If the queue create mailbox command
15154  * fails this function will return -ENXIO.
15155  **/
15156 int
15157 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15158                struct lpfc_queue *cq, uint32_t subtype)
15159 {
15160         struct lpfc_mbx_wq_create *wq_create;
15161         struct lpfc_dmabuf *dmabuf;
15162         LPFC_MBOXQ_t *mbox;
15163         int rc, length, status = 0;
15164         uint32_t shdr_status, shdr_add_status;
15165         union lpfc_sli4_cfg_shdr *shdr;
15166         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15167         struct dma_address *page;
15168         void __iomem *bar_memmap_p;
15169         uint32_t db_offset;
15170         uint16_t pci_barset;
15171         uint8_t dpp_barset;
15172         uint32_t dpp_offset;
15173         unsigned long pg_addr;
15174         uint8_t wq_create_version;
15175
15176         /* sanity check on queue memory */
15177         if (!wq || !cq)
15178                 return -ENODEV;
15179         if (!phba->sli4_hba.pc_sli4_params.supported)
15180                 hw_page_size = wq->page_size;
15181
15182         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15183         if (!mbox)
15184                 return -ENOMEM;
15185         length = (sizeof(struct lpfc_mbx_wq_create) -
15186                   sizeof(struct lpfc_sli4_cfg_mhdr));
15187         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15188                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15189                          length, LPFC_SLI4_MBX_EMBED);
15190         wq_create = &mbox->u.mqe.un.wq_create;
15191         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
15192         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15193                     wq->page_count);
15194         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15195                     cq->queue_id);
15196
15197         /* wqv is the earliest version supported, NOT the latest */
15198         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15199                phba->sli4_hba.pc_sli4_params.wqv);
15200
15201         if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15202             (wq->page_size > SLI4_PAGE_SIZE))
15203                 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15204         else
15205                 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15206
15207
15208         if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15209                 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15210         else
15211                 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15212
15213         switch (wq_create_version) {
15214         case LPFC_Q_CREATE_VERSION_1:
15215                 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15216                        wq->entry_count);
15217                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15218                        LPFC_Q_CREATE_VERSION_1);
15219
15220                 switch (wq->entry_size) {
15221                 default:
15222                 case 64:
15223                         bf_set(lpfc_mbx_wq_create_wqe_size,
15224                                &wq_create->u.request_1,
15225                                LPFC_WQ_WQE_SIZE_64);
15226                         break;
15227                 case 128:
15228                         bf_set(lpfc_mbx_wq_create_wqe_size,
15229                                &wq_create->u.request_1,
15230                                LPFC_WQ_WQE_SIZE_128);
15231                         break;
15232                 }
15233                 /* Request DPP by default */
15234                 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
15235                 bf_set(lpfc_mbx_wq_create_page_size,
15236                        &wq_create->u.request_1,
15237                        (wq->page_size / SLI4_PAGE_SIZE));
15238                 page = wq_create->u.request_1.page;
15239                 break;
15240         default:
15241                 page = wq_create->u.request.page;
15242                 break;
15243         }
15244
15245         list_for_each_entry(dmabuf, &wq->page_list, list) {
15246                 memset(dmabuf->virt, 0, hw_page_size);
15247                 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15248                 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
15249         }
15250
15251         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15252                 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15253
15254         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15255         /* The IOCTL status is embedded in the mailbox subheader. */
15256         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15257         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15258         if (shdr_status || shdr_add_status || rc) {
15259                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15260                                 "2503 WQ_CREATE mailbox failed with "
15261                                 "status x%x add_status x%x, mbx status x%x\n",
15262                                 shdr_status, shdr_add_status, rc);
15263                 status = -ENXIO;
15264                 goto out;
15265         }
15266
15267         if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15268                 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15269                                         &wq_create->u.response);
15270         else
15271                 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15272                                         &wq_create->u.response_1);
15273
15274         if (wq->queue_id == 0xFFFF) {
15275                 status = -ENXIO;
15276                 goto out;
15277         }
15278
15279         wq->db_format = LPFC_DB_LIST_FORMAT;
15280         if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15281                 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15282                         wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15283                                                &wq_create->u.response);
15284                         if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15285                             (wq->db_format != LPFC_DB_RING_FORMAT)) {
15286                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15287                                                 "3265 WQ[%d] doorbell format "
15288                                                 "not supported: x%x\n",
15289                                                 wq->queue_id, wq->db_format);
15290                                 status = -EINVAL;
15291                                 goto out;
15292                         }
15293                         pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15294                                             &wq_create->u.response);
15295                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15296                                                                    pci_barset);
15297                         if (!bar_memmap_p) {
15298                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15299                                                 "3263 WQ[%d] failed to memmap "
15300                                                 "pci barset:x%x\n",
15301                                                 wq->queue_id, pci_barset);
15302                                 status = -ENOMEM;
15303                                 goto out;
15304                         }
15305                         db_offset = wq_create->u.response.doorbell_offset;
15306                         if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15307                             (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15308                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15309                                                 "3252 WQ[%d] doorbell offset "
15310                                                 "not supported: x%x\n",
15311                                                 wq->queue_id, db_offset);
15312                                 status = -EINVAL;
15313                                 goto out;
15314                         }
15315                         wq->db_regaddr = bar_memmap_p + db_offset;
15316                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15317                                         "3264 WQ[%d]: barset:x%x, offset:x%x, "
15318                                         "format:x%x\n", wq->queue_id,
15319                                         pci_barset, db_offset, wq->db_format);
15320                 } else
15321                         wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15322         } else {
15323                 /* Check if DPP was honored by the firmware */
15324                 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15325                                     &wq_create->u.response_1);
15326                 if (wq->dpp_enable) {
15327                         pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15328                                             &wq_create->u.response_1);
15329                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15330                                                                    pci_barset);
15331                         if (!bar_memmap_p) {
15332                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15333                                                 "3267 WQ[%d] failed to memmap "
15334                                                 "pci barset:x%x\n",
15335                                                 wq->queue_id, pci_barset);
15336                                 status = -ENOMEM;
15337                                 goto out;
15338                         }
15339                         db_offset = wq_create->u.response_1.doorbell_offset;
15340                         wq->db_regaddr = bar_memmap_p + db_offset;
15341                         wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15342                                             &wq_create->u.response_1);
15343                         dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15344                                             &wq_create->u.response_1);
15345                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15346                                                                    dpp_barset);
15347                         if (!bar_memmap_p) {
15348                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15349                                                 "3268 WQ[%d] failed to memmap "
15350                                                 "pci barset:x%x\n",
15351                                                 wq->queue_id, dpp_barset);
15352                                 status = -ENOMEM;
15353                                 goto out;
15354                         }
15355                         dpp_offset = wq_create->u.response_1.dpp_offset;
15356                         wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15357                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15358                                         "3271 WQ[%d]: barset:x%x, offset:x%x, "
15359                                         "dpp_id:x%x dpp_barset:x%x "
15360                                         "dpp_offset:x%x\n",
15361                                         wq->queue_id, pci_barset, db_offset,
15362                                         wq->dpp_id, dpp_barset, dpp_offset);
15363
15364                         /* Enable combined writes for DPP aperture */
15365                         pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15366 #ifdef CONFIG_X86
15367                         rc = set_memory_wc(pg_addr, 1);
15368                         if (rc) {
15369                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15370                                         "3272 Cannot setup Combined "
15371                                         "Write on WQ[%d] - disable DPP\n",
15372                                         wq->queue_id);
15373                                 phba->cfg_enable_dpp = 0;
15374                         }
15375 #else
15376                         phba->cfg_enable_dpp = 0;
15377 #endif
15378                 } else
15379                         wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15380         }
15381         wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15382         if (wq->pring == NULL) {
15383                 status = -ENOMEM;
15384                 goto out;
15385         }
15386         wq->type = LPFC_WQ;
15387         wq->assoc_qid = cq->queue_id;
15388         wq->subtype = subtype;
15389         wq->host_index = 0;
15390         wq->hba_index = 0;
15391         wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
15392
15393         /* link the wq onto the parent cq child list */
15394         list_add_tail(&wq->list, &cq->child_list);
15395 out:
15396         mempool_free(mbox, phba->mbox_mem_pool);
15397         return status;
15398 }
15399
15400 /**
15401  * lpfc_rq_create - Create a Receive Queue on the HBA
15402  * @phba: HBA structure that indicates port to create a queue on.
15403  * @hrq: The queue structure to use to create the header receive queue.
15404  * @drq: The queue structure to use to create the data receive queue.
15405  * @cq: The completion queue to bind this work queue to.
15406  *
15407  * This function creates a receive buffer queue pair , as detailed in @hrq and
15408  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15409  * to the HBA.
15410  *
15411  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15412  * struct is used to get the entry count that is necessary to determine the
15413  * number of pages to use for this queue. The @cq is used to indicate which
15414  * completion queue to bind received buffers that are posted to these queues to.
15415  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15416  * receive queue pair. This function is asynchronous and will wait for the
15417  * mailbox command to finish before continuing.
15418  *
15419  * On success this function will return a zero. If unable to allocate enough
15420  * memory this function will return -ENOMEM. If the queue create mailbox command
15421  * fails this function will return -ENXIO.
15422  **/
15423 int
15424 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15425                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15426 {
15427         struct lpfc_mbx_rq_create *rq_create;
15428         struct lpfc_dmabuf *dmabuf;
15429         LPFC_MBOXQ_t *mbox;
15430         int rc, length, status = 0;
15431         uint32_t shdr_status, shdr_add_status;
15432         union lpfc_sli4_cfg_shdr *shdr;
15433         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15434         void __iomem *bar_memmap_p;
15435         uint32_t db_offset;
15436         uint16_t pci_barset;
15437
15438         /* sanity check on queue memory */
15439         if (!hrq || !drq || !cq)
15440                 return -ENODEV;
15441         if (!phba->sli4_hba.pc_sli4_params.supported)
15442                 hw_page_size = SLI4_PAGE_SIZE;
15443
15444         if (hrq->entry_count != drq->entry_count)
15445                 return -EINVAL;
15446         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15447         if (!mbox)
15448                 return -ENOMEM;
15449         length = (sizeof(struct lpfc_mbx_rq_create) -
15450                   sizeof(struct lpfc_sli4_cfg_mhdr));
15451         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15452                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15453                          length, LPFC_SLI4_MBX_EMBED);
15454         rq_create = &mbox->u.mqe.un.rq_create;
15455         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15456         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15457                phba->sli4_hba.pc_sli4_params.rqv);
15458         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15459                 bf_set(lpfc_rq_context_rqe_count_1,
15460                        &rq_create->u.request.context,
15461                        hrq->entry_count);
15462                 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
15463                 bf_set(lpfc_rq_context_rqe_size,
15464                        &rq_create->u.request.context,
15465                        LPFC_RQE_SIZE_8);
15466                 bf_set(lpfc_rq_context_page_size,
15467                        &rq_create->u.request.context,
15468                        LPFC_RQ_PAGE_SIZE_4096);
15469         } else {
15470                 switch (hrq->entry_count) {
15471                 default:
15472                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15473                                         "2535 Unsupported RQ count. (%d)\n",
15474                                         hrq->entry_count);
15475                         if (hrq->entry_count < 512) {
15476                                 status = -EINVAL;
15477                                 goto out;
15478                         }
15479                         /* otherwise default to smallest count (drop through) */
15480                 case 512:
15481                         bf_set(lpfc_rq_context_rqe_count,
15482                                &rq_create->u.request.context,
15483                                LPFC_RQ_RING_SIZE_512);
15484                         break;
15485                 case 1024:
15486                         bf_set(lpfc_rq_context_rqe_count,
15487                                &rq_create->u.request.context,
15488                                LPFC_RQ_RING_SIZE_1024);
15489                         break;
15490                 case 2048:
15491                         bf_set(lpfc_rq_context_rqe_count,
15492                                &rq_create->u.request.context,
15493                                LPFC_RQ_RING_SIZE_2048);
15494                         break;
15495                 case 4096:
15496                         bf_set(lpfc_rq_context_rqe_count,
15497                                &rq_create->u.request.context,
15498                                LPFC_RQ_RING_SIZE_4096);
15499                         break;
15500                 }
15501                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15502                        LPFC_HDR_BUF_SIZE);
15503         }
15504         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15505                cq->queue_id);
15506         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15507                hrq->page_count);
15508         list_for_each_entry(dmabuf, &hrq->page_list, list) {
15509                 memset(dmabuf->virt, 0, hw_page_size);
15510                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15511                                         putPaddrLow(dmabuf->phys);
15512                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15513                                         putPaddrHigh(dmabuf->phys);
15514         }
15515         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15516                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15517
15518         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15519         /* The IOCTL status is embedded in the mailbox subheader. */
15520         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15521         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15522         if (shdr_status || shdr_add_status || rc) {
15523                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15524                                 "2504 RQ_CREATE mailbox failed with "
15525                                 "status x%x add_status x%x, mbx status x%x\n",
15526                                 shdr_status, shdr_add_status, rc);
15527                 status = -ENXIO;
15528                 goto out;
15529         }
15530         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15531         if (hrq->queue_id == 0xFFFF) {
15532                 status = -ENXIO;
15533                 goto out;
15534         }
15535
15536         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15537                 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15538                                         &rq_create->u.response);
15539                 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15540                     (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15541                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15542                                         "3262 RQ [%d] doorbell format not "
15543                                         "supported: x%x\n", hrq->queue_id,
15544                                         hrq->db_format);
15545                         status = -EINVAL;
15546                         goto out;
15547                 }
15548
15549                 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15550                                     &rq_create->u.response);
15551                 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15552                 if (!bar_memmap_p) {
15553                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15554                                         "3269 RQ[%d] failed to memmap pci "
15555                                         "barset:x%x\n", hrq->queue_id,
15556                                         pci_barset);
15557                         status = -ENOMEM;
15558                         goto out;
15559                 }
15560
15561                 db_offset = rq_create->u.response.doorbell_offset;
15562                 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15563                     (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15564                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15565                                         "3270 RQ[%d] doorbell offset not "
15566                                         "supported: x%x\n", hrq->queue_id,
15567                                         db_offset);
15568                         status = -EINVAL;
15569                         goto out;
15570                 }
15571                 hrq->db_regaddr = bar_memmap_p + db_offset;
15572                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15573                                 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15574                                 "format:x%x\n", hrq->queue_id, pci_barset,
15575                                 db_offset, hrq->db_format);
15576         } else {
15577                 hrq->db_format = LPFC_DB_RING_FORMAT;
15578                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15579         }
15580         hrq->type = LPFC_HRQ;
15581         hrq->assoc_qid = cq->queue_id;
15582         hrq->subtype = subtype;
15583         hrq->host_index = 0;
15584         hrq->hba_index = 0;
15585         hrq->entry_repost = LPFC_RQ_REPOST;
15586
15587         /* now create the data queue */
15588         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15589                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15590                          length, LPFC_SLI4_MBX_EMBED);
15591         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15592                phba->sli4_hba.pc_sli4_params.rqv);
15593         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15594                 bf_set(lpfc_rq_context_rqe_count_1,
15595                        &rq_create->u.request.context, hrq->entry_count);
15596                 if (subtype == LPFC_NVMET)
15597                         rq_create->u.request.context.buffer_size =
15598                                 LPFC_NVMET_DATA_BUF_SIZE;
15599                 else
15600                         rq_create->u.request.context.buffer_size =
15601                                 LPFC_DATA_BUF_SIZE;
15602                 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15603                        LPFC_RQE_SIZE_8);
15604                 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15605                        (PAGE_SIZE/SLI4_PAGE_SIZE));
15606         } else {
15607                 switch (drq->entry_count) {
15608                 default:
15609                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15610                                         "2536 Unsupported RQ count. (%d)\n",
15611                                         drq->entry_count);
15612                         if (drq->entry_count < 512) {
15613                                 status = -EINVAL;
15614                                 goto out;
15615                         }
15616                         /* otherwise default to smallest count (drop through) */
15617                 case 512:
15618                         bf_set(lpfc_rq_context_rqe_count,
15619                                &rq_create->u.request.context,
15620                                LPFC_RQ_RING_SIZE_512);
15621                         break;
15622                 case 1024:
15623                         bf_set(lpfc_rq_context_rqe_count,
15624                                &rq_create->u.request.context,
15625                                LPFC_RQ_RING_SIZE_1024);
15626                         break;
15627                 case 2048:
15628                         bf_set(lpfc_rq_context_rqe_count,
15629                                &rq_create->u.request.context,
15630                                LPFC_RQ_RING_SIZE_2048);
15631                         break;
15632                 case 4096:
15633                         bf_set(lpfc_rq_context_rqe_count,
15634                                &rq_create->u.request.context,
15635                                LPFC_RQ_RING_SIZE_4096);
15636                         break;
15637                 }
15638                 if (subtype == LPFC_NVMET)
15639                         bf_set(lpfc_rq_context_buf_size,
15640                                &rq_create->u.request.context,
15641                                LPFC_NVMET_DATA_BUF_SIZE);
15642                 else
15643                         bf_set(lpfc_rq_context_buf_size,
15644                                &rq_create->u.request.context,
15645                                LPFC_DATA_BUF_SIZE);
15646         }
15647         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15648                cq->queue_id);
15649         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15650                drq->page_count);
15651         list_for_each_entry(dmabuf, &drq->page_list, list) {
15652                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15653                                         putPaddrLow(dmabuf->phys);
15654                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15655                                         putPaddrHigh(dmabuf->phys);
15656         }
15657         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15658                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15659         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15660         /* The IOCTL status is embedded in the mailbox subheader. */
15661         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15662         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15663         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15664         if (shdr_status || shdr_add_status || rc) {
15665                 status = -ENXIO;
15666                 goto out;
15667         }
15668         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15669         if (drq->queue_id == 0xFFFF) {
15670                 status = -ENXIO;
15671                 goto out;
15672         }
15673         drq->type = LPFC_DRQ;
15674         drq->assoc_qid = cq->queue_id;
15675         drq->subtype = subtype;
15676         drq->host_index = 0;
15677         drq->hba_index = 0;
15678         drq->entry_repost = LPFC_RQ_REPOST;
15679
15680         /* link the header and data RQs onto the parent cq child list */
15681         list_add_tail(&hrq->list, &cq->child_list);
15682         list_add_tail(&drq->list, &cq->child_list);
15683
15684 out:
15685         mempool_free(mbox, phba->mbox_mem_pool);
15686         return status;
15687 }
15688
15689 /**
15690  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15691  * @phba: HBA structure that indicates port to create a queue on.
15692  * @hrqp: The queue structure array to use to create the header receive queues.
15693  * @drqp: The queue structure array to use to create the data receive queues.
15694  * @cqp: The completion queue array to bind these receive queues to.
15695  *
15696  * This function creates a receive buffer queue pair , as detailed in @hrq and
15697  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15698  * to the HBA.
15699  *
15700  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15701  * struct is used to get the entry count that is necessary to determine the
15702  * number of pages to use for this queue. The @cq is used to indicate which
15703  * completion queue to bind received buffers that are posted to these queues to.
15704  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15705  * receive queue pair. This function is asynchronous and will wait for the
15706  * mailbox command to finish before continuing.
15707  *
15708  * On success this function will return a zero. If unable to allocate enough
15709  * memory this function will return -ENOMEM. If the queue create mailbox command
15710  * fails this function will return -ENXIO.
15711  **/
15712 int
15713 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15714                 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15715                 uint32_t subtype)
15716 {
15717         struct lpfc_queue *hrq, *drq, *cq;
15718         struct lpfc_mbx_rq_create_v2 *rq_create;
15719         struct lpfc_dmabuf *dmabuf;
15720         LPFC_MBOXQ_t *mbox;
15721         int rc, length, alloclen, status = 0;
15722         int cnt, idx, numrq, page_idx = 0;
15723         uint32_t shdr_status, shdr_add_status;
15724         union lpfc_sli4_cfg_shdr *shdr;
15725         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15726
15727         numrq = phba->cfg_nvmet_mrq;
15728         /* sanity check on array memory */
15729         if (!hrqp || !drqp || !cqp || !numrq)
15730                 return -ENODEV;
15731         if (!phba->sli4_hba.pc_sli4_params.supported)
15732                 hw_page_size = SLI4_PAGE_SIZE;
15733
15734         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15735         if (!mbox)
15736                 return -ENOMEM;
15737
15738         length = sizeof(struct lpfc_mbx_rq_create_v2);
15739         length += ((2 * numrq * hrqp[0]->page_count) *
15740                    sizeof(struct dma_address));
15741
15742         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15743                                     LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15744                                     LPFC_SLI4_MBX_NEMBED);
15745         if (alloclen < length) {
15746                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15747                                 "3099 Allocated DMA memory size (%d) is "
15748                                 "less than the requested DMA memory size "
15749                                 "(%d)\n", alloclen, length);
15750                 status = -ENOMEM;
15751                 goto out;
15752         }
15753
15754
15755
15756         rq_create = mbox->sge_array->addr[0];
15757         shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15758
15759         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15760         cnt = 0;
15761
15762         for (idx = 0; idx < numrq; idx++) {
15763                 hrq = hrqp[idx];
15764                 drq = drqp[idx];
15765                 cq  = cqp[idx];
15766
15767                 /* sanity check on queue memory */
15768                 if (!hrq || !drq || !cq) {
15769                         status = -ENODEV;
15770                         goto out;
15771                 }
15772
15773                 if (hrq->entry_count != drq->entry_count) {
15774                         status = -EINVAL;
15775                         goto out;
15776                 }
15777
15778                 if (idx == 0) {
15779                         bf_set(lpfc_mbx_rq_create_num_pages,
15780                                &rq_create->u.request,
15781                                hrq->page_count);
15782                         bf_set(lpfc_mbx_rq_create_rq_cnt,
15783                                &rq_create->u.request, (numrq * 2));
15784                         bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15785                                1);
15786                         bf_set(lpfc_rq_context_base_cq,
15787                                &rq_create->u.request.context,
15788                                cq->queue_id);
15789                         bf_set(lpfc_rq_context_data_size,
15790                                &rq_create->u.request.context,
15791                                LPFC_NVMET_DATA_BUF_SIZE);
15792                         bf_set(lpfc_rq_context_hdr_size,
15793                                &rq_create->u.request.context,
15794                                LPFC_HDR_BUF_SIZE);
15795                         bf_set(lpfc_rq_context_rqe_count_1,
15796                                &rq_create->u.request.context,
15797                                hrq->entry_count);
15798                         bf_set(lpfc_rq_context_rqe_size,
15799                                &rq_create->u.request.context,
15800                                LPFC_RQE_SIZE_8);
15801                         bf_set(lpfc_rq_context_page_size,
15802                                &rq_create->u.request.context,
15803                                (PAGE_SIZE/SLI4_PAGE_SIZE));
15804                 }
15805                 rc = 0;
15806                 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15807                         memset(dmabuf->virt, 0, hw_page_size);
15808                         cnt = page_idx + dmabuf->buffer_tag;
15809                         rq_create->u.request.page[cnt].addr_lo =
15810                                         putPaddrLow(dmabuf->phys);
15811                         rq_create->u.request.page[cnt].addr_hi =
15812                                         putPaddrHigh(dmabuf->phys);
15813                         rc++;
15814                 }
15815                 page_idx += rc;
15816
15817                 rc = 0;
15818                 list_for_each_entry(dmabuf, &drq->page_list, list) {
15819                         memset(dmabuf->virt, 0, hw_page_size);
15820                         cnt = page_idx + dmabuf->buffer_tag;
15821                         rq_create->u.request.page[cnt].addr_lo =
15822                                         putPaddrLow(dmabuf->phys);
15823                         rq_create->u.request.page[cnt].addr_hi =
15824                                         putPaddrHigh(dmabuf->phys);
15825                         rc++;
15826                 }
15827                 page_idx += rc;
15828
15829                 hrq->db_format = LPFC_DB_RING_FORMAT;
15830                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15831                 hrq->type = LPFC_HRQ;
15832                 hrq->assoc_qid = cq->queue_id;
15833                 hrq->subtype = subtype;
15834                 hrq->host_index = 0;
15835                 hrq->hba_index = 0;
15836                 hrq->entry_repost = LPFC_RQ_REPOST;
15837
15838                 drq->db_format = LPFC_DB_RING_FORMAT;
15839                 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15840                 drq->type = LPFC_DRQ;
15841                 drq->assoc_qid = cq->queue_id;
15842                 drq->subtype = subtype;
15843                 drq->host_index = 0;
15844                 drq->hba_index = 0;
15845                 drq->entry_repost = LPFC_RQ_REPOST;
15846
15847                 list_add_tail(&hrq->list, &cq->child_list);
15848                 list_add_tail(&drq->list, &cq->child_list);
15849         }
15850
15851         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15852         /* The IOCTL status is embedded in the mailbox subheader. */
15853         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15854         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15855         if (shdr_status || shdr_add_status || rc) {
15856                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15857                                 "3120 RQ_CREATE mailbox failed with "
15858                                 "status x%x add_status x%x, mbx status x%x\n",
15859                                 shdr_status, shdr_add_status, rc);
15860                 status = -ENXIO;
15861                 goto out;
15862         }
15863         rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15864         if (rc == 0xFFFF) {
15865                 status = -ENXIO;
15866                 goto out;
15867         }
15868
15869         /* Initialize all RQs with associated queue id */
15870         for (idx = 0; idx < numrq; idx++) {
15871                 hrq = hrqp[idx];
15872                 hrq->queue_id = rc + (2 * idx);
15873                 drq = drqp[idx];
15874                 drq->queue_id = rc + (2 * idx) + 1;
15875         }
15876
15877 out:
15878         lpfc_sli4_mbox_cmd_free(phba, mbox);
15879         return status;
15880 }
15881
15882 /**
15883  * lpfc_eq_destroy - Destroy an event Queue on the HBA
15884  * @eq: The queue structure associated with the queue to destroy.
15885  *
15886  * This function destroys a queue, as detailed in @eq by sending an mailbox
15887  * command, specific to the type of queue, to the HBA.
15888  *
15889  * The @eq struct is used to get the queue ID of the queue to destroy.
15890  *
15891  * On success this function will return a zero. If the queue destroy mailbox
15892  * command fails this function will return -ENXIO.
15893  **/
15894 int
15895 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15896 {
15897         LPFC_MBOXQ_t *mbox;
15898         int rc, length, status = 0;
15899         uint32_t shdr_status, shdr_add_status;
15900         union lpfc_sli4_cfg_shdr *shdr;
15901
15902         /* sanity check on queue memory */
15903         if (!eq)
15904                 return -ENODEV;
15905         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15906         if (!mbox)
15907                 return -ENOMEM;
15908         length = (sizeof(struct lpfc_mbx_eq_destroy) -
15909                   sizeof(struct lpfc_sli4_cfg_mhdr));
15910         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15911                          LPFC_MBOX_OPCODE_EQ_DESTROY,
15912                          length, LPFC_SLI4_MBX_EMBED);
15913         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15914                eq->queue_id);
15915         mbox->vport = eq->phba->pport;
15916         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15917
15918         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15919         /* The IOCTL status is embedded in the mailbox subheader. */
15920         shdr = (union lpfc_sli4_cfg_shdr *)
15921                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15922         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15923         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15924         if (shdr_status || shdr_add_status || rc) {
15925                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15926                                 "2505 EQ_DESTROY mailbox failed with "
15927                                 "status x%x add_status x%x, mbx status x%x\n",
15928                                 shdr_status, shdr_add_status, rc);
15929                 status = -ENXIO;
15930         }
15931
15932         /* Remove eq from any list */
15933         list_del_init(&eq->list);
15934         mempool_free(mbox, eq->phba->mbox_mem_pool);
15935         return status;
15936 }
15937
15938 /**
15939  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15940  * @cq: The queue structure associated with the queue to destroy.
15941  *
15942  * This function destroys a queue, as detailed in @cq by sending an mailbox
15943  * command, specific to the type of queue, to the HBA.
15944  *
15945  * The @cq struct is used to get the queue ID of the queue to destroy.
15946  *
15947  * On success this function will return a zero. If the queue destroy mailbox
15948  * command fails this function will return -ENXIO.
15949  **/
15950 int
15951 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15952 {
15953         LPFC_MBOXQ_t *mbox;
15954         int rc, length, status = 0;
15955         uint32_t shdr_status, shdr_add_status;
15956         union lpfc_sli4_cfg_shdr *shdr;
15957
15958         /* sanity check on queue memory */
15959         if (!cq)
15960                 return -ENODEV;
15961         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15962         if (!mbox)
15963                 return -ENOMEM;
15964         length = (sizeof(struct lpfc_mbx_cq_destroy) -
15965                   sizeof(struct lpfc_sli4_cfg_mhdr));
15966         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15967                          LPFC_MBOX_OPCODE_CQ_DESTROY,
15968                          length, LPFC_SLI4_MBX_EMBED);
15969         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15970                cq->queue_id);
15971         mbox->vport = cq->phba->pport;
15972         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15973         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15974         /* The IOCTL status is embedded in the mailbox subheader. */
15975         shdr = (union lpfc_sli4_cfg_shdr *)
15976                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15977         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15978         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15979         if (shdr_status || shdr_add_status || rc) {
15980                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15981                                 "2506 CQ_DESTROY mailbox failed with "
15982                                 "status x%x add_status x%x, mbx status x%x\n",
15983                                 shdr_status, shdr_add_status, rc);
15984                 status = -ENXIO;
15985         }
15986         /* Remove cq from any list */
15987         list_del_init(&cq->list);
15988         mempool_free(mbox, cq->phba->mbox_mem_pool);
15989         return status;
15990 }
15991
15992 /**
15993  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15994  * @qm: The queue structure associated with the queue to destroy.
15995  *
15996  * This function destroys a queue, as detailed in @mq by sending an mailbox
15997  * command, specific to the type of queue, to the HBA.
15998  *
15999  * The @mq struct is used to get the queue ID of the queue to destroy.
16000  *
16001  * On success this function will return a zero. If the queue destroy mailbox
16002  * command fails this function will return -ENXIO.
16003  **/
16004 int
16005 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16006 {
16007         LPFC_MBOXQ_t *mbox;
16008         int rc, length, status = 0;
16009         uint32_t shdr_status, shdr_add_status;
16010         union lpfc_sli4_cfg_shdr *shdr;
16011
16012         /* sanity check on queue memory */
16013         if (!mq)
16014                 return -ENODEV;
16015         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16016         if (!mbox)
16017                 return -ENOMEM;
16018         length = (sizeof(struct lpfc_mbx_mq_destroy) -
16019                   sizeof(struct lpfc_sli4_cfg_mhdr));
16020         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16021                          LPFC_MBOX_OPCODE_MQ_DESTROY,
16022                          length, LPFC_SLI4_MBX_EMBED);
16023         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16024                mq->queue_id);
16025         mbox->vport = mq->phba->pport;
16026         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16027         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16028         /* The IOCTL status is embedded in the mailbox subheader. */
16029         shdr = (union lpfc_sli4_cfg_shdr *)
16030                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16031         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16032         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16033         if (shdr_status || shdr_add_status || rc) {
16034                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16035                                 "2507 MQ_DESTROY mailbox failed with "
16036                                 "status x%x add_status x%x, mbx status x%x\n",
16037                                 shdr_status, shdr_add_status, rc);
16038                 status = -ENXIO;
16039         }
16040         /* Remove mq from any list */
16041         list_del_init(&mq->list);
16042         mempool_free(mbox, mq->phba->mbox_mem_pool);
16043         return status;
16044 }
16045
16046 /**
16047  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16048  * @wq: The queue structure associated with the queue to destroy.
16049  *
16050  * This function destroys a queue, as detailed in @wq by sending an mailbox
16051  * command, specific to the type of queue, to the HBA.
16052  *
16053  * The @wq struct is used to get the queue ID of the queue to destroy.
16054  *
16055  * On success this function will return a zero. If the queue destroy mailbox
16056  * command fails this function will return -ENXIO.
16057  **/
16058 int
16059 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16060 {
16061         LPFC_MBOXQ_t *mbox;
16062         int rc, length, status = 0;
16063         uint32_t shdr_status, shdr_add_status;
16064         union lpfc_sli4_cfg_shdr *shdr;
16065
16066         /* sanity check on queue memory */
16067         if (!wq)
16068                 return -ENODEV;
16069         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16070         if (!mbox)
16071                 return -ENOMEM;
16072         length = (sizeof(struct lpfc_mbx_wq_destroy) -
16073                   sizeof(struct lpfc_sli4_cfg_mhdr));
16074         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16075                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16076                          length, LPFC_SLI4_MBX_EMBED);
16077         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16078                wq->queue_id);
16079         mbox->vport = wq->phba->pport;
16080         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16081         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16082         shdr = (union lpfc_sli4_cfg_shdr *)
16083                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16084         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16085         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16086         if (shdr_status || shdr_add_status || rc) {
16087                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16088                                 "2508 WQ_DESTROY mailbox failed with "
16089                                 "status x%x add_status x%x, mbx status x%x\n",
16090                                 shdr_status, shdr_add_status, rc);
16091                 status = -ENXIO;
16092         }
16093         /* Remove wq from any list */
16094         list_del_init(&wq->list);
16095         kfree(wq->pring);
16096         wq->pring = NULL;
16097         mempool_free(mbox, wq->phba->mbox_mem_pool);
16098         return status;
16099 }
16100
16101 /**
16102  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16103  * @rq: The queue structure associated with the queue to destroy.
16104  *
16105  * This function destroys a queue, as detailed in @rq by sending an mailbox
16106  * command, specific to the type of queue, to the HBA.
16107  *
16108  * The @rq struct is used to get the queue ID of the queue to destroy.
16109  *
16110  * On success this function will return a zero. If the queue destroy mailbox
16111  * command fails this function will return -ENXIO.
16112  **/
16113 int
16114 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16115                 struct lpfc_queue *drq)
16116 {
16117         LPFC_MBOXQ_t *mbox;
16118         int rc, length, status = 0;
16119         uint32_t shdr_status, shdr_add_status;
16120         union lpfc_sli4_cfg_shdr *shdr;
16121
16122         /* sanity check on queue memory */
16123         if (!hrq || !drq)
16124                 return -ENODEV;
16125         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16126         if (!mbox)
16127                 return -ENOMEM;
16128         length = (sizeof(struct lpfc_mbx_rq_destroy) -
16129                   sizeof(struct lpfc_sli4_cfg_mhdr));
16130         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16131                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16132                          length, LPFC_SLI4_MBX_EMBED);
16133         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16134                hrq->queue_id);
16135         mbox->vport = hrq->phba->pport;
16136         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16137         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16138         /* The IOCTL status is embedded in the mailbox subheader. */
16139         shdr = (union lpfc_sli4_cfg_shdr *)
16140                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16141         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16142         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16143         if (shdr_status || shdr_add_status || rc) {
16144                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16145                                 "2509 RQ_DESTROY mailbox failed with "
16146                                 "status x%x add_status x%x, mbx status x%x\n",
16147                                 shdr_status, shdr_add_status, rc);
16148                 if (rc != MBX_TIMEOUT)
16149                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
16150                 return -ENXIO;
16151         }
16152         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16153                drq->queue_id);
16154         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16155         shdr = (union lpfc_sli4_cfg_shdr *)
16156                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16157         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16158         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16159         if (shdr_status || shdr_add_status || rc) {
16160                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16161                                 "2510 RQ_DESTROY mailbox failed with "
16162                                 "status x%x add_status x%x, mbx status x%x\n",
16163                                 shdr_status, shdr_add_status, rc);
16164                 status = -ENXIO;
16165         }
16166         list_del_init(&hrq->list);
16167         list_del_init(&drq->list);
16168         mempool_free(mbox, hrq->phba->mbox_mem_pool);
16169         return status;
16170 }
16171
16172 /**
16173  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16174  * @phba: The virtual port for which this call being executed.
16175  * @pdma_phys_addr0: Physical address of the 1st SGL page.
16176  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16177  * @xritag: the xritag that ties this io to the SGL pages.
16178  *
16179  * This routine will post the sgl pages for the IO that has the xritag
16180  * that is in the iocbq structure. The xritag is assigned during iocbq
16181  * creation and persists for as long as the driver is loaded.
16182  * if the caller has fewer than 256 scatter gather segments to map then
16183  * pdma_phys_addr1 should be 0.
16184  * If the caller needs to map more than 256 scatter gather segment then
16185  * pdma_phys_addr1 should be a valid physical address.
16186  * physical address for SGLs must be 64 byte aligned.
16187  * If you are going to map 2 SGL's then the first one must have 256 entries
16188  * the second sgl can have between 1 and 256 entries.
16189  *
16190  * Return codes:
16191  *      0 - Success
16192  *      -ENXIO, -ENOMEM - Failure
16193  **/
16194 int
16195 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16196                 dma_addr_t pdma_phys_addr0,
16197                 dma_addr_t pdma_phys_addr1,
16198                 uint16_t xritag)
16199 {
16200         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16201         LPFC_MBOXQ_t *mbox;
16202         int rc;
16203         uint32_t shdr_status, shdr_add_status;
16204         uint32_t mbox_tmo;
16205         union lpfc_sli4_cfg_shdr *shdr;
16206
16207         if (xritag == NO_XRI) {
16208                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16209                                 "0364 Invalid param:\n");
16210                 return -EINVAL;
16211         }
16212
16213         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16214         if (!mbox)
16215                 return -ENOMEM;
16216
16217         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16218                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16219                         sizeof(struct lpfc_mbx_post_sgl_pages) -
16220                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16221
16222         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16223                                 &mbox->u.mqe.un.post_sgl_pages;
16224         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16225         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16226
16227         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16228                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16229         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16230                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16231
16232         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16233                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16234         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16235                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16236         if (!phba->sli4_hba.intr_enable)
16237                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16238         else {
16239                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16240                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16241         }
16242         /* The IOCTL status is embedded in the mailbox subheader. */
16243         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16244         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16245         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16246         if (rc != MBX_TIMEOUT)
16247                 mempool_free(mbox, phba->mbox_mem_pool);
16248         if (shdr_status || shdr_add_status || rc) {
16249                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16250                                 "2511 POST_SGL mailbox failed with "
16251                                 "status x%x add_status x%x, mbx status x%x\n",
16252                                 shdr_status, shdr_add_status, rc);
16253         }
16254         return 0;
16255 }
16256
16257 /**
16258  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
16259  * @phba: pointer to lpfc hba data structure.
16260  *
16261  * This routine is invoked to post rpi header templates to the
16262  * HBA consistent with the SLI-4 interface spec.  This routine
16263  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16264  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
16265  *
16266  * Returns
16267  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16268  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
16269  **/
16270 static uint16_t
16271 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16272 {
16273         unsigned long xri;
16274
16275         /*
16276          * Fetch the next logical xri.  Because this index is logical,
16277          * the driver starts at 0 each time.
16278          */
16279         spin_lock_irq(&phba->hbalock);
16280         xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16281                                  phba->sli4_hba.max_cfg_param.max_xri, 0);
16282         if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16283                 spin_unlock_irq(&phba->hbalock);
16284                 return NO_XRI;
16285         } else {
16286                 set_bit(xri, phba->sli4_hba.xri_bmask);
16287                 phba->sli4_hba.max_cfg_param.xri_used++;
16288         }
16289         spin_unlock_irq(&phba->hbalock);
16290         return xri;
16291 }
16292
16293 /**
16294  * lpfc_sli4_free_xri - Release an xri for reuse.
16295  * @phba: pointer to lpfc hba data structure.
16296  *
16297  * This routine is invoked to release an xri to the pool of
16298  * available rpis maintained by the driver.
16299  **/
16300 static void
16301 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16302 {
16303         if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
16304                 phba->sli4_hba.max_cfg_param.xri_used--;
16305         }
16306 }
16307
16308 /**
16309  * lpfc_sli4_free_xri - Release an xri for reuse.
16310  * @phba: pointer to lpfc hba data structure.
16311  *
16312  * This routine is invoked to release an xri to the pool of
16313  * available rpis maintained by the driver.
16314  **/
16315 void
16316 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16317 {
16318         spin_lock_irq(&phba->hbalock);
16319         __lpfc_sli4_free_xri(phba, xri);
16320         spin_unlock_irq(&phba->hbalock);
16321 }
16322
16323 /**
16324  * lpfc_sli4_next_xritag - Get an xritag for the io
16325  * @phba: Pointer to HBA context object.
16326  *
16327  * This function gets an xritag for the iocb. If there is no unused xritag
16328  * it will return 0xffff.
16329  * The function returns the allocated xritag if successful, else returns zero.
16330  * Zero is not a valid xritag.
16331  * The caller is not required to hold any lock.
16332  **/
16333 uint16_t
16334 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16335 {
16336         uint16_t xri_index;
16337
16338         xri_index = lpfc_sli4_alloc_xri(phba);
16339         if (xri_index == NO_XRI)
16340                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16341                                 "2004 Failed to allocate XRI.last XRITAG is %d"
16342                                 " Max XRI is %d, Used XRI is %d\n",
16343                                 xri_index,
16344                                 phba->sli4_hba.max_cfg_param.max_xri,
16345                                 phba->sli4_hba.max_cfg_param.xri_used);
16346         return xri_index;
16347 }
16348
16349 /**
16350  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
16351  * @phba: pointer to lpfc hba data structure.
16352  * @post_sgl_list: pointer to els sgl entry list.
16353  * @count: number of els sgl entries on the list.
16354  *
16355  * This routine is invoked to post a block of driver's sgl pages to the
16356  * HBA using non-embedded mailbox command. No Lock is held. This routine
16357  * is only called when the driver is loading and after all IO has been
16358  * stopped.
16359  **/
16360 static int
16361 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
16362                             struct list_head *post_sgl_list,
16363                             int post_cnt)
16364 {
16365         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
16366         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16367         struct sgl_page_pairs *sgl_pg_pairs;
16368         void *viraddr;
16369         LPFC_MBOXQ_t *mbox;
16370         uint32_t reqlen, alloclen, pg_pairs;
16371         uint32_t mbox_tmo;
16372         uint16_t xritag_start = 0;
16373         int rc = 0;
16374         uint32_t shdr_status, shdr_add_status;
16375         union lpfc_sli4_cfg_shdr *shdr;
16376
16377         reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
16378                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
16379         if (reqlen > SLI4_PAGE_SIZE) {
16380                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16381                                 "2559 Block sgl registration required DMA "
16382                                 "size (%d) great than a page\n", reqlen);
16383                 return -ENOMEM;
16384         }
16385
16386         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16387         if (!mbox)
16388                 return -ENOMEM;
16389
16390         /* Allocate DMA memory and set up the non-embedded mailbox command */
16391         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16392                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16393                          LPFC_SLI4_MBX_NEMBED);
16394
16395         if (alloclen < reqlen) {
16396                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16397                                 "0285 Allocated DMA memory size (%d) is "
16398                                 "less than the requested DMA memory "
16399                                 "size (%d)\n", alloclen, reqlen);
16400                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16401                 return -ENOMEM;
16402         }
16403         /* Set up the SGL pages in the non-embedded DMA pages */
16404         viraddr = mbox->sge_array->addr[0];
16405         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16406         sgl_pg_pairs = &sgl->sgl_pg_pairs;
16407
16408         pg_pairs = 0;
16409         list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
16410                 /* Set up the sge entry */
16411                 sgl_pg_pairs->sgl_pg0_addr_lo =
16412                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16413                 sgl_pg_pairs->sgl_pg0_addr_hi =
16414                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16415                 sgl_pg_pairs->sgl_pg1_addr_lo =
16416                                 cpu_to_le32(putPaddrLow(0));
16417                 sgl_pg_pairs->sgl_pg1_addr_hi =
16418                                 cpu_to_le32(putPaddrHigh(0));
16419
16420                 /* Keep the first xritag on the list */
16421                 if (pg_pairs == 0)
16422                         xritag_start = sglq_entry->sli4_xritag;
16423                 sgl_pg_pairs++;
16424                 pg_pairs++;
16425         }
16426
16427         /* Complete initialization and perform endian conversion. */
16428         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16429         bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
16430         sgl->word0 = cpu_to_le32(sgl->word0);
16431
16432         if (!phba->sli4_hba.intr_enable)
16433                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16434         else {
16435                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16436                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16437         }
16438         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16439         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16440         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16441         if (rc != MBX_TIMEOUT)
16442                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16443         if (shdr_status || shdr_add_status || rc) {
16444                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16445                                 "2513 POST_SGL_BLOCK mailbox command failed "
16446                                 "status x%x add_status x%x mbx status x%x\n",
16447                                 shdr_status, shdr_add_status, rc);
16448                 rc = -ENXIO;
16449         }
16450         return rc;
16451 }
16452
16453 /**
16454  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16455  * @phba: pointer to lpfc hba data structure.
16456  * @sblist: pointer to scsi buffer list.
16457  * @count: number of scsi buffers on the list.
16458  *
16459  * This routine is invoked to post a block of @count scsi sgl pages from a
16460  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16461  * No Lock is held.
16462  *
16463  **/
16464 int
16465 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16466                               struct list_head *sblist,
16467                               int count)
16468 {
16469         struct lpfc_scsi_buf *psb;
16470         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16471         struct sgl_page_pairs *sgl_pg_pairs;
16472         void *viraddr;
16473         LPFC_MBOXQ_t *mbox;
16474         uint32_t reqlen, alloclen, pg_pairs;
16475         uint32_t mbox_tmo;
16476         uint16_t xritag_start = 0;
16477         int rc = 0;
16478         uint32_t shdr_status, shdr_add_status;
16479         dma_addr_t pdma_phys_bpl1;
16480         union lpfc_sli4_cfg_shdr *shdr;
16481
16482         /* Calculate the requested length of the dma memory */
16483         reqlen = count * sizeof(struct sgl_page_pairs) +
16484                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
16485         if (reqlen > SLI4_PAGE_SIZE) {
16486                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16487                                 "0217 Block sgl registration required DMA "
16488                                 "size (%d) great than a page\n", reqlen);
16489                 return -ENOMEM;
16490         }
16491         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16492         if (!mbox) {
16493                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16494                                 "0283 Failed to allocate mbox cmd memory\n");
16495                 return -ENOMEM;
16496         }
16497
16498         /* Allocate DMA memory and set up the non-embedded mailbox command */
16499         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16500                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16501                                 LPFC_SLI4_MBX_NEMBED);
16502
16503         if (alloclen < reqlen) {
16504                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16505                                 "2561 Allocated DMA memory size (%d) is "
16506                                 "less than the requested DMA memory "
16507                                 "size (%d)\n", alloclen, reqlen);
16508                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16509                 return -ENOMEM;
16510         }
16511
16512         /* Get the first SGE entry from the non-embedded DMA memory */
16513         viraddr = mbox->sge_array->addr[0];
16514
16515         /* Set up the SGL pages in the non-embedded DMA pages */
16516         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16517         sgl_pg_pairs = &sgl->sgl_pg_pairs;
16518
16519         pg_pairs = 0;
16520         list_for_each_entry(psb, sblist, list) {
16521                 /* Set up the sge entry */
16522                 sgl_pg_pairs->sgl_pg0_addr_lo =
16523                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16524                 sgl_pg_pairs->sgl_pg0_addr_hi =
16525                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16526                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16527                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16528                 else
16529                         pdma_phys_bpl1 = 0;
16530                 sgl_pg_pairs->sgl_pg1_addr_lo =
16531                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16532                 sgl_pg_pairs->sgl_pg1_addr_hi =
16533                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16534                 /* Keep the first xritag on the list */
16535                 if (pg_pairs == 0)
16536                         xritag_start = psb->cur_iocbq.sli4_xritag;
16537                 sgl_pg_pairs++;
16538                 pg_pairs++;
16539         }
16540         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16541         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16542         /* Perform endian conversion if necessary */
16543         sgl->word0 = cpu_to_le32(sgl->word0);
16544
16545         if (!phba->sli4_hba.intr_enable)
16546                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16547         else {
16548                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16549                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16550         }
16551         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16552         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16553         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16554         if (rc != MBX_TIMEOUT)
16555                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16556         if (shdr_status || shdr_add_status || rc) {
16557                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16558                                 "2564 POST_SGL_BLOCK mailbox command failed "
16559                                 "status x%x add_status x%x mbx status x%x\n",
16560                                 shdr_status, shdr_add_status, rc);
16561                 rc = -ENXIO;
16562         }
16563         return rc;
16564 }
16565
16566 /**
16567  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16568  * @phba: pointer to lpfc_hba struct that the frame was received on
16569  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16570  *
16571  * This function checks the fields in the @fc_hdr to see if the FC frame is a
16572  * valid type of frame that the LPFC driver will handle. This function will
16573  * return a zero if the frame is a valid frame or a non zero value when the
16574  * frame does not pass the check.
16575  **/
16576 static int
16577 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16578 {
16579         /*  make rctl_names static to save stack space */
16580         struct fc_vft_header *fc_vft_hdr;
16581         uint32_t *header = (uint32_t *) fc_hdr;
16582
16583 #define FC_RCTL_MDS_DIAGS       0xF4
16584
16585         switch (fc_hdr->fh_r_ctl) {
16586         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
16587         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
16588         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
16589         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
16590         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
16591         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
16592         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
16593         case FC_RCTL_DD_CMD_STATUS:     /* command status */
16594         case FC_RCTL_ELS_REQ:   /* extended link services request */
16595         case FC_RCTL_ELS_REP:   /* extended link services reply */
16596         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
16597         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
16598         case FC_RCTL_BA_NOP:    /* basic link service NOP */
16599         case FC_RCTL_BA_ABTS:   /* basic link service abort */
16600         case FC_RCTL_BA_RMC:    /* remove connection */
16601         case FC_RCTL_BA_ACC:    /* basic accept */
16602         case FC_RCTL_BA_RJT:    /* basic reject */
16603         case FC_RCTL_BA_PRMT:
16604         case FC_RCTL_ACK_1:     /* acknowledge_1 */
16605         case FC_RCTL_ACK_0:     /* acknowledge_0 */
16606         case FC_RCTL_P_RJT:     /* port reject */
16607         case FC_RCTL_F_RJT:     /* fabric reject */
16608         case FC_RCTL_P_BSY:     /* port busy */
16609         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
16610         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
16611         case FC_RCTL_LCR:       /* link credit reset */
16612         case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
16613         case FC_RCTL_END:       /* end */
16614                 break;
16615         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
16616                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16617                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16618                 return lpfc_fc_frame_check(phba, fc_hdr);
16619         default:
16620                 goto drop;
16621         }
16622
16623 #define FC_TYPE_VENDOR_UNIQUE   0xFF
16624
16625         switch (fc_hdr->fh_type) {
16626         case FC_TYPE_BLS:
16627         case FC_TYPE_ELS:
16628         case FC_TYPE_FCP:
16629         case FC_TYPE_CT:
16630         case FC_TYPE_NVME:
16631         case FC_TYPE_VENDOR_UNIQUE:
16632                 break;
16633         case FC_TYPE_IP:
16634         case FC_TYPE_ILS:
16635         default:
16636                 goto drop;
16637         }
16638
16639         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
16640                         "2538 Received frame rctl:x%x, type:x%x, "
16641                         "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
16642                         fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16643                         be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16644                         be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16645                         be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16646                         be32_to_cpu(header[6]));
16647         return 0;
16648 drop:
16649         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
16650                         "2539 Dropped frame rctl:x%x type:x%x\n",
16651                         fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16652         return 1;
16653 }
16654
16655 /**
16656  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16657  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16658  *
16659  * This function processes the FC header to retrieve the VFI from the VF
16660  * header, if one exists. This function will return the VFI if one exists
16661  * or 0 if no VSAN Header exists.
16662  **/
16663 static uint32_t
16664 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16665 {
16666         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16667
16668         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16669                 return 0;
16670         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16671 }
16672
16673 /**
16674  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16675  * @phba: Pointer to the HBA structure to search for the vport on
16676  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16677  * @fcfi: The FC Fabric ID that the frame came from
16678  *
16679  * This function searches the @phba for a vport that matches the content of the
16680  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16681  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16682  * returns the matching vport pointer or NULL if unable to match frame to a
16683  * vport.
16684  **/
16685 static struct lpfc_vport *
16686 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
16687                        uint16_t fcfi, uint32_t did)
16688 {
16689         struct lpfc_vport **vports;
16690         struct lpfc_vport *vport = NULL;
16691         int i;
16692
16693         if (did == Fabric_DID)
16694                 return phba->pport;
16695         if ((phba->pport->fc_flag & FC_PT2PT) &&
16696                 !(phba->link_state == LPFC_HBA_READY))
16697                 return phba->pport;
16698
16699         vports = lpfc_create_vport_work_array(phba);
16700         if (vports != NULL) {
16701                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16702                         if (phba->fcf.fcfi == fcfi &&
16703                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16704                             vports[i]->fc_myDID == did) {
16705                                 vport = vports[i];
16706                                 break;
16707                         }
16708                 }
16709         }
16710         lpfc_destroy_vport_work_array(phba, vports);
16711         return vport;
16712 }
16713
16714 /**
16715  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16716  * @vport: The vport to work on.
16717  *
16718  * This function updates the receive sequence time stamp for this vport. The
16719  * receive sequence time stamp indicates the time that the last frame of the
16720  * the sequence that has been idle for the longest amount of time was received.
16721  * the driver uses this time stamp to indicate if any received sequences have
16722  * timed out.
16723  **/
16724 static void
16725 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16726 {
16727         struct lpfc_dmabuf *h_buf;
16728         struct hbq_dmabuf *dmabuf = NULL;
16729
16730         /* get the oldest sequence on the rcv list */
16731         h_buf = list_get_first(&vport->rcv_buffer_list,
16732                                struct lpfc_dmabuf, list);
16733         if (!h_buf)
16734                 return;
16735         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16736         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16737 }
16738
16739 /**
16740  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16741  * @vport: The vport that the received sequences were sent to.
16742  *
16743  * This function cleans up all outstanding received sequences. This is called
16744  * by the driver when a link event or user action invalidates all the received
16745  * sequences.
16746  **/
16747 void
16748 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16749 {
16750         struct lpfc_dmabuf *h_buf, *hnext;
16751         struct lpfc_dmabuf *d_buf, *dnext;
16752         struct hbq_dmabuf *dmabuf = NULL;
16753
16754         /* start with the oldest sequence on the rcv list */
16755         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16756                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16757                 list_del_init(&dmabuf->hbuf.list);
16758                 list_for_each_entry_safe(d_buf, dnext,
16759                                          &dmabuf->dbuf.list, list) {
16760                         list_del_init(&d_buf->list);
16761                         lpfc_in_buf_free(vport->phba, d_buf);
16762                 }
16763                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16764         }
16765 }
16766
16767 /**
16768  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16769  * @vport: The vport that the received sequences were sent to.
16770  *
16771  * This function determines whether any received sequences have timed out by
16772  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16773  * indicates that there is at least one timed out sequence this routine will
16774  * go through the received sequences one at a time from most inactive to most
16775  * active to determine which ones need to be cleaned up. Once it has determined
16776  * that a sequence needs to be cleaned up it will simply free up the resources
16777  * without sending an abort.
16778  **/
16779 void
16780 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16781 {
16782         struct lpfc_dmabuf *h_buf, *hnext;
16783         struct lpfc_dmabuf *d_buf, *dnext;
16784         struct hbq_dmabuf *dmabuf = NULL;
16785         unsigned long timeout;
16786         int abort_count = 0;
16787
16788         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16789                    vport->rcv_buffer_time_stamp);
16790         if (list_empty(&vport->rcv_buffer_list) ||
16791             time_before(jiffies, timeout))
16792                 return;
16793         /* start with the oldest sequence on the rcv list */
16794         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16795                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16796                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16797                            dmabuf->time_stamp);
16798                 if (time_before(jiffies, timeout))
16799                         break;
16800                 abort_count++;
16801                 list_del_init(&dmabuf->hbuf.list);
16802                 list_for_each_entry_safe(d_buf, dnext,
16803                                          &dmabuf->dbuf.list, list) {
16804                         list_del_init(&d_buf->list);
16805                         lpfc_in_buf_free(vport->phba, d_buf);
16806                 }
16807                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16808         }
16809         if (abort_count)
16810                 lpfc_update_rcv_time_stamp(vport);
16811 }
16812
16813 /**
16814  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16815  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16816  *
16817  * This function searches through the existing incomplete sequences that have
16818  * been sent to this @vport. If the frame matches one of the incomplete
16819  * sequences then the dbuf in the @dmabuf is added to the list of frames that
16820  * make up that sequence. If no sequence is found that matches this frame then
16821  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16822  * This function returns a pointer to the first dmabuf in the sequence list that
16823  * the frame was linked to.
16824  **/
16825 static struct hbq_dmabuf *
16826 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16827 {
16828         struct fc_frame_header *new_hdr;
16829         struct fc_frame_header *temp_hdr;
16830         struct lpfc_dmabuf *d_buf;
16831         struct lpfc_dmabuf *h_buf;
16832         struct hbq_dmabuf *seq_dmabuf = NULL;
16833         struct hbq_dmabuf *temp_dmabuf = NULL;
16834         uint8_t found = 0;
16835
16836         INIT_LIST_HEAD(&dmabuf->dbuf.list);
16837         dmabuf->time_stamp = jiffies;
16838         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16839
16840         /* Use the hdr_buf to find the sequence that this frame belongs to */
16841         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16842                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16843                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16844                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16845                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16846                         continue;
16847                 /* found a pending sequence that matches this frame */
16848                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16849                 break;
16850         }
16851         if (!seq_dmabuf) {
16852                 /*
16853                  * This indicates first frame received for this sequence.
16854                  * Queue the buffer on the vport's rcv_buffer_list.
16855                  */
16856                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16857                 lpfc_update_rcv_time_stamp(vport);
16858                 return dmabuf;
16859         }
16860         temp_hdr = seq_dmabuf->hbuf.virt;
16861         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16862                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
16863                 list_del_init(&seq_dmabuf->hbuf.list);
16864                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16865                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16866                 lpfc_update_rcv_time_stamp(vport);
16867                 return dmabuf;
16868         }
16869         /* move this sequence to the tail to indicate a young sequence */
16870         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16871         seq_dmabuf->time_stamp = jiffies;
16872         lpfc_update_rcv_time_stamp(vport);
16873         if (list_empty(&seq_dmabuf->dbuf.list)) {
16874                 temp_hdr = dmabuf->hbuf.virt;
16875                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16876                 return seq_dmabuf;
16877         }
16878         /* find the correct place in the sequence to insert this frame */
16879         d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16880         while (!found) {
16881                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16882                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16883                 /*
16884                  * If the frame's sequence count is greater than the frame on
16885                  * the list then insert the frame right after this frame
16886                  */
16887                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16888                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
16889                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
16890                         found = 1;
16891                         break;
16892                 }
16893
16894                 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16895                         break;
16896                 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
16897         }
16898
16899         if (found)
16900                 return seq_dmabuf;
16901         return NULL;
16902 }
16903
16904 /**
16905  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16906  * @vport: pointer to a vitural port
16907  * @dmabuf: pointer to a dmabuf that describes the FC sequence
16908  *
16909  * This function tries to abort from the partially assembed sequence, described
16910  * by the information from basic abbort @dmabuf. It checks to see whether such
16911  * partially assembled sequence held by the driver. If so, it shall free up all
16912  * the frames from the partially assembled sequence.
16913  *
16914  * Return
16915  * true  -- if there is matching partially assembled sequence present and all
16916  *          the frames freed with the sequence;
16917  * false -- if there is no matching partially assembled sequence present so
16918  *          nothing got aborted in the lower layer driver
16919  **/
16920 static bool
16921 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16922                             struct hbq_dmabuf *dmabuf)
16923 {
16924         struct fc_frame_header *new_hdr;
16925         struct fc_frame_header *temp_hdr;
16926         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16927         struct hbq_dmabuf *seq_dmabuf = NULL;
16928
16929         /* Use the hdr_buf to find the sequence that matches this frame */
16930         INIT_LIST_HEAD(&dmabuf->dbuf.list);
16931         INIT_LIST_HEAD(&dmabuf->hbuf.list);
16932         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16933         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16934                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16935                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16936                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16937                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16938                         continue;
16939                 /* found a pending sequence that matches this frame */
16940                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16941                 break;
16942         }
16943
16944         /* Free up all the frames from the partially assembled sequence */
16945         if (seq_dmabuf) {
16946                 list_for_each_entry_safe(d_buf, n_buf,
16947                                          &seq_dmabuf->dbuf.list, list) {
16948                         list_del_init(&d_buf->list);
16949                         lpfc_in_buf_free(vport->phba, d_buf);
16950                 }
16951                 return true;
16952         }
16953         return false;
16954 }
16955
16956 /**
16957  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16958  * @vport: pointer to a vitural port
16959  * @dmabuf: pointer to a dmabuf that describes the FC sequence
16960  *
16961  * This function tries to abort from the assembed sequence from upper level
16962  * protocol, described by the information from basic abbort @dmabuf. It
16963  * checks to see whether such pending context exists at upper level protocol.
16964  * If so, it shall clean up the pending context.
16965  *
16966  * Return
16967  * true  -- if there is matching pending context of the sequence cleaned
16968  *          at ulp;
16969  * false -- if there is no matching pending context of the sequence present
16970  *          at ulp.
16971  **/
16972 static bool
16973 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16974 {
16975         struct lpfc_hba *phba = vport->phba;
16976         int handled;
16977
16978         /* Accepting abort at ulp with SLI4 only */
16979         if (phba->sli_rev < LPFC_SLI_REV4)
16980                 return false;
16981
16982         /* Register all caring upper level protocols to attend abort */
16983         handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16984         if (handled)
16985                 return true;
16986
16987         return false;
16988 }
16989
16990 /**
16991  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
16992  * @phba: Pointer to HBA context object.
16993  * @cmd_iocbq: pointer to the command iocbq structure.
16994  * @rsp_iocbq: pointer to the response iocbq structure.
16995  *
16996  * This function handles the sequence abort response iocb command complete
16997  * event. It properly releases the memory allocated to the sequence abort
16998  * accept iocb.
16999  **/
17000 static void
17001 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
17002                              struct lpfc_iocbq *cmd_iocbq,
17003                              struct lpfc_iocbq *rsp_iocbq)
17004 {
17005         struct lpfc_nodelist *ndlp;
17006
17007         if (cmd_iocbq) {
17008                 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17009                 lpfc_nlp_put(ndlp);
17010                 lpfc_nlp_not_used(ndlp);
17011                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
17012         }
17013
17014         /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17015         if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17016                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17017                         "3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
17018                         rsp_iocbq->iocb.ulpStatus,
17019                         rsp_iocbq->iocb.un.ulpWord[4]);
17020 }
17021
17022 /**
17023  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17024  * @phba: Pointer to HBA context object.
17025  * @xri: xri id in transaction.
17026  *
17027  * This function validates the xri maps to the known range of XRIs allocated an
17028  * used by the driver.
17029  **/
17030 uint16_t
17031 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17032                       uint16_t xri)
17033 {
17034         uint16_t i;
17035
17036         for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17037                 if (xri == phba->sli4_hba.xri_ids[i])
17038                         return i;
17039         }
17040         return NO_XRI;
17041 }
17042
17043 /**
17044  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
17045  * @phba: Pointer to HBA context object.
17046  * @fc_hdr: pointer to a FC frame header.
17047  *
17048  * This function sends a basic response to a previous unsol sequence abort
17049  * event after aborting the sequence handling.
17050  **/
17051 void
17052 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17053                         struct fc_frame_header *fc_hdr, bool aborted)
17054 {
17055         struct lpfc_hba *phba = vport->phba;
17056         struct lpfc_iocbq *ctiocb = NULL;
17057         struct lpfc_nodelist *ndlp;
17058         uint16_t oxid, rxid, xri, lxri;
17059         uint32_t sid, fctl;
17060         IOCB_t *icmd;
17061         int rc;
17062
17063         if (!lpfc_is_link_up(phba))
17064                 return;
17065
17066         sid = sli4_sid_from_fc_hdr(fc_hdr);
17067         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
17068         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
17069
17070         ndlp = lpfc_findnode_did(vport, sid);
17071         if (!ndlp) {
17072                 ndlp = lpfc_nlp_init(vport, sid);
17073                 if (!ndlp) {
17074                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17075                                          "1268 Failed to allocate ndlp for "
17076                                          "oxid:x%x SID:x%x\n", oxid, sid);
17077                         return;
17078                 }
17079                 /* Put ndlp onto pport node list */
17080                 lpfc_enqueue_node(vport, ndlp);
17081         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17082                 /* re-setup ndlp without removing from node list */
17083                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17084                 if (!ndlp) {
17085                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17086                                          "3275 Failed to active ndlp found "
17087                                          "for oxid:x%x SID:x%x\n", oxid, sid);
17088                         return;
17089                 }
17090         }
17091
17092         /* Allocate buffer for rsp iocb */
17093         ctiocb = lpfc_sli_get_iocbq(phba);
17094         if (!ctiocb)
17095                 return;
17096
17097         /* Extract the F_CTL field from FC_HDR */
17098         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17099
17100         icmd = &ctiocb->iocb;
17101         icmd->un.xseq64.bdl.bdeSize = 0;
17102         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
17103         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17104         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17105         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17106
17107         /* Fill in the rest of iocb fields */
17108         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17109         icmd->ulpBdeCount = 0;
17110         icmd->ulpLe = 1;
17111         icmd->ulpClass = CLASS3;
17112         icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
17113         ctiocb->context1 = lpfc_nlp_get(ndlp);
17114
17115         ctiocb->iocb_cmpl = NULL;
17116         ctiocb->vport = phba->pport;
17117         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
17118         ctiocb->sli4_lxritag = NO_XRI;
17119         ctiocb->sli4_xritag = NO_XRI;
17120
17121         if (fctl & FC_FC_EX_CTX)
17122                 /* Exchange responder sent the abort so we
17123                  * own the oxid.
17124                  */
17125                 xri = oxid;
17126         else
17127                 xri = rxid;
17128         lxri = lpfc_sli4_xri_inrange(phba, xri);
17129         if (lxri != NO_XRI)
17130                 lpfc_set_rrq_active(phba, ndlp, lxri,
17131                         (xri == oxid) ? rxid : oxid, 0);
17132         /* For BA_ABTS from exchange responder, if the logical xri with
17133          * the oxid maps to the FCP XRI range, the port no longer has
17134          * that exchange context, send a BLS_RJT. Override the IOCB for
17135          * a BA_RJT.
17136          */
17137         if ((fctl & FC_FC_EX_CTX) &&
17138             (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
17139                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17140                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17141                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17142                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17143         }
17144
17145         /* If BA_ABTS failed to abort a partially assembled receive sequence,
17146          * the driver no longer has that exchange, send a BLS_RJT. Override
17147          * the IOCB for a BA_RJT.
17148          */
17149         if (aborted == false) {
17150                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17151                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17152                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17153                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17154         }
17155
17156         if (fctl & FC_FC_EX_CTX) {
17157                 /* ABTS sent by responder to CT exchange, construction
17158                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17159                  * field and RX_ID from ABTS for RX_ID field.
17160                  */
17161                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
17162         } else {
17163                 /* ABTS sent by initiator to CT exchange, construction
17164                  * of BA_ACC will need to allocate a new XRI as for the
17165                  * XRI_TAG field.
17166                  */
17167                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
17168         }
17169         bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
17170         bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
17171
17172         /* Xmit CT abts response on exchange <xid> */
17173         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17174                          "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17175                          icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
17176
17177         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17178         if (rc == IOCB_ERROR) {
17179                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17180                                  "2925 Failed to issue CT ABTS RSP x%x on "
17181                                  "xri x%x, Data x%x\n",
17182                                  icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17183                                  phba->link_state);
17184                 lpfc_nlp_put(ndlp);
17185                 ctiocb->context1 = NULL;
17186                 lpfc_sli_release_iocbq(phba, ctiocb);
17187         }
17188 }
17189
17190 /**
17191  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17192  * @vport: Pointer to the vport on which this sequence was received
17193  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17194  *
17195  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17196  * receive sequence is only partially assembed by the driver, it shall abort
17197  * the partially assembled frames for the sequence. Otherwise, if the
17198  * unsolicited receive sequence has been completely assembled and passed to
17199  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17200  * unsolicited sequence has been aborted. After that, it will issue a basic
17201  * accept to accept the abort.
17202  **/
17203 static void
17204 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17205                              struct hbq_dmabuf *dmabuf)
17206 {
17207         struct lpfc_hba *phba = vport->phba;
17208         struct fc_frame_header fc_hdr;
17209         uint32_t fctl;
17210         bool aborted;
17211
17212         /* Make a copy of fc_hdr before the dmabuf being released */
17213         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
17214         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
17215
17216         if (fctl & FC_FC_EX_CTX) {
17217                 /* ABTS by responder to exchange, no cleanup needed */
17218                 aborted = true;
17219         } else {
17220                 /* ABTS by initiator to exchange, need to do cleanup */
17221                 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17222                 if (aborted == false)
17223                         aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
17224         }
17225         lpfc_in_buf_free(phba, &dmabuf->dbuf);
17226
17227         if (phba->nvmet_support) {
17228                 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17229                 return;
17230         }
17231
17232         /* Respond with BA_ACC or BA_RJT accordingly */
17233         lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
17234 }
17235
17236 /**
17237  * lpfc_seq_complete - Indicates if a sequence is complete
17238  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17239  *
17240  * This function checks the sequence, starting with the frame described by
17241  * @dmabuf, to see if all the frames associated with this sequence are present.
17242  * the frames associated with this sequence are linked to the @dmabuf using the
17243  * dbuf list. This function looks for two major things. 1) That the first frame
17244  * has a sequence count of zero. 2) There is a frame with last frame of sequence
17245  * set. 3) That there are no holes in the sequence count. The function will
17246  * return 1 when the sequence is complete, otherwise it will return 0.
17247  **/
17248 static int
17249 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17250 {
17251         struct fc_frame_header *hdr;
17252         struct lpfc_dmabuf *d_buf;
17253         struct hbq_dmabuf *seq_dmabuf;
17254         uint32_t fctl;
17255         int seq_count = 0;
17256
17257         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17258         /* make sure first fame of sequence has a sequence count of zero */
17259         if (hdr->fh_seq_cnt != seq_count)
17260                 return 0;
17261         fctl = (hdr->fh_f_ctl[0] << 16 |
17262                 hdr->fh_f_ctl[1] << 8 |
17263                 hdr->fh_f_ctl[2]);
17264         /* If last frame of sequence we can return success. */
17265         if (fctl & FC_FC_END_SEQ)
17266                 return 1;
17267         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17268                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17269                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17270                 /* If there is a hole in the sequence count then fail. */
17271                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
17272                         return 0;
17273                 fctl = (hdr->fh_f_ctl[0] << 16 |
17274                         hdr->fh_f_ctl[1] << 8 |
17275                         hdr->fh_f_ctl[2]);
17276                 /* If last frame of sequence we can return success. */
17277                 if (fctl & FC_FC_END_SEQ)
17278                         return 1;
17279         }
17280         return 0;
17281 }
17282
17283 /**
17284  * lpfc_prep_seq - Prep sequence for ULP processing
17285  * @vport: Pointer to the vport on which this sequence was received
17286  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17287  *
17288  * This function takes a sequence, described by a list of frames, and creates
17289  * a list of iocbq structures to describe the sequence. This iocbq list will be
17290  * used to issue to the generic unsolicited sequence handler. This routine
17291  * returns a pointer to the first iocbq in the list. If the function is unable
17292  * to allocate an iocbq then it throw out the received frames that were not
17293  * able to be described and return a pointer to the first iocbq. If unable to
17294  * allocate any iocbqs (including the first) this function will return NULL.
17295  **/
17296 static struct lpfc_iocbq *
17297 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17298 {
17299         struct hbq_dmabuf *hbq_buf;
17300         struct lpfc_dmabuf *d_buf, *n_buf;
17301         struct lpfc_iocbq *first_iocbq, *iocbq;
17302         struct fc_frame_header *fc_hdr;
17303         uint32_t sid;
17304         uint32_t len, tot_len;
17305         struct ulp_bde64 *pbde;
17306
17307         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17308         /* remove from receive buffer list */
17309         list_del_init(&seq_dmabuf->hbuf.list);
17310         lpfc_update_rcv_time_stamp(vport);
17311         /* get the Remote Port's SID */
17312         sid = sli4_sid_from_fc_hdr(fc_hdr);
17313         tot_len = 0;
17314         /* Get an iocbq struct to fill in. */
17315         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17316         if (first_iocbq) {
17317                 /* Initialize the first IOCB. */
17318                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
17319                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
17320                 first_iocbq->vport = vport;
17321
17322                 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17323                 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17324                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17325                         first_iocbq->iocb.un.rcvels.parmRo =
17326                                 sli4_did_from_fc_hdr(fc_hdr);
17327                         first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17328                 } else
17329                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
17330                 first_iocbq->iocb.ulpContext = NO_XRI;
17331                 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17332                         be16_to_cpu(fc_hdr->fh_ox_id);
17333                 /* iocbq is prepped for internal consumption.  Physical vpi. */
17334                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17335                         vport->phba->vpi_ids[vport->vpi];
17336                 /* put the first buffer into the first IOCBq */
17337                 tot_len = bf_get(lpfc_rcqe_length,
17338                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17339
17340                 first_iocbq->context2 = &seq_dmabuf->dbuf;
17341                 first_iocbq->context3 = NULL;
17342                 first_iocbq->iocb.ulpBdeCount = 1;
17343                 if (tot_len > LPFC_DATA_BUF_SIZE)
17344                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17345                                                         LPFC_DATA_BUF_SIZE;
17346                 else
17347                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17348
17349                 first_iocbq->iocb.un.rcvels.remoteID = sid;
17350
17351                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17352         }
17353         iocbq = first_iocbq;
17354         /*
17355          * Each IOCBq can have two Buffers assigned, so go through the list
17356          * of buffers for this sequence and save two buffers in each IOCBq
17357          */
17358         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17359                 if (!iocbq) {
17360                         lpfc_in_buf_free(vport->phba, d_buf);
17361                         continue;
17362                 }
17363                 if (!iocbq->context3) {
17364                         iocbq->context3 = d_buf;
17365                         iocbq->iocb.ulpBdeCount++;
17366                         /* We need to get the size out of the right CQE */
17367                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17368                         len = bf_get(lpfc_rcqe_length,
17369                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
17370                         pbde = (struct ulp_bde64 *)
17371                                         &iocbq->iocb.unsli3.sli3Words[4];
17372                         if (len > LPFC_DATA_BUF_SIZE)
17373                                 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17374                         else
17375                                 pbde->tus.f.bdeSize = len;
17376
17377                         iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17378                         tot_len += len;
17379                 } else {
17380                         iocbq = lpfc_sli_get_iocbq(vport->phba);
17381                         if (!iocbq) {
17382                                 if (first_iocbq) {
17383                                         first_iocbq->iocb.ulpStatus =
17384                                                         IOSTAT_FCP_RSP_ERROR;
17385                                         first_iocbq->iocb.un.ulpWord[4] =
17386                                                         IOERR_NO_RESOURCES;
17387                                 }
17388                                 lpfc_in_buf_free(vport->phba, d_buf);
17389                                 continue;
17390                         }
17391                         /* We need to get the size out of the right CQE */
17392                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17393                         len = bf_get(lpfc_rcqe_length,
17394                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
17395                         iocbq->context2 = d_buf;
17396                         iocbq->context3 = NULL;
17397                         iocbq->iocb.ulpBdeCount = 1;
17398                         if (len > LPFC_DATA_BUF_SIZE)
17399                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17400                                                         LPFC_DATA_BUF_SIZE;
17401                         else
17402                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17403
17404                         tot_len += len;
17405                         iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17406
17407                         iocbq->iocb.un.rcvels.remoteID = sid;
17408                         list_add_tail(&iocbq->list, &first_iocbq->list);
17409                 }
17410         }
17411         return first_iocbq;
17412 }
17413
17414 static void
17415 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17416                           struct hbq_dmabuf *seq_dmabuf)
17417 {
17418         struct fc_frame_header *fc_hdr;
17419         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17420         struct lpfc_hba *phba = vport->phba;
17421
17422         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17423         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17424         if (!iocbq) {
17425                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17426                                 "2707 Ring %d handler: Failed to allocate "
17427                                 "iocb Rctl x%x Type x%x received\n",
17428                                 LPFC_ELS_RING,
17429                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17430                 return;
17431         }
17432         if (!lpfc_complete_unsol_iocb(phba,
17433                                       phba->sli4_hba.els_wq->pring,
17434                                       iocbq, fc_hdr->fh_r_ctl,
17435                                       fc_hdr->fh_type))
17436                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17437                                 "2540 Ring %d handler: unexpected Rctl "
17438                                 "x%x Type x%x received\n",
17439                                 LPFC_ELS_RING,
17440                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17441
17442         /* Free iocb created in lpfc_prep_seq */
17443         list_for_each_entry_safe(curr_iocb, next_iocb,
17444                 &iocbq->list, list) {
17445                 list_del_init(&curr_iocb->list);
17446                 lpfc_sli_release_iocbq(phba, curr_iocb);
17447         }
17448         lpfc_sli_release_iocbq(phba, iocbq);
17449 }
17450
17451 static void
17452 lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17453                             struct lpfc_iocbq *rspiocb)
17454 {
17455         struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17456
17457         if (pcmd && pcmd->virt)
17458                 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
17459         kfree(pcmd);
17460         lpfc_sli_release_iocbq(phba, cmdiocb);
17461 }
17462
17463 static void
17464 lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17465                               struct hbq_dmabuf *dmabuf)
17466 {
17467         struct fc_frame_header *fc_hdr;
17468         struct lpfc_hba *phba = vport->phba;
17469         struct lpfc_iocbq *iocbq = NULL;
17470         union  lpfc_wqe *wqe;
17471         struct lpfc_dmabuf *pcmd = NULL;
17472         uint32_t frame_len;
17473         int rc;
17474
17475         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17476         frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17477
17478         /* Send the received frame back */
17479         iocbq = lpfc_sli_get_iocbq(phba);
17480         if (!iocbq)
17481                 goto exit;
17482
17483         /* Allocate buffer for command payload */
17484         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17485         if (pcmd)
17486                 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
17487                                             &pcmd->phys);
17488         if (!pcmd || !pcmd->virt)
17489                 goto exit;
17490
17491         INIT_LIST_HEAD(&pcmd->list);
17492
17493         /* copyin the payload */
17494         memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17495
17496         /* fill in BDE's for command */
17497         iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17498         iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17499         iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17500         iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17501
17502         iocbq->context2 = pcmd;
17503         iocbq->vport = vport;
17504         iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17505         iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17506
17507         /*
17508          * Setup rest of the iocb as though it were a WQE
17509          * Build the SEND_FRAME WQE
17510          */
17511         wqe = (union lpfc_wqe *)&iocbq->iocb;
17512
17513         wqe->send_frame.frame_len = frame_len;
17514         wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17515         wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17516         wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17517         wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17518         wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17519         wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17520
17521         iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17522         iocbq->iocb.ulpLe = 1;
17523         iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17524         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17525         if (rc == IOCB_ERROR)
17526                 goto exit;
17527
17528         lpfc_in_buf_free(phba, &dmabuf->dbuf);
17529         return;
17530
17531 exit:
17532         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17533                         "2023 Unable to process MDS loopback frame\n");
17534         if (pcmd && pcmd->virt)
17535                 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
17536         kfree(pcmd);
17537         if (iocbq)
17538                 lpfc_sli_release_iocbq(phba, iocbq);
17539         lpfc_in_buf_free(phba, &dmabuf->dbuf);
17540 }
17541
17542 /**
17543  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17544  * @phba: Pointer to HBA context object.
17545  *
17546  * This function is called with no lock held. This function processes all
17547  * the received buffers and gives it to upper layers when a received buffer
17548  * indicates that it is the final frame in the sequence. The interrupt
17549  * service routine processes received buffers at interrupt contexts.
17550  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17551  * appropriate receive function when the final frame in a sequence is received.
17552  **/
17553 void
17554 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17555                                  struct hbq_dmabuf *dmabuf)
17556 {
17557         struct hbq_dmabuf *seq_dmabuf;
17558         struct fc_frame_header *fc_hdr;
17559         struct lpfc_vport *vport;
17560         uint32_t fcfi;
17561         uint32_t did;
17562
17563         /* Process each received buffer */
17564         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17565
17566         /* check to see if this a valid type of frame */
17567         if (lpfc_fc_frame_check(phba, fc_hdr)) {
17568                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17569                 return;
17570         }
17571
17572         if ((bf_get(lpfc_cqe_code,
17573                     &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17574                 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17575                               &dmabuf->cq_event.cqe.rcqe_cmpl);
17576         else
17577                 fcfi = bf_get(lpfc_rcqe_fcf_id,
17578                               &dmabuf->cq_event.cqe.rcqe_cmpl);
17579
17580         if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17581                 vport = phba->pport;
17582                 /* Handle MDS Loopback frames */
17583                 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17584                 return;
17585         }
17586
17587         /* d_id this frame is directed to */
17588         did = sli4_did_from_fc_hdr(fc_hdr);
17589
17590         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
17591         if (!vport) {
17592                 /* throw out the frame */
17593                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17594                 return;
17595         }
17596
17597         /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17598         if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17599                 (did != Fabric_DID)) {
17600                 /*
17601                  * Throw out the frame if we are not pt2pt.
17602                  * The pt2pt protocol allows for discovery frames
17603                  * to be received without a registered VPI.
17604                  */
17605                 if (!(vport->fc_flag & FC_PT2PT) ||
17606                         (phba->link_state == LPFC_HBA_READY)) {
17607                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
17608                         return;
17609                 }
17610         }
17611
17612         /* Handle the basic abort sequence (BA_ABTS) event */
17613         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17614                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17615                 return;
17616         }
17617
17618         /* Link this frame */
17619         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17620         if (!seq_dmabuf) {
17621                 /* unable to add frame to vport - throw it out */
17622                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17623                 return;
17624         }
17625         /* If not last frame in sequence continue processing frames. */
17626         if (!lpfc_seq_complete(seq_dmabuf))
17627                 return;
17628
17629         /* Send the complete sequence to the upper layer protocol */
17630         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
17631 }
17632
17633 /**
17634  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17635  * @phba: pointer to lpfc hba data structure.
17636  *
17637  * This routine is invoked to post rpi header templates to the
17638  * HBA consistent with the SLI-4 interface spec.  This routine
17639  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17640  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17641  *
17642  * This routine does not require any locks.  It's usage is expected
17643  * to be driver load or reset recovery when the driver is
17644  * sequential.
17645  *
17646  * Return codes
17647  *      0 - successful
17648  *      -EIO - The mailbox failed to complete successfully.
17649  *      When this error occurs, the driver is not guaranteed
17650  *      to have any rpi regions posted to the device and
17651  *      must either attempt to repost the regions or take a
17652  *      fatal error.
17653  **/
17654 int
17655 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17656 {
17657         struct lpfc_rpi_hdr *rpi_page;
17658         uint32_t rc = 0;
17659         uint16_t lrpi = 0;
17660
17661         /* SLI4 ports that support extents do not require RPI headers. */
17662         if (!phba->sli4_hba.rpi_hdrs_in_use)
17663                 goto exit;
17664         if (phba->sli4_hba.extents_in_use)
17665                 return -EIO;
17666
17667         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
17668                 /*
17669                  * Assign the rpi headers a physical rpi only if the driver
17670                  * has not initialized those resources.  A port reset only
17671                  * needs the headers posted.
17672                  */
17673                 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17674                     LPFC_RPI_RSRC_RDY)
17675                         rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17676
17677                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17678                 if (rc != MBX_SUCCESS) {
17679                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17680                                         "2008 Error %d posting all rpi "
17681                                         "headers\n", rc);
17682                         rc = -EIO;
17683                         break;
17684                 }
17685         }
17686
17687  exit:
17688         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17689                LPFC_RPI_RSRC_RDY);
17690         return rc;
17691 }
17692
17693 /**
17694  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17695  * @phba: pointer to lpfc hba data structure.
17696  * @rpi_page:  pointer to the rpi memory region.
17697  *
17698  * This routine is invoked to post a single rpi header to the
17699  * HBA consistent with the SLI-4 interface spec.  This memory region
17700  * maps up to 64 rpi context regions.
17701  *
17702  * Return codes
17703  *      0 - successful
17704  *      -ENOMEM - No available memory
17705  *      -EIO - The mailbox failed to complete successfully.
17706  **/
17707 int
17708 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17709 {
17710         LPFC_MBOXQ_t *mboxq;
17711         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17712         uint32_t rc = 0;
17713         uint32_t shdr_status, shdr_add_status;
17714         union lpfc_sli4_cfg_shdr *shdr;
17715
17716         /* SLI4 ports that support extents do not require RPI headers. */
17717         if (!phba->sli4_hba.rpi_hdrs_in_use)
17718                 return rc;
17719         if (phba->sli4_hba.extents_in_use)
17720                 return -EIO;
17721
17722         /* The port is notified of the header region via a mailbox command. */
17723         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17724         if (!mboxq) {
17725                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17726                                 "2001 Unable to allocate memory for issuing "
17727                                 "SLI_CONFIG_SPECIAL mailbox command\n");
17728                 return -ENOMEM;
17729         }
17730
17731         /* Post all rpi memory regions to the port. */
17732         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
17733         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17734                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17735                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
17736                          sizeof(struct lpfc_sli4_cfg_mhdr),
17737                          LPFC_SLI4_MBX_EMBED);
17738
17739
17740         /* Post the physical rpi to the port for this rpi header. */
17741         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17742                rpi_page->start_rpi);
17743         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17744                hdr_tmpl, rpi_page->page_count);
17745
17746         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17747         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
17748         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
17749         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17750         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17751         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17752         if (rc != MBX_TIMEOUT)
17753                 mempool_free(mboxq, phba->mbox_mem_pool);
17754         if (shdr_status || shdr_add_status || rc) {
17755                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17756                                 "2514 POST_RPI_HDR mailbox failed with "
17757                                 "status x%x add_status x%x, mbx status x%x\n",
17758                                 shdr_status, shdr_add_status, rc);
17759                 rc = -ENXIO;
17760         } else {
17761                 /*
17762                  * The next_rpi stores the next logical module-64 rpi value used
17763                  * to post physical rpis in subsequent rpi postings.
17764                  */
17765                 spin_lock_irq(&phba->hbalock);
17766                 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17767                 spin_unlock_irq(&phba->hbalock);
17768         }
17769         return rc;
17770 }
17771
17772 /**
17773  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17774  * @phba: pointer to lpfc hba data structure.
17775  *
17776  * This routine is invoked to post rpi header templates to the
17777  * HBA consistent with the SLI-4 interface spec.  This routine
17778  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17779  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17780  *
17781  * Returns
17782  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
17783  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
17784  **/
17785 int
17786 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17787 {
17788         unsigned long rpi;
17789         uint16_t max_rpi, rpi_limit;
17790         uint16_t rpi_remaining, lrpi = 0;
17791         struct lpfc_rpi_hdr *rpi_hdr;
17792         unsigned long iflag;
17793
17794         /*
17795          * Fetch the next logical rpi.  Because this index is logical,
17796          * the  driver starts at 0 each time.
17797          */
17798         spin_lock_irqsave(&phba->hbalock, iflag);
17799         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17800         rpi_limit = phba->sli4_hba.next_rpi;
17801
17802         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17803         if (rpi >= rpi_limit)
17804                 rpi = LPFC_RPI_ALLOC_ERROR;
17805         else {
17806                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17807                 phba->sli4_hba.max_cfg_param.rpi_used++;
17808                 phba->sli4_hba.rpi_count++;
17809         }
17810         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17811                         "0001 rpi:%x max:%x lim:%x\n",
17812                         (int) rpi, max_rpi, rpi_limit);
17813
17814         /*
17815          * Don't try to allocate more rpi header regions if the device limit
17816          * has been exhausted.
17817          */
17818         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17819             (phba->sli4_hba.rpi_count >= max_rpi)) {
17820                 spin_unlock_irqrestore(&phba->hbalock, iflag);
17821                 return rpi;
17822         }
17823
17824         /*
17825          * RPI header postings are not required for SLI4 ports capable of
17826          * extents.
17827          */
17828         if (!phba->sli4_hba.rpi_hdrs_in_use) {
17829                 spin_unlock_irqrestore(&phba->hbalock, iflag);
17830                 return rpi;
17831         }
17832
17833         /*
17834          * If the driver is running low on rpi resources, allocate another
17835          * page now.  Note that the next_rpi value is used because
17836          * it represents how many are actually in use whereas max_rpi notes
17837          * how many are supported max by the device.
17838          */
17839         rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
17840         spin_unlock_irqrestore(&phba->hbalock, iflag);
17841         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17842                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17843                 if (!rpi_hdr) {
17844                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17845                                         "2002 Error Could not grow rpi "
17846                                         "count\n");
17847                 } else {
17848                         lrpi = rpi_hdr->start_rpi;
17849                         rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17850                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17851                 }
17852         }
17853
17854         return rpi;
17855 }
17856
17857 /**
17858  * lpfc_sli4_free_rpi - Release an rpi for reuse.
17859  * @phba: pointer to lpfc hba data structure.
17860  *
17861  * This routine is invoked to release an rpi to the pool of
17862  * available rpis maintained by the driver.
17863  **/
17864 static void
17865 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17866 {
17867         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17868                 phba->sli4_hba.rpi_count--;
17869                 phba->sli4_hba.max_cfg_param.rpi_used--;
17870         }
17871 }
17872
17873 /**
17874  * lpfc_sli4_free_rpi - Release an rpi for reuse.
17875  * @phba: pointer to lpfc hba data structure.
17876  *
17877  * This routine is invoked to release an rpi to the pool of
17878  * available rpis maintained by the driver.
17879  **/
17880 void
17881 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17882 {
17883         spin_lock_irq(&phba->hbalock);
17884         __lpfc_sli4_free_rpi(phba, rpi);
17885         spin_unlock_irq(&phba->hbalock);
17886 }
17887
17888 /**
17889  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17890  * @phba: pointer to lpfc hba data structure.
17891  *
17892  * This routine is invoked to remove the memory region that
17893  * provided rpi via a bitmask.
17894  **/
17895 void
17896 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17897 {
17898         kfree(phba->sli4_hba.rpi_bmask);
17899         kfree(phba->sli4_hba.rpi_ids);
17900         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
17901 }
17902
17903 /**
17904  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17905  * @phba: pointer to lpfc hba data structure.
17906  *
17907  * This routine is invoked to remove the memory region that
17908  * provided rpi via a bitmask.
17909  **/
17910 int
17911 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17912         void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
17913 {
17914         LPFC_MBOXQ_t *mboxq;
17915         struct lpfc_hba *phba = ndlp->phba;
17916         int rc;
17917
17918         /* The port is notified of the header region via a mailbox command. */
17919         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17920         if (!mboxq)
17921                 return -ENOMEM;
17922
17923         /* Post all rpi memory regions to the port. */
17924         lpfc_resume_rpi(mboxq, ndlp);
17925         if (cmpl) {
17926                 mboxq->mbox_cmpl = cmpl;
17927                 mboxq->context1 = arg;
17928                 mboxq->context2 = ndlp;
17929         } else
17930                 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17931         mboxq->vport = ndlp->vport;
17932         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17933         if (rc == MBX_NOT_FINISHED) {
17934                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17935                                 "2010 Resume RPI Mailbox failed "
17936                                 "status %d, mbxStatus x%x\n", rc,
17937                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17938                 mempool_free(mboxq, phba->mbox_mem_pool);
17939                 return -EIO;
17940         }
17941         return 0;
17942 }
17943
17944 /**
17945  * lpfc_sli4_init_vpi - Initialize a vpi with the port
17946  * @vport: Pointer to the vport for which the vpi is being initialized
17947  *
17948  * This routine is invoked to activate a vpi with the port.
17949  *
17950  * Returns:
17951  *    0 success
17952  *    -Evalue otherwise
17953  **/
17954 int
17955 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
17956 {
17957         LPFC_MBOXQ_t *mboxq;
17958         int rc = 0;
17959         int retval = MBX_SUCCESS;
17960         uint32_t mbox_tmo;
17961         struct lpfc_hba *phba = vport->phba;
17962         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17963         if (!mboxq)
17964                 return -ENOMEM;
17965         lpfc_init_vpi(phba, mboxq, vport->vpi);
17966         mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
17967         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
17968         if (rc != MBX_SUCCESS) {
17969                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
17970                                 "2022 INIT VPI Mailbox failed "
17971                                 "status %d, mbxStatus x%x\n", rc,
17972                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17973                 retval = -EIO;
17974         }
17975         if (rc != MBX_TIMEOUT)
17976                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
17977
17978         return retval;
17979 }
17980
17981 /**
17982  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17983  * @phba: pointer to lpfc hba data structure.
17984  * @mboxq: Pointer to mailbox object.
17985  *
17986  * This routine is invoked to manually add a single FCF record. The caller
17987  * must pass a completely initialized FCF_Record.  This routine takes
17988  * care of the nonembedded mailbox operations.
17989  **/
17990 static void
17991 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17992 {
17993         void *virt_addr;
17994         union lpfc_sli4_cfg_shdr *shdr;
17995         uint32_t shdr_status, shdr_add_status;
17996
17997         virt_addr = mboxq->sge_array->addr[0];
17998         /* The IOCTL status is embedded in the mailbox subheader. */
17999         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18000         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18001         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18002
18003         if ((shdr_status || shdr_add_status) &&
18004                 (shdr_status != STATUS_FCF_IN_USE))
18005                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18006                         "2558 ADD_FCF_RECORD mailbox failed with "
18007                         "status x%x add_status x%x\n",
18008                         shdr_status, shdr_add_status);
18009
18010         lpfc_sli4_mbox_cmd_free(phba, mboxq);
18011 }
18012
18013 /**
18014  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18015  * @phba: pointer to lpfc hba data structure.
18016  * @fcf_record:  pointer to the initialized fcf record to add.
18017  *
18018  * This routine is invoked to manually add a single FCF record. The caller
18019  * must pass a completely initialized FCF_Record.  This routine takes
18020  * care of the nonembedded mailbox operations.
18021  **/
18022 int
18023 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18024 {
18025         int rc = 0;
18026         LPFC_MBOXQ_t *mboxq;
18027         uint8_t *bytep;
18028         void *virt_addr;
18029         struct lpfc_mbx_sge sge;
18030         uint32_t alloc_len, req_len;
18031         uint32_t fcfindex;
18032
18033         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18034         if (!mboxq) {
18035                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18036                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18037                 return -ENOMEM;
18038         }
18039
18040         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18041                   sizeof(uint32_t);
18042
18043         /* Allocate DMA memory and set up the non-embedded mailbox command */
18044         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18045                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18046                                      req_len, LPFC_SLI4_MBX_NEMBED);
18047         if (alloc_len < req_len) {
18048                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18049                         "2523 Allocated DMA memory size (x%x) is "
18050                         "less than the requested DMA memory "
18051                         "size (x%x)\n", alloc_len, req_len);
18052                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18053                 return -ENOMEM;
18054         }
18055
18056         /*
18057          * Get the first SGE entry from the non-embedded DMA memory.  This
18058          * routine only uses a single SGE.
18059          */
18060         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
18061         virt_addr = mboxq->sge_array->addr[0];
18062         /*
18063          * Configure the FCF record for FCFI 0.  This is the driver's
18064          * hardcoded default and gets used in nonFIP mode.
18065          */
18066         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18067         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18068         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18069
18070         /*
18071          * Copy the fcf_index and the FCF Record Data. The data starts after
18072          * the FCoE header plus word10. The data copy needs to be endian
18073          * correct.
18074          */
18075         bytep += sizeof(uint32_t);
18076         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18077         mboxq->vport = phba->pport;
18078         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18079         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18080         if (rc == MBX_NOT_FINISHED) {
18081                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18082                         "2515 ADD_FCF_RECORD mailbox failed with "
18083                         "status 0x%x\n", rc);
18084                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18085                 rc = -EIO;
18086         } else
18087                 rc = 0;
18088
18089         return rc;
18090 }
18091
18092 /**
18093  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18094  * @phba: pointer to lpfc hba data structure.
18095  * @fcf_record:  pointer to the fcf record to write the default data.
18096  * @fcf_index: FCF table entry index.
18097  *
18098  * This routine is invoked to build the driver's default FCF record.  The
18099  * values used are hardcoded.  This routine handles memory initialization.
18100  *
18101  **/
18102 void
18103 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18104                                 struct fcf_record *fcf_record,
18105                                 uint16_t fcf_index)
18106 {
18107         memset(fcf_record, 0, sizeof(struct fcf_record));
18108         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18109         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18110         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18111         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18112         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18113         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18114         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18115         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18116         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18117         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18118         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18119         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18120         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
18121         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
18122         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18123         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18124                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18125         /* Set the VLAN bit map */
18126         if (phba->valid_vlan) {
18127                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18128                         = 1 << (phba->vlan_id % 8);
18129         }
18130 }
18131
18132 /**
18133  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
18134  * @phba: pointer to lpfc hba data structure.
18135  * @fcf_index: FCF table entry offset.
18136  *
18137  * This routine is invoked to scan the entire FCF table by reading FCF
18138  * record and processing it one at a time starting from the @fcf_index
18139  * for initial FCF discovery or fast FCF failover rediscovery.
18140  *
18141  * Return 0 if the mailbox command is submitted successfully, none 0
18142  * otherwise.
18143  **/
18144 int
18145 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18146 {
18147         int rc = 0, error;
18148         LPFC_MBOXQ_t *mboxq;
18149
18150         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
18151         phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
18152         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18153         if (!mboxq) {
18154                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18155                                 "2000 Failed to allocate mbox for "
18156                                 "READ_FCF cmd\n");
18157                 error = -ENOMEM;
18158                 goto fail_fcf_scan;
18159         }
18160         /* Construct the read FCF record mailbox command */
18161         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18162         if (rc) {
18163                 error = -EINVAL;
18164                 goto fail_fcf_scan;
18165         }
18166         /* Issue the mailbox command asynchronously */
18167         mboxq->vport = phba->pport;
18168         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
18169
18170         spin_lock_irq(&phba->hbalock);
18171         phba->hba_flag |= FCF_TS_INPROG;
18172         spin_unlock_irq(&phba->hbalock);
18173
18174         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18175         if (rc == MBX_NOT_FINISHED)
18176                 error = -EIO;
18177         else {
18178                 /* Reset eligible FCF count for new scan */
18179                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
18180                         phba->fcf.eligible_fcf_cnt = 0;
18181                 error = 0;
18182         }
18183 fail_fcf_scan:
18184         if (error) {
18185                 if (mboxq)
18186                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
18187                 /* FCF scan failed, clear FCF_TS_INPROG flag */
18188                 spin_lock_irq(&phba->hbalock);
18189                 phba->hba_flag &= ~FCF_TS_INPROG;
18190                 spin_unlock_irq(&phba->hbalock);
18191         }
18192         return error;
18193 }
18194
18195 /**
18196  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
18197  * @phba: pointer to lpfc hba data structure.
18198  * @fcf_index: FCF table entry offset.
18199  *
18200  * This routine is invoked to read an FCF record indicated by @fcf_index
18201  * and to use it for FLOGI roundrobin FCF failover.
18202  *
18203  * Return 0 if the mailbox command is submitted successfully, none 0
18204  * otherwise.
18205  **/
18206 int
18207 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18208 {
18209         int rc = 0, error;
18210         LPFC_MBOXQ_t *mboxq;
18211
18212         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18213         if (!mboxq) {
18214                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18215                                 "2763 Failed to allocate mbox for "
18216                                 "READ_FCF cmd\n");
18217                 error = -ENOMEM;
18218                 goto fail_fcf_read;
18219         }
18220         /* Construct the read FCF record mailbox command */
18221         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18222         if (rc) {
18223                 error = -EINVAL;
18224                 goto fail_fcf_read;
18225         }
18226         /* Issue the mailbox command asynchronously */
18227         mboxq->vport = phba->pport;
18228         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18229         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18230         if (rc == MBX_NOT_FINISHED)
18231                 error = -EIO;
18232         else
18233                 error = 0;
18234
18235 fail_fcf_read:
18236         if (error && mboxq)
18237                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18238         return error;
18239 }
18240
18241 /**
18242  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18243  * @phba: pointer to lpfc hba data structure.
18244  * @fcf_index: FCF table entry offset.
18245  *
18246  * This routine is invoked to read an FCF record indicated by @fcf_index to
18247  * determine whether it's eligible for FLOGI roundrobin failover list.
18248  *
18249  * Return 0 if the mailbox command is submitted successfully, none 0
18250  * otherwise.
18251  **/
18252 int
18253 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18254 {
18255         int rc = 0, error;
18256         LPFC_MBOXQ_t *mboxq;
18257
18258         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18259         if (!mboxq) {
18260                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18261                                 "2758 Failed to allocate mbox for "
18262                                 "READ_FCF cmd\n");
18263                                 error = -ENOMEM;
18264                                 goto fail_fcf_read;
18265         }
18266         /* Construct the read FCF record mailbox command */
18267         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18268         if (rc) {
18269                 error = -EINVAL;
18270                 goto fail_fcf_read;
18271         }
18272         /* Issue the mailbox command asynchronously */
18273         mboxq->vport = phba->pport;
18274         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18275         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18276         if (rc == MBX_NOT_FINISHED)
18277                 error = -EIO;
18278         else
18279                 error = 0;
18280
18281 fail_fcf_read:
18282         if (error && mboxq)
18283                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18284         return error;
18285 }
18286
18287 /**
18288  * lpfc_check_next_fcf_pri_level
18289  * phba pointer to the lpfc_hba struct for this port.
18290  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18291  * routine when the rr_bmask is empty. The FCF indecies are put into the
18292  * rr_bmask based on their priority level. Starting from the highest priority
18293  * to the lowest. The most likely FCF candidate will be in the highest
18294  * priority group. When this routine is called it searches the fcf_pri list for
18295  * next lowest priority group and repopulates the rr_bmask with only those
18296  * fcf_indexes.
18297  * returns:
18298  * 1=success 0=failure
18299  **/
18300 static int
18301 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18302 {
18303         uint16_t next_fcf_pri;
18304         uint16_t last_index;
18305         struct lpfc_fcf_pri *fcf_pri;
18306         int rc;
18307         int ret = 0;
18308
18309         last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18310                         LPFC_SLI4_FCF_TBL_INDX_MAX);
18311         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18312                         "3060 Last IDX %d\n", last_index);
18313
18314         /* Verify the priority list has 2 or more entries */
18315         spin_lock_irq(&phba->hbalock);
18316         if (list_empty(&phba->fcf.fcf_pri_list) ||
18317             list_is_singular(&phba->fcf.fcf_pri_list)) {
18318                 spin_unlock_irq(&phba->hbalock);
18319                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18320                         "3061 Last IDX %d\n", last_index);
18321                 return 0; /* Empty rr list */
18322         }
18323         spin_unlock_irq(&phba->hbalock);
18324
18325         next_fcf_pri = 0;
18326         /*
18327          * Clear the rr_bmask and set all of the bits that are at this
18328          * priority.
18329          */
18330         memset(phba->fcf.fcf_rr_bmask, 0,
18331                         sizeof(*phba->fcf.fcf_rr_bmask));
18332         spin_lock_irq(&phba->hbalock);
18333         list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18334                 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18335                         continue;
18336                 /*
18337                  * the 1st priority that has not FLOGI failed
18338                  * will be the highest.
18339                  */
18340                 if (!next_fcf_pri)
18341                         next_fcf_pri = fcf_pri->fcf_rec.priority;
18342                 spin_unlock_irq(&phba->hbalock);
18343                 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18344                         rc = lpfc_sli4_fcf_rr_index_set(phba,
18345                                                 fcf_pri->fcf_rec.fcf_index);
18346                         if (rc)
18347                                 return 0;
18348                 }
18349                 spin_lock_irq(&phba->hbalock);
18350         }
18351         /*
18352          * if next_fcf_pri was not set above and the list is not empty then
18353          * we have failed flogis on all of them. So reset flogi failed
18354          * and start at the beginning.
18355          */
18356         if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18357                 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18358                         fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18359                         /*
18360                          * the 1st priority that has not FLOGI failed
18361                          * will be the highest.
18362                          */
18363                         if (!next_fcf_pri)
18364                                 next_fcf_pri = fcf_pri->fcf_rec.priority;
18365                         spin_unlock_irq(&phba->hbalock);
18366                         if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18367                                 rc = lpfc_sli4_fcf_rr_index_set(phba,
18368                                                 fcf_pri->fcf_rec.fcf_index);
18369                                 if (rc)
18370                                         return 0;
18371                         }
18372                         spin_lock_irq(&phba->hbalock);
18373                 }
18374         } else
18375                 ret = 1;
18376         spin_unlock_irq(&phba->hbalock);
18377
18378         return ret;
18379 }
18380 /**
18381  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18382  * @phba: pointer to lpfc hba data structure.
18383  *
18384  * This routine is to get the next eligible FCF record index in a round
18385  * robin fashion. If the next eligible FCF record index equals to the
18386  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
18387  * shall be returned, otherwise, the next eligible FCF record's index
18388  * shall be returned.
18389  **/
18390 uint16_t
18391 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18392 {
18393         uint16_t next_fcf_index;
18394
18395 initial_priority:
18396         /* Search start from next bit of currently registered FCF index */
18397         next_fcf_index = phba->fcf.current_rec.fcf_indx;
18398
18399 next_priority:
18400         /* Determine the next fcf index to check */
18401         next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
18402         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18403                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
18404                                        next_fcf_index);
18405
18406         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
18407         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18408                 /*
18409                  * If we have wrapped then we need to clear the bits that
18410                  * have been tested so that we can detect when we should
18411                  * change the priority level.
18412                  */
18413                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18414                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
18415         }
18416
18417
18418         /* Check roundrobin failover list empty condition */
18419         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18420                 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18421                 /*
18422                  * If next fcf index is not found check if there are lower
18423                  * Priority level fcf's in the fcf_priority list.
18424                  * Set up the rr_bmask with all of the avaiable fcf bits
18425                  * at that level and continue the selection process.
18426                  */
18427                 if (lpfc_check_next_fcf_pri_level(phba))
18428                         goto initial_priority;
18429                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18430                                 "2844 No roundrobin failover FCF available\n");
18431                 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18432                         return LPFC_FCOE_FCF_NEXT_NONE;
18433                 else {
18434                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18435                                 "3063 Only FCF available idx %d, flag %x\n",
18436                                 next_fcf_index,
18437                         phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18438                         return next_fcf_index;
18439                 }
18440         }
18441
18442         if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18443                 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
18444                 LPFC_FCF_FLOGI_FAILED) {
18445                 if (list_is_singular(&phba->fcf.fcf_pri_list))
18446                         return LPFC_FCOE_FCF_NEXT_NONE;
18447
18448                 goto next_priority;
18449         }
18450
18451         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18452                         "2845 Get next roundrobin failover FCF (x%x)\n",
18453                         next_fcf_index);
18454
18455         return next_fcf_index;
18456 }
18457
18458 /**
18459  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18460  * @phba: pointer to lpfc hba data structure.
18461  *
18462  * This routine sets the FCF record index in to the eligible bmask for
18463  * roundrobin failover search. It checks to make sure that the index
18464  * does not go beyond the range of the driver allocated bmask dimension
18465  * before setting the bit.
18466  *
18467  * Returns 0 if the index bit successfully set, otherwise, it returns
18468  * -EINVAL.
18469  **/
18470 int
18471 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18472 {
18473         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18474                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18475                                 "2610 FCF (x%x) reached driver's book "
18476                                 "keeping dimension:x%x\n",
18477                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18478                 return -EINVAL;
18479         }
18480         /* Set the eligible FCF record index bmask */
18481         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18482
18483         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18484                         "2790 Set FCF (x%x) to roundrobin FCF failover "
18485                         "bmask\n", fcf_index);
18486
18487         return 0;
18488 }
18489
18490 /**
18491  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
18492  * @phba: pointer to lpfc hba data structure.
18493  *
18494  * This routine clears the FCF record index from the eligible bmask for
18495  * roundrobin failover search. It checks to make sure that the index
18496  * does not go beyond the range of the driver allocated bmask dimension
18497  * before clearing the bit.
18498  **/
18499 void
18500 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18501 {
18502         struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
18503         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18504                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18505                                 "2762 FCF (x%x) reached driver's book "
18506                                 "keeping dimension:x%x\n",
18507                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18508                 return;
18509         }
18510         /* Clear the eligible FCF record index bmask */
18511         spin_lock_irq(&phba->hbalock);
18512         list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18513                                  list) {
18514                 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18515                         list_del_init(&fcf_pri->list);
18516                         break;
18517                 }
18518         }
18519         spin_unlock_irq(&phba->hbalock);
18520         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18521
18522         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18523                         "2791 Clear FCF (x%x) from roundrobin failover "
18524                         "bmask\n", fcf_index);
18525 }
18526
18527 /**
18528  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18529  * @phba: pointer to lpfc hba data structure.
18530  *
18531  * This routine is the completion routine for the rediscover FCF table mailbox
18532  * command. If the mailbox command returned failure, it will try to stop the
18533  * FCF rediscover wait timer.
18534  **/
18535 static void
18536 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18537 {
18538         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18539         uint32_t shdr_status, shdr_add_status;
18540
18541         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18542
18543         shdr_status = bf_get(lpfc_mbox_hdr_status,
18544                              &redisc_fcf->header.cfg_shdr.response);
18545         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18546                              &redisc_fcf->header.cfg_shdr.response);
18547         if (shdr_status || shdr_add_status) {
18548                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18549                                 "2746 Requesting for FCF rediscovery failed "
18550                                 "status x%x add_status x%x\n",
18551                                 shdr_status, shdr_add_status);
18552                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
18553                         spin_lock_irq(&phba->hbalock);
18554                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
18555                         spin_unlock_irq(&phba->hbalock);
18556                         /*
18557                          * CVL event triggered FCF rediscover request failed,
18558                          * last resort to re-try current registered FCF entry.
18559                          */
18560                         lpfc_retry_pport_discovery(phba);
18561                 } else {
18562                         spin_lock_irq(&phba->hbalock);
18563                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
18564                         spin_unlock_irq(&phba->hbalock);
18565                         /*
18566                          * DEAD FCF event triggered FCF rediscover request
18567                          * failed, last resort to fail over as a link down
18568                          * to FCF registration.
18569                          */
18570                         lpfc_sli4_fcf_dead_failthrough(phba);
18571                 }
18572         } else {
18573                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18574                                 "2775 Start FCF rediscover quiescent timer\n");
18575                 /*
18576                  * Start FCF rediscovery wait timer for pending FCF
18577                  * before rescan FCF record table.
18578                  */
18579                 lpfc_fcf_redisc_wait_start_timer(phba);
18580         }
18581
18582         mempool_free(mbox, phba->mbox_mem_pool);
18583 }
18584
18585 /**
18586  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
18587  * @phba: pointer to lpfc hba data structure.
18588  *
18589  * This routine is invoked to request for rediscovery of the entire FCF table
18590  * by the port.
18591  **/
18592 int
18593 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18594 {
18595         LPFC_MBOXQ_t *mbox;
18596         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18597         int rc, length;
18598
18599         /* Cancel retry delay timers to all vports before FCF rediscover */
18600         lpfc_cancel_all_vport_retry_delay_timer(phba);
18601
18602         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18603         if (!mbox) {
18604                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18605                                 "2745 Failed to allocate mbox for "
18606                                 "requesting FCF rediscover.\n");
18607                 return -ENOMEM;
18608         }
18609
18610         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18611                   sizeof(struct lpfc_sli4_cfg_mhdr));
18612         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18613                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18614                          length, LPFC_SLI4_MBX_EMBED);
18615
18616         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18617         /* Set count to 0 for invalidating the entire FCF database */
18618         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18619
18620         /* Issue the mailbox command asynchronously */
18621         mbox->vport = phba->pport;
18622         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18623         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18624
18625         if (rc == MBX_NOT_FINISHED) {
18626                 mempool_free(mbox, phba->mbox_mem_pool);
18627                 return -EIO;
18628         }
18629         return 0;
18630 }
18631
18632 /**
18633  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18634  * @phba: pointer to lpfc hba data structure.
18635  *
18636  * This function is the failover routine as a last resort to the FCF DEAD
18637  * event when driver failed to perform fast FCF failover.
18638  **/
18639 void
18640 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18641 {
18642         uint32_t link_state;
18643
18644         /*
18645          * Last resort as FCF DEAD event failover will treat this as
18646          * a link down, but save the link state because we don't want
18647          * it to be changed to Link Down unless it is already down.
18648          */
18649         link_state = phba->link_state;
18650         lpfc_linkdown(phba);
18651         phba->link_state = link_state;
18652
18653         /* Unregister FCF if no devices connected to it */
18654         lpfc_unregister_unused_fcf(phba);
18655 }
18656
18657 /**
18658  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
18659  * @phba: pointer to lpfc hba data structure.
18660  * @rgn23_data: pointer to configure region 23 data.
18661  *
18662  * This function gets SLI3 port configure region 23 data through memory dump
18663  * mailbox command. When it successfully retrieves data, the size of the data
18664  * will be returned, otherwise, 0 will be returned.
18665  **/
18666 static uint32_t
18667 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18668 {
18669         LPFC_MBOXQ_t *pmb = NULL;
18670         MAILBOX_t *mb;
18671         uint32_t offset = 0;
18672         int rc;
18673
18674         if (!rgn23_data)
18675                 return 0;
18676
18677         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18678         if (!pmb) {
18679                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18680                                 "2600 failed to allocate mailbox memory\n");
18681                 return 0;
18682         }
18683         mb = &pmb->u.mb;
18684
18685         do {
18686                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18687                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18688
18689                 if (rc != MBX_SUCCESS) {
18690                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
18691                                         "2601 failed to read config "
18692                                         "region 23, rc 0x%x Status 0x%x\n",
18693                                         rc, mb->mbxStatus);
18694                         mb->un.varDmp.word_cnt = 0;
18695                 }
18696                 /*
18697                  * dump mem may return a zero when finished or we got a
18698                  * mailbox error, either way we are done.
18699                  */
18700                 if (mb->un.varDmp.word_cnt == 0)
18701                         break;
18702                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18703                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18704
18705                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
18706                                        rgn23_data + offset,
18707                                        mb->un.varDmp.word_cnt);
18708                 offset += mb->un.varDmp.word_cnt;
18709         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18710
18711         mempool_free(pmb, phba->mbox_mem_pool);
18712         return offset;
18713 }
18714
18715 /**
18716  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18717  * @phba: pointer to lpfc hba data structure.
18718  * @rgn23_data: pointer to configure region 23 data.
18719  *
18720  * This function gets SLI4 port configure region 23 data through memory dump
18721  * mailbox command. When it successfully retrieves data, the size of the data
18722  * will be returned, otherwise, 0 will be returned.
18723  **/
18724 static uint32_t
18725 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18726 {
18727         LPFC_MBOXQ_t *mboxq = NULL;
18728         struct lpfc_dmabuf *mp = NULL;
18729         struct lpfc_mqe *mqe;
18730         uint32_t data_length = 0;
18731         int rc;
18732
18733         if (!rgn23_data)
18734                 return 0;
18735
18736         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18737         if (!mboxq) {
18738                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18739                                 "3105 failed to allocate mailbox memory\n");
18740                 return 0;
18741         }
18742
18743         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18744                 goto out;
18745         mqe = &mboxq->u.mqe;
18746         mp = (struct lpfc_dmabuf *) mboxq->context1;
18747         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18748         if (rc)
18749                 goto out;
18750         data_length = mqe->un.mb_words[5];
18751         if (data_length == 0)
18752                 goto out;
18753         if (data_length > DMP_RGN23_SIZE) {
18754                 data_length = 0;
18755                 goto out;
18756         }
18757         lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18758 out:
18759         mempool_free(mboxq, phba->mbox_mem_pool);
18760         if (mp) {
18761                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18762                 kfree(mp);
18763         }
18764         return data_length;
18765 }
18766
18767 /**
18768  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18769  * @phba: pointer to lpfc hba data structure.
18770  *
18771  * This function read region 23 and parse TLV for port status to
18772  * decide if the user disaled the port. If the TLV indicates the
18773  * port is disabled, the hba_flag is set accordingly.
18774  **/
18775 void
18776 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18777 {
18778         uint8_t *rgn23_data = NULL;
18779         uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18780         uint32_t offset = 0;
18781
18782         /* Get adapter Region 23 data */
18783         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18784         if (!rgn23_data)
18785                 goto out;
18786
18787         if (phba->sli_rev < LPFC_SLI_REV4)
18788                 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18789         else {
18790                 if_type = bf_get(lpfc_sli_intf_if_type,
18791                                  &phba->sli4_hba.sli_intf);
18792                 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18793                         goto out;
18794                 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18795         }
18796
18797         if (!data_size)
18798                 goto out;
18799
18800         /* Check the region signature first */
18801         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18802                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18803                         "2619 Config region 23 has bad signature\n");
18804                         goto out;
18805         }
18806         offset += 4;
18807
18808         /* Check the data structure version */
18809         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18810                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18811                         "2620 Config region 23 has bad version\n");
18812                 goto out;
18813         }
18814         offset += 4;
18815
18816         /* Parse TLV entries in the region */
18817         while (offset < data_size) {
18818                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18819                         break;
18820                 /*
18821                  * If the TLV is not driver specific TLV or driver id is
18822                  * not linux driver id, skip the record.
18823                  */
18824                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18825                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18826                     (rgn23_data[offset + 3] != 0)) {
18827                         offset += rgn23_data[offset + 1] * 4 + 4;
18828                         continue;
18829                 }
18830
18831                 /* Driver found a driver specific TLV in the config region */
18832                 sub_tlv_len = rgn23_data[offset + 1] * 4;
18833                 offset += 4;
18834                 tlv_offset = 0;
18835
18836                 /*
18837                  * Search for configured port state sub-TLV.
18838                  */
18839                 while ((offset < data_size) &&
18840                         (tlv_offset < sub_tlv_len)) {
18841                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18842                                 offset += 4;
18843                                 tlv_offset += 4;
18844                                 break;
18845                         }
18846                         if (rgn23_data[offset] != PORT_STE_TYPE) {
18847                                 offset += rgn23_data[offset + 1] * 4 + 4;
18848                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18849                                 continue;
18850                         }
18851
18852                         /* This HBA contains PORT_STE configured */
18853                         if (!rgn23_data[offset + 2])
18854                                 phba->hba_flag |= LINK_DISABLED;
18855
18856                         goto out;
18857                 }
18858         }
18859
18860 out:
18861         kfree(rgn23_data);
18862         return;
18863 }
18864
18865 /**
18866  * lpfc_wr_object - write an object to the firmware
18867  * @phba: HBA structure that indicates port to create a queue on.
18868  * @dmabuf_list: list of dmabufs to write to the port.
18869  * @size: the total byte value of the objects to write to the port.
18870  * @offset: the current offset to be used to start the transfer.
18871  *
18872  * This routine will create a wr_object mailbox command to send to the port.
18873  * the mailbox command will be constructed using the dma buffers described in
18874  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18875  * BDEs that the imbedded mailbox can support. The @offset variable will be
18876  * used to indicate the starting offset of the transfer and will also return
18877  * the offset after the write object mailbox has completed. @size is used to
18878  * determine the end of the object and whether the eof bit should be set.
18879  *
18880  * Return 0 is successful and offset will contain the the new offset to use
18881  * for the next write.
18882  * Return negative value for error cases.
18883  **/
18884 int
18885 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18886                uint32_t size, uint32_t *offset)
18887 {
18888         struct lpfc_mbx_wr_object *wr_object;
18889         LPFC_MBOXQ_t *mbox;
18890         int rc = 0, i = 0;
18891         uint32_t shdr_status, shdr_add_status;
18892         uint32_t mbox_tmo;
18893         union lpfc_sli4_cfg_shdr *shdr;
18894         struct lpfc_dmabuf *dmabuf;
18895         uint32_t written = 0;
18896
18897         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18898         if (!mbox)
18899                 return -ENOMEM;
18900
18901         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18902                         LPFC_MBOX_OPCODE_WRITE_OBJECT,
18903                         sizeof(struct lpfc_mbx_wr_object) -
18904                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18905
18906         wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18907         wr_object->u.request.write_offset = *offset;
18908         sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18909         wr_object->u.request.object_name[0] =
18910                 cpu_to_le32(wr_object->u.request.object_name[0]);
18911         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18912         list_for_each_entry(dmabuf, dmabuf_list, list) {
18913                 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18914                         break;
18915                 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18916                 wr_object->u.request.bde[i].addrHigh =
18917                         putPaddrHigh(dmabuf->phys);
18918                 if (written + SLI4_PAGE_SIZE >= size) {
18919                         wr_object->u.request.bde[i].tus.f.bdeSize =
18920                                 (size - written);
18921                         written += (size - written);
18922                         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18923                 } else {
18924                         wr_object->u.request.bde[i].tus.f.bdeSize =
18925                                 SLI4_PAGE_SIZE;
18926                         written += SLI4_PAGE_SIZE;
18927                 }
18928                 i++;
18929         }
18930         wr_object->u.request.bde_count = i;
18931         bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18932         if (!phba->sli4_hba.intr_enable)
18933                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18934         else {
18935                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18936                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18937         }
18938         /* The IOCTL status is embedded in the mailbox subheader. */
18939         shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18940         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18941         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18942         if (rc != MBX_TIMEOUT)
18943                 mempool_free(mbox, phba->mbox_mem_pool);
18944         if (shdr_status || shdr_add_status || rc) {
18945                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18946                                 "3025 Write Object mailbox failed with "
18947                                 "status x%x add_status x%x, mbx status x%x\n",
18948                                 shdr_status, shdr_add_status, rc);
18949                 rc = -ENXIO;
18950                 *offset = shdr_add_status;
18951         } else
18952                 *offset += wr_object->u.response.actual_write_length;
18953         return rc;
18954 }
18955
18956 /**
18957  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18958  * @vport: pointer to vport data structure.
18959  *
18960  * This function iterate through the mailboxq and clean up all REG_LOGIN
18961  * and REG_VPI mailbox commands associated with the vport. This function
18962  * is called when driver want to restart discovery of the vport due to
18963  * a Clear Virtual Link event.
18964  **/
18965 void
18966 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18967 {
18968         struct lpfc_hba *phba = vport->phba;
18969         LPFC_MBOXQ_t *mb, *nextmb;
18970         struct lpfc_dmabuf *mp;
18971         struct lpfc_nodelist *ndlp;
18972         struct lpfc_nodelist *act_mbx_ndlp = NULL;
18973         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
18974         LIST_HEAD(mbox_cmd_list);
18975         uint8_t restart_loop;
18976
18977         /* Clean up internally queued mailbox commands with the vport */
18978         spin_lock_irq(&phba->hbalock);
18979         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18980                 if (mb->vport != vport)
18981                         continue;
18982
18983                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18984                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
18985                         continue;
18986
18987                 list_del(&mb->list);
18988                 list_add_tail(&mb->list, &mbox_cmd_list);
18989         }
18990         /* Clean up active mailbox command with the vport */
18991         mb = phba->sli.mbox_active;
18992         if (mb && (mb->vport == vport)) {
18993                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18994                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
18995                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18996                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18997                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18998                         /* Put reference count for delayed processing */
18999                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19000                         /* Unregister the RPI when mailbox complete */
19001                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19002                 }
19003         }
19004         /* Cleanup any mailbox completions which are not yet processed */
19005         do {
19006                 restart_loop = 0;
19007                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19008                         /*
19009                          * If this mailox is already processed or it is
19010                          * for another vport ignore it.
19011                          */
19012                         if ((mb->vport != vport) ||
19013                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19014                                 continue;
19015
19016                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19017                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19018                                 continue;
19019
19020                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19021                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19022                                 ndlp = (struct lpfc_nodelist *)mb->context2;
19023                                 /* Unregister the RPI when mailbox complete */
19024                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19025                                 restart_loop = 1;
19026                                 spin_unlock_irq(&phba->hbalock);
19027                                 spin_lock(shost->host_lock);
19028                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19029                                 spin_unlock(shost->host_lock);
19030                                 spin_lock_irq(&phba->hbalock);
19031                                 break;
19032                         }
19033                 }
19034         } while (restart_loop);
19035
19036         spin_unlock_irq(&phba->hbalock);
19037
19038         /* Release the cleaned-up mailbox commands */
19039         while (!list_empty(&mbox_cmd_list)) {
19040                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
19041                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19042                         mp = (struct lpfc_dmabuf *) (mb->context1);
19043                         if (mp) {
19044                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19045                                 kfree(mp);
19046                         }
19047                         ndlp = (struct lpfc_nodelist *) mb->context2;
19048                         mb->context2 = NULL;
19049                         if (ndlp) {
19050                                 spin_lock(shost->host_lock);
19051                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19052                                 spin_unlock(shost->host_lock);
19053                                 lpfc_nlp_put(ndlp);
19054                         }
19055                 }
19056                 mempool_free(mb, phba->mbox_mem_pool);
19057         }
19058
19059         /* Release the ndlp with the cleaned-up active mailbox command */
19060         if (act_mbx_ndlp) {
19061                 spin_lock(shost->host_lock);
19062                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19063                 spin_unlock(shost->host_lock);
19064                 lpfc_nlp_put(act_mbx_ndlp);
19065         }
19066 }
19067
19068 /**
19069  * lpfc_drain_txq - Drain the txq
19070  * @phba: Pointer to HBA context object.
19071  *
19072  * This function attempt to submit IOCBs on the txq
19073  * to the adapter.  For SLI4 adapters, the txq contains
19074  * ELS IOCBs that have been deferred because the there
19075  * are no SGLs.  This congestion can occur with large
19076  * vport counts during node discovery.
19077  **/
19078
19079 uint32_t
19080 lpfc_drain_txq(struct lpfc_hba *phba)
19081 {
19082         LIST_HEAD(completions);
19083         struct lpfc_sli_ring *pring;
19084         struct lpfc_iocbq *piocbq = NULL;
19085         unsigned long iflags = 0;
19086         char *fail_msg = NULL;
19087         struct lpfc_sglq *sglq;
19088         union lpfc_wqe128 wqe;
19089         uint32_t txq_cnt = 0;
19090         struct lpfc_queue *wq;
19091
19092         if (phba->link_flag & LS_MDS_LOOPBACK) {
19093                 /* MDS WQE are posted only to first WQ*/
19094                 wq = phba->sli4_hba.fcp_wq[0];
19095                 if (unlikely(!wq))
19096                         return 0;
19097                 pring = wq->pring;
19098         } else {
19099                 wq = phba->sli4_hba.els_wq;
19100                 if (unlikely(!wq))
19101                         return 0;
19102                 pring = lpfc_phba_elsring(phba);
19103         }
19104
19105         if (unlikely(!pring) || list_empty(&pring->txq))
19106                 return 0;
19107
19108         spin_lock_irqsave(&pring->ring_lock, iflags);
19109         list_for_each_entry(piocbq, &pring->txq, list) {
19110                 txq_cnt++;
19111         }
19112
19113         if (txq_cnt > pring->txq_max)
19114                 pring->txq_max = txq_cnt;
19115
19116         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19117
19118         while (!list_empty(&pring->txq)) {
19119                 spin_lock_irqsave(&pring->ring_lock, iflags);
19120
19121                 piocbq = lpfc_sli_ringtx_get(phba, pring);
19122                 if (!piocbq) {
19123                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19124                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19125                                 "2823 txq empty and txq_cnt is %d\n ",
19126                                 txq_cnt);
19127                         break;
19128                 }
19129                 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
19130                 if (!sglq) {
19131                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
19132                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19133                         break;
19134                 }
19135                 txq_cnt--;
19136
19137                 /* The xri and iocb resources secured,
19138                  * attempt to issue request
19139                  */
19140                 piocbq->sli4_lxritag = sglq->sli4_lxritag;
19141                 piocbq->sli4_xritag = sglq->sli4_xritag;
19142                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19143                         fail_msg = "to convert bpl to sgl";
19144                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
19145                         fail_msg = "to convert iocb to wqe";
19146                 else if (lpfc_sli4_wq_put(wq, &wqe))
19147                         fail_msg = " - Wq is full";
19148                 else
19149                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19150
19151                 if (fail_msg) {
19152                         /* Failed means we can't issue and need to cancel */
19153                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19154                                         "2822 IOCB failed %s iotag 0x%x "
19155                                         "xri 0x%x\n",
19156                                         fail_msg,
19157                                         piocbq->iotag, piocbq->sli4_xritag);
19158                         list_add_tail(&piocbq->list, &completions);
19159                 }
19160                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19161         }
19162
19163         /* Cancel all the IOCBs that cannot be issued */
19164         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19165                                 IOERR_SLI_ABORTED);
19166
19167         return txq_cnt;
19168 }
19169
19170 /**
19171  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19172  * @phba: Pointer to HBA context object.
19173  * @pwqe: Pointer to command WQE.
19174  * @sglq: Pointer to the scatter gather queue object.
19175  *
19176  * This routine converts the bpl or bde that is in the WQE
19177  * to a sgl list for the sli4 hardware. The physical address
19178  * of the bpl/bde is converted back to a virtual address.
19179  * If the WQE contains a BPL then the list of BDE's is
19180  * converted to sli4_sge's. If the WQE contains a single
19181  * BDE then it is converted to a single sli_sge.
19182  * The WQE is still in cpu endianness so the contents of
19183  * the bpl can be used without byte swapping.
19184  *
19185  * Returns valid XRI = Success, NO_XRI = Failure.
19186  */
19187 static uint16_t
19188 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19189                  struct lpfc_sglq *sglq)
19190 {
19191         uint16_t xritag = NO_XRI;
19192         struct ulp_bde64 *bpl = NULL;
19193         struct ulp_bde64 bde;
19194         struct sli4_sge *sgl  = NULL;
19195         struct lpfc_dmabuf *dmabuf;
19196         union lpfc_wqe128 *wqe;
19197         int numBdes = 0;
19198         int i = 0;
19199         uint32_t offset = 0; /* accumulated offset in the sg request list */
19200         int inbound = 0; /* number of sg reply entries inbound from firmware */
19201         uint32_t cmd;
19202
19203         if (!pwqeq || !sglq)
19204                 return xritag;
19205
19206         sgl  = (struct sli4_sge *)sglq->sgl;
19207         wqe = &pwqeq->wqe;
19208         pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19209
19210         cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19211         if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19212                 return sglq->sli4_xritag;
19213         numBdes = pwqeq->rsvd2;
19214         if (numBdes) {
19215                 /* The addrHigh and addrLow fields within the WQE
19216                  * have not been byteswapped yet so there is no
19217                  * need to swap them back.
19218                  */
19219                 if (pwqeq->context3)
19220                         dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19221                 else
19222                         return xritag;
19223
19224                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
19225                 if (!bpl)
19226                         return xritag;
19227
19228                 for (i = 0; i < numBdes; i++) {
19229                         /* Should already be byte swapped. */
19230                         sgl->addr_hi = bpl->addrHigh;
19231                         sgl->addr_lo = bpl->addrLow;
19232
19233                         sgl->word2 = le32_to_cpu(sgl->word2);
19234                         if ((i+1) == numBdes)
19235                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
19236                         else
19237                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
19238                         /* swap the size field back to the cpu so we
19239                          * can assign it to the sgl.
19240                          */
19241                         bde.tus.w = le32_to_cpu(bpl->tus.w);
19242                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19243                         /* The offsets in the sgl need to be accumulated
19244                          * separately for the request and reply lists.
19245                          * The request is always first, the reply follows.
19246                          */
19247                         switch (cmd) {
19248                         case CMD_GEN_REQUEST64_WQE:
19249                                 /* add up the reply sg entries */
19250                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19251                                         inbound++;
19252                                 /* first inbound? reset the offset */
19253                                 if (inbound == 1)
19254                                         offset = 0;
19255                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19256                                 bf_set(lpfc_sli4_sge_type, sgl,
19257                                         LPFC_SGE_TYPE_DATA);
19258                                 offset += bde.tus.f.bdeSize;
19259                                 break;
19260                         case CMD_FCP_TRSP64_WQE:
19261                                 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19262                                 bf_set(lpfc_sli4_sge_type, sgl,
19263                                         LPFC_SGE_TYPE_DATA);
19264                                 break;
19265                         case CMD_FCP_TSEND64_WQE:
19266                         case CMD_FCP_TRECEIVE64_WQE:
19267                                 bf_set(lpfc_sli4_sge_type, sgl,
19268                                         bpl->tus.f.bdeFlags);
19269                                 if (i < 3)
19270                                         offset = 0;
19271                                 else
19272                                         offset += bde.tus.f.bdeSize;
19273                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19274                                 break;
19275                         }
19276                         sgl->word2 = cpu_to_le32(sgl->word2);
19277                         bpl++;
19278                         sgl++;
19279                 }
19280         } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19281                 /* The addrHigh and addrLow fields of the BDE have not
19282                  * been byteswapped yet so they need to be swapped
19283                  * before putting them in the sgl.
19284                  */
19285                 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19286                 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19287                 sgl->word2 = le32_to_cpu(sgl->word2);
19288                 bf_set(lpfc_sli4_sge_last, sgl, 1);
19289                 sgl->word2 = cpu_to_le32(sgl->word2);
19290                 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19291         }
19292         return sglq->sli4_xritag;
19293 }
19294
19295 /**
19296  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19297  * @phba: Pointer to HBA context object.
19298  * @ring_number: Base sli ring number
19299  * @pwqe: Pointer to command WQE.
19300  **/
19301 int
19302 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19303                     struct lpfc_iocbq *pwqe)
19304 {
19305         union lpfc_wqe128 *wqe = &pwqe->wqe;
19306         struct lpfc_nvmet_rcv_ctx *ctxp;
19307         struct lpfc_queue *wq;
19308         struct lpfc_sglq *sglq;
19309         struct lpfc_sli_ring *pring;
19310         unsigned long iflags;
19311         uint32_t ret = 0;
19312
19313         /* NVME_LS and NVME_LS ABTS requests. */
19314         if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19315                 pring =  phba->sli4_hba.nvmels_wq->pring;
19316                 spin_lock_irqsave(&pring->ring_lock, iflags);
19317                 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19318                 if (!sglq) {
19319                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19320                         return WQE_BUSY;
19321                 }
19322                 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19323                 pwqe->sli4_xritag = sglq->sli4_xritag;
19324                 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19325                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19326                         return WQE_ERROR;
19327                 }
19328                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19329                        pwqe->sli4_xritag);
19330                 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19331                 if (ret) {
19332                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19333                         return ret;
19334                 }
19335
19336                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19337                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19338                 return 0;
19339         }
19340
19341         /* NVME_FCREQ and NVME_ABTS requests */
19342         if (pwqe->iocb_flag & LPFC_IO_NVME) {
19343                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19344                 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19345
19346                 spin_lock_irqsave(&pring->ring_lock, iflags);
19347                 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19348                 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19349                       phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
19350                 ret = lpfc_sli4_wq_put(wq, wqe);
19351                 if (ret) {
19352                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19353                         return ret;
19354                 }
19355                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19356                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19357                 return 0;
19358         }
19359
19360         /* NVMET requests */
19361         if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19362                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19363                 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19364
19365                 spin_lock_irqsave(&pring->ring_lock, iflags);
19366                 ctxp = pwqe->context2;
19367                 sglq = ctxp->ctxbuf->sglq;
19368                 if (pwqe->sli4_xritag ==  NO_XRI) {
19369                         pwqe->sli4_lxritag = sglq->sli4_lxritag;
19370                         pwqe->sli4_xritag = sglq->sli4_xritag;
19371                 }
19372                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19373                        pwqe->sli4_xritag);
19374                 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19375                 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19376                       phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
19377                 ret = lpfc_sli4_wq_put(wq, wqe);
19378                 if (ret) {
19379                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19380                         return ret;
19381                 }
19382                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19383                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19384                 return 0;
19385         }
19386         return WQE_ERROR;
19387 }